summaryrefslogtreecommitdiffstats
path: root/src/graphs3d/widget/q3dbarswidgetitem.cpp
blob: 0cf833e59761b7ea7b9d09378a31e6a69de8ecfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtGraphsWidgets/q3dbarswidgetitem.h>
#include <private/q3dbarswidgetitem_p.h>
#include <private/qquickgraphsbars_p.h>

QT_BEGIN_NAMESPACE

/*!
 * \class Q3DBarsWidgetItem
 * \inmodule QtGraphsWidgets
 * \ingroup graphs_3D_widgets
 * \brief The Q3DBarsWidgetItem class provides methods for rendering 3D bar graphs.
 *
 * This class enables developers to render 3D bar graphs and view them by
 * freely rotating the scene. Rotation is achieved by holding down the right
 * mouse button and moving the mouse, while zooming is accomplished using
 * the mouse wheel. If enabled, selection is performed with the left mouse
 * button. The scene can be reset to the default camera view by clicking the
 * mouse wheel. On touch devices, rotation is achieved by tap-and-move,
 * selection by tap-and-hold, and zooming by pinch.
 *
 * If no axes are set explicitly for Q3DBarsWidgetItem, temporary default axes without
 * labels are created. These default axes can be modified via axis accessors,
 * but as soon as any axis is set explicitly for the orientation, the default
 * axis for that orientation is destroyed.
 *
 * Q3DBarsWidgetItem supports more than one visible series at the same time. All series
 * don't need to have the same number of rows and columns. Row and column
 * labels are taken from the first added series unless explicitly defined
 * for row and column axes.
 *
 * Q3DBarsWidgetItem has transparency support. This feature allows you to adjust the
 * opacity of the bars, making them partially see-through, fully transparent,
 * or opaque.
 *
 * \section1 How to construct a minimal Q3DBarsWidgetItem graph
 *
 * First, construct an instance of Q3DBarsWidgetItem:
 *
 * \snippet doc_src_q3dbars_construction.cpp 4
 *
 * After constructing Q3DBarsWidgetItem, you can set the data window by changing the range
 * on the row and column axes. It is not mandatory, as the data window will default
 * to showing all of the data in the series. If the amount of data is large, it
 * is usually preferable to show just a portion of it. For example, let's set
 * the data window to display the first five rows and columns:
 *
 * \snippet doc_src_q3dbars_construction.cpp 0
 *
 * Now, Q3DBarsWidgetItem is ready to receive data to be rendered. Create a series with one
 * row of 5 values:
 *
 * \snippet doc_src_q3dbars_construction.cpp 1
 *
 * \note We set the data window to 5 x 5, but we are adding only one row of
 * data. This is okay; the rest of the rows will just be blank.
 *
 * Finally you will need to set it visible:
 *
 * \snippet doc_src_q3dbars_construction.cpp 2
 *
 * The complete code needed to create and display this graph is:
 *
 * \snippet doc_src_q3dbars_construction.cpp 3
 *
 * And this is what those few lines of code produce:
 *
 * \image q3dbars-minimal.png
 *
 * The scene can be rotated, zoomed into, and a bar can be selected to view its
 * value, but no other interactions are included in this minimal code example.
 * You can learn more by familiarizing yourself with the examples provided, like
 * the \l{Simple Bar Graph}.
 *
 * \sa Q3DScatterWidgetItem, Q3DSurfaceWidgetItem, {Qt Graphs C++ Classes for 3D}
 */

/*!
 * Constructs a new 3D bar graph with the optional \a parent.
 */
Q3DBarsWidgetItem::Q3DBarsWidgetItem(QObject *parent)
    : Q3DGraphsWidgetItem(*(new Q3DBarsWidgetItemPrivate()), parent, QStringLiteral("Bars3D"))
{}

/*!
 * Destroys the 3D bar graph.
 */
Q3DBarsWidgetItem::~Q3DBarsWidgetItem() {}

