| 1 | // Copyright (C) 2016 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only | 
| 3 |  | 
| 4 | #include <private/chartcategoryaxisy_p.h> | 
| 5 | #include <QtCharts/QCategoryAxis> | 
| 6 | #include <QtCharts/QAbstractAxis> | 
| 7 | #include <private/chartpresenter_p.h> | 
| 8 | #include <private/abstractchartlayout_p.h> | 
| 9 | #include <QtWidgets/QGraphicsLayout> | 
| 10 | #include <QtCore/QtMath> | 
| 11 | #include <QtCore/QDebug> | 
| 12 |  | 
| 13 | QT_BEGIN_NAMESPACE | 
| 14 |  | 
| 15 | ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, QGraphicsItem* item) | 
| 16 |     : VerticalAxis(axis, item, true), | 
| 17 |       m_axis(axis) | 
| 18 | { | 
| 19 |     QObject::connect(sender: axis, SIGNAL(categoriesChanged()), receiver: this, SLOT(handleCategoriesChanged())); | 
| 20 | } | 
| 21 |  | 
| 22 | ChartCategoryAxisY::~ChartCategoryAxisY() | 
| 23 | { | 
| 24 | } | 
| 25 |  | 
| 26 | QList<qreal> ChartCategoryAxisY::calculateLayout() const | 
| 27 | { | 
| 28 |     int tickCount = m_axis->categoriesLabels().size() + 1; | 
| 29 |     QList<qreal> points; | 
| 30 |  | 
| 31 |     if (tickCount < 2) | 
| 32 |         return points; | 
| 33 |  | 
| 34 |     const QRectF &gridRect = gridGeometry(); | 
| 35 |     qreal range = max() - min(); | 
| 36 |     if (range > 0) { | 
| 37 |         points.resize(size: tickCount); | 
| 38 |         qreal scale = gridRect.height() / range; | 
| 39 |         for (int i = 0; i < tickCount; ++i) { | 
| 40 |             if (i < tickCount - 1) { | 
| 41 |                 qreal y = -(m_axis->startValue(categoryLabel: m_axis->categoriesLabels().at(i)) - min()) * scale + gridRect.bottom(); | 
| 42 |                 points[i] = y; | 
| 43 |             } else { | 
| 44 |                 qreal y = -(m_axis->endValue(categoryLabel: m_axis->categoriesLabels().at(i: i - 1)) - min())  * scale + gridRect.bottom(); | 
| 45 |                 points[i] = y; | 
| 46 |             } | 
| 47 |         } | 
| 48 |     } | 
| 49 |  | 
| 50 |     return points; | 
| 51 | } | 
| 52 |  | 
| 53 | void ChartCategoryAxisY::updateGeometry() | 
| 54 | { | 
| 55 |     setLabels(m_axis->categoriesLabels() << QString()); | 
| 56 |     VerticalAxis::updateGeometry(); | 
| 57 | } | 
| 58 |  | 
| 59 | QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | 
| 60 | { | 
| 61 |     Q_UNUSED(constraint); | 
| 62 |  | 
| 63 |     QSizeF sh; | 
| 64 |     QSizeF base = VerticalAxis::sizeHint(which, constraint); | 
| 65 |     const QStringList ticksList = m_axis->categoriesLabels(); | 
| 66 |     qreal width = 0; | 
| 67 |     qreal height = 0; // Height is irrelevant for Y axes with interval labels | 
| 68 |  | 
| 69 |     switch (which) { | 
| 70 |     case Qt::MinimumSize: { | 
| 71 |         if (labelsVisible()) { | 
| 72 |             QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), | 
| 73 |                                                                    QStringLiteral("..." ), | 
| 74 |                                                                    angle: axis()->labelsAngle()); | 
| 75 |             width = boundingRect.width() + labelPadding() + base.width() + 1.0; | 
| 76 |         } else { | 
| 77 |             width = base.width() + 1.0; | 
| 78 |         } | 
| 79 |         sh = QSizeF(width, height); | 
| 80 |         break; | 
| 81 |     } | 
| 82 |     case Qt::PreferredSize: { | 
| 83 |         if (labelsVisible()) { | 
| 84 |             qreal labelWidth = 0.0; | 
| 85 |             for (const QString &s : ticksList) { | 
| 86 |                 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle()); | 
| 87 |                 labelWidth = qMax(a: rect.width(), b: labelWidth); | 
| 88 |             } | 
| 89 |             width = labelWidth + labelPadding() + base.width() + 1.0; | 
| 90 |         } else { | 
| 91 |             width = base.width() + 1.0; | 
| 92 |         } | 
| 93 |         sh = QSizeF(width, height); | 
| 94 |         break; | 
| 95 |     } | 
| 96 |     default: | 
| 97 |         break; | 
| 98 |     } | 
| 99 |     return sh; | 
| 100 | } | 
| 101 |  | 
| 102 | void ChartCategoryAxisY::handleCategoriesChanged() | 
| 103 | { | 
| 104 |     QGraphicsLayoutItem::updateGeometry(); | 
| 105 |     presenter()->layout()->invalidate(); | 
| 106 | } | 
| 107 |  | 
| 108 | QT_END_NAMESPACE | 
| 109 |  | 
| 110 | #include "moc_chartcategoryaxisy_p.cpp" | 
| 111 |  |