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

source code of qtcharts/src/chartsqml2/declarativecandlestickseries.cpp