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