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