summaryrefslogtreecommitdiffstats
path: root/src/charts/xychart/xychart.cpp
blob: f689dc0109bc2c719ff3dfa843bf7057cd8901e2 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <private/xychart_p.h>
#include <QtCharts/QXYSeries>
#include <private/qxyseries_p.h>
#include <private/chartpresenter_p.h>
#include <private/abstractdomain_p.h>
#include <private/chartdataset_p.h>
#include <private/glxyseriesdata_p.h>
#include <QtCharts/QXYModelMapper>
#include <private/qabstractaxis_p.h>
#include <QtGui/QPainter>
#include <QtCore/QAbstractItemModel>


QT_BEGIN_NAMESPACE

XYChart::XYChart(QXYSeries *series, QGraphicsItem *item):
      ChartItem(series->d_func(),item),
      m_series(series),
      m_animation(0),
      m_dirty(true)
{
    connect(series->d_func(), &QXYSeriesPrivate::seriesUpdated,
            this, &XYChart::handleSeriesUpdated);
    connect(series, &QXYSeries::pointReplaced, this, &XYChart::handlePointReplaced);
    connect(series, &QXYSeries::pointsReplaced, this, &XYChart::handlePointsReplaced);
    connect(series, &QXYSeries::pointAdded, this, &XYChart::handlePointAdded);
    connect(series, &QXYSeries::pointRemoved, this, &XYChart::handlePointRemoved);
    connect(series, &QXYSeries::pointsRemoved, this, &XYChart::handlePointsRemoved);
    connect(this, &XYChart::clicked, series, &QXYSeries::clicked);
    connect(this, &XYChart::hovered, series, &QXYSeries::hovered);
    connect(this, &XYChart::pressed, series, &QXYSeries::pressed);
    connect(this, &XYChart::released, series, &QXYSeries::released);
    connect(this, &XYChart::doubleClicked, series, &QXYSeries::doubleClicked);
    connect(series, &QAbstractSeries::useOpenGLChanged, this, &XYChart::handleDomainUpdated);
}

void XYChart::setGeometryPoints(const QList<QPointF> &points)
{
    m_points = points;
}

void XYChart::setAnimation(XYAnimation *animation)
{
    m_animation = animation;
}

void XYChart::setDirty(bool dirty)
{
    m_dirty = dirty;
}

// Returns a list with same size as geometryPoints list, indicating
// the off grid status of points.
QList<bool> XYChart::offGridStatusVector()
{
    qreal minX = domain()->minX();
    qreal maxX = domain()->maxX();
    qreal minY = domain()->minY();
    qreal maxY = domain()->maxY();

    QList<bool> returnVector;
    returnVector.resize(m_points.size());
    // During remove animation series may have different number of points,
    // so ensure we don't go over the index. No need to check for zero points, this
    // will not be called in such a situation.
    const int seriesLastIndex = m_series->count() - 1;

    for (int i = 0; i < m_points.size(); i++) {
        const QPointF &seriesPoint = m_series->at(qMin(seriesLastIndex, i));
        if (seriesPoint.x() < minX
            || seriesPoint.x() > maxX
            || seriesPoint.y() < minY
            || seriesPoint.y() > maxY) {
            returnVector[i] = true;
        } else {
            returnVector[i] = false;
        }
    }
    return returnVector;
}

void XYChart::updateChart(const QList<QPointF> &oldPoints, const QList<QPointF> &newPoints,
                          int index)
{

    if (m_animation) {
        m_animation->setup(oldPoints, newPoints, index);
        m_points = newPoints;
        setDirty(false);
        presenter()->startAnimation(m_animation);
    } else {
        m_points = newPoints;
        updateGeometry();
    }
}

void XYChart::updateGlChart()
{
    dataSet()->glXYSeriesDataManager()->setPoints(m_series, domain());
    presenter()->updateGLWidget();
    updateGeometry();
}

// Doesn't update gl geometry, but refreshes the chart
void XYChart::refreshGlChart()
{
    if (presenter())
        presenter()->updateGLWidget();
}

//handlers

void XYChart::handlePointAdded(int index)
{
    Q_ASSERT(index < m_series->count());
    Q_ASSERT(index >= 0);

    if (m_series->useOpenGL()) {
        updateGlChart();
    } else {
        QList<QPointF> points;
        if (m_dirty || m_points.isEmpty()) {
            points = domain()->calculateGeometryPoints(m_series->points());
        } else {
            points = m_points;
            QPointF point =
                    domain()->calculateGeometryPoint(m_series->points().at(index), m_validData);
            if (!m_validData)
                m_points.clear();
            else
                points.insert(index, point);
        }
        updateChart(m_points, points, index);
    }
}

