1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include <private/chartdatetimeaxisx_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 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | ChartDateTimeAxisX::ChartDateTimeAxisX(QDateTimeAxis *axis, QGraphicsItem *item) |
15 | : HorizontalAxis(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 | |
22 | ChartDateTimeAxisX::~ChartDateTimeAxisX() |
23 | { |
24 | } |
25 | |
26 | QList<qreal> ChartDateTimeAxisX::calculateLayout() const |
27 | { |
28 | 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 deltaX = gridRect.width() / (qreal(tickCount) - 1.0); |
36 | for (int i = 0; i < tickCount; ++i) |
37 | points[i] = qreal(i) * deltaX + gridRect.left(); |
38 | return points; |
39 | } |
40 | |
41 | void ChartDateTimeAxisX::updateGeometry() |
42 | { |
43 | const QList<qreal> &layout = ChartAxisElement::layout(); |
44 | if (layout.isEmpty()) |
45 | return; |
46 | setLabels(createDateTimeLabels(max: min(), min: max(), ticks: layout.size(), format: m_axis->format())); |
47 | HorizontalAxis::updateGeometry(); |
48 | updateLabelsDateTimes(); |
49 | } |
50 | |
51 | void ChartDateTimeAxisX::handleTickCountChanged(int tick) |
52 | { |
53 | Q_UNUSED(tick); |
54 | QGraphicsLayoutItem::updateGeometry(); |
55 | if (presenter()) |
56 | presenter()->layout()->invalidate(); |
57 | } |
58 | |
59 | void ChartDateTimeAxisX::handleFormatChanged(const QString &format) |
60 | { |
61 | Q_UNUSED(format); |
62 | QGraphicsLayoutItem::updateGeometry(); |
63 | if (presenter()) |
64 | presenter()->layout()->invalidate(); |
65 | } |
66 | |
67 | QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
68 | { |
69 | Q_UNUSED(constraint); |
70 | |
71 | QSizeF sh; |
72 | |
73 | QSizeF base = HorizontalAxis::sizeHint(which, constraint); |
74 | QStringList ticksList = createDateTimeLabels(max: min(), min: max(), ticks: m_axis->tickCount(), format: m_axis->format()); |
75 | // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past |
76 | // first and last ticks. Base width is irrelevant. |
77 | qreal width = 0; |
78 | qreal height = 0; |
79 | |
80 | if (ticksList.empty()) |
81 | return sh; |
82 | |
83 | switch (which) { |
84 | case Qt::MinimumSize: { |
85 | if (labelsVisible()) { |
86 | QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), |
87 | QStringLiteral("..." ), |
88 | angle: axis()->labelsAngle()); |
89 | width = boundingRect.width() / 2.0; |
90 | height = boundingRect.height() + labelPadding() + base.height() + 1.0; |
91 | } else { |
92 | width = 0; |
93 | height = base.height() + 1.0; |
94 | } |
95 | sh = QSizeF(width, height); |
96 | break; |
97 | } |
98 | case Qt::PreferredSize: { |
99 | if (labelsVisible()) { |
100 | qreal labelHeight = 0.0; |
101 | qreal firstWidth = -1.0; |
102 | foreach (const QString& s, ticksList) { |
103 | QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle()); |
104 | labelHeight = qMax(a: rect.height(), b: labelHeight); |
105 | width = rect.width(); |
106 | if (firstWidth < 0.0) |
107 | firstWidth = width; |
108 | } |
109 | height = labelHeight + labelPadding() + base.height() + 1.0; |
110 | width = qMax(a: width, b: firstWidth) / 2.0; |
111 | } else { |
112 | height = base.height() + 1.0; |
113 | width = 0; |
114 | } |
115 | sh = QSizeF(width, height); |
116 | break; |
117 | } |
118 | default: |
119 | break; |
120 | } |
121 | |
122 | return sh; |
123 | } |
124 | |
125 | QT_END_NAMESPACE |
126 | |
127 | #include "moc_chartdatetimeaxisx_p.cpp" |
128 | |