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 "declarativecategoryaxis_p.h" |
31 | #include <QtCore/QDebug> |
32 | |
33 | #include <algorithm> |
34 | |
35 | QT_CHARTS_BEGIN_NAMESPACE |
36 | |
37 | /*! |
38 | \qmltype CategoryRange |
39 | \inqmlmodule QtCharts |
40 | |
41 | \brief Defines a range on a category axis. |
42 | |
43 | The CategoryRange type is used together with the CategoryAxis type to |
44 | specify labeled ranges on category axes. The widths of the category ranges |
45 | can be specified freely. |
46 | |
47 | \sa CategoryAxis |
48 | */ |
49 | |
50 | /*! |
51 | \qmlproperty int CategoryRange::endValue |
52 | The high end of the category. |
53 | */ |
54 | |
55 | /*! |
56 | \qmlproperty string CategoryRange::label |
57 | The label of the category. |
58 | */ |
59 | |
60 | DeclarativeCategoryRange::DeclarativeCategoryRange(QObject *parent) : |
61 | QObject(parent), |
62 | m_endValue(0), |
63 | m_label(QString()) |
64 | { |
65 | } |
66 | |
67 | void DeclarativeCategoryRange::setLabel(const QString &label) |
68 | { |
69 | auto catAxis = qobject_cast<QCategoryAxis *>(object: parent()); |
70 | if (catAxis) |
71 | catAxis->replaceLabel(oldLabel: m_label, newLabel: label); |
72 | m_label = label; |
73 | } |
74 | |
75 | DeclarativeCategoryAxis::DeclarativeCategoryAxis(QObject *parent) : |
76 | QCategoryAxis(parent), |
77 | m_labelsPosition(AxisLabelsPositionCenter) |
78 | { |
79 | } |
80 | |
81 | void DeclarativeCategoryAxis::classBegin() |
82 | { |
83 | } |
84 | |
85 | void DeclarativeCategoryAxis::componentComplete() |
86 | { |
87 | QList<QPair<QString, qreal> > ranges; |
88 | foreach (QObject *child, children()) { |
89 | if (qobject_cast<DeclarativeCategoryRange *>(object: child)) { |
90 | DeclarativeCategoryRange *range = qobject_cast<DeclarativeCategoryRange *>(object: child); |
91 | ranges.append(t: QPair<QString, qreal>(range->label(), range->endValue())); |
92 | } |
93 | } |
94 | |
95 | // Sort and append the range objects according to end value |
96 | std::sort(first: ranges.begin(), last: ranges.end(), comp: endValueLessThan); |
97 | for (int i(0); i < ranges.count(); i++) |
98 | append(label: ranges.at(i).first, categoryEndValue: ranges.at(i).second); |
99 | } |
100 | |
101 | bool DeclarativeCategoryAxis::endValueLessThan(const QPair<QString, qreal> &value1, const QPair<QString, qreal> &value2) |
102 | { |
103 | return value1.second < value2.second; |
104 | } |
105 | |
106 | QQmlListProperty<QObject> DeclarativeCategoryAxis::axisChildren() |
107 | { |
108 | return QQmlListProperty<QObject>(this, 0, &DeclarativeCategoryAxis::appendAxisChildren ,0,0,0); |
109 | } |
110 | |
111 | void DeclarativeCategoryAxis::append(const QString &label, qreal categoryEndValue) |
112 | { |
113 | QCategoryAxis::append(label, categoryEndValue); |
114 | } |
115 | |
116 | void DeclarativeCategoryAxis::remove(const QString &label) |
117 | { |
118 | QCategoryAxis::remove(label); |
119 | } |
120 | |
121 | void DeclarativeCategoryAxis::replace(const QString &oldLabel, const QString &newLabel) |
122 | { |
123 | QCategoryAxis::replaceLabel(oldLabel, newLabel); |
124 | } |
125 | |
126 | void DeclarativeCategoryAxis::appendAxisChildren(QQmlListProperty<QObject> *list, QObject *element) |
127 | { |
128 | // Empty implementation; the children are parsed in componentComplete instead |
129 | Q_UNUSED(list) |
130 | Q_UNUSED(element) |
131 | } |
132 | |
133 | DeclarativeCategoryAxis::AxisLabelsPosition DeclarativeCategoryAxis::labelsPosition() const |
134 | { |
135 | return (DeclarativeCategoryAxis::AxisLabelsPosition) QCategoryAxis::labelsPosition(); |
136 | } |
137 | |
138 | void DeclarativeCategoryAxis::setLabelsPosition(AxisLabelsPosition position) |
139 | { |
140 | if (position != m_labelsPosition) { |
141 | QCategoryAxis::setLabelsPosition((QCategoryAxis::AxisLabelsPosition)position); |
142 | emit labelsPositionChanged(position); |
143 | } |
144 | } |
145 | |
146 | QT_CHARTS_END_NAMESPACE |
147 | |
148 | #include "moc_declarativecategoryaxis_p.cpp" |
149 | |