1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qcustom3dlabel_p.h"
5#include "utils_p.h"
6#include "qgraphs3dlogging_p.h"
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 * \class QCustom3DLabel
12 * \inmodule QtGraphs
13 * \ingroup graphs_3D
14 * \brief The QCustom3DLabel class adds a custom label to a graph.
15 *
16 * The text, font, position, scaling, rotation, and colors of a custom label can
17 * be set. In addition, the visibility of the borders and background of the
18 * label can be toggled. Colors, borders, and background are determined by the
19 * active theme unless set explicitly.
20 *
21 * \note In scaling, the z-coordinate has no effect. Setting the same x- and
22 * y-coordinates retains the original font dimensions.
23 *
24 * \sa Q3DGraphsWidgetItem::addCustomItem()
25 */
26
27/*!
28 * \qmltype Custom3DLabel
29 * \inqmlmodule QtGraphs
30 * \ingroup graphs_qml_3D
31 * \nativetype QCustom3DLabel
32 * \inherits Custom3DItem
33 * \brief Adds a custom label to a graph.
34 *
35 * The text, font, position, scaling, rotation, and colors of a custom label can
36 * be set. In addition, the visibility of the borders and background of the
37 * label can be toggled. Colors, borders, and background are determined by the
38 * active theme unless set explicitly.
39 *
40 * \note In scaling, the z-coordinate has no effect. Setting the same x- and
41 * y-coordinates retains the original font dimensions.
42 */
43
44/*! \qmlproperty string Custom3DLabel::text
45 *
46 * The text for the label. Rich text is not supported.
47 */
48
49/*! \qmlproperty font Custom3DLabel::font
50 *
51 * The font to be used for the label. Defaults to \c{Font {family: "Arial";
52 * pointSize: 20}}. Special formatting (for example, outlined) is not supported.
53 */
54
55/*! \qmlproperty color Custom3DLabel::textColor
56 *
57 * The color for the label text. Also affects label border, if enabled. Defaults
58 * to \c{"white"}.
59 *
60 * \sa borderVisible
61 */
62
63/*! \qmlproperty color Custom3DLabel::backgroundColor
64 *
65 * The color for the label background, if enabled. Defaults to \c{"gray"}.
66 *
67 * \sa backgroundVisible
68 */
69
70/*! \qmlproperty bool Custom3DLabel::backgroundVisible
71 *
72 * Defines whether the label background is visible. If set to \c{false},
73 * backgroundColor has no effect. Defaults to \c{true}.
74 */
75
76/*! \qmlproperty bool Custom3DLabel::borderVisible
77 *
78 * Defines whether label borders are visible. Defaults to \c{true}.
79 */
80
81/*! \qmlproperty bool Custom3DLabel::facingCamera
82 *
83 * Defines whether the label will always face the camera. Defaults to \c{false}.
84 * If set to \c{true}, \l {QCustom3DItem::}{rotation} has no effect.
85 */
86
87/*!
88 \qmlsignal Custom3DLabel::textChanged(string text)
89
90 This signal is emitted when \l text changes to \a text.
91*/
92
93/*!
94 \qmlsignal Custom3DLabel::fontChanged(font font)
95
96 This signal is emitted when \l font changes to \a font.
97*/
98
99/*!
100 \qmlsignal Custom3DLabel::textColorChanged(color color)
101
102 This signal is emitted when textColor changes to \a color.
103*/
104
105/*!
106 \qmlsignal Custom3DLabel::backgroundColorChanged(color color)
107
108 This signal is emitted when backgroundColor changes to \a color.
109*/
110
111/*!
112 \qmlsignal Custom3DLabel::borderEnabledChanged(bool enabled)
113
114 This signal is emitted when borderEnabled changes to \a enabled.
115*/
116
117/*!
118 \qmlsignal Custom3DLabel::backgroundEnabledChanged(bool enabled)
119
120 This signal is emitted when backgroundEnabled changes to \a enabled.
121*/
122
123/*!
124 \qmlsignal Custom3DLabel::facingCameraChanged(bool enabled)
125
126 This signal is emitted when facingCamera changes to \a enabled.
127*/
128
129/*!
130 * Constructs a custom 3D label with the given \a parent.
131 */
132QCustom3DLabel::QCustom3DLabel(QObject *parent)
133 : QCustom3DItem(*(new QCustom3DLabelPrivate()), parent)
134{}
135
136/*!
137 * Constructs a custom 3D label with the given \a text, \a font, \a position, \a
138 * scaling, \a rotation, and optional \a parent.
139 *
140 * \note Setting the same x- and y-coordinates for \a scaling retains the
141 * original font dimensions.
142 */
143QCustom3DLabel::QCustom3DLabel(const QString &text,
144 const QFont &font,
145 QVector3D position,
146 QVector3D scaling,
147 const QQuaternion &rotation,
148 QObject *parent)
149 : QCustom3DItem(*(new QCustom3DLabelPrivate(text, font, position, scaling, rotation)), parent)
150{}
151
152/*!
153 * Deletes the custom 3D label.
154 */
155QCustom3DLabel::~QCustom3DLabel() {}
156
157/*! \property QCustom3DLabel::text
158 *
159 * \brief The text for the label.
160 *
161 * Rich text is not supported.
162 */
163void QCustom3DLabel::setText(const QString &text)
164{
165 Q_D(QCustom3DLabel);
166 if (d->m_text == text) {
167 qCDebug(lcProperties3D, "%s value is already set to: %s",
168 qUtf8Printable(QLatin1String(__FUNCTION__)), qUtf8Printable(text));
169 return;
170 }
171 d->m_text = text;
172 emit textChanged(text);
173 emit needUpdate();
174}
175
176QString QCustom3DLabel::text() const
177{
178 Q_D(const QCustom3DLabel);
179 return d->m_text;
180}
181
182/*! \property QCustom3DLabel::font
183 *
184 * \brief The font to be used for the label.
185 *
186 * Defaults to \c{QFont("Arial", 20)}. Special formatting
187 * (for example, outlined) is not supported.
188 */
189void QCustom3DLabel::setFont(const QFont &font)
190{
191 Q_D(QCustom3DLabel);
192 if (d->m_font == font) {
193 qCDebug(lcProperties3D) << __FUNCTION__
194 << "value is already set to:" << font;
195 return;
196 }
197 d->m_font = font;
198 emit fontChanged(font);
199 emit needUpdate();
200}
201
202QFont QCustom3DLabel::font() const
203{
204 Q_D(const QCustom3DLabel);
205 return d->m_font;
206}
207
208/*! \property QCustom3DLabel::textColor
209 *
210 * \brief The color for the label text.
211 *
212 * Also affects the label border, if enabled. Defaults to \c{Qt::white}.
213 *
214 * \sa borderVisible
215 */
216void QCustom3DLabel::setTextColor(QColor color)
217{
218 Q_D(QCustom3DLabel);
219 if (d->m_txtColor == color) {
220 qCDebug(lcProperties3D, "%s value is already set to: %s",
221 qUtf8Printable(QLatin1String(__FUNCTION__)), qUtf8Printable(color.name()));
222 return;
223 }
224
225 d->m_txtColor = color;
226 d->m_customVisuals = true;
227 emit textColorChanged(color);
228 emit needUpdate();
229}
230
231QColor QCustom3DLabel::textColor() const
232{
233 Q_D(const QCustom3DLabel);
234 return d->m_txtColor;
235}
236
237/*! \property QCustom3DLabel::backgroundColor
238 *
239 * \brief The color for the label background, if enabled.
240 *
241 * Defaults to \c{Qt::gray}.
242 *
243 * \sa backgroundVisible
244 */
245void QCustom3DLabel::setBackgroundColor(QColor color)
246{
247 Q_D(QCustom3DLabel);
248 if (d->m_bgrColor == color) {
249 qCDebug(lcProperties3D, "%s value is already set to: %s",
250 qUtf8Printable(QLatin1String(__FUNCTION__)), qUtf8Printable(color.name()));
251 return;
252 }
253
254 d->m_bgrColor = color;
255 d->m_customVisuals = true;
256 emit backgroundColorChanged(color);
257 emit needUpdate();
258}
259
260QColor QCustom3DLabel::backgroundColor() const
261{
262 Q_D(const QCustom3DLabel);
263 return d->m_bgrColor;
264}
265
266/*! \property QCustom3DLabel::borderVisible
267 *
268 * \brief Whether label borders are visible.
269 *
270 * Defaults to \c{true}.
271 */
272void QCustom3DLabel::setBorderVisible(bool visible)
273{
274 Q_D(QCustom3DLabel);
275 if (d->m_borders == visible) {
276 qCDebug(lcProperties3D) << __FUNCTION__
277 << "value is already set to:" << visible;
278 return;
279 }
280
281 d->m_borders = visible;
282 d->m_customVisuals = true;
283 emit borderVisibleChanged(visible);
284 emit needUpdate();
285}
286
287bool QCustom3DLabel::isBorderVisible() const
288{
289 Q_D(const QCustom3DLabel);
290 return d->m_borders;
291}
292
293/*! \property QCustom3DLabel::backgroundVisible
294 *
295 * \brief Whether the label background is visible.
296 *
297 * If set to \c{false}, backgroundColor() has no effect. Defaults
298 * to \c{true}.
299 */
300void QCustom3DLabel::setBackgroundVisible(bool visible)
301{
302 Q_D(QCustom3DLabel);
303 if (d->m_background == visible) {
304 qCDebug(lcProperties3D) << __FUNCTION__
305 << "value is already set to:" << visible;
306 return;
307 }
308
309 d->m_background = visible;
310 d->m_customVisuals = true;
311 emit backgroundVisibleChanged(visible);
312 emit needUpdate();
313}
314
315bool QCustom3DLabel::isBackgroundVisible() const
316{
317 Q_D(const QCustom3DLabel);
318 return d->m_background;
319}
320
321/*! \property QCustom3DLabel::facingCamera
322 *
323 * \brief Whether the label will always face the camera.
324 *
325 * Defaults to \c{false}. If set to \c{true}, rotation()
326 * has no effect.
327 */
328void QCustom3DLabel::setFacingCamera(bool enabled)
329{
330 Q_D(QCustom3DLabel);
331 if (d->m_facingCamera == enabled) {
332 qCDebug(lcProperties3D) << __FUNCTION__
333 << "value is already set to:" << enabled;
334 return;
335 }
336 d->m_facingCamera = enabled;
337 d->m_facingCameraDirty = true;
338 emit facingCameraChanged(enabled);
339 emit needUpdate();
340}
341
342bool QCustom3DLabel::isFacingCamera() const
343{
344 Q_D(const QCustom3DLabel);
345 return d->m_facingCamera;
346}
347
348QCustom3DLabelPrivate::QCustom3DLabelPrivate()
349 : m_font(QFont(QStringLiteral("Arial"), 20))
350 , m_bgrColor(Qt::gray)
351 , m_txtColor(Qt::white)
352 , m_background(true)
353 , m_borders(true)
354 , m_facingCamera(false)
355 , m_customVisuals(false)
356 , m_facingCameraDirty(false)
357{
358 m_isLabelItem = true;
359 m_shadowCasting = false;
360 m_meshFile = QStringLiteral(":/defaultMeshes/plane");
361}
362
363QCustom3DLabelPrivate::QCustom3DLabelPrivate(const QString &text,
364 const QFont &font,
365 QVector3D position,
366 QVector3D scaling,
367 const QQuaternion &rotation)
368 : QCustom3DItemPrivate(QStringLiteral(":/defaultMeshes/plane"), position, scaling, rotation)
369 , m_text(text)
370 , m_font(font)
371 , m_bgrColor(Qt::gray)
372 , m_txtColor(Qt::white)
373 , m_background(true)
374 , m_borders(true)
375 , m_facingCamera(false)
376 , m_customVisuals(false)
377 , m_facingCameraDirty(false)
378{
379 m_isLabelItem = true;
380 m_shadowCasting = false;
381}
382
383QCustom3DLabelPrivate::~QCustom3DLabelPrivate() {}
384
385void QCustom3DLabelPrivate::resetDirtyBits()
386{
387 QCustom3DItemPrivate::resetDirtyBits();
388 m_facingCameraDirty = false;
389}
390
391QT_END_NAMESPACE
392

source code of qtgraphs/src/graphs3d/data/qcustom3dlabel.cpp