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

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