1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qcategory3daxis_p.h"
5#include "qquickgraphsitem_p.h"
6#include "qgraphs3dlogging_p.h"
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 * \class QCategory3DAxis
12 * \inmodule QtGraphs
13 * \ingroup graphs_3D
14 * \brief The QCategory3DAxis class manipulates an axis of a graph.
15 *
16 * QCategory3DAxis provides an axis that can be given labels. The axis is
17 * divided into equal-sized categories based on the data window size defined by
18 * setting the axis range.
19 *
20 * Grid lines are drawn between categories, if visible. Labels are drawn to
21 * positions of categories if provided.
22 */
23
24/*!
25 * \qmltype Category3DAxis
26 * \inqmlmodule QtGraphs
27 * \ingroup graphs_qml_3D
28 * \nativetype QCategory3DAxis
29 * \inherits Abstract3DAxis
30 * \brief Manipulates an axis of a graph.
31 *
32 * This type provides an axis that can be given labels.
33 */
34
35/*!
36 * \qmlproperty list Category3DAxis::labels
37 *
38 * The labels for the axis are applied to categories. If there are fewer labels
39 * than categories, the remaining ones do not have a label. If category labels
40 * are not defined explicitly, labels are generated from the data row (or column)
41 * labels of the primary series of the graph.
42 */
43
44/*!
45 * \qmlsignal Category3DAxis::rowLabelsChanged()
46 *
47 * This signal is emitted when row \l labels change.
48 */
49
50/*!
51 * \qmlsignal Category3DAxis::columnLabelsChanged()
52 *
53 * This signal is emitted when column \l labels change.
54 */
55
56/*!
57 * Constructs a category 3D axis with the parent \a parent.
58 */
59QCategory3DAxis::QCategory3DAxis(QObject *parent)
60 : QAbstract3DAxis(*(new QCategory3DAxisPrivate()), parent)
61{}
62
63/*!
64 * Destroys the category 3D axis.
65 */
66QCategory3DAxis::~QCategory3DAxis() {}
67
68/*!
69 * \property QCategory3DAxis::labels
70 *
71 * \brief The labels for the axis applied to categories.
72 *
73 * If there are fewer labels than categories, the
74 * remaining ones do not have a label. If category labels are not defined
75 * explicitly, labels are generated from the data row (or column) labels of the
76 * primary series of the graph.
77 */
78QStringList QCategory3DAxis::labels() const
79{
80 return QAbstract3DAxis::labels();
81}
82
83void QCategory3DAxis::setLabels(const QStringList &labels)
84{
85 Q_D(QCategory3DAxis);
86 d->m_labelsExplicitlySet = !labels.isEmpty();
87 bool labelsFromData = false;
88
89 // Get labels from data proxy if axis is attached to a bar graph and an active
90 // axis there
91 if (labels.isEmpty()) {
92 QQuickGraphsItem *graph = qobject_cast<QQuickGraphsItem *>(object: parent());
93 // TODO: QCategory3DAxis is only used with QBars3D, so is it still necessary to check if
94 // the item is bars or something else?
95 if (graph) {
96 if (graph->axisX() == this) {
97 emit rowLabelsChanged();
98 labelsFromData = true;
99 } else if (graph->axisZ() == this) {
100 emit columnLabelsChanged();
101 labelsFromData = true;
102 }
103 }
104 }
105
106 if (!labelsFromData && d->m_labels != labels) {
107 d->m_labels = labels;
108 emit QAbstract3DAxis::labelsChanged();
109 }
110}
111
112/*!
113 * \internal
114 */
115QCategory3DAxisPrivate::QCategory3DAxisPrivate()
116 : QAbstract3DAxisPrivate(QAbstract3DAxis::AxisType::Category)
117 , m_labelsExplicitlySet(false)
118{}
119
120QCategory3DAxisPrivate::~QCategory3DAxisPrivate() {}
121
122/*!
123 * \internal
124 * The graph uses this function to set labels from the data proxy as category
125 * labels.
126 * If the labels have been set explicitly by the user, data proxy labels
127 * are not used.
128 */
129void QCategory3DAxisPrivate::setDataLabels(const QStringList &labels)
130{
131 Q_Q(QCategory3DAxis);
132 if (m_labelsExplicitlySet || m_labels == labels) {
133 qCDebug(lcAProperties3D) << __FUNCTION__
134 << "value is already set to:" << labels;
135 return;
136 }
137
138 m_labels = labels;
139 emit q->QAbstract3DAxis::labelsChanged();
140}
141
142bool QCategory3DAxisPrivate::allowZero()
143{
144 return true;
145}
146
147bool QCategory3DAxisPrivate::allowNegatives()
148{
149 return false;
150}
151
152bool QCategory3DAxisPrivate::allowMinMaxSame()
153{
154 return true;
155}
156
157QT_END_NAMESPACE
158

source code of qtgraphs/src/graphs3d/axis/qcategory3daxis.cpp