/*!
 * \property Q3DBarsWidgetItem::primarySeries
 *
 * \brief The primary series of the graph.
 *
 * Sets \a series as the primary series of the graph. The primary series
 * determines the row and column axis labels when the labels are not explicitly
 * set to the axes.
 *
 * If the specified series is not yet added to the graph, setting it as the
 * primary series will also implicitly add it to the graph.
 *
 * If the primary series itself is removed from the graph, this property
 * resets to default.
 *
 * If \a series is null, this property resets to default.
 * Defaults to the first added series or zero if no series are added to the
 * graph.
 */
void Q3DBarsWidgetItem::setPrimarySeries(QBar3DSeries *series)
{
    graphBars()->setPrimarySeries(series);
    emit primarySeriesChanged(series);
}

QBar3DSeries *Q3DBarsWidgetItem::primarySeries() const
{
    return graphBars()->primarySeries();
}

/*!
 * Adds the \a series to the graph. A graph can contain multiple series, but
 * only one set of axes, so the rows and columns of all series must match for
 * the visualized data to be meaningful. If the graph has multiple visible
 * series, only the primary series will generate the row or column labels on the
 * axes in cases where the labels are not explicitly set for the axes. If the
 * newly added series has specified a selected bar, it will be highlighted and
 * any existing selection will be cleared. Only one added series can have an
 * active selection.
 *
 * \sa seriesList(), primarySeries, Q3DGraphsWidgetItem::hasSeries()
 */
void Q3DBarsWidgetItem::addSeries(QBar3DSeries *series)
{
    graphBars()->addSeries(series);
}

/*!
 * Removes the \a series from the graph.
 *
 * \sa Q3DGraphsWidgetItem::hasSeries()
 */
void Q3DBarsWidgetItem::removeSeries(QBar3DSeries *series)
{
    graphBars()->removeSeries(series);
}

/*!
 * Inserts the \a series into the position \a index in the series list.
 * If the \a series has already been added to the list, it is moved to the
 * new \a index.
 * \note When moving a series to a new \a index that is after its old index,
 * the new position in the list is calculated as if the series was still in its
 * old index, so the final index is actually the \a index decremented by one.
 *
 * \sa addSeries(), seriesList(), Q3DGraphsWidgetItem::hasSeries()
 */
void Q3DBarsWidgetItem::insertSeries(int index, QBar3DSeries *series)
{
    graphBars()->insertSeries(index, series);
}

/*!
 * Returns the list of series added to this graph.
 *
 * \sa Q3DGraphsWidgetItem::hasSeries()
 */
QList<QBar3DSeries *> Q3DBarsWidgetItem::seriesList() const
{
    QList<QBar3DSeries *> barSeriesList;
    for (QAbstract3DSeries *abstractSeries : graphBars()->m_seriesList) {
        QBar3DSeries *barSeries = qobject_cast<QBar3DSeries *>(abstractSeries);
        if (barSeries)
            barSeriesList.append(barSeries);
    }

    return barSeriesList;
}

/*!
 * \property Q3DBarsWidgetItem::multiSeriesUniform
 *
 * \brief Whether bars are to be scaled with proportions set to a single series
 * bar even if there are multiple series displayed.
 *
 * If set to \c {true}, \l{barSpacing}{bar spacing} will be correctly applied
 * only to the X-axis. Preset to \c false by default.
 */
void Q3DBarsWidgetItem::setMultiSeriesUniform(bool uniform)
{
    graphBars()->setMultiSeriesUniform(uniform);
    emit multiSeriesUniformChanged(uniform);
}

bool Q3DBarsWidgetItem::isMultiSeriesUniform() const
{
    return graphBars()->isMultiSeriesUniform();
}

/*!
 * \property Q3DBarsWidgetItem::barThickness
 *
 * \brief The bar thickness ratio between the X and Z dimensions.
 *
 * The value \c 1.0 means that the bars are as wide as they are deep, whereas
 *\c 0.5 makes them twice as deep as they are wide. Preset to \c 1.0 by default.
 */
void Q3DBarsWidgetItem::setBarThickness(float thicknessRatio)
{
    graphBars()->setBarThickness(thicknessRatio);
    emit barThicknessChanged(thicknessRatio);
}

