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