1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the QtGraphs API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13
14#ifndef QQUICKGRAPHSBARS_H
15#define QQUICKGRAPHSBARS_H
16
17#include "qbar3dseries.h"
18#include "qquickgraphsitem_p.h"
19#include "bars3dcontroller_p.h"
20#include "barinstancing_p.h"
21
22#include <QtQuick3D/private/qquick3dmaterial_p.h>
23
24QT_BEGIN_NAMESPACE
25
26class Q3DBars;
27
28class QQuickGraphsBars : public QQuickGraphsItem
29{
30 Q_OBJECT
31 Q_PROPERTY(QCategory3DAxis *rowAxis READ rowAxis WRITE setRowAxis NOTIFY rowAxisChanged)
32 Q_PROPERTY(QValue3DAxis *valueAxis READ valueAxis WRITE setValueAxis NOTIFY valueAxisChanged)
33 Q_PROPERTY(QCategory3DAxis *columnAxis READ columnAxis WRITE setColumnAxis NOTIFY columnAxisChanged)
34 Q_PROPERTY(bool multiSeriesUniform READ isMultiSeriesUniform WRITE setMultiSeriesUniform NOTIFY multiSeriesUniformChanged)
35 Q_PROPERTY(float barThickness READ barThickness WRITE setBarThickness NOTIFY barThicknessChanged)
36 Q_PROPERTY(QSizeF barSpacing READ barSpacing WRITE setBarSpacing NOTIFY barSpacingChanged)
37 Q_PROPERTY(bool barSpacingRelative READ isBarSpacingRelative WRITE setBarSpacingRelative NOTIFY barSpacingRelativeChanged)
38 Q_PROPERTY(QSizeF barSeriesMargin READ barSeriesMargin WRITE setBarSeriesMargin NOTIFY barSeriesMarginChanged)
39 Q_PROPERTY(QQmlListProperty<QBar3DSeries> seriesList READ seriesList CONSTANT)
40 Q_PROPERTY(QBar3DSeries *selectedSeries READ selectedSeries NOTIFY selectedSeriesChanged)
41 Q_PROPERTY(QBar3DSeries *primarySeries READ primarySeries WRITE setPrimarySeries NOTIFY primarySeriesChanged)
42 Q_PROPERTY(float floorLevel READ floorLevel WRITE setFloorLevel NOTIFY floorLevelChanged)
43 Q_CLASSINFO("DefaultProperty", "seriesList")
44
45 QML_NAMED_ELEMENT(Bars3D)
46
47public:
48 explicit QQuickGraphsBars(QQuickItem *parent = 0);
49 ~QQuickGraphsBars();
50
51 QCategory3DAxis *rowAxis() const;
52 void setRowAxis(QCategory3DAxis *axis);
53 QValue3DAxis *valueAxis() const;
54 void setValueAxis(QValue3DAxis *axis);
55 QCategory3DAxis *columnAxis() const;
56 void setColumnAxis(QCategory3DAxis *axis);
57
58 void setMultiSeriesUniform(bool uniform);
59 bool isMultiSeriesUniform() const;
60
61 void setBarThickness(float thicknessRatio);
62 float barThickness() const;
63
64 void setBarSpacing(const QSizeF &spacing);
65 QSizeF barSpacing() const;
66
67 void setBarSpacingRelative(bool relative);
68 bool isBarSpacingRelative() const;
69
70 void setBarSeriesMargin(const QSizeF &margin);
71 QSizeF barSeriesMargin() const;
72
73 QQmlListProperty<QBar3DSeries> seriesList();
74 static void appendSeriesFunc(QQmlListProperty<QBar3DSeries> *list, QBar3DSeries *series);
75 static qsizetype countSeriesFunc(QQmlListProperty<QBar3DSeries> *list);
76 static QBar3DSeries *atSeriesFunc(QQmlListProperty<QBar3DSeries> *list, qsizetype index);
77 static void clearSeriesFunc(QQmlListProperty<QBar3DSeries> *list);
78 Q_INVOKABLE void addSeries(QBar3DSeries *series);
79 Q_INVOKABLE void removeSeries(QBar3DSeries *series);
80 Q_INVOKABLE void insertSeries(int index, QBar3DSeries *series);
81
82 void setPrimarySeries(QBar3DSeries *series);
83 QBar3DSeries *primarySeries() const;
84 QBar3DSeries *selectedSeries() const;
85 static inline QPoint invalidSelectionPosition(){ return QPoint(-1, -1); }
86
87 void setFloorLevel(float level);
88 float floorLevel() const;
89
90protected:
91 void componentComplete() override;
92 void synchData() override;
93 void updateParameters();
94 void updateFloorLevel(float level);
95 void updateGraph() override;
96 void updateAxisRange(float min, float max) override;
97 void updateAxisReversed(bool enable) override;
98 void calculateSceneScalingFactors() override;
99 QVector3D calculateCategoryLabelPosition(QAbstract3DAxis *axis, QVector3D labelPosition,
100 int index) override;
101 float calculateCategoryGridLinePosition(QAbstract3DAxis *axis, int index) override;
102 bool handleMousePressedEvent(QMouseEvent *event) override;
103 bool handleTouchEvent(QTouchEvent *event) override;
104 void createSliceView() override;
105 void updateSliceGraph() override;
106 void handleLabelCountChanged(QQuick3DRepeater *repeater) override;
107 void updateSelectionMode(QAbstract3DGraph::SelectionFlags mode) override;
108 bool doPicking(const QPointF &position) override;
109
110public Q_SLOTS:
111 void handleAxisXChanged(QAbstract3DAxis *axis) override;
112 void handleAxisYChanged(QAbstract3DAxis *axis) override;
113 void handleAxisZChanged(QAbstract3DAxis *axis) override;
114 void handleSeriesMeshChanged(QAbstract3DSeries::Mesh mesh);
115 void handleMeshSmoothChanged(bool enable);
116 void handleRowCountChanged();
117 void handleColCountChanged();
118 void handleRowColorsChanged();
119 void handleCameraRotationChanged();
120
121Q_SIGNALS:
122 void rowAxisChanged(QCategory3DAxis *axis);
123 void valueAxisChanged(QValue3DAxis *axis);
124 void columnAxisChanged(QCategory3DAxis *axis);
125 void multiSeriesUniformChanged(bool uniform);
126 void barThicknessChanged(float thicknessRatio);
127 void barSpacingChanged(const QSizeF &spacing);
128 void barSpacingRelativeChanged(bool relative);
129 void barSeriesMarginChanged(const QSizeF &margin);
130 void meshFileNameChanged(const QString &filename);
131 void primarySeriesChanged(QBar3DSeries *series);
132 void selectedSeriesChanged(QBar3DSeries *series);
133 void floorLevelChanged(float level);
134
135private:
136 Bars3DController *m_barsController;
137
138 int m_cachedRowCount;
139 int m_cachedColumnCount;
140 int m_minRow;
141 int m_maxRow;
142 int m_minCol;
143 int m_maxCol;
144 int m_newRows;
145 int m_newCols;
146
147 float m_maxSceneSize;
148 float m_rowWidth;
149 float m_columnDepth;
150 float m_maxDimension;
151 float m_scaleFactor;
152 float m_xScaleFactor;
153 float m_zScaleFactor;
154
155 QSizeF m_cachedBarSeriesMargin;
156 QSizeF m_cachedBarThickness;
157 QSizeF m_cachedBarSpacing;
158
159 // Testing sketching
160 AxisHelper m_helperAxisX;
161 AxisHelper m_helperAxisY;
162 AxisHelper m_helperAxisZ;
163
164 float m_scaleXWithBackground = scaleWithBackground().x();
165 float m_scaleYWithBackground = scaleWithBackground().y();
166 float m_scaleZWithBackground = scaleWithBackground().z();
167 float m_xScale = scale().x();
168 float m_yScale = scale().y();
169 float m_zScale = scale().z();
170
171 float m_requestedMargin = -1.0f;
172 float m_vBackgroundMargin = 0.1f;
173 float m_hBackgroundMargin = 0.1f;
174
175 bool m_hasNegativeValues;
176 bool m_noZeroInRange;
177 float m_actualFloorLevel;
178 float m_heightNormalizer;
179 float m_gradientFraction;
180 float m_backgroundAdjustment;
181
182 float m_minHeight;
183 float m_maxHeight;
184
185 bool m_axisRangeChanged = false;
186
187 QQuick3DModel *m_floorBackground = nullptr;
188 QQuick3DNode *m_floorBackgroundScale = nullptr;
189 QQuick3DNode *m_floorBackgroundRotation = nullptr;
190
191 // Selected bar
192 QBar3DSeries *m_selectedBarSeries;
193 QPoint m_selectedBarCoord;
194 QVector3D m_selectedBarPos;
195
196 // Generate bars
197 struct BarModel
198 {
199 QQuick3DModel *model;
200 QBarDataItem *barItem;
201 QPoint coord;
202 int visualIndex;
203 float heightValue;
204 QQuick3DTexture *texture;
205
206 //instancing
207 BarInstancing *instancing = nullptr;
208 };
209
210 QHash<QBar3DSeries *, QList<BarModel *> *> m_barModelsMap;
211 QAbstract3DSeries::Mesh m_meshType = QAbstract3DSeries::MeshSphere;
212 bool m_smooth = false;
213 bool m_keepSeriesUniform;
214 bool m_hasHighlightTexture = false;
215 bool m_selectionActive = false;
216 float m_seriesScaleX;
217 float m_seriesScaleZ;
218 float m_seriesStep;
219 float m_seriesStart;
220 float m_zeroPosition;
221 int m_visibleSeriesCount;
222 QQuaternion m_meshRotation;
223 QQuick3DTexture *m_highlightTexture = nullptr;
224 QQuick3DTexture *m_multiHighlightTexture = nullptr;
225 QHash<QBar3DSeries *, QList<QQuick3DModel *> *> m_slicedBarModels;
226 bool m_selectionDirty = false;
227
228 QHash<QBar3DSeries *, QList<QQuick3DModel *> *> m_selectedModels;
229 QAbstract3DGraph::SelectionFlags m_selectionMode = QAbstract3DGraph::SelectionNone;
230
231 void calculateHeightAdjustment();
232 void calculateSeriesStartPosition();
233 void connectSeries(QBar3DSeries *series);
234 void disconnectSeries(QBar3DSeries *series);
235 void generateBars(QList<QBar3DSeries *> &barSeriesList);
236 QQuick3DModel *createDataItem(QQuick3DNode *scene, QAbstract3DSeries *series);
237 QString getMeshFileName();
238 void fixMeshFileName(QString &fileName, QAbstract3DSeries::Mesh meshType);
239 void updateBarVisuality(QBar3DSeries *series, int visualIndex);
240 void updateBarPositions(QBar3DSeries *series);
241 float updateBarHeightParameters(const QBarDataItem *item);
242 void updateBarVisuals(QBar3DSeries *series);
243 void updateItemMaterial(QQuick3DModel *item, bool useGradient, bool rangeGradient,
244 const QString &materialName);
245 void updateCustomMaterial(QQuick3DModel *item, bool isHighlight = false,
246 bool isMultiHighlight = false, QQuick3DTexture *texture = nullptr);
247 void updatePrincipledMaterial(QQuick3DModel *model, const QColor &color, bool useGradient,
248 bool isHighlight, QQuick3DTexture *texture);
249 void removeBarModels();
250 void deleteBarModels(BarModel *barModel);
251 void deleteBarItemHolders();
252 QQuick3DTexture *createTexture();
253 void setSelectedBar(QBar3DSeries *series, const QPoint &coord);
254 void createSelectedModels(QBar3DSeries *series);
255 void updateSelectedBar();
256 Abstract3DController::SelectionType isSelected(int row, int bar, QBar3DSeries *series);
257 void resetClickedStatus();
258 void removeSlicedBarModels();
259 void removeSelectedModels();
260
261 void updateBarSpecs(float thicknessRatio, const QSizeF &spacing, bool relative);
262 void updateBarSeriesMargin(const QSizeF &margin);
263
264 friend class Bars3DController;
265 friend class Q3DBars;
266};
267
268QT_END_NAMESPACE
269#endif // QQUICKGRAPHSBARS_H
270

source code of qtgraphs/src/graphs/qml/qquickgraphsbars_p.h