| 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 | const QStringList ticksList = |
| 75 | createDateTimeLabels(max: min(), min: max(), ticks: m_axis->tickCount(), format: m_axis->format()); |
| 76 | // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past |
| 77 | // first and last ticks. Base width is irrelevant. |
| 78 | qreal width = 0; |
| 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() / 2.0; |
| 91 | height = boundingRect.height() + labelPadding() + base.height() + 1.0; |
| 92 | } else { |
| 93 | width = 0; |
| 94 | height = base.height() + 1.0; |
| 95 | } |
| 96 | sh = QSizeF(width, height); |
| 97 | break; |
| 98 | } |
| 99 | case Qt::PreferredSize: { |
| 100 | if (labelsVisible()) { |
| 101 | qreal labelHeight = 0.0; |
| 102 | qreal firstWidth = -1.0; |
| 103 | for (const QString &s : ticksList) { |
| 104 | QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle()); |
| 105 | labelHeight = qMax(a: rect.height(), b: labelHeight); |
| 106 | width = rect.width(); |
| 107 | if (firstWidth < 0.0) |
| 108 | firstWidth = width; |
| 109 | } |
| 110 | height = labelHeight + labelPadding() + base.height() + 1.0; |
| 111 | width = qMax(a: width, b: firstWidth) / 2.0; |
| 112 | } else { |
| 113 | height = base.height() + 1.0; |
| 114 | width = 0; |
| 115 | } |
| 116 | sh = QSizeF(width, height); |
| 117 | break; |
| 118 | } |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | return sh; |
| 124 | } |
| 125 | |
| 126 | QT_END_NAMESPACE |
| 127 | |
| 128 | #include "moc_chartdatetimeaxisx_p.cpp" |
| 129 | |