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/chartdatetimeaxisx_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
40ChartDateTimeAxisX::ChartDateTimeAxisX(QDateTimeAxis *axis, QGraphicsItem *item)
41 : HorizontalAxis(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
48ChartDateTimeAxisX::~ChartDateTimeAxisX()
49{
50}
51
52QVector<qreal> ChartDateTimeAxisX::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 deltaX = gridRect.width() / (qreal(tickCount) - 1.0);
62 for (int i = 0; i < tickCount; ++i)
63 points[i] = qreal(i) * deltaX + gridRect.left();
64 return points;
65}
66
67void ChartDateTimeAxisX::updateGeometry()
68{
69 const QVector<qreal>& layout = ChartAxisElement::layout();
70 if (layout.isEmpty())
71 return;
72 setLabels(createDateTimeLabels(max: min(), min: max(), ticks: layout.size(), format: m_axis->format()));
73 HorizontalAxis::updateGeometry();
74 updateLabelsDateTimes();
75}
76
77void ChartDateTimeAxisX::handleTickCountChanged(int tick)
78{
79 Q_UNUSED(tick)
80 QGraphicsLayoutItem::updateGeometry();
81 if (presenter())
82 presenter()->layout()->invalidate();
83}
84
85void ChartDateTimeAxisX::handleFormatChanged(const QString &format)
86{
87 Q_UNUSED(format);
88 QGraphicsLayoutItem::updateGeometry();
89 if (presenter())
90 presenter()->layout()->invalidate();
91}
92
93QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
94{
95 Q_UNUSED(constraint)
96
97 QSizeF sh;
98
99 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
100 QStringList ticksList = createDateTimeLabels(max: min(), min: max(), ticks: m_axis->tickCount(), format: m_axis->format());
101 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
102 // first and last ticks. Base width is irrelevant.
103 qreal width = 0;
104 qreal height = 0;
105
106 if (ticksList.empty())
107 return sh;
108
109 switch (which) {
110 case Qt::MinimumSize: {
111 QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(),
112 QStringLiteral("..."),
113 angle: axis()->labelsAngle());
114 width = boundingRect.width() / 2.0;
115 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
116 sh = QSizeF(width, height);
117 break;
118 }
119 case Qt::PreferredSize: {
120 qreal labelHeight = 0.0;
121 qreal firstWidth = -1.0;
122 foreach (const QString& s, ticksList) {
123 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle());
124 labelHeight = qMax(a: rect.height(), b: labelHeight);
125 width = rect.width();
126 if (firstWidth < 0.0)
127 firstWidth = width;
128 }
129 height = labelHeight + labelPadding() + base.height() + 1.0;
130 width = qMax(a: width, b: firstWidth) / 2.0;
131 sh = QSizeF(width, height);
132 break;
133 }
134 default:
135 break;
136 }
137
138 return sh;
139}
140
141QT_CHARTS_END_NAMESPACE
142
143#include "moc_chartdatetimeaxisx_p.cpp"
144

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