| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
| 5 | |
| 6 | #include <QtCharts/QHCandlestickModelMapper> |
| 7 | #include <QtCharts/QVCandlestickModelMapper> |
| 8 | #include "declarativeaxes_p.h" |
| 9 | #include "declarativecandlestickseries_p.h" |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | DeclarativeCandlestickSet::DeclarativeCandlestickSet(qreal timestamp, QObject *parent) |
| 14 | : QCandlestickSet(timestamp, parent) |
| 15 | { |
| 16 | connect(sender: this, SIGNAL(brushChanged()), receiver: this, SLOT(handleBrushChanged())); |
| 17 | } |
| 18 | |
| 19 | void DeclarativeCandlestickSet::setBrushFilename(const QString &brushFilename) |
| 20 | { |
| 21 | QImage brushImage(brushFilename); |
| 22 | if (QCandlestickSet::brush().textureImage() != brushImage) { |
| 23 | QBrush brush = QCandlestickSet::brush(); |
| 24 | brush.setTextureImage(brushImage); |
| 25 | |
| 26 | QCandlestickSet::setBrush(brush); |
| 27 | |
| 28 | m_brushFilename = brushFilename; |
| 29 | m_brushImage = brushImage; |
| 30 | |
| 31 | emit brushFilenameChanged(brushFilename); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | QString DeclarativeCandlestickSet::brushFilename() const |
| 36 | { |
| 37 | return m_brushFilename; |
| 38 | } |
| 39 | |
| 40 | void DeclarativeCandlestickSet::handleBrushChanged() |
| 41 | { |
| 42 | // If the texture image of the brush has changed along the brush |
| 43 | // the brush file name needs to be cleared. |
| 44 | if (!m_brushFilename.isEmpty() && QCandlestickSet::brush().textureImage() != m_brushImage) { |
| 45 | m_brushFilename.clear(); |
| 46 | emit brushFilenameChanged(brushFilename: QString()); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Declarative candlestick series ================================================================== |
| 51 | |
| 52 | DeclarativeCandlestickSeries::DeclarativeCandlestickSeries(QQuickItem *parent) |
| 53 | : QCandlestickSeries(parent), |
| 54 | m_axes(new DeclarativeAxes(this)) |
| 55 | { |
| 56 | connect(sender: m_axes, SIGNAL(axisXChanged(QAbstractAxis*)), |
| 57 | receiver: this, SIGNAL(axisXChanged(QAbstractAxis*))); |
| 58 | connect(sender: m_axes, SIGNAL(axisYChanged(QAbstractAxis*)), |
| 59 | receiver: this, SIGNAL(axisYChanged(QAbstractAxis*))); |
| 60 | connect(sender: m_axes, SIGNAL(axisXTopChanged(QAbstractAxis*)), |
| 61 | receiver: this, SIGNAL(axisXTopChanged(QAbstractAxis*))); |
| 62 | connect(sender: m_axes, SIGNAL(axisYRightChanged(QAbstractAxis*)), |
| 63 | receiver: this, SIGNAL(axisYRightChanged(QAbstractAxis*))); |
| 64 | |
| 65 | connect(sender: this, SIGNAL(hovered(bool, QCandlestickSet *)), |
| 66 | receiver: this, SLOT(onHovered(bool, QCandlestickSet *))); |
| 67 | connect(sender: this, SIGNAL(clicked(QCandlestickSet *)), receiver: this, SLOT(onClicked(QCandlestickSet *))); |
| 68 | connect(sender: this, SIGNAL(pressed(QCandlestickSet *)), receiver: this, SLOT(onPressed(QCandlestickSet *))); |
| 69 | connect(sender: this, SIGNAL(released(QCandlestickSet *)), receiver: this, SLOT(onReleased(QCandlestickSet *))); |
| 70 | connect(sender: this, SIGNAL(doubleClicked(QCandlestickSet *)), |
| 71 | receiver: this, SLOT(onDoubleClicked(QCandlestickSet *))); |
| 72 | |
| 73 | connect(sender: this, SIGNAL(brushChanged()), receiver: this, SLOT(handleBrushChanged())); |
| 74 | } |
| 75 | |
| 76 | QQmlListProperty<QObject> DeclarativeCandlestickSeries::seriesChildren() |
| 77 | { |
| 78 | return QQmlListProperty<QObject>(this, 0, &DeclarativeCandlestickSeries::appendSeriesChildren, |
| 79 | 0, 0, 0); |
| 80 | } |
| 81 | |
| 82 | void DeclarativeCandlestickSeries::setBrushFilename(const QString &brushFilename) |
| 83 | { |
| 84 | QImage brushImage(brushFilename); |
| 85 | if (QCandlestickSeries::brush().textureImage() != brushImage) { |
| 86 | QBrush brush = QCandlestickSeries::brush(); |
| 87 | brush.setTextureImage(brushImage); |
| 88 | |
| 89 | QCandlestickSeries::setBrush(brush); |
| 90 | |
| 91 | m_brushFilename = brushFilename; |
| 92 | m_brushImage = brushImage; |
| 93 | |
| 94 | emit brushFilenameChanged(brushFilename); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | QString DeclarativeCandlestickSeries::brushFilename() const |
| 99 | { |
| 100 | return m_brushFilename; |
| 101 | } |
| 102 | |
| 103 | DeclarativeCandlestickSet *DeclarativeCandlestickSeries::at(int index) |
| 104 | { |
| 105 | QList<QCandlestickSet *> sets = this->sets(); |
| 106 | if (index >= 0 && index < sets.size()) |
| 107 | return qobject_cast<DeclarativeCandlestickSet *>(object: sets[index]); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | bool DeclarativeCandlestickSeries::append(DeclarativeCandlestickSet *set) |
| 113 | { |
| 114 | return QCandlestickSeries::append(set: qobject_cast<QCandlestickSet *>(object: set)); |
| 115 | } |
| 116 | |
| 117 | bool DeclarativeCandlestickSeries::remove(DeclarativeCandlestickSet *set) |
| 118 | { |
| 119 | return QCandlestickSeries::remove(set: qobject_cast<QCandlestickSet *>(object: set)); |
| 120 | } |
| 121 | |
| 122 | bool DeclarativeCandlestickSeries::append(qreal open, qreal high, qreal low, qreal close, |
| 123 | qreal timestamp) |
| 124 | { |
| 125 | QCandlestickSet *set = new QCandlestickSet(open, high, low, close, timestamp); |
| 126 | if (!QCandlestickSeries::append(set)) { |
| 127 | delete set; |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | bool DeclarativeCandlestickSeries::remove(qreal timestamp) |
| 135 | { |
| 136 | for (int i = 0; i < count(); ++i) { |
| 137 | QCandlestickSet *set = sets().at(i); |
| 138 | if (set->timestamp() == timestamp) |
| 139 | return QCandlestickSeries::remove(set); |
| 140 | } |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | bool DeclarativeCandlestickSeries::insert(int index, DeclarativeCandlestickSet *set) |
| 146 | { |
| 147 | return QCandlestickSeries::insert(index, set: qobject_cast<QCandlestickSet *>(object: set)); |
| 148 | } |
| 149 | |
| 150 | void DeclarativeCandlestickSeries::clear() |
| 151 | { |
| 152 | QCandlestickSeries::clear(); |
| 153 | } |
| 154 | |
| 155 | void DeclarativeCandlestickSeries::classBegin() |
| 156 | { |
| 157 | // do nothing |
| 158 | } |
| 159 | |
| 160 | void DeclarativeCandlestickSeries::componentComplete() |
| 161 | { |
| 162 | foreach (QObject *child, children()) { |
| 163 | if (qobject_cast<DeclarativeCandlestickSet *>(object: child)) { |
| 164 | QCandlestickSeries::append(set: qobject_cast<DeclarativeCandlestickSet *>(object: child)); |
| 165 | } else if (qobject_cast<QHCandlestickModelMapper *>(object: child)) { |
| 166 | QHCandlestickModelMapper *mapper = qobject_cast<QHCandlestickModelMapper *>(object: child); |
| 167 | mapper->setSeries(this); |
| 168 | } else if (qobject_cast<QVCandlestickModelMapper *>(object: child)) { |
| 169 | QVCandlestickModelMapper *mapper = qobject_cast<QVCandlestickModelMapper *>(object: child); |
| 170 | mapper->setSeries(this); |
| 171 | } // else: do nothing |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void DeclarativeCandlestickSeries::appendSeriesChildren(QQmlListProperty<QObject> *list, |
| 176 | QObject *element) |
| 177 | { |
| 178 | // Empty implementation; the children are parsed in componentComplete instead |
| 179 | Q_UNUSED(list); |
| 180 | Q_UNUSED(element); |
| 181 | } |
| 182 | |
| 183 | void DeclarativeCandlestickSeries::onClicked(QCandlestickSet *set) |
| 184 | { |
| 185 | emit clicked(set: qobject_cast<DeclarativeCandlestickSet *>(object: set)); |
| 186 | } |
| 187 | |
| 188 | void DeclarativeCandlestickSeries::onHovered(bool status, QCandlestickSet *set) |
| 189 | { |
| 190 | emit hovered(status, set: qobject_cast<DeclarativeCandlestickSet *>(object: set)); |
| 191 | } |
| 192 | |
| 193 | void DeclarativeCandlestickSeries::onPressed(QCandlestickSet *set) |
| 194 | { |
| 195 | emit pressed(set: qobject_cast<DeclarativeCandlestickSet *>(object: set)); |
| 196 | } |
| 197 | |
| 198 | void DeclarativeCandlestickSeries::onReleased(QCandlestickSet *set) |
| 199 | { |
| 200 | emit released(set: qobject_cast<DeclarativeCandlestickSet *>(object: set)); |
| 201 | } |
| 202 | |
| 203 | void DeclarativeCandlestickSeries::onDoubleClicked(QCandlestickSet *set) |
| 204 | { |
| 205 | emit doubleClicked(set: qobject_cast<DeclarativeCandlestickSet *>(object: set)); |
| 206 | } |
| 207 | |
| 208 | void DeclarativeCandlestickSeries::handleBrushChanged() |
| 209 | { |
| 210 | // If the texture image of the brush has changed along the brush |
| 211 | // the brush file name needs to be cleared. |
| 212 | if (!m_brushFilename.isEmpty() && QCandlestickSeries::brush().textureImage() != m_brushImage) { |
| 213 | m_brushFilename.clear(); |
| 214 | emit brushFilenameChanged(brushFilename: QString()); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | QT_END_NAMESPACE |
| 219 | |
| 220 | #include "moc_declarativecandlestickseries_p.cpp" |
| 221 | |