| 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/chartvalueaxisx_p.h> | 
| 31 | #include <QtCharts/QAbstractAxis> | 
| 32 | #include <private/chartpresenter_p.h> | 
| 33 | #include <QtCharts/QValueAxis> | 
| 34 | #include <private/abstractchartlayout_p.h> | 
| 35 | #include <private/valueaxislabel_p.h> | 
| 36 | #include <QtWidgets/QGraphicsLayout> | 
| 37 | #include <QtCore/QtMath> | 
| 38 | #include <QtCore/QDebug> | 
| 39 |  | 
| 40 |  | 
| 41 | QT_CHARTS_BEGIN_NAMESPACE | 
| 42 |  | 
| 43 | ChartValueAxisX::ChartValueAxisX(QValueAxis *axis, QGraphicsItem *item ) | 
| 44 |     : HorizontalAxis(axis, item), | 
| 45 |       m_axis(axis) | 
| 46 | { | 
| 47 |     QObject::connect(sender: m_axis, SIGNAL(tickCountChanged(int)), receiver: this, SLOT(handleTickCountChanged(int))); | 
| 48 |     QObject::connect(sender: m_axis, SIGNAL(minorTickCountChanged(int)), | 
| 49 |                      receiver: this, SLOT(handleMinorTickCountChanged(int))); | 
| 50 |     QObject::connect(sender: m_axis, SIGNAL(labelFormatChanged(QString)), receiver: this, SLOT(handleLabelFormatChanged(QString))); | 
| 51 |     QObject::connect(sender: m_axis, SIGNAL(tickIntervalChanged(qreal)), receiver: this, SLOT(handleTickIntervalChanged(qreal))); | 
| 52 |     QObject::connect(sender: m_axis, SIGNAL(tickAnchorChanged(qreal)), receiver: this, SLOT(handleTickAnchorChanged(qreal))); | 
| 53 |     QObject::connect(sender: m_axis, SIGNAL(tickTypeChanged(QValueAxis::TickType)), receiver: this, | 
| 54 |                      SLOT(handleTickTypeChanged(QValueAxis::TickType))); | 
| 55 | } | 
| 56 |  | 
| 57 | ChartValueAxisX::~ChartValueAxisX() | 
| 58 | { | 
| 59 | } | 
| 60 |  | 
| 61 | QVector<qreal> ChartValueAxisX::calculateLayout() const | 
| 62 | { | 
| 63 |     if (m_axis->tickType() == QValueAxis::TicksFixed) { | 
| 64 |         int tickCount = m_axis->tickCount(); | 
| 65 |  | 
| 66 |         Q_ASSERT(tickCount >= 2); | 
| 67 |  | 
| 68 |         QVector<qreal> points; | 
| 69 |         points.resize(size: tickCount); | 
| 70 |  | 
| 71 |         const QRectF &gridRect = gridGeometry(); | 
| 72 |         const qreal deltaX = gridRect.width() / (qreal(tickCount) - 1.0); | 
| 73 |         for (int i = 0; i < tickCount; ++i) | 
| 74 |             points[i] = qreal(i) * deltaX + gridRect.left(); | 
| 75 |         return points; | 
| 76 |     } else { // QValueAxis::TicksDynamic | 
| 77 |         const qreal interval = m_axis->tickInterval(); | 
| 78 |         qreal value = m_axis->tickAnchor(); | 
| 79 |         const qreal maxValue = max(); | 
| 80 |         const qreal minValue = min(); | 
| 81 |  | 
| 82 |         // Find the first major tick right after the min of range | 
| 83 |         if (value > minValue) | 
| 84 |             value = value - int((value - minValue) / interval) * interval; | 
| 85 |         else | 
| 86 |             value = value + qCeil(v: (minValue - value) / interval) * interval; | 
| 87 |  | 
| 88 |         const QRectF &gridRect = gridGeometry(); | 
| 89 |         const qreal deltaX = gridRect.width() / (maxValue - minValue); | 
| 90 |  | 
| 91 |         QVector<qreal> points; | 
| 92 |         const qreal leftPos = gridRect.left(); | 
| 93 |         while (value <= maxValue || qFuzzyCompare(p1: value, p2: maxValue)) { | 
| 94 |             points << (value - minValue) * deltaX + leftPos; | 
| 95 |             value += interval; | 
| 96 |         } | 
| 97 |  | 
| 98 |         return points; | 
| 99 |     } | 
| 100 | } | 
| 101 |  | 
| 102 | void ChartValueAxisX::updateGeometry() | 
| 103 | { | 
| 104 |     const QVector<qreal>& layout = ChartAxisElement::layout(); | 
| 105 |     const QVector<qreal>& dynamicMinorTicklayout = ChartAxisElement::dynamicMinorTicklayout(); | 
| 106 |     if (layout.isEmpty() && dynamicMinorTicklayout.isEmpty()) | 
| 107 |         return; | 
| 108 |     setLabels(createValueLabels(max: min(), min: max(), ticks: layout.size(), tickInterval: m_axis->tickInterval(), | 
| 109 |                                 tickAnchor: m_axis->tickAnchor(), tickType: m_axis->tickType(), format: m_axis->labelFormat())); | 
| 110 |     HorizontalAxis::updateGeometry(); | 
| 111 |     updateLabelsValues(axis: m_axis); | 
| 112 | } | 
| 113 |  | 
| 114 | void ChartValueAxisX::handleTickCountChanged(int tick) | 
| 115 | { | 
| 116 |     Q_UNUSED(tick); | 
| 117 |     QGraphicsLayoutItem::updateGeometry(); | 
| 118 |     if (presenter()) presenter()->layout()->invalidate(); | 
| 119 | } | 
| 120 |  | 
| 121 | void ChartValueAxisX::handleMinorTickCountChanged(int tick) | 
| 122 | { | 
| 123 |     Q_UNUSED(tick); | 
| 124 |     QGraphicsLayoutItem::updateGeometry(); | 
| 125 |     if (presenter()) | 
| 126 |         presenter()->layout()->invalidate(); | 
| 127 | } | 
| 128 |  | 
| 129 | void ChartValueAxisX::handleLabelFormatChanged(const QString &format) | 
| 130 | { | 
| 131 |     Q_UNUSED(format); | 
| 132 |     QGraphicsLayoutItem::updateGeometry(); | 
| 133 |     if (presenter()) presenter()->layout()->invalidate(); | 
| 134 | } | 
| 135 |  | 
| 136 | void ChartValueAxisX::handleTickIntervalChanged(qreal interval) | 
| 137 | { | 
| 138 |     Q_UNUSED(interval) | 
| 139 |     QGraphicsLayoutItem::updateGeometry(); | 
| 140 |     if (presenter()) presenter()->layout()->invalidate(); | 
| 141 | } | 
| 142 |  | 
| 143 | void ChartValueAxisX::handleTickAnchorChanged(qreal anchor) | 
| 144 | { | 
| 145 |     Q_UNUSED(anchor) | 
| 146 |     QGraphicsLayoutItem::updateGeometry(); | 
| 147 |     if (presenter()) presenter()->layout()->invalidate(); | 
| 148 | } | 
| 149 |  | 
| 150 | void ChartValueAxisX::handleTickTypeChanged(QValueAxis::TickType type) | 
| 151 | { | 
| 152 |     Q_UNUSED(type) | 
| 153 |     QGraphicsLayoutItem::updateGeometry(); | 
| 154 |     if (presenter()) presenter()->layout()->invalidate(); | 
| 155 | } | 
| 156 |  | 
| 157 | QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | 
| 158 | { | 
| 159 |     Q_UNUSED(constraint) | 
| 160 |  | 
| 161 |     QSizeF sh; | 
| 162 |  | 
| 163 |     QSizeF base = HorizontalAxis::sizeHint(which, constraint); | 
| 164 |     QStringList ticksList = createValueLabels(max: min(), min: max(), ticks: m_axis->tickCount(), | 
| 165 |                                               tickInterval: m_axis->tickInterval(), tickAnchor: m_axis->tickAnchor(), | 
| 166 |                                               tickType: m_axis->tickType(), format: m_axis->labelFormat()); | 
| 167 |     // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past | 
| 168 |     // first and last ticks. Base width is irrelevant. | 
| 169 |     qreal width = 0; | 
| 170 |     qreal height = 0; | 
| 171 |  | 
| 172 |     switch (which) { | 
| 173 |         case Qt::MinimumSize: { | 
| 174 |             QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), | 
| 175 |                                                                    QStringLiteral("..." ), | 
| 176 |                                                                    angle: axis()->labelsAngle()); | 
| 177 |             width = boundingRect.width() / 2.0; | 
| 178 |             height = boundingRect.height() + labelPadding() + base.height() + 1.0; | 
| 179 |             sh = QSizeF(width, height); | 
| 180 |             break; | 
| 181 |         } | 
| 182 |         case Qt::PreferredSize: { | 
| 183 |             qreal labelHeight = 0.0; | 
| 184 |             qreal firstWidth = -1.0; | 
| 185 |             foreach (const QString& s, ticksList) { | 
| 186 |                 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle()); | 
| 187 |                 labelHeight = qMax(a: rect.height(), b: labelHeight); | 
| 188 |                 width = rect.width(); | 
| 189 |                 if (firstWidth < 0.0) | 
| 190 |                     firstWidth = width; | 
| 191 |             } | 
| 192 |             height = labelHeight + labelPadding() + base.height() + 1.0; | 
| 193 |             width = qMax(a: width, b: firstWidth) / 2.0; | 
| 194 |             sh = QSizeF(width, height); | 
| 195 |             break; | 
| 196 |         } | 
| 197 |         default: | 
| 198 |             break; | 
| 199 |     } | 
| 200 |     return sh; | 
| 201 | } | 
| 202 |  | 
| 203 | QT_CHARTS_END_NAMESPACE | 
| 204 |  | 
| 205 | #include "moc_chartvalueaxisx_p.cpp" | 
| 206 |  |