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 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | /*! |
10 | * \class QCustom3DLabel |
11 | * \inmodule QtGraphs |
12 | * \brief The QCustom3DLabel class adds a custom label to a graph. |
13 | * |
14 | * The text, font, position, scaling, rotation, and colors of a custom label can |
15 | * be set. In addition, the visibility of the borders and background of the |
16 | * label can be toggled. Colors, borders, and background are determined by the |
17 | * active theme unless set explicitly. |
18 | * |
19 | * \note In scaling, the z-coordinate has no effect. Setting the same x- and |
20 | * y-coordinates retains the original font dimensions. |
21 | * |
22 | * \sa QAbstract3DGraph::addCustomItem() |
23 | */ |
24 | |
25 | /*! |
26 | * \qmltype Custom3DLabel |
27 | * \inqmlmodule QtGraphs |
28 | * \ingroup graphs_qml |
29 | * \instantiates QCustom3DLabel |
30 | * \inherits Custom3DItem |
31 | * \brief Adds a custom label to a graph. |
32 | * |
33 | * The text, font, position, scaling, rotation, and colors of a custom label can |
34 | * be set. In addition, the visibility of the borders and background of the |
35 | * label can be toggled. Colors, borders, and background are determined by the |
36 | * active theme unless set explicitly. |
37 | * |
38 | * \note In scaling, the z-coordinate has no effect. Setting the same x- and |
39 | * y-coordinates retains the original font dimensions. |
40 | */ |
41 | |
42 | /*! \qmlproperty string Custom3DLabel::text |
43 | * |
44 | * The text for the label. Rich text is not supported. |
45 | */ |
46 | |
47 | /*! \qmlproperty font Custom3DLabel::font |
48 | * |
49 | * The font to be used for the label. Defaults to \c{Font {family: "Arial"; pointSize: 20}}. |
50 | * Special formatting (for example, outlined) is not supported. |
51 | */ |
52 | |
53 | /*! \qmlproperty color Custom3DLabel::textColor |
54 | * |
55 | * The color for the label text. Also affects label border, if enabled. Defaults to \c{"white"}. |
56 | * |
57 | * \sa borderEnabled |
58 | */ |
59 | |
60 | /*! \qmlproperty color Custom3DLabel::backgroundColor |
61 | * |
62 | * The color for the label background, if enabled. Defaults to \c{"gray"}. |
63 | * |
64 | * \sa backgroundEnabled |
65 | */ |
66 | |
67 | /*! \qmlproperty bool Custom3DLabel::backgroundEnabled |
68 | * |
69 | * Defines whether the label background is enabled. If set to \c{false}, |
70 | * backgroundColor has no effect. Defaults to \c{true}. |
71 | */ |
72 | |
73 | /*! \qmlproperty bool Custom3DLabel::borderEnabled |
74 | * |
75 | * Defines whether label borders are enabled. Defaults to \c{true}. |
76 | */ |
77 | |
78 | /*! \qmlproperty bool Custom3DLabel::facingCamera |
79 | * |
80 | * Defines whether the label will always face the camera. Defaults to \c{false}. |
81 | * If set to \c{true}, \l {QCustom3DItem::}{rotation} has no effect. |
82 | */ |
83 | |
84 | /*! |
85 | * Constructs a custom 3D label with the given \a parent. |
86 | */ |
87 | QCustom3DLabel::QCustom3DLabel(QObject *parent) : |
88 | QCustom3DItem(new QCustom3DLabelPrivate(this), parent) |
89 | { |
90 | } |
91 | |
92 | /*! |
93 | * Constructs a custom 3D label with the given \a text, \a font, \a position, \a scaling, |
94 | * \a rotation, and optional \a parent. |
95 | * |
96 | * \note Setting the same x- and y-coordinates for \a scaling retains the |
97 | * original font dimensions. |
98 | */ |
99 | QCustom3DLabel::QCustom3DLabel(const QString &text, const QFont &font, |
100 | const QVector3D &position, const QVector3D &scaling, |
101 | const QQuaternion &rotation, QObject *parent) : |
102 | QCustom3DItem(new QCustom3DLabelPrivate(this, text, font, position, scaling, rotation), |
103 | parent) |
104 | { |
105 | } |
106 | |
107 | /*! |
108 | * Deletes the custom 3D label. |
109 | */ |
110 | QCustom3DLabel::~QCustom3DLabel() |
111 | { |
112 | } |
113 | |
114 | /*! \property QCustom3DLabel::text |
115 | * |
116 | * \brief The text for the label. |
117 | * |
118 | * Rich text is not supported. |
119 | */ |
120 | void QCustom3DLabel::setText(const QString &text) |
121 | { |
122 | Q_D(QCustom3DLabel); |
123 | if (d->m_text != text) { |
124 | d->m_text = text; |
125 | emit textChanged(text); |
126 | emit needUpdate(); |
127 | } |
128 | } |
129 | |
130 | QString QCustom3DLabel::text() const |
131 | { |
132 | const Q_D(QCustom3DLabel); |
133 | return d->m_text; |
134 | } |
135 | |
136 | /*! \property QCustom3DLabel::font |
137 | * |
138 | * \brief The font to be used for the label. |
139 | * |
140 | * Defaults to \c{QFont("Arial", 20)}. Special formatting |
141 | * (for example, outlined) is not supported. |
142 | */ |
143 | void QCustom3DLabel::setFont(const QFont &font) |
144 | { |
145 | Q_D(QCustom3DLabel); |
146 | if (d->m_font != font) { |
147 | d->m_font = font; |
148 | emit fontChanged(font); |
149 | emit needUpdate(); |
150 | } |
151 | } |
152 | |
153 | QFont QCustom3DLabel::font() const |
154 | { |
155 | const Q_D(QCustom3DLabel); |
156 | return d->m_font; |
157 | } |
158 | |
159 | /*! \property QCustom3DLabel::textColor |
160 | * |
161 | * \brief The color for the label text. |
162 | * |
163 | * Also affects the label border, if enabled. Defaults to \c{Qt::white}. |
164 | * |
165 | * \sa borderEnabled |
166 | */ |
167 | void QCustom3DLabel::setTextColor(const QColor &color) |
168 | { |
169 | Q_D(QCustom3DLabel); |
170 | if (d->m_txtColor != color) { |
171 | d->m_txtColor = color; |
172 | d->m_customVisuals = true; |
173 | emit textColorChanged(color); |
174 | emit needUpdate(); |
175 | } |
176 | } |
177 | |
178 | QColor QCustom3DLabel::textColor() const |
179 | { |
180 | const Q_D(QCustom3DLabel); |
181 | return d->m_txtColor; |
182 | } |
183 | |
184 | /*! \property QCustom3DLabel::backgroundColor |
185 | * |
186 | * \brief The color for the label background, if enabled. |
187 | * |
188 | * Defaults to \c{Qt::gray}. |
189 | * |
190 | * \sa backgroundEnabled |
191 | */ |
192 | void QCustom3DLabel::setBackgroundColor(const QColor &color) |
193 | { |
194 | Q_D(QCustom3DLabel); |
195 | if (d->m_bgrColor != color) { |
196 | d->m_bgrColor = color; |
197 | d->m_customVisuals = true; |
198 | emit backgroundColorChanged(color); |
199 | emit needUpdate(); |
200 | } |
201 | } |
202 | |
203 | QColor QCustom3DLabel::backgroundColor() const |
204 | { |
205 | const Q_D(QCustom3DLabel); |
206 | return d->m_bgrColor; |
207 | } |
208 | |
209 | /*! \property QCustom3DLabel::borderEnabled |
210 | * |
211 | * \brief Whether label borders are enabled. |
212 | * |
213 | * Defaults to \c{true}. |
214 | */ |
215 | void QCustom3DLabel::setBorderEnabled(bool enabled) |
216 | { |
217 | Q_D(QCustom3DLabel); |
218 | if (d->m_borders != enabled) { |
219 | d->m_borders = enabled; |
220 | d->m_customVisuals = true; |
221 | emit borderEnabledChanged(enabled); |
222 | emit needUpdate(); |
223 | } |
224 | } |
225 | |
226 | bool QCustom3DLabel::isBorderEnabled() const |
227 | { |
228 | const Q_D(QCustom3DLabel); |
229 | return d->m_borders; |
230 | } |
231 | |
232 | /*! \property QCustom3DLabel::backgroundEnabled |
233 | * |
234 | * \brief Whether the label background is enabled. |
235 | * |
236 | * If set to \c{false}, backgroundColor() has no effect. Defaults |
237 | * to \c{true}. |
238 | */ |
239 | void QCustom3DLabel::setBackgroundEnabled(bool enabled) |
240 | { |
241 | Q_D(QCustom3DLabel); |
242 | if (d->m_background != enabled) { |
243 | d->m_background = enabled; |
244 | d->m_customVisuals = true; |
245 | emit backgroundEnabledChanged(enabled); |
246 | emit needUpdate(); |
247 | } |
248 | } |
249 | |
250 | bool QCustom3DLabel::isBackgroundEnabled() const |
251 | { |
252 | const Q_D(QCustom3DLabel); |
253 | return d->m_background; |
254 | } |
255 | |
256 | /*! \property QCustom3DLabel::facingCamera |
257 | * |
258 | * \brief Whether the label will always face the camera. |
259 | * |
260 | * Defaults to \c{false}. If set to \c{true}, rotation() |
261 | * has no effect. |
262 | */ |
263 | void QCustom3DLabel::setFacingCamera(bool enabled) |
264 | { |
265 | Q_D(QCustom3DLabel); |
266 | if (d->m_facingCamera != enabled) { |
267 | d->m_facingCamera = enabled; |
268 | d->m_facingCameraDirty = true; |
269 | emit facingCameraChanged(enabled); |
270 | emit needUpdate(); |
271 | } |
272 | } |
273 | |
274 | bool QCustom3DLabel::isFacingCamera() const |
275 | { |
276 | const Q_D(QCustom3DLabel); |
277 | return d->m_facingCamera; |
278 | } |
279 | |
280 | QCustom3DLabelPrivate::QCustom3DLabelPrivate(QCustom3DLabel *q) : |
281 | QCustom3DItemPrivate(q), |
282 | m_font(QFont(QStringLiteral("Arial" ), 20)), |
283 | m_bgrColor(Qt::gray), |
284 | m_txtColor(Qt::white), |
285 | m_background(true), |
286 | m_borders(true), |
287 | m_facingCamera(false), |
288 | m_customVisuals(false), |
289 | m_facingCameraDirty(false) |
290 | { |
291 | m_isLabelItem = true; |
292 | m_shadowCasting = false; |
293 | m_meshFile = QStringLiteral(":/defaultMeshes/plane" ); |
294 | } |
295 | |
296 | QCustom3DLabelPrivate::QCustom3DLabelPrivate(QCustom3DLabel *q, const QString &text, |
297 | const QFont &font, const QVector3D &position, |
298 | const QVector3D &scaling, |
299 | const QQuaternion &rotation) : |
300 | QCustom3DItemPrivate(q, QStringLiteral(":/defaultMeshes/plane" ), position, scaling, rotation), |
301 | m_text(text), |
302 | m_font(font), |
303 | m_bgrColor(Qt::gray), |
304 | m_txtColor(Qt::white), |
305 | m_background(true), |
306 | m_borders(true), |
307 | m_facingCamera(false), |
308 | m_customVisuals(false), |
309 | m_facingCameraDirty(false) |
310 | { |
311 | m_isLabelItem = true; |
312 | m_shadowCasting = false; |
313 | } |
314 | |
315 | QCustom3DLabelPrivate::~QCustom3DLabelPrivate() |
316 | { |
317 | } |
318 | |
319 | void QCustom3DLabelPrivate::resetDirtyBits() |
320 | { |
321 | QCustom3DItemPrivate::resetDirtyBits(); |
322 | m_facingCameraDirty = false; |
323 | } |
324 | |
325 | QT_END_NAMESPACE |
326 | |