1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtCharts/qlogvalueaxis.h> |
31 | #include <QtCore/qmath.h> |
32 | #include <QtWidgets/qgraphicslayout.h> |
33 | #include <private/abstractchartlayout_p.h> |
34 | #include <private/chartlogvalueaxisy_p.h> |
35 | #include <private/chartpresenter_p.h> |
36 | |
37 | QT_CHARTS_BEGIN_NAMESPACE |
38 | |
39 | ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem *item) |
40 | : VerticalAxis(axis, item), |
41 | m_axis(axis) |
42 | { |
43 | QObject::connect(sender: m_axis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleBaseChanged(qreal))); |
44 | QObject::connect(sender: m_axis, SIGNAL(labelFormatChanged(QString)), receiver: this, SLOT(handleLabelFormatChanged(QString))); |
45 | } |
46 | |
47 | ChartLogValueAxisY::~ChartLogValueAxisY() |
48 | { |
49 | } |
50 | |
51 | QVector<qreal> ChartLogValueAxisY::calculateLayout() const |
52 | { |
53 | QVector<qreal> points; |
54 | points.resize(size: m_axis->tickCount()); |
55 | |
56 | const qreal logMax = std::log10(x: m_axis->max()) / std::log10(x: m_axis->base()); |
57 | const qreal logMin = std::log10(x: m_axis->min()) / std::log10(x: m_axis->base()); |
58 | const qreal leftEdge = qMin(a: logMin, b: logMax); |
59 | const qreal ceilEdge = qCeil(v: leftEdge); |
60 | |
61 | const QRectF &gridRect = gridGeometry(); |
62 | const qreal deltaY = gridRect.height() / qAbs(t: logMax - logMin); |
63 | for (int i = 0; i < m_axis->tickCount(); ++i) |
64 | points[i] = (ceilEdge + qreal(i)) * -deltaY - leftEdge * -deltaY + gridRect.bottom(); |
65 | |
66 | return points; |
67 | } |
68 | |
69 | |
70 | void ChartLogValueAxisY::updateGeometry() |
71 | { |
72 | const QVector<qreal> &layout = ChartAxisElement::layout(); |
73 | setLabels(createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: layout.size(), format: m_axis->labelFormat())); |
74 | VerticalAxis::updateGeometry(); |
75 | } |
76 | |
77 | void ChartLogValueAxisY::handleBaseChanged(qreal base) |
78 | { |
79 | Q_UNUSED(base); |
80 | QGraphicsLayoutItem::updateGeometry(); |
81 | if(presenter()) presenter()->layout()->invalidate(); |
82 | } |
83 | |
84 | void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format) |
85 | { |
86 | Q_UNUSED(format); |
87 | QGraphicsLayoutItem::updateGeometry(); |
88 | if(presenter()) presenter()->layout()->invalidate(); |
89 | } |
90 | |
91 | QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
92 | { |
93 | Q_UNUSED(constraint) |
94 | |
95 | QSizeF sh; |
96 | |
97 | QSizeF base = VerticalAxis::sizeHint(which, constraint); |
98 | QStringList ticksList; |
99 | qreal logMax = std::log10(x: m_axis->max()) / std::log10(x: m_axis->base()); |
100 | qreal logMin = std::log10(x: m_axis->min()) / std::log10(x: m_axis->base()); |
101 | int tickCount = qAbs(t: qCeil(v: logMax) - qCeil(v: logMin)); |
102 | |
103 | // If the high edge sits exactly on the tick value, add a tick |
104 | qreal highValue = logMin < logMax ? logMax : logMin; |
105 | if (qFuzzyCompare(p1: highValue, p2: qreal(qCeil(v: highValue)))) |
106 | tickCount++; |
107 | |
108 | if (m_axis->max() > m_axis->min() && tickCount > 0) |
109 | ticksList = createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: tickCount, format: m_axis->labelFormat()); |
110 | else |
111 | ticksList.append(QStringLiteral(" " )); |
112 | qreal width = 0; |
113 | // Height of vertical axis sizeHint indicates the maximum distance labels can extend past |
114 | // first and last ticks. Base height is irrelevant. |
115 | qreal height = 0; |
116 | |
117 | switch (which) { |
118 | case Qt::MinimumSize: { |
119 | QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), |
120 | QStringLiteral("..." ), |
121 | angle: axis()->labelsAngle()); |
122 | width = boundingRect.width() + labelPadding() + base.width() + 1.0; |
123 | height = boundingRect.height() / 2.0; |
124 | sh = QSizeF(width, height); |
125 | break; |
126 | } |
127 | case Qt::PreferredSize: { |
128 | qreal labelWidth = 0.0; |
129 | qreal firstHeight = -1.0; |
130 | foreach (const QString& s, ticksList) { |
131 | QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle()); |
132 | labelWidth = qMax(a: rect.width(), b: labelWidth); |
133 | height = rect.height(); |
134 | if (firstHeight < 0.0) |
135 | firstHeight = height; |
136 | } |
137 | width = labelWidth + labelPadding() + base.width() + 2.0; //two pixels of tolerance |
138 | height = qMax(a: height, b: firstHeight) / 2.0; |
139 | sh = QSizeF(width, height); |
140 | break; |
141 | } |
142 | default: |
143 | break; |
144 | } |
145 | |
146 | return sh; |
147 | } |
148 | |
149 | QT_CHARTS_END_NAMESPACE |
150 | |
151 | #include "moc_chartlogvalueaxisy_p.cpp" |
152 | |