1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include <private/chartdatetimeaxisy_p.h>
31#include <private/chartpresenter_p.h>
32#include <QtCharts/QDateTimeAxis>
33#include <private/abstractchartlayout_p.h>
34#include <QtWidgets/QGraphicsLayout>
35#include <QtCore/QDateTime>
36#include <QtCore/QtMath>
37
38QT_CHARTS_BEGIN_NAMESPACE
39
40ChartDateTimeAxisY::ChartDateTimeAxisY(QDateTimeAxis *axis, QGraphicsItem *item)
41 : VerticalAxis(axis, item),
42 m_axis(axis)
43{
44 QObject::connect(sender: m_axis, SIGNAL(tickCountChanged(int)), receiver: this, SLOT(handleTickCountChanged(int)));
45 QObject::connect(sender: m_axis, SIGNAL(formatChanged(QString)), receiver: this, SLOT(handleFormatChanged(QString)));
46}
47
48ChartDateTimeAxisY::~ChartDateTimeAxisY()
49{
50}
51
52QVector<qreal> ChartDateTimeAxisY::calculateLayout() const
53{
54 int tickCount = m_axis->tickCount();
55
56 Q_ASSERT(tickCount >= 2);
57
58 QVector<qreal> points;
59 points.resize(size: tickCount);
60 const QRectF &gridRect = gridGeometry();
61 const qreal deltaY = gridRect.height() / (qreal(tickCount) - 1.0);
62 for (int i = 0; i < tickCount; ++i)
63 points[i] = qreal(i) * -deltaY + gridRect.bottom();
64
65 return points;
66}
67
68void ChartDateTimeAxisY::updateGeometry()
69{
70 const QVector<qreal> &layout = ChartAxisElement::layout();
71 if (layout.isEmpty())
72 return;
73 setLabels(createDateTimeLabels(max: min(), min: max(), ticks: layout.size(), format: m_axis->format()));
74 VerticalAxis::updateGeometry();
75 updateLabelsDateTimes();
76}
77
78void ChartDateTimeAxisY::handleTickCountChanged(int tick)
79{
80 Q_UNUSED(tick)
81 QGraphicsLayoutItem::updateGeometry();
82 if (presenter())
83 presenter()->layout()->invalidate();
84}
85
86void ChartDateTimeAxisY::handleFormatChanged(const QString &format)
87{
88 Q_UNUSED(format);
89 QGraphicsLayoutItem::updateGeometry();
90 if (presenter())
91 presenter()->layout()->invalidate();
92}
93
94QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
95{
96 Q_UNUSED(constraint)
97
98 QSizeF sh;
99
100 QSizeF base = VerticalAxis::sizeHint(which, constraint);
101 QStringList ticksList = createDateTimeLabels(max: min(), min: max(), ticks: m_axis->tickCount(), format: m_axis->format());
102 qreal width = 0;
103 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
104 // first and last ticks. Base height is irrelevant.
105 qreal height = 0;
106
107 if (ticksList.empty())
108 return sh;
109
110 switch (which) {
111 case Qt::MinimumSize: {
112 QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(),
113 QStringLiteral("..."),
114 angle: axis()->labelsAngle());
115 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
116 height = boundingRect.height() / 2.0;
117 sh = QSizeF(width, height);
118 break;
119 }
120 case Qt::PreferredSize: {
121 qreal labelWidth = 0.0;
122 qreal firstHeight = -1.0;
123 foreach (const QString& s, ticksList) {
124 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle());
125 labelWidth = qMax(a: rect.width(), b: labelWidth);
126 height = rect.height();
127 if (firstHeight < 0.0)
128 firstHeight = height;
129 }
130 width = labelWidth + labelPadding() + base.width() + 2.0; //two pixels of tolerance
131 height = qMax(a: height, b: firstHeight) / 2.0;
132 sh = QSizeF(width, height);
133 break;
134 }
135 default:
136 break;
137 }
138
139 return sh;
140}
141
142QT_CHARTS_END_NAMESPACE
143
144#include "moc_chartdatetimeaxisy_p.cpp"
145

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