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
11QT_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
36DeclarativeCategoryRange::DeclarativeCategoryRange(QObject *parent) :
37 QObject(parent),
38 m_endValue(0),
39 m_label(QString())
40{
41}
42
43void 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
51DeclarativeCategoryAxis::DeclarativeCategoryAxis(QObject *parent) :
52 QCategoryAxis(parent),
53 m_labelsPosition(AxisLabelsPositionCenter)
54{
55}
56
57void DeclarativeCategoryAxis::classBegin()
58{
59}
60
61void 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
77bool DeclarativeCategoryAxis::endValueLessThan(const QPair<QString, qreal> &value1, const QPair<QString, qreal> &value2)
78{
79 return value1.second < value2.second;
80}
81
82QQmlListProperty<QObject> DeclarativeCategoryAxis::axisChildren()
83{
84 return QQmlListProperty<QObject>(this, 0, &DeclarativeCategoryAxis::appendAxisChildren ,0,0,0);
85}
86
87void DeclarativeCategoryAxis::append(const QString &label, qreal categoryEndValue)
88{
89 QCategoryAxis::append(label, categoryEndValue);
90}
91
92void DeclarativeCategoryAxis::remove(const QString &label)
93{
94 QCategoryAxis::remove(label);
95}
96
97void DeclarativeCategoryAxis::replace(const QString &oldLabel, const QString &newLabel)
98{
99 QCategoryAxis::replaceLabel(oldLabel, newLabel);
100}
101
102void 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
109DeclarativeCategoryAxis::AxisLabelsPosition DeclarativeCategoryAxis::labelsPosition() const
110{
111 return (DeclarativeCategoryAxis::AxisLabelsPosition) QCategoryAxis::labelsPosition();
112}
113
114void DeclarativeCategoryAxis::setLabelsPosition(AxisLabelsPosition position)
115{
116 if (position != m_labelsPosition) {
117 QCategoryAxis::setLabelsPosition((QCategoryAxis::AxisLabelsPosition)position);
118 emit labelsPositionChanged(position);
119 }
120}
121
122QT_END_NAMESPACE
123
124#include "moc_declarativecategoryaxis_p.cpp"
125

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

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