| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
| 5 | |
| 6 | #include "declarativecategoryaxis_p.h" |
| 7 | #include <QtCore/QDebug> |
| 8 | |
| 9 | #include <algorithm> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | /*! |
| 14 | \qmltype CategoryRange |
| 15 | \inqmlmodule QtCharts |
| 16 | |
| 17 | \brief Defines a range on a category axis. |
| 18 | |
| 19 | The CategoryRange type is used together with the CategoryAxis type to |
| 20 | specify labeled ranges on category axes. The widths of the category ranges |
| 21 | can be specified freely. |
| 22 | |
| 23 | \sa CategoryAxis |
| 24 | */ |
| 25 | |
| 26 | /*! |
| 27 | \qmlproperty int CategoryRange::endValue |
| 28 | The high end of the category. |
| 29 | */ |
| 30 | |
| 31 | /*! |
| 32 | \qmlproperty string CategoryRange::label |
| 33 | The label of the category. |
| 34 | */ |
| 35 | |
| 36 | DeclarativeCategoryRange::DeclarativeCategoryRange(QObject *parent) : |
| 37 | QObject(parent), |
| 38 | m_endValue(0), |
| 39 | m_label(QString()) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | void DeclarativeCategoryRange::setLabel(const QString &label) |
| 44 | { |
| 45 | auto catAxis = qobject_cast<QCategoryAxis *>(object: parent()); |
| 46 | if (catAxis) |
| 47 | catAxis->replaceLabel(oldLabel: m_label, newLabel: label); |
| 48 | m_label = label; |
| 49 | } |
| 50 | |
| 51 | DeclarativeCategoryAxis::DeclarativeCategoryAxis(QObject *parent) : |
| 52 | QCategoryAxis(parent), |
| 53 | m_labelsPosition(AxisLabelsPositionCenter) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | void DeclarativeCategoryAxis::classBegin() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | void DeclarativeCategoryAxis::componentComplete() |
| 62 | { |
| 63 | QList<QPair<QString, qreal> > ranges; |
| 64 | foreach (QObject *child, children()) { |
| 65 | if (qobject_cast<DeclarativeCategoryRange *>(object: child)) { |
| 66 | DeclarativeCategoryRange *range = qobject_cast<DeclarativeCategoryRange *>(object: child); |
| 67 | ranges.append(t: QPair<QString, qreal>(range->label(), range->endValue())); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Sort and append the range objects according to end value |
| 72 | std::sort(first: ranges.begin(), last: ranges.end(), comp: endValueLessThan); |
| 73 | for (int i(0); i < ranges.size(); i++) |
| 74 | append(label: ranges.at(i).first, categoryEndValue: ranges.at(i).second); |
| 75 | } |
| 76 | |
| 77 | bool DeclarativeCategoryAxis::endValueLessThan(const QPair<QString, qreal> &value1, const QPair<QString, qreal> &value2) |
| 78 | { |
| 79 | return value1.second < value2.second; |
| 80 | } |
| 81 | |
| 82 | QQmlListProperty<QObject> DeclarativeCategoryAxis::axisChildren() |
| 83 | { |
| 84 | return QQmlListProperty<QObject>(this, 0, &DeclarativeCategoryAxis::appendAxisChildren ,0,0,0); |
| 85 | } |
| 86 | |
| 87 | void DeclarativeCategoryAxis::append(const QString &label, qreal categoryEndValue) |
| 88 | { |
| 89 | QCategoryAxis::append(label, categoryEndValue); |
| 90 | } |
| 91 | |
| 92 | void DeclarativeCategoryAxis::remove(const QString &label) |
| 93 | { |
| 94 | QCategoryAxis::remove(label); |
| 95 | } |
| 96 | |
| 97 | void DeclarativeCategoryAxis::replace(const QString &oldLabel, const QString &newLabel) |
| 98 | { |
| 99 | QCategoryAxis::replaceLabel(oldLabel, newLabel); |
| 100 | } |
| 101 | |
| 102 | void DeclarativeCategoryAxis::appendAxisChildren(QQmlListProperty<QObject> *list, QObject *element) |
| 103 | { |
| 104 | // Empty implementation; the children are parsed in componentComplete instead |
| 105 | Q_UNUSED(list); |
| 106 | Q_UNUSED(element); |
| 107 | } |
| 108 | |
| 109 | DeclarativeCategoryAxis::AxisLabelsPosition DeclarativeCategoryAxis::labelsPosition() const |
| 110 | { |
| 111 | return (DeclarativeCategoryAxis::AxisLabelsPosition) QCategoryAxis::labelsPosition(); |
| 112 | } |
| 113 | |
| 114 | void DeclarativeCategoryAxis::setLabelsPosition(AxisLabelsPosition position) |
| 115 | { |
| 116 | if (position != m_labelsPosition) { |
| 117 | QCategoryAxis::setLabelsPosition((QCategoryAxis::AxisLabelsPosition)position); |
| 118 | emit labelsPositionChanged(position); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | QT_END_NAMESPACE |
| 123 | |
| 124 | #include "moc_declarativecategoryaxis_p.cpp" |
| 125 | |