summaryrefslogtreecommitdiffstats
path: root/src/datavisualization/data/qcustom3dlabel.cpp
blob: 28b265edc534e4ca3acd1182d7a1d37a7c427707 (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
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.io
**
** This file is part of the Qt Data Visualization module.
**
** Licensees holding valid commercial license for Qt may use this file in
** accordance with the Qt License Agreement provided with the Software
** or, alternatively, in accordance with the terms contained in a written
** agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.io
**
****************************************************************************/

#include "qcustom3dlabel_p.h"
#include "utils_p.h"

QT_BEGIN_NAMESPACE_DATAVISUALIZATION

/*!
 * \class QCustom3DLabel
 * \inmodule QtDataVisualization
 * \brief The QCustom3DLabel class is for creating custom labels to be added to a graph.
 * \since QtDataVisualization 1.1
 *
 * This class is for creating custom labels to be added to a graph. You can set text, font,
 * position, scaling, rotation, and colors. You can also toggle borders and background for the
 * label. Colors, borders and background are used from active theme unless any of them is set
 * explicitly.
 *
 * \note In scaling, z has no effect. Setting the same x and y retains the original
 * font dimensions.
 *
 * \sa QAbstract3DGraph::addCustomItem()
 */

/*!
 * \qmltype Custom3DLabel
 * \inqmlmodule QtDataVisualization
 * \since QtDataVisualization 1.1
 * \ingroup datavisualization_qml
 * \instantiates QCustom3DLabel
 * \inherits Custom3DItem
 * \brief The Custom3DLabel type is for creating custom labels to be added to a graph.
 *
 * This type is for creating custom labels to be added to a graph. You can set text, font,
 * position, scaling, rotation, and colors. You can also toggle borders and background for the
 * label. Colors, borders and background are used from active theme unless any of them is set
 * explicitly.
 *
 * \note In scaling, z has no effect. Setting the same x and y retains the original
 * font dimensions.
 */

/*! \qmlproperty string Custom3DLabel::text
 *
 * The text for the label. Rich text is not supported.
 */

/*! \qmlproperty font Custom3DLabel::font
 *
 * The font to be used for the label. Defaults to \c{Font {family: "Arial"; pointSize: 20}}.
 * Special formatting (for example outlined) is not supported.
 */

/*! \qmlproperty color Custom3DLabel::textColor
 *
 * Color for the label text. Also affects label border, if enabled. Defaults to \c{"white"}.
 *
 * \sa borderEnabled
 */

/*! \qmlproperty color Custom3DLabel::backgroundColor
 *
 * Color for the label background, if enabled. Defaults to \c{"gray"}.
 *
 * \sa backgroundEnabled
 */

/*! \qmlproperty bool Custom3DLabel::backgroundEnabled
 *
 * Enable label background. If set to \c{false}, backgroundColor has no effect. Defaults
 * to \c{true}.
 */

/*! \qmlproperty bool Custom3DLabel::borderEnabled
 *
 * Enable label borders. Defaults to \c{true}.
 */

/*! \qmlproperty bool Custom3DLabel::facingCamera
 *
 * Forces the label to face camera always. Defaults to \c{false}. If set to \c{true}, rotation()
 * has no effect.
 */

/*!
 * Constructs QCustom3DLabel with given \a parent.
 */
QCustom3DLabel::QCustom3DLabel(QObject *parent) :
    QCustom3DItem(new QCustom3DLabelPrivate(this), parent)
{
}

/*!
 * Constructs QCustom3DLabel with given \a text, \a font, \a position, \a scaling,
 * \a rotation, and optional \a parent.
 *
 * \note Setting the same x and y for \a scaling retains the original font dimensions.
 */
QCustom3DLabel::QCustom3DLabel(const QString &text, const QFont &font,
                               const QVector3D &position, const QVector3D &scaling,
                               const QQuaternion &rotation, QObject *parent) :
    QCustom3DItem(new QCustom3DLabelPrivate(this, text, font, position, scaling, rotation),
                  parent)
{
}

/*!
 * Destroys QCustom3DLabel.
 */
QCustom3DLabel::~QCustom3DLabel()
{
}

/*! \property QCustom3DLabel::text
 *
 * The text for the label. Rich text is not supported.
 */
void QCustom3DLabel::setText(const QString &text)
{
    if (dptr()->m_text != text) {
        dptr()->m_text = text;
        dptr()->handleTextureChange();
        emit textChanged(text);
        emit dptr()->needUpdate();
    }
}

QString QCustom3DLabel::text() const
{
    return dptrc()->m_text;
}

/*! \property QCustom3DLabel::font
 *
 * The font to be used for the label. Defaults to \c{QFont("Arial", 20)}. Special formatting
 * (for example outlined) is not supported.
 */
void QCustom3DLabel::setFont(const QFont &font)
{
    if (dptr()->m_font != font) {
        dptr()->m_font = font;
        dptr()->handleTextureChange();
        emit fontChanged(font);
        emit dptr()->needUpdate();
    }
}

QFont QCustom3DLabel::font() const
{
    return dptrc()->m_font;
}

/*! \property QCustom3DLabel::textColor
 *
 * Color for the label text. Also affects label border, if enabled. Defaults to \c{Qt::white}.
 *
 * \sa borderEnabled
 */
void QCustom3DLabel::setTextColor(const QColor &color)
{
    if (dptr()->m_txtColor != color) {
        dptr()->m_txtColor = color;
        dptr()->m_customVisuals = true;
        dptr()->handleTextureChange();
        emit textColorChanged(color);
        emit dptr()->needUpdate();
    }
}

QColor QCustom3DLabel::textColor() const
{
    return dptrc()->m_txtColor;
}

/*! \property QCustom3DLabel::backgroundColor
 *
 * Color for the label background, if enabled. Defaults to \c{Qt::gray}.
 *
 * \sa backgroundEnabled
 */
void QCustom3DLabel::setBackgroundColor(const QColor &color)
{
    if (dptr()->m_bgrColor != color) {
        dptr()->m_bgrColor = color;
        dptr()->m_customVisuals = true;
        dptr()->handleTextureChange();
        emit backgroundColorChanged(color);
        emit dptr()->needUpdate();
    }
}

QColor QCustom3DLabel::backgroundColor() const
{
    return dptrc()->m_bgrColor;
}

/*! \property QCustom3DLabel::borderEnabled
 *
 * Enable label borders. Defaults to \c{true}.
 */
void QCustom3DLabel::setBorderEnabled(bool enabled)
{
    if (dptr()->m_borders != enabled) {
        dptr()->m_borders = enabled;
        dptr()->m_customVisuals = true;
        dptr()->handleTextureChange();
        emit borderEnabledChanged(enabled);
        emit dptr()->needUpdate();
    }
}

bool QCustom3DLabel::isBorderEnabled() const
{
    return dptrc()->m_borders;
}

/*! \property QCustom3DLabel::backgroundEnabled
 *
 * Enable label background. If set to \c{false}, backgroundColor() has no effect. Defaults
 * to \c{true}.
 */
void QCustom3DLabel::setBackgroundEnabled(bool enabled)
{
    if (dptr()->m_background != enabled) {
        dptr()->m_background = enabled;
        dptr()->m_customVisuals = true;
        dptr()->handleTextureChange();
        emit backgroundEnabledChanged(enabled);
        emit dptr()->needUpdate();
    }
}

bool QCustom3DLabel::isBackgroundEnabled() const
{
    return dptrc()->m_background;
}

/*! \property QCustom3DLabel::facingCamera
 *
 * Forces the label to face camera always. Defaults to \c{false}. If set to \c{true}, rotation()
 * has no effect.
 */
void QCustom3DLabel::setFacingCamera(bool enabled)
{
    if (dptr()->m_facingCamera != enabled) {
        dptr()->m_facingCamera = enabled;
        dptr()->m_facingCameraDirty = true;
        emit facingCameraChanged(enabled);
        emit dptr()->needUpdate();
    }
}

bool QCustom3DLabel::isFacingCamera() const
{
    return dptrc()->m_facingCamera;
}

/*!
 * \internal
 */
QCustom3DLabelPrivate *QCustom3DLabel::dptr()
{
    return static_cast<QCustom3DLabelPrivate *>(d_ptr.data());
}

/*!
 * \internal
 */
const QCustom3DLabelPrivate *QCustom3DLabel::dptrc() const
{
    return static_cast<const QCustom3DLabelPrivate *>(d_ptr.data());
}

QCustom3DLabelPrivate::QCustom3DLabelPrivate(QCustom3DLabel *q) :
    QCustom3DItemPrivate(q),
    m_font(QFont(QStringLiteral("Arial"), 20)),
    m_bgrColor(Qt::gray),
    m_txtColor(Qt::white),
    m_background(true),
    m_borders(true),
    m_facingCamera(false),
    m_customVisuals(false),
    m_facingCameraDirty(false)
{
    m_isLabelItem = true;
    m_shadowCasting = false;
    m_meshFile = QStringLiteral(":/defaultMeshes/plane");
    createTextureImage();
}

QCustom3DLabelPrivate::QCustom3DLabelPrivate(QCustom3DLabel *q, const QString &text,
                                             const QFont &font, const QVector3D &position,
                                             const QVector3D &scaling,
                                             const QQuaternion &rotation) :
    QCustom3DItemPrivate(q, QStringLiteral(":/defaultMeshes/plane"), position, scaling, rotation),
    m_text(text),
    m_font(font),
    m_bgrColor(Qt::gray),
    m_txtColor(Qt::white),
    m_background(true),
    m_borders(true),
    m_facingCamera(false),
    m_customVisuals(false),
    m_facingCameraDirty(false)
{
    m_isLabelItem = true;
    m_shadowCasting = false;
    createTextureImage();
}

QCustom3DLabelPrivate::~QCustom3DLabelPrivate()
{
}

void QCustom3DLabelPrivate::resetDirtyBits()
{
    QCustom3DItemPrivate::resetDirtyBits();
    m_facingCameraDirty = false;
}

void QCustom3DLabelPrivate::createTextureImage()
{
    createTextureImage(m_bgrColor, m_txtColor, m_background, m_borders);
}

void QCustom3DLabelPrivate::createTextureImage(const QColor &bgrColor, const QColor &txtColor,
                                               bool background, bool borders)
{
    m_textureImage = Utils::printTextToImage(m_font, m_text, bgrColor, txtColor, background,
                                             borders, 0);
}

void QCustom3DLabelPrivate::handleTextureChange()
{
    createTextureImage();
    m_dirtyBits.textureDirty = true;
    if (!m_textureFile.isEmpty()) {
        m_textureFile.clear();
        emit q_ptr->textureFileChanged(m_textureFile);
    }
}

QT_END_NAMESPACE_DATAVISUALIZATION