float Q3DBarsWidgetItem::barThickness() const
{
    return graphBars()->barThickness();
}

/*!
 * \property Q3DBarsWidgetItem::barSpacing
 *
 * \brief Bar spacing in the X and Z dimensions.
 *
 * Preset to \c {(1.0, 1.0)} by default. Spacing is affected by the
 * barSpacingRelative property.
 *
 * \sa barSpacingRelative, multiSeriesUniform, barSeriesMargin
 */
void Q3DBarsWidgetItem::setBarSpacing(QSizeF spacing)
{
    graphBars()->setBarSpacing(spacing);
    emit barSpacingChanged(spacing);
}

QSizeF Q3DBarsWidgetItem::barSpacing() const
{
    return graphBars()->barSpacing();
}

/*!
 * \property Q3DBarsWidgetItem::barSpacingRelative
 *
 * \brief Whether spacing is absolute or relative to bar thickness.
 *
 * If it is \c true, the value of \c 0.0 means that the bars are placed
 * side-to-side, \c 1.0 means that a space as wide as the thickness of one bar
 * is left between the bars, and so on. Preset to \c true.
 */
void Q3DBarsWidgetItem::setBarSpacingRelative(bool relative)
{
    graphBars()->setBarSpacingRelative(relative);
    emit barSpacingRelativeChanged(relative);
}

bool Q3DBarsWidgetItem::isBarSpacingRelative() const
{
    return graphBars()->isBarSpacingRelative();
}

/*!
 * \property Q3DBarsWidgetItem::barSeriesMargin
 *
 * \brief Margin between series columns in X and Z dimensions.
 * Sensible values are on the range [0,1).
 *
 * Preset to \c {(0.0, 0.0)} by default. This property enables
 * showing bars from different series side by side, but with space between
 * columns.
 *
 * \sa barSpacing
 */
void Q3DBarsWidgetItem::setBarSeriesMargin(QSizeF margin)
{
    graphBars()->setBarSeriesMargin(margin);
    emit barSeriesMarginChanged(margin);
}

QSizeF Q3DBarsWidgetItem::barSeriesMargin() const
{
    return graphBars()->barSeriesMargin();
}

/*!
 * \property Q3DBarsWidgetItem::rowAxis
 *
 * \brief The axis attached to the active row.
 *
 * Sets the axis of the active row to \a axis. Implicitly calls addAxis() to
 * transfer the ownership of the axis to this graph.
 *
 * If \a axis is null, a temporary default axis with no labels is created.
 * This temporary axis is destroyed if another axis is set explicitly to the
 * same orientation.
 *
 * \sa addAxis(), releaseAxis()
 */
void Q3DBarsWidgetItem::setRowAxis(QCategory3DAxis *axis)
{
    graphBars()->setRowAxis(axis);
    emit rowAxisChanged(rowAxis());
}

QCategory3DAxis *Q3DBarsWidgetItem::rowAxis() const
{
    return graphBars()->rowAxis();
}

/*!
 * \property Q3DBarsWidgetItem::columnAxis
 *
 * \brief The axis attached to the active column.
 *
 * Sets the axis of the active column to \a axis. Implicitly calls addAxis() to
 * transfer the ownership of the axis to this graph.
 *
 * If \a axis is null, a temporary default axis with no labels is created.
 * This temporary axis is destroyed if another axis is set explicitly to the
 * same orientation.
 *
 * \sa addAxis(), releaseAxis()
 */
void Q3DBarsWidgetItem::setColumnAxis(QCategory3DAxis *axis)
{
    graphBars()->setColumnAxis(axis);
    emit columnAxisChanged(columnAxis());
}

QCategory3DAxis *Q3DBarsWidgetItem::columnAxis() const
{
    return graphBars()->columnAxis();
}

