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/chartlogvalueaxisx_p.h>
9#include <private/chartpresenter_p.h>
10
11QT_BEGIN_NAMESPACE
12
13ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem *item)
14 : HorizontalAxis(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
21ChartLogValueAxisX::~ChartLogValueAxisX()
22{
23}
24
25QList<qreal> ChartLogValueAxisX::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 deltaX = gridRect.width() / qAbs(t: logMax - logMin);
37 for (int i = 0; i < m_axis->tickCount(); ++i)
38 points[i] = (ceilEdge + qreal(i)) * deltaX - leftEdge * deltaX + gridRect.left();
39
40 return points;
41}
42
43void ChartLogValueAxisX::updateGeometry()
44{
45 const QList<qreal> &layout = ChartAxisElement::layout();
46 setLabels(createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: layout.size(), format: m_axis->labelFormat()));
47 HorizontalAxis::updateGeometry();
48}
49
50void ChartLogValueAxisX::handleBaseChanged(qreal base)
51{
52 Q_UNUSED(base);
53 QGraphicsLayoutItem::updateGeometry();
54 if(presenter()) presenter()->layout()->invalidate();
55}
56
57void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
58{
59 Q_UNUSED(format);
60 QGraphicsLayoutItem::updateGeometry();
61 if(presenter()) presenter()->layout()->invalidate();
62}
63
64QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
65{
66 Q_UNUSED(constraint);
67
68 const QSizeF base = HorizontalAxis::sizeHint(which, constraint);
69 const int tickCount = m_axis->tickCount();
70 QStringList ticksList;
71 QSizeF sh;
72
73 if (m_axis->max() > m_axis->min() && tickCount > 0)
74 ticksList = createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: tickCount, format: m_axis->labelFormat());
75 else
76 ticksList.append(QStringLiteral(" "));
77 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
78 // first and last ticks. Base width is irrelevant.
79 qreal width = 0;
80 qreal height = 0;
81
82 switch (which) {
83 case Qt::MinimumSize:{
84 if (labelsVisible()) {
85 QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(),
86 QStringLiteral("..."),
87 angle: axis()->labelsAngle());
88 width = boundingRect.width() / 2.0;
89 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
90 } else {
91 width = 0;
92 height = base.height() + 1.0;
93 }
94 sh = QSizeF(width, height);
95 break;
96 }
97 case Qt::PreferredSize: {
98 if (labelsVisible()) {
99 qreal labelHeight = 0.0;
100 qreal firstWidth = -1.0;
101 foreach (const QString& s, ticksList) {
102 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle());
103 labelHeight = qMax(a: rect.height(), b: labelHeight);
104 width = rect.width();
105 if (firstWidth < 0.0)
106 firstWidth = width;
107 }
108 height = labelHeight + labelPadding() + base.height() + 1.0;
109 width = qMax(a: width, b: firstWidth) / 2.0;
110 } else {
111 height = base.height() + 1.0;
112 width = 0;
113 }
114 sh = QSizeF(width, height);
115 break;
116 }
117 default:
118 break;
119 }
120
121 return sh;
122}
123
124QT_END_NAMESPACE
125
126#include "moc_chartlogvalueaxisx_p.cpp"
127

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