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 QStringList ticksList = createDateTimeLabels(max: min(), min: max(), ticks: m_axis->tickCount(), format: m_axis->format());
76 qreal width = 0;
77 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
78 // first and last ticks. Base height is irrelevant.
79 qreal height = 0;
80
81 if (ticksList.empty())
82 return sh;
83
84 switch (which) {
85 case Qt::MinimumSize: {
86 if (labelsVisible()) {
87 QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(),
88 QStringLiteral("..."),
89 angle: axis()->labelsAngle());
90 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
91 height = boundingRect.height() / 2.0;
92 } else {
93 width = base.width() + 1.0;
94 height = 0;
95 }
96 sh = QSizeF(width, height);
97 break;
98 }
99 case Qt::PreferredSize: {
100 if (labelsVisible()) {
101 qreal labelWidth = 0.0;
102 qreal firstHeight = -1.0;
103 foreach (const QString& s, ticksList) {
104 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle());
105 labelWidth = qMax(a: rect.width(), b: labelWidth);
106 height = rect.height();
107 if (firstHeight < 0.0)
108 firstHeight = height;
109 }
110 width = labelWidth + labelPadding() + base.width() + 2.0; //two pixels of tolerance
111 height = qMax(a: height, b: firstHeight) / 2.0;
112 } else {
113 width = base.width() + 2.0;
114 height = 0;
115 }
116 sh = QSizeF(width, height);
117 break;
118 }
119 default:
120 break;
121 }
122
123 return sh;
124}
125
126QT_END_NAMESPACE
127
128#include "moc_chartdatetimeaxisy_p.cpp"
129

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