1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
5
6#include <QtCharts/qlogvalueaxis.h>
7#include <QtCore/qmath.h>
8#include <QtWidgets/qgraphicslayout.h>
9#include <private/abstractchartlayout_p.h>
10#include <private/chartlogvalueaxisy_p.h>
11#include <private/chartpresenter_p.h>
12
13QT_BEGIN_NAMESPACE
14
15ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem *item)
16 : VerticalAxis(axis, item),
17 m_axis(axis)
18{
19 QObject::connect(sender: m_axis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleBaseChanged(qreal)));
20 QObject::connect(sender: m_axis, SIGNAL(labelFormatChanged(QString)), receiver: this, SLOT(handleLabelFormatChanged(QString)));
21}
22
23ChartLogValueAxisY::~ChartLogValueAxisY()
24{
25}
26
27QList<qreal> ChartLogValueAxisY::calculateLayout() const
28{
29 QList<qreal> points;
30 points.resize(size: m_axis->tickCount());
31
32 const qreal logMax = qLn(v: m_axis->max()) / qLn(v: m_axis->base());
33 const qreal logMin = qLn(v: m_axis->min()) / qLn(v: m_axis->base());
34 const qreal leftEdge = qMin(a: logMin, b: logMax);
35 const qreal ceilEdge = std::ceil(x: leftEdge);
36
37 const QRectF &gridRect = gridGeometry();
38 const qreal deltaY = gridRect.height() / qAbs(t: logMax - logMin);
39 for (int i = 0; i < m_axis->tickCount(); ++i)
40 points[i] = (ceilEdge + qreal(i)) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
41
42 return points;
43}
44
45
46void ChartLogValueAxisY::updateGeometry()
47{
48 const QList<qreal> &layout = ChartAxisElement::layout();
49 setLabels(createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: layout.size(), format: m_axis->labelFormat()));
50 VerticalAxis::updateGeometry();
51}
52
53void ChartLogValueAxisY::handleBaseChanged(qreal base)
54{
55 Q_UNUSED(base);
56 QGraphicsLayoutItem::updateGeometry();
57 if(presenter()) presenter()->layout()->invalidate();
58}
59
60void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format)
61{
62 Q_UNUSED(format);
63 QGraphicsLayoutItem::updateGeometry();
64 if(presenter()) presenter()->layout()->invalidate();
65}
66
67QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
68{
69 Q_UNUSED(constraint);
70
71 const QSizeF base = VerticalAxis::sizeHint(which, constraint);
72 const int tickCount = m_axis->tickCount();
73 QStringList ticksList;
74 QSizeF sh;
75
76 if (m_axis->max() > m_axis->min() && tickCount > 0)
77 ticksList = createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: tickCount, format: m_axis->labelFormat());
78 else
79 ticksList.append(QStringLiteral(" "));
80 qreal width = 0;
81 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
82 // first and last ticks. Base height is irrelevant.
83 qreal height = 0;
84
85 switch (which) {
86 case Qt::MinimumSize: {
87 if (labelsVisible()) {
88 QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(),
89 QStringLiteral("..."),
90 angle: axis()->labelsAngle());
91 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
92 height = boundingRect.height() / 2.0;
93 } else {
94 width = base.width() + 1.0;
95 height = 0;
96 }
97 sh = QSizeF(width, height);
98 break;
99 }
100 case Qt::PreferredSize: {
101 if (labelsVisible()) {
102 qreal labelWidth = 0.0;
103 qreal firstHeight = -1.0;
104 foreach (const QString& s, ticksList) {
105 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle());
106 labelWidth = qMax(a: rect.width(), b: labelWidth);
107 height = rect.height();
108 if (firstHeight < 0.0)
109 firstHeight = height;
110 }
111 width = labelWidth + labelPadding() + base.width() + 2.0; //two pixels of tolerance
112 height = qMax(a: height, b: firstHeight) / 2.0;
113 } else {
114 width = base.width() + 2.0;
115 height = 0;
116 }
117 sh = QSizeF(width, height);
118 break;
119 }
120 default:
121 break;
122 }
123
124 return sh;
125}
126
127QT_END_NAMESPACE
128
129#include "moc_chartlogvalueaxisy_p.cpp"
130

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