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

source code of qtcharts/src/chartsqml2/declarativecategoryaxis.cpp