summaryrefslogtreecommitdiffstats
path: root/src/chartsqml2/declarativecandlestickseries.cpp
blob: 79c1ca97fc7c89461acda54208b60886f4a65803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include <QtCharts/QHCandlestickModelMapper>
#include <QtCharts/QVCandlestickModelMapper>
#include "declarativeaxes_p.h"
#include "declarativecandlestickseries_p.h"

QT_CHARTS_BEGIN_NAMESPACE

DeclarativeCandlestickSet::DeclarativeCandlestickSet(qreal timestamp, QObject *parent)
    : QCandlestickSet(timestamp, parent)
{
    connect(this, SIGNAL(brushChanged()), this, SLOT(handleBrushChanged()));
}

void DeclarativeCandlestickSet::setBrushFilename(const QString &brushFilename)
{
    QImage brushImage(brushFilename);
    if (QCandlestickSet::brush().textureImage() != brushImage) {
        QBrush brush = QCandlestickSet::brush();
        brush.setTextureImage(brushImage);

        QCandlestickSet::setBrush(brush);

        m_brushFilename = brushFilename;
        m_brushImage = brushImage;

        emit brushFilenameChanged(brushFilename);
    }
}

QString DeclarativeCandlestickSet::brushFilename() const
{
    return m_brushFilename;
}

void DeclarativeCandlestickSet::handleBrushChanged()
{
    // If the texture image of the brush has changed along the brush
    // the brush file name needs to be cleared.
    if (!m_brushFilename.isEmpty() && QCandlestickSet::brush().textureImage() != m_brushImage) {
        m_brushFilename.clear();
        emit brushFilenameChanged(QString());
    }
}

// Declarative candlestick series ==================================================================

DeclarativeCandlestickSeries::DeclarativeCandlestickSeries(QQuickItem *parent)
    : QCandlestickSeries(parent),
      m_axes(new DeclarativeAxes(this))
{
    connect(m_axes, SIGNAL(axisXChanged(QAbstractAxis*)),
            this, SIGNAL(axisXChanged(QAbstractAxis*)));
    connect(m_axes, SIGNAL(axisYChanged(QAbstractAxis*)),
            this, SIGNAL(axisYChanged(QAbstractAxis*)));
    connect(m_axes, SIGNAL(axisXTopChanged(QAbstractAxis*)),
            this, SIGNAL(axisXTopChanged(QAbstractAxis*)));
    connect(m_axes, SIGNAL(axisYRightChanged(QAbstractAxis*)),
            this, SIGNAL(axisYRightChanged(QAbstractAxis*)));

    connect(this, SIGNAL(hovered(bool, QCandlestickSet *)),
            this, SLOT(onHovered(bool, QCandlestickSet *)));
    connect(this, SIGNAL(clicked(QCandlestickSet *)), this, SLOT(onClicked(QCandlestickSet *)));
    connect(this, SIGNAL(pressed(QCandlestickSet *)), this, SLOT(onPressed(QCandlestickSet *)));
    connect(this, SIGNAL(released(QCandlestickSet *)), this, SLOT(onReleased(QCandlestickSet *)));
    connect(this, SIGNAL(doubleClicked(QCandlestickSet *)),
            this, SLOT(onDoubleClicked(QCandlestickSet *)));

    connect(this, SIGNAL(brushChanged()), this, SLOT(handleBrushChanged()));
}

QQmlListProperty<QObject> DeclarativeCandlestickSeries::seriesChildren()
{
    return QQmlListProperty<QObject>(this, 0, &DeclarativeCandlestickSeries::appendSeriesChildren,
                                     0, 0, 0);
}

void DeclarativeCandlestickSeries::setBrushFilename(const QString &brushFilename)
{
    QImage brushImage(brushFilename);
    if (QCandlestickSeries::brush().textureImage() != brushImage) {
        QBrush brush = QCandlestickSeries::brush();
        brush.setTextureImage(brushImage);

        QCandlestickSeries::setBrush(brush);

        m_brushFilename = brushFilename;
        m_brushImage = brushImage;

        emit brushFilenameChanged(brushFilename);
    }
}

QString DeclarativeCandlestickSeries::brushFilename() const
{
    return m_brushFilename;
}