void XYChart::handlePointRemoved(int index)
{
    Q_ASSERT(index <= m_series->count());
    Q_ASSERT(index >= 0);

    if (m_series->useOpenGL()) {
        updateGlChart();
    } else {
        QList<QPointF> points;
        if (m_dirty || m_points.isEmpty()) {
            points = domain()->calculateGeometryPoints(m_series->points());
        } else {
            points = m_points;
            points.remove(index);
        }
        updateChart(m_points, points, index);
    }
}

void XYChart::handlePointsRemoved(int index, int count)
{
    Q_ASSERT(index <= m_series->count());
    Q_ASSERT(index >= 0);

    if (m_series->useOpenGL()) {
        updateGlChart();
    } else {
        QList<QPointF> points;
        if (m_dirty || m_points.isEmpty()) {
            points = domain()->calculateGeometryPoints(m_series->points());
        } else {
            points = m_points;
            points.remove(index, count);
        }
        updateChart(m_points, points, index);
    }
}

void XYChart::handlePointReplaced(int index)
{
    Q_ASSERT(index < m_series->count());
    Q_ASSERT(index >= 0);

    if (m_series->useOpenGL()) {
        updateGlChart();
    } else {
        QList<QPointF> points;
        if (m_dirty || m_points.isEmpty()) {
            points = domain()->calculateGeometryPoints(m_series->points());
        } else {
            QPointF point =
                    domain()->calculateGeometryPoint(m_series->points().at(index), m_validData);
            if (!m_validData)
                m_points.clear();
            points = m_points;
            if (m_validData)
                points.replace(index, point);
        }
        updateChart(m_points, points, index);
    }
}

void XYChart::handlePointsReplaced()
{
    if (m_series->useOpenGL()) {
        updateGlChart();
    } else {
        // All the points were replaced -> recalculate
        QList<QPointF> points = domain()->calculateGeometryPoints(m_series->points());
        updateChart(m_points, points, -1);
    }
}

void XYChart::handleDomainUpdated()
{
    if (m_series->useOpenGL()) {
        updateGlChart();
    } else {
        if (isEmpty()) return;
        QList<QPointF> points = domain()->calculateGeometryPoints(m_series->points());
        updateChart(m_points, points);
    }
}

void XYChart::handleSeriesUpdated()
{
}

bool XYChart::isEmpty()
{
    return domain()->isEmpty() || m_series->points().isEmpty();
}

QPointF XYChart::matchForLightMarker(const QPointF &eventPos)
{
    if (m_series->lightMarker().isNull()
            && (m_series->selectedLightMarker().isNull()
                || m_series->selectedPoints().isEmpty()))
        return QPointF(qQNaN(), qQNaN()); // 0,0 could actually be in points()

    const bool useSelectedMarker = m_series->lightMarker().isNull();

    QList<QPointF> points;
    if (useSelectedMarker) {
        const auto selectedPoints = m_series->selectedPoints();
        for (const int &selectedPointIndex : selectedPoints)
            points << m_series->at(selectedPointIndex);
    } else {
        points = m_series->points();
    }

    for (const QPointF &dp : points) {
        bool ok;
        const QPointF gp = domain()->calculateGeometryPoint(dp, ok);
        if (ok) {
            // '+2' and '+4': There is an addRect for the (mouse-)shape
            // in LineChartItem::updateGeometry()
            // This has a margin of 1 to make sure a press in the icon will always be detected,
            // but as there is a bunch of 'translations' and therefore inaccuracies,
            // so it is necessary to increase that margin to 2
            // (otherwise you can click next to an icon, get a click event but not match it)
            QRectF r(gp.x() - (m_series->markerSize() / 2 + 2),
                     gp.y() - (m_series->markerSize() / 2 + 2),
                     m_series->markerSize() + 4, m_series->markerSize() + 4);

            if (r.contains(eventPos))
                return dp;
        }
    }
    return QPointF(qQNaN(), qQNaN()); // 0,0 could actually be in points()
}

QT_END_NAMESPACE

#include "moc_xychart_p.cpp"