/*!
 * \property Q3DBarsWidgetItem::valueAxis
 *
 * Sets the active value axis (the Y-axis) to \a axis. Implicitly calls
 * addAxis() to transfer the ownership of \a axis to this graph.
 *
 * If \a axis is null, a temporary default axis with no labels and
 * an automatically adjusting range is created.
 * This temporary axis is destroyed if another axis is set explicitly to the
 * same orientation.
 *
 * \sa addAxis(), releaseAxis()
 */
void Q3DBarsWidgetItem::setValueAxis(QValue3DAxis *axis)
{
    graphBars()->setValueAxis(axis);
    emit valueAxisChanged(valueAxis());
}

QValue3DAxis *Q3DBarsWidgetItem::valueAxis() const
{
    return graphBars()->valueAxis();
}

/*!
 * \property Q3DBarsWidgetItem::selectedSeries
 * \readonly
 *
 * \brief The selected series or a null value.
 *
 * If selectionMode has the \c SelectionMultiSeries flag set, this
 * property holds the series that owns the selected bar.
 */
QBar3DSeries *Q3DBarsWidgetItem::selectedSeries() const
{
    return graphBars()->selectedSeries();
}

/*!
 * \property Q3DBarsWidgetItem::floorLevel
 *
 * \brief The floor level for the bar graph in Y-axis data coordinates.
 *
 * The actual floor level will be restricted by the Y-axis minimum and maximum
 * values.
 * Defaults to zero.
 */
void Q3DBarsWidgetItem::setFloorLevel(float level)
{
    graphBars()->setFloorLevel(level);
    emit floorLevelChanged(level);
}

float Q3DBarsWidgetItem::floorLevel() const
{
    return graphBars()->floorLevel();
}

bool Q3DBarsWidgetItem::event(QEvent *event)
{
    return Q3DGraphsWidgetItem::event(event);
}

/*!
 * Adds \a axis to the graph. The axes added via addAxis are not yet taken to
 * use, addAxis is simply used to give the ownership of the \a axis to the
 * graph. The \a axis must not be null or added to another graph.
 *
 * \sa releaseAxis(), setValueAxis(), setRowAxis(), setColumnAxis()
 */
void Q3DBarsWidgetItem::addAxis(QAbstract3DAxis *axis)
{
    graphBars()->addAxis(axis);
}

/*!
 * Releases the ownership of the \a axis back to the caller, if it is added to
 * this graph. If the released \a axis is in use, a new default axis will be
 * created and set active.
 *
 * If the default axis is released and added back later, it behaves as any other
 * axis would.
 *
 * \sa addAxis(), setValueAxis(), setRowAxis(), setColumnAxis()
 */
void Q3DBarsWidgetItem::releaseAxis(QAbstract3DAxis *axis)
{
    graphBars()->releaseAxis(axis);
}

/*!
 * Returns the list of all added axes.
 *
 * \sa addAxis()
 */
QList<QAbstract3DAxis *> Q3DBarsWidgetItem::axes() const
{
    return graphBars()->axes();
}

/*!
 * \internal
 */
QQuickGraphsBars *Q3DBarsWidgetItem::graphBars()
{
    Q_D(Q3DBarsWidgetItem);
    return static_cast<QQuickGraphsBars *>(d->m_graphsItem.get());
}

/*!
 * \internal
 */
const QQuickGraphsBars *Q3DBarsWidgetItem::graphBars() const
{
    Q_D(const Q3DBarsWidgetItem);
    return static_cast<const QQuickGraphsBars *>(d->m_graphsItem.get());
}

/*!
 * Exports the requested slice view to an image.
 * The exported slice is bars of row or column, which is defined by \a sliceType,
 * at a given \a requestedIndex.
 * Returns a shared pointer to grab result which can be used to access the
 * exported image when it's ready. Image is rendered with the current
 * antialiasing settings.
 *
 * \sa QQuickItem::grabToImage()
 *
 * \since 6.10
 */
QSharedPointer<QQuickItemGrabResult>
Q3DBarsWidgetItem::renderSliceToImage(int requestedIndex, QtGraphs3D::SliceType sliceType)
{
    return graphBars()->renderSliceToImage(requestedIndex, sliceType);
}

QT_END_NAMESPACE