DeclarativeCandlestickSet *DeclarativeCandlestickSeries::at(int index)
{
    QList<QCandlestickSet *> sets = this->sets();
    if (index >= 0 && index < sets.count())
        return qobject_cast<DeclarativeCandlestickSet *>(sets[index]);

    return 0;
}

bool DeclarativeCandlestickSeries::append(DeclarativeCandlestickSet *set)
{
    return QCandlestickSeries::append(qobject_cast<QCandlestickSet *>(set));
}

bool DeclarativeCandlestickSeries::remove(DeclarativeCandlestickSet *set)
{
    return QCandlestickSeries::remove(qobject_cast<QCandlestickSet *>(set));
}

bool DeclarativeCandlestickSeries::append(qreal open, qreal high, qreal low, qreal close,
                                          qreal timestamp)
{
    QCandlestickSet *set = new QCandlestickSet(open, high, low, close, timestamp);
    if (!QCandlestickSeries::append(set)) {
        delete set;
        return false;
    }

    return true;
}

bool DeclarativeCandlestickSeries::remove(qreal timestamp)
{
    for (int i = 0; i < count(); ++i) {
        QCandlestickSet *set = sets().at(i);
        if (set->timestamp() == timestamp)
            return QCandlestickSeries::remove(set);
    }

    return false;
}

bool DeclarativeCandlestickSeries::insert(int index, DeclarativeCandlestickSet *set)
{
    return QCandlestickSeries::insert(index, qobject_cast<QCandlestickSet *>(set));
}

void DeclarativeCandlestickSeries::clear()
{
    QCandlestickSeries::clear();
}

void DeclarativeCandlestickSeries::classBegin()
{
    // do nothing
}

void DeclarativeCandlestickSeries::componentComplete()
{
    foreach (QObject *child, children()) {
        if (qobject_cast<DeclarativeCandlestickSet *>(child)) {
            QCandlestickSeries::append(qobject_cast<DeclarativeCandlestickSet *>(child));
        } else if (qobject_cast<QHCandlestickModelMapper *>(child)) {
            QHCandlestickModelMapper *mapper = qobject_cast<QHCandlestickModelMapper *>(child);
            mapper->setSeries(this);
        } else if (qobject_cast<QVCandlestickModelMapper *>(child)) {
            QVCandlestickModelMapper *mapper = qobject_cast<QVCandlestickModelMapper *>(child);
            mapper->setSeries(this);
        } // else: do nothing
    }
}

void DeclarativeCandlestickSeries::appendSeriesChildren(QQmlListProperty<QObject> *list,
                                                        QObject *element)
{
    // Empty implementation; the children are parsed in componentComplete instead
    Q_UNUSED(list);
    Q_UNUSED(element);
}

void DeclarativeCandlestickSeries::onClicked(QCandlestickSet *set)
{
    emit clicked(qobject_cast<DeclarativeCandlestickSet *>(set));
}

void DeclarativeCandlestickSeries::onHovered(bool status, QCandlestickSet *set)
{
    emit hovered(status, qobject_cast<DeclarativeCandlestickSet *>(set));
}

void DeclarativeCandlestickSeries::onPressed(QCandlestickSet *set)
{
    emit pressed(qobject_cast<DeclarativeCandlestickSet *>(set));
}

void DeclarativeCandlestickSeries::onReleased(QCandlestickSet *set)
{
    emit released(qobject_cast<DeclarativeCandlestickSet *>(set));
}

void DeclarativeCandlestickSeries::onDoubleClicked(QCandlestickSet *set)
{
    emit doubleClicked(qobject_cast<DeclarativeCandlestickSet *>(set));
}

void DeclarativeCandlestickSeries::handleBrushChanged()
{
    // If the texture image of the brush has changed along the brush
    // the brush file name needs to be cleared.
    if (!m_brushFilename.isEmpty() && QCandlestickSeries::brush().textureImage() != m_brushImage) {
        m_brushFilename.clear();
        emit brushFilenameChanged(QString());
    }
}

QT_CHARTS_END_NAMESPACE

#include "moc_declarativecandlestickseries_p.cpp"