| 1 | // Copyright (C) 2016 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only | 
| 3 |  | 
| 4 | #include <QtCharts/QCandlestickSet> | 
| 5 | #include <private/qcandlestickset_p.h> | 
| 6 |  | 
| 7 | QT_BEGIN_NAMESPACE | 
| 8 |  | 
| 9 | /*! | 
| 10 |     \class QCandlestickSet | 
| 11 |     \since 5.8 | 
| 12 |     \inmodule QtCharts | 
| 13 |     \brief The QCandlestickSet class represents a single candlestick item in a | 
| 14 |     candlestick chart. | 
| 15 |  | 
| 16 |     Five values are needed to create a graphical representation of a candlestick | 
| 17 |     item: \e open, \e high, \e low, \e close, and \e timestamp. These values can | 
| 18 |     be either passed to a QCandlestickSet constructor | 
| 19 |     or set by using setOpen(), setHigh(), setLow(), setClose(), and setTimestamp(). | 
| 20 |  | 
| 21 |     \sa QCandlestickSeries | 
| 22 | */ | 
| 23 |  | 
| 24 | /*! | 
| 25 |     \qmltype CandlestickSet | 
| 26 |     \since QtCharts 2.2 | 
| 27 |     \nativetype QCandlestickSet | 
| 28 |     \inqmlmodule QtCharts | 
| 29 |     \brief Represents a single candlestick item in a candlestick chart. | 
| 30 |  | 
| 31 |     Five values are needed to create a graphical representation of a candlestick | 
| 32 |     item: \l open, \l high, \l low, \l close, and \l timestamp. | 
| 33 |  | 
| 34 |     \sa CandlestickSeries | 
| 35 | */ | 
| 36 |  | 
| 37 | /*! | 
| 38 |     \property QCandlestickSet::timestamp | 
| 39 |     \brief The timestamp value of the candlestick item. | 
| 40 | */ | 
| 41 |  | 
| 42 | /*! | 
| 43 |     \qmlproperty real CandlestickSet::timestamp | 
| 44 |     The timestamp value of the candlestick item. | 
| 45 | */ | 
| 46 |  | 
| 47 | /*! | 
| 48 |     \property QCandlestickSet::open | 
| 49 |     \brief The open value of the candlestick item. | 
| 50 | */ | 
| 51 |  | 
| 52 | /*! | 
| 53 |     \qmlproperty real CandlestickSet::open | 
| 54 |     The open value of the candlestick item. | 
| 55 | */ | 
| 56 |  | 
| 57 | /*! | 
| 58 |     \property QCandlestickSet::high | 
| 59 |     \brief The high value of the candlestick item. | 
| 60 | */ | 
| 61 |  | 
| 62 | /*! | 
| 63 |     \qmlproperty real CandlestickSet::high | 
| 64 |     The high value of the candlestick item. | 
| 65 | */ | 
| 66 |  | 
| 67 | /*! | 
| 68 |     \property QCandlestickSet::low | 
| 69 |     \brief The low value of the candlestick item. | 
| 70 | */ | 
| 71 |  | 
| 72 | /*! | 
| 73 |     \qmlproperty real CandlestickSet::low | 
| 74 |     The low value of the candlestick item. | 
| 75 | */ | 
| 76 |  | 
| 77 | /*! | 
| 78 |     \property QCandlestickSet::close | 
| 79 |     \brief The close value of the candlestick item. | 
| 80 | */ | 
| 81 |  | 
| 82 | /*! | 
| 83 |     \qmlproperty real CandlestickSet::close | 
| 84 |     The close value of the candlestick item. | 
| 85 | */ | 
| 86 |  | 
| 87 | /*! | 
| 88 |     \property QCandlestickSet::brush | 
| 89 |     \brief The brush used to fill the candlestick item. | 
| 90 | */ | 
| 91 |  | 
| 92 | /*! | 
| 93 |     \property QCandlestickSet::pen | 
| 94 |     \brief The pen used to draw the lines of the candlestick item. | 
| 95 | */ | 
| 96 |  | 
| 97 | /*! | 
| 98 |     \qmlproperty string CandlestickSet::brushFilename | 
| 99 |     The name of the file used as a brush for the candlestick item. | 
| 100 | */ | 
| 101 |  | 
| 102 | /*! | 
| 103 |     \fn void QCandlestickSet::clicked() | 
| 104 |     This signal is emitted when the candlestick item is clicked. | 
| 105 | */ | 
| 106 |  | 
| 107 | /*! | 
| 108 |     \qmlsignal CandlestickSet::clicked() | 
| 109 |     This signal is emitted when the candlestick item is clicked. | 
| 110 |  | 
| 111 |     The corresponding signal handler is \c {onClicked}. | 
| 112 | */ | 
| 113 |  | 
| 114 | /*! | 
| 115 |     \fn void QCandlestickSet::hovered(bool status) | 
| 116 |     This signal is emitted when a mouse is hovered over a candlestick | 
| 117 |     item. | 
| 118 |  | 
| 119 |     When the mouse moves over the item, \a status turns \c true, and when the | 
| 120 |     mouse moves away again, it turns \c false. | 
| 121 | */ | 
| 122 |  | 
| 123 | /*! | 
| 124 |     \qmlsignal CandlestickSet::hovered(bool status) | 
| 125 |     This signal is emitted when a mouse is hovered over a candlestick | 
| 126 |     item. | 
| 127 |  | 
| 128 |     When the mouse moves over the item, \a status turns \c true, and when the | 
| 129 |     mouse moves away again, it turns \c false. | 
| 130 |  | 
| 131 |     The corresponding signal handler is \c {onHovered}. | 
| 132 | */ | 
| 133 |  | 
| 134 | /*! | 
| 135 |     \fn void QCandlestickSet::pressed() | 
| 136 |     This signal is emitted when the user clicks the candlestick item and | 
| 137 |     holds down the mouse button. | 
| 138 | */ | 
| 139 |  | 
| 140 | /*! | 
| 141 |     \qmlsignal CandlestickSet::pressed() | 
| 142 |     This signal is emitted when the user clicks the candlestick item and | 
| 143 |     holds down the mouse button. | 
| 144 |  | 
| 145 |     The corresponding signal handler is \c {onPressed}. | 
| 146 | */ | 
| 147 |  | 
| 148 | /*! | 
| 149 |     \fn void QCandlestickSet::released() | 
| 150 |     This signal is emitted when the user releases the mouse press on the | 
| 151 |     candlestick item. | 
| 152 | */ | 
| 153 |  | 
| 154 | /*! | 
| 155 |     \qmlsignal CandlestickSet::released() | 
| 156 |     This signal is emitted when the user releases the mouse press on the | 
| 157 |     candlestick item. | 
| 158 |  | 
| 159 |     The corresponding signal handler is \c {onReleased}. | 
| 160 | */ | 
| 161 |  | 
| 162 | /*! | 
| 163 |     \fn void QCandlestickSet::doubleClicked() | 
| 164 |     This signal is emitted when the user double-clicks a candlestick item. | 
| 165 | */ | 
| 166 |  | 
| 167 | /*! | 
| 168 |     \qmlsignal CandlestickSet::doubleClicked() | 
| 169 |     This signal is emitted when the user double-clicks a candlestick item. | 
| 170 |  | 
| 171 |     The corresponding signal handler is \c {onDoubleClicked}. | 
| 172 | */ | 
| 173 |  | 
| 174 | /*! | 
| 175 |     \fn void QCandlestickSet::timestampChanged() | 
| 176 |     This signal is emitted when the candlestick item timestamp changes. | 
| 177 |     \sa timestamp | 
| 178 | */ | 
| 179 |  | 
| 180 | /*! | 
| 181 |     \fn void QCandlestickSet::openChanged() | 
| 182 |     This signal is emitted when the candlestick item open value changes. | 
| 183 |     \sa open | 
| 184 | */ | 
| 185 |  | 
| 186 | /*! | 
| 187 |     \fn void QCandlestickSet::highChanged() | 
| 188 |     This signal is emitted when the candlestick item high value changes. | 
| 189 |     \sa high | 
| 190 | */ | 
| 191 |  | 
| 192 | /*! | 
| 193 |     \fn void QCandlestickSet::lowChanged() | 
| 194 |     This signal is emitted when the candlestick item low value changes. | 
| 195 |     \sa low | 
| 196 | */ | 
| 197 |  | 
| 198 | /*! | 
| 199 |     \fn void QCandlestickSet::closeChanged() | 
| 200 |     This signal is emitted when the candlestick item close value changes. | 
| 201 |     \sa close | 
| 202 | */ | 
| 203 |  | 
| 204 | /*! | 
| 205 |     \fn void QCandlestickSet::brushChanged() | 
| 206 |     This signal is emitted when the candlestick item brush changes. | 
| 207 |     \sa brush | 
| 208 | */ | 
| 209 |  | 
| 210 | /*! | 
| 211 |     \fn void QCandlestickSet::penChanged() | 
| 212 |     This signal is emitted when the candlestick item pen changes. | 
| 213 |     \sa pen | 
| 214 | */ | 
| 215 |  | 
| 216 | /*! | 
| 217 |     Constructs a candlestick item with an optional \a timestamp and a \a parent. | 
| 218 | */ | 
| 219 | QCandlestickSet::QCandlestickSet(qreal timestamp, QObject *parent) | 
| 220 |     : QObject(parent), | 
| 221 |       d_ptr(new QCandlestickSetPrivate(timestamp, this)) | 
| 222 | { | 
| 223 | } | 
| 224 |  | 
| 225 | /*! | 
| 226 |     Constructs a candlestick item with given ordered values. The values \a open, \a high, \a low, | 
| 227 |     and \a close are mandatory. The values \a timestamp and \a parent are optional. | 
| 228 | */ | 
| 229 | QCandlestickSet::QCandlestickSet(qreal open, qreal high, qreal low, qreal close, qreal timestamp, | 
| 230 |                                  QObject *parent) | 
| 231 |     : QObject(parent), | 
| 232 |       d_ptr(new QCandlestickSetPrivate(timestamp, this)) | 
| 233 | { | 
| 234 |     Q_D(QCandlestickSet); | 
| 235 |  | 
| 236 |     d->m_open = open; | 
| 237 |     d->m_high = high; | 
| 238 |     d->m_low = low; | 
| 239 |     d->m_close = close; | 
| 240 |  | 
| 241 |     emit d->updatedLayout(); | 
| 242 | } | 
| 243 |  | 
| 244 | /*! | 
| 245 |     Destroys the candlestick item. | 
| 246 | */ | 
| 247 | QCandlestickSet::~QCandlestickSet() | 
| 248 | { | 
| 249 | } | 
| 250 |  | 
| 251 | void QCandlestickSet::setTimestamp(qreal timestamp) | 
| 252 | { | 
| 253 |     Q_D(QCandlestickSet); | 
| 254 |  | 
| 255 |     bool changed = d->setTimestamp(timestamp); | 
| 256 |     if (!changed) | 
| 257 |         return; | 
| 258 |  | 
| 259 |     emit d->updatedLayout(); | 
| 260 |     emit timestampChanged(); | 
| 261 | } | 
| 262 |  | 
| 263 | qreal QCandlestickSet::timestamp() const | 
| 264 | { | 
| 265 |     Q_D(const QCandlestickSet); | 
| 266 |  | 
| 267 |     return d->m_timestamp; | 
| 268 | } | 
| 269 |  | 
| 270 | void QCandlestickSet::setOpen(qreal open) | 
| 271 | { | 
| 272 |     Q_D(QCandlestickSet); | 
| 273 |  | 
| 274 |     if (d->m_open == open) | 
| 275 |         return; | 
| 276 |  | 
| 277 |     d->m_open = open; | 
| 278 |  | 
| 279 |     emit d->updatedLayout(); | 
| 280 |     emit openChanged(); | 
| 281 | } | 
| 282 |  | 
| 283 | qreal QCandlestickSet::open() const | 
| 284 | { | 
| 285 |     Q_D(const QCandlestickSet); | 
| 286 |  | 
| 287 |     return d->m_open; | 
| 288 | } | 
| 289 |  | 
| 290 | void QCandlestickSet::setHigh(qreal high) | 
| 291 | { | 
| 292 |     Q_D(QCandlestickSet); | 
| 293 |  | 
| 294 |     if (d->m_high == high) | 
| 295 |         return; | 
| 296 |  | 
| 297 |     d->m_high = high; | 
| 298 |  | 
| 299 |     emit d->updatedLayout(); | 
| 300 |     emit highChanged(); | 
| 301 | } | 
| 302 |  | 
| 303 | qreal QCandlestickSet::high() const | 
| 304 | { | 
| 305 |     Q_D(const QCandlestickSet); | 
| 306 |  | 
| 307 |     return d->m_high; | 
| 308 | } | 
| 309 |  | 
| 310 | void QCandlestickSet::setLow(qreal low) | 
| 311 | { | 
| 312 |     Q_D(QCandlestickSet); | 
| 313 |  | 
| 314 |     if (d->m_low == low) | 
| 315 |         return; | 
| 316 |  | 
| 317 |     d->m_low = low; | 
| 318 |  | 
| 319 |     emit d->updatedLayout(); | 
| 320 |     emit lowChanged(); | 
| 321 | } | 
| 322 |  | 
| 323 | qreal QCandlestickSet::low() const | 
| 324 | { | 
| 325 |     Q_D(const QCandlestickSet); | 
| 326 |  | 
| 327 |     return d->m_low; | 
| 328 | } | 
| 329 |  | 
| 330 | void QCandlestickSet::setClose(qreal close) | 
| 331 | { | 
| 332 |     Q_D(QCandlestickSet); | 
| 333 |  | 
| 334 |     if (d->m_close == close) | 
| 335 |         return; | 
| 336 |  | 
| 337 |     d->m_close = close; | 
| 338 |  | 
| 339 |     emit d->updatedLayout(); | 
| 340 |     emit closeChanged(); | 
| 341 | } | 
| 342 |  | 
| 343 | qreal QCandlestickSet::close() const | 
| 344 | { | 
| 345 |     Q_D(const QCandlestickSet); | 
| 346 |  | 
| 347 |     return d->m_close; | 
| 348 | } | 
| 349 |  | 
| 350 | void QCandlestickSet::setBrush(const QBrush &brush) | 
| 351 | { | 
| 352 |     Q_D(QCandlestickSet); | 
| 353 |  | 
| 354 |     if (d->m_brush == brush) | 
| 355 |         return; | 
| 356 |  | 
| 357 |     d->m_brush = brush; | 
| 358 |  | 
| 359 |     emit d->updatedCandlestick(); | 
| 360 |     emit brushChanged(); | 
| 361 | } | 
| 362 |  | 
| 363 | QBrush QCandlestickSet::brush() const | 
| 364 | { | 
| 365 |     Q_D(const QCandlestickSet); | 
| 366 |  | 
| 367 |     return d->m_brush; | 
| 368 | } | 
| 369 |  | 
| 370 | void QCandlestickSet::setPen(const QPen &pen) | 
| 371 | { | 
| 372 |     Q_D(QCandlestickSet); | 
| 373 |  | 
| 374 |     if (d->m_pen == pen) | 
| 375 |         return; | 
| 376 |  | 
| 377 |     d->m_pen = pen; | 
| 378 |  | 
| 379 |     emit d->updatedCandlestick(); | 
| 380 |     emit penChanged(); | 
| 381 | } | 
| 382 |  | 
| 383 | QPen QCandlestickSet::pen() const | 
| 384 | { | 
| 385 |     Q_D(const QCandlestickSet); | 
| 386 |  | 
| 387 |     return d->m_pen; | 
| 388 | } | 
| 389 |  | 
| 390 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 
| 391 |  | 
| 392 | QCandlestickSetPrivate::QCandlestickSetPrivate(qreal timestamp, QCandlestickSet *parent) | 
| 393 |     : QObject(parent), | 
| 394 |       q_ptr(parent), | 
| 395 |       m_timestamp(0.0), | 
| 396 |       m_open(0.0), | 
| 397 |       m_high(0.0), | 
| 398 |       m_low(0.0), | 
| 399 |       m_close(0.0), | 
| 400 |       m_brush(QBrush(Qt::NoBrush)), | 
| 401 |       m_pen(QPen(Qt::NoPen)), | 
| 402 |       m_series(nullptr) | 
| 403 | { | 
| 404 |     setTimestamp(timestamp); | 
| 405 | } | 
| 406 |  | 
| 407 | QCandlestickSetPrivate::~QCandlestickSetPrivate() | 
| 408 | { | 
| 409 | } | 
| 410 |  | 
| 411 | bool QCandlestickSetPrivate::setTimestamp(qreal timestamp) | 
| 412 | { | 
| 413 |     timestamp = qMax(a: timestamp, b: qreal(0.0)); | 
| 414 |     timestamp = qRound64(d: timestamp); | 
| 415 |  | 
| 416 |     if (m_timestamp == timestamp) | 
| 417 |         return false; | 
| 418 |  | 
| 419 |     m_timestamp = timestamp; | 
| 420 |  | 
| 421 |     return true; | 
| 422 | } | 
| 423 |  | 
| 424 | QT_END_NAMESPACE | 
| 425 |  | 
| 426 | #include "moc_qcandlestickset.cpp" | 
| 427 | #include "moc_qcandlestickset_p.cpp" | 
| 428 |  |