1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <private/chartdatetimeaxisy_p.h>
5#include <private/chartpresenter_p.h>
6#include <QtCharts/QDateTimeAxis>
7#include <private/abstractchartlayout_p.h>
8#include <QtWidgets/QGraphicsLayout>
9#include <QtCore/QDateTime>
10#include <QtCore/QtMath>
11
12QT_BEGIN_NAMESPACE
13
14ChartDateTimeAxisY::ChartDateTimeAxisY(QDateTimeAxis *axis, QGraphicsItem *item)
15 : VerticalAxis(axis, item),
16 m_axis(axis)
17{
18 QObject::connect(sender: m_axis, SIGNAL(tickCountChanged(int)), receiver: this, SLOT(handleTickCountChanged(int)));
19 QObject::connect(sender: m_axis, SIGNAL(formatChanged(QString)), receiver: this, SLOT(handleFormatChanged(QString)));
20}
21
22ChartDateTimeAxisY::~ChartDateTimeAxisY()
23{
24}
25
26QList<qreal> ChartDateTimeAxisY::calculateLayout() const
27{
28 const int tickCount = m_axis->tickCount();
29
30 Q_ASSERT(tickCount >= 2);
31
32 QList<qreal> points;
33 points.resize(size: tickCount);
34 const QRectF &gridRect = gridGeometry();
35 const qreal deltaY = gridRect.height() / (qreal(tickCount) - 1.0);
36 for (int i = 0; i < tickCount; ++i)
37 points[i] = qreal(i) * -deltaY + gridRect.bottom();
38
39 return points;
40}
41
42void ChartDateTimeAxisY::updateGeometry()
43{
44 const QList<qreal> &layout = ChartAxisElement::layout();
45 if (layout.isEmpty())
46 return;
47 setLabels(createDateTimeLabels(max: min(), min: max(), ticks: layout.size(), format: m_axis->format()));
48 VerticalAxis::updateGeometry();
49 updateLabelsDateTimes();
50}
51
52void ChartDateTimeAxisY::handleTickCountChanged(int tick)
53{
54 Q_UNUSED(tick);
55 QGraphicsLayoutItem::updateGeometry();
56 if (presenter())
57 presenter()->layout()->invalidate();
58}
59
60void ChartDateTimeAxisY::handleFormatChanged(const QString &format)
61{
62 Q_UNUSED(format);
63 QGraphicsLayoutItem::updateGeometry();
64 if (presenter())
65 presenter()->layout()->invalidate();
66}
67
68QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
69{
70 Q_UNUSED(constraint);
71
72 QSizeF sh;
73
74 QSizeF base = VerticalAxis::sizeHint(which, constraint);
75 const QStringList ticksList =
76 createDateTimeLabels(max: min(), min: max(), ticks: m_axis->tickCount(), format: m_axis->format());
77 qreal width = 0;
78 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
79 // first and last ticks. Base height is irrelevant.
80 qreal height = 0;
81
82 if (ticksList.empty())
83 return sh;
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 for (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_chartdatetimeaxisy_p.cpp"
130

source code of qtcharts/src/charts/axis/datetimeaxis/chartdatetimeaxisy.cpp