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/chartlogvalueaxisx_p.h>
35#include <private/chartpresenter_p.h>
36
37QT_CHARTS_BEGIN_NAMESPACE
38
39ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem *item)
40 : HorizontalAxis(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
47ChartLogValueAxisX::~ChartLogValueAxisX()
48{
49}
50
51QVector<qreal> ChartLogValueAxisX::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 deltaX = gridRect.width() / qAbs(t: logMax - logMin);
63 for (int i = 0; i < m_axis->tickCount(); ++i)
64 points[i] = (ceilEdge + qreal(i)) * deltaX - leftEdge * deltaX + gridRect.left();
65
66 return points;
67}
68
69void ChartLogValueAxisX::updateGeometry()
70{
71 const QVector<qreal>& layout = ChartAxisElement::layout();
72 setLabels(createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: layout.size(), format: m_axis->labelFormat()));
73 HorizontalAxis::updateGeometry();
74}
75
76void ChartLogValueAxisX::handleBaseChanged(qreal base)
77{
78 Q_UNUSED(base);
79 QGraphicsLayoutItem::updateGeometry();
80 if(presenter()) presenter()->layout()->invalidate();
81}
82
83void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
84{
85 Q_UNUSED(format);
86 QGraphicsLayoutItem::updateGeometry();
87 if(presenter()) presenter()->layout()->invalidate();
88}
89
90QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
91{
92 Q_UNUSED(constraint)
93
94 QSizeF sh;
95
96 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
97 QStringList ticksList;
98 qreal logMax = std::log10(x: m_axis->max()) / std::log10(x: m_axis->base());
99 qreal logMin = std::log10(x: m_axis->min()) / std::log10(x: m_axis->base());
100 int tickCount = qAbs(t: qCeil(v: logMax) - qCeil(v: logMin));
101
102 // If the high edge sits exactly on the tick value, add a tick
103 qreal highValue = logMin < logMax ? logMax : logMin;
104 if (qFuzzyCompare(p1: highValue, p2: qreal(qCeil(v: highValue))))
105 tickCount++;
106
107 if (m_axis->max() > m_axis->min() && tickCount > 0)
108 ticksList = createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: tickCount, format: m_axis->labelFormat());
109 else
110 ticksList.append(QStringLiteral(" "));
111 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
112 // first and last ticks. Base width is irrelevant.
113 qreal width = 0;
114 qreal height = 0;
115
116 switch (which) {
117 case Qt::MinimumSize:{
118 QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(),
119 QStringLiteral("..."),
120 angle: axis()->labelsAngle());
121 width = boundingRect.width() / 2.0;
122 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
123 sh = QSizeF(width, height);
124 break;
125 }
126 case Qt::PreferredSize: {
127 qreal labelHeight = 0.0;
128 qreal firstWidth = -1.0;
129 foreach (const QString& s, ticksList) {
130 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle());
131 labelHeight = qMax(a: rect.height(), b: labelHeight);
132 width = rect.width();
133 if (firstWidth < 0.0)
134 firstWidth = width;
135 }
136 height = labelHeight + labelPadding() + base.height() + 1.0;
137 width = qMax(a: width, b: firstWidth) / 2.0;
138 sh = QSizeF(width, height);
139 break;
140 }
141 default:
142 break;
143 }
144
145 return sh;
146}
147
148QT_CHARTS_END_NAMESPACE
149
150#include "moc_chartlogvalueaxisx_p.cpp"
151

source code of qtcharts/src/charts/axis/logvalueaxis/chartlogvalueaxisx.cpp