|
@@
-1,196
+1,202
|
|
1
|
/****************************************************************************
|
|
1
|
/****************************************************************************
|
|
2
|
**
|
|
2
|
**
|
|
3
|
** Copyright (C) 2012 Digia Plc
|
|
3
|
** Copyright (C) 2012 Digia Plc
|
|
4
|
** All rights reserved.
|
|
4
|
** All rights reserved.
|
|
5
|
** For any questions to Digia, please use contact form at http://qt.digia.com
|
|
5
|
** For any questions to Digia, please use contact form at http://qt.digia.com
|
|
6
|
**
|
|
6
|
**
|
|
7
|
** This file is part of the Qt Commercial Charts Add-on.
|
|
7
|
** This file is part of the Qt Commercial Charts Add-on.
|
|
8
|
**
|
|
8
|
**
|
|
9
|
** $QT_BEGIN_LICENSE$
|
|
9
|
** $QT_BEGIN_LICENSE$
|
|
10
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
10
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
11
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
12
|
** Software or, alternatively, in accordance with the terms contained in
|
|
12
|
** Software or, alternatively, in accordance with the terms contained in
|
|
13
|
** a written agreement between you and Digia.
|
|
13
|
** a written agreement between you and Digia.
|
|
14
|
**
|
|
14
|
**
|
|
15
|
** If you have questions regarding the use of this file, please use
|
|
15
|
** If you have questions regarding the use of this file, please use
|
|
16
|
** contact form at http://qt.digia.com
|
|
16
|
** contact form at http://qt.digia.com
|
|
17
|
** $QT_END_LICENSE$
|
|
17
|
** $QT_END_LICENSE$
|
|
18
|
**
|
|
18
|
**
|
|
19
|
****************************************************************************/
|
|
19
|
****************************************************************************/
|
|
20
|
|
|
20
|
|
|
21
|
#include "xychartitem_p.h"
|
|
21
|
#include "xychartitem_p.h"
|
|
22
|
#include "qxyseries.h"
|
|
22
|
#include "qxyseries.h"
|
|
23
|
#include "qxyseries_p.h"
|
|
23
|
#include "qxyseries_p.h"
|
|
24
|
#include "chartpresenter_p.h"
|
|
24
|
#include "chartpresenter_p.h"
|
|
25
|
#include "chartanimator_p.h"
|
|
25
|
#include "chartanimator_p.h"
|
|
26
|
#include <QPainter>
|
|
26
|
#include <QPainter>
|
|
27
|
#include <QGraphicsSceneMouseEvent>
|
|
27
|
#include <QGraphicsSceneMouseEvent>
|
|
28
|
|
|
28
|
|
|
29
|
|
|
29
|
|
|
30
|
QTCOMMERCIALCHART_BEGIN_NAMESPACE
|
|
30
|
QTCOMMERCIALCHART_BEGIN_NAMESPACE
|
|
31
|
|
|
31
|
|
|
32
|
//TODO: optimize : remove points which are not visible
|
|
32
|
//TODO: optimize : remove points which are not visible
|
|
33
|
|
|
33
|
|
|
34
|
XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
|
|
34
|
XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
|
|
35
|
m_minX(0),
|
|
35
|
m_minX(0),
|
|
36
|
m_maxX(0),
|
|
36
|
m_maxX(0),
|
|
37
|
m_minY(0),
|
|
37
|
m_minY(0),
|
|
38
|
m_maxY(0),
|
|
38
|
m_maxY(0),
|
|
39
|
m_series(series)
|
|
39
|
m_series(series)
|
|
40
|
{
|
|
40
|
{
|
|
41
|
connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
|
|
41
|
connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
|
|
42
|
connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
|
|
42
|
connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
|
|
43
|
connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
|
|
43
|
connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
|
|
44
|
connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
|
|
44
|
connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
|
|
45
|
}
|
|
45
|
}
|
|
46
|
|
|
46
|
|
|
47
|
QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
|
|
47
|
QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
|
|
48
|
{
|
|
48
|
{
|
|
49
|
const qreal deltaX = m_size.width()/(m_maxX-m_minX);
|
|
49
|
const qreal deltaX = m_size.width()/(m_maxX-m_minX);
|
|
50
|
const qreal deltaY = m_size.height()/(m_maxY-m_minY);
|
|
50
|
const qreal deltaY = m_size.height()/(m_maxY-m_minY);
|
|
51
|
qreal x = (point.x() - m_minX)* deltaX;
|
|
51
|
qreal x = (point.x() - m_minX)* deltaX;
|
|
52
|
qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
|
|
52
|
qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
|
|
53
|
return QPointF(x,y);
|
|
53
|
return QPointF(x,y);
|
|
54
|
}
|
|
54
|
}
|
|
55
|
|
|
55
|
|
|
56
|
|
|
56
|
|
|
57
|
QPointF XYChartItem::calculateGeometryPoint(int index) const
|
|
57
|
QPointF XYChartItem::calculateGeometryPoint(int index) const
|
|
58
|
{
|
|
58
|
{
|
|
59
|
const qreal deltaX = m_size.width()/(m_maxX-m_minX);
|
|
59
|
const qreal deltaX = m_size.width()/(m_maxX-m_minX);
|
|
60
|
const qreal deltaY = m_size.height()/(m_maxY-m_minY);
|
|
60
|
const qreal deltaY = m_size.height()/(m_maxY-m_minY);
|
|
61
|
qreal x = (m_series->x(index) - m_minX)* deltaX;
|
|
61
|
qreal x = (m_series->x(index) - m_minX)* deltaX;
|
|
62
|
qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
|
|
62
|
qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
|
|
63
|
return QPointF(x,y);
|
|
63
|
return QPointF(x,y);
|
|
64
|
}
|
|
64
|
}
|
|
65
|
|
|
65
|
|
|
66
|
QVector<QPointF> XYChartItem::calculateGeometryPoints() const
|
|
66
|
QVector<QPointF> XYChartItem::calculateGeometryPoints() const
|
|
67
|
{
|
|
67
|
{
|
|
68
|
const qreal deltaX = m_size.width()/(m_maxX-m_minX);
|
|
68
|
const qreal deltaX = m_size.width()/(m_maxX-m_minX);
|
|
69
|
const qreal deltaY = m_size.height()/(m_maxY-m_minY);
|
|
69
|
const qreal deltaY = m_size.height()/(m_maxY-m_minY);
|
|
70
|
|
|
70
|
|
|
71
|
QVector<QPointF> points;
|
|
71
|
QVector<QPointF> points;
|
|
72
|
points.reserve(m_series->count());
|
|
72
|
points.reserve(m_series->count());
|
|
73
|
for (int i = 0; i < m_series->count(); ++i) {
|
|
73
|
for (int i = 0; i < m_series->count(); ++i) {
|
|
74
|
qreal x = (m_series->x(i) - m_minX)* deltaX;
|
|
74
|
qreal x = (m_series->x(i) - m_minX)* deltaX;
|
|
75
|
qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
|
|
75
|
qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
|
|
76
|
points << QPointF(x,y);
|
|
76
|
points << QPointF(x,y);
|
|
77
|
}
|
|
77
|
}
|
|
78
|
return points;
|
|
78
|
return points;
|
|
79
|
}
|
|
79
|
}
|
|
80
|
|
|
80
|
|
|
81
|
QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
|
|
81
|
QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
|
|
82
|
{
|
|
82
|
{
|
|
83
|
const qreal deltaX = m_size.width()/(m_maxX-m_minX);
|
|
83
|
const qreal deltaX = m_size.width()/(m_maxX-m_minX);
|
|
84
|
const qreal deltaY = m_size.height()/(m_maxY-m_minY);
|
|
84
|
const qreal deltaY = m_size.height()/(m_maxY-m_minY);
|
|
85
|
qreal x = point.x()/deltaX +m_minX;
|
|
85
|
qreal x = point.x()/deltaX +m_minX;
|
|
86
|
qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
|
|
86
|
qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
|
|
87
|
return QPointF(x,y);
|
|
87
|
return QPointF(x,y);
|
|
88
|
}
|
|
88
|
}
|
|
89
|
|
|
89
|
|
|
90
|
void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
|
|
90
|
void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
|
|
91
|
{
|
|
91
|
{
|
|
92
|
if (animator()) {
|
|
92
|
if (animator()) {
|
|
93
|
animator()->updateLayout(this,oldPoints,newPoints,index);
|
|
93
|
animator()->updateLayout(this,oldPoints,newPoints,index);
|
|
94
|
} else {
|
|
94
|
} else {
|
|
95
|
setLayout(newPoints);
|
|
95
|
setLayout(newPoints);
|
|
96
|
}
|
|
96
|
}
|
|
97
|
}
|
|
97
|
}
|
|
98
|
|
|
98
|
|
|
99
|
void XYChartItem::setLayout(QVector<QPointF> &points)
|
|
99
|
void XYChartItem::setLayout(QVector<QPointF> &points)
|
|
100
|
{
|
|
100
|
{
|
|
101
|
m_points = points;
|
|
101
|
m_points = points;
|
|
102
|
update();
|
|
102
|
update();
|
|
103
|
}
|
|
103
|
}
|
|
104
|
|
|
104
|
|
|
105
|
//handlers
|
|
105
|
//handlers
|
|
106
|
|
|
106
|
|
|
107
|
void XYChartItem::handlePointAdded(int index)
|
|
107
|
void XYChartItem::handlePointAdded(int index)
|
|
108
|
{
|
|
108
|
{
|
|
109
|
QVector<QPointF> points = m_points;
|
|
109
|
QVector<QPointF> points = m_points;
|
|
110
|
QPointF point;
|
|
110
|
QPointF point;
|
|
111
|
if (m_series->model()) {
|
|
111
|
if (m_series->model()) {
|
|
112
|
point = calculateGeometryPoint(index - m_series->mapFirst());
|
|
112
|
point = calculateGeometryPoint(index - m_series->mapFirst());
|
|
|
|
|
113
|
if (index < m_series->mapFirst()) {
|
|
|
|
|
114
|
points.insert(0, point);
|
|
|
|
|
115
|
updateLayout(m_points, points, 0);
|
|
|
|
|
116
|
}
|
|
|
|
|
117
|
else {
|
|
113
|
points.insert(index - m_series->mapFirst(), point);
|
|
118
|
points.insert(index - m_series->mapFirst(), point);
|
|
114
|
updateLayout(m_points, points, index - m_series->mapFirst());
|
|
119
|
updateLayout(m_points, points, index - m_series->mapFirst());
|
|
115
|
}
|
|
120
|
}
|
|
|
|
|
121
|
}
|
|
116
|
else {
|
|
122
|
else {
|
|
117
|
// this checks do not work correctly if model is set
|
|
123
|
// this checks do not work correctly if model is set
|
|
118
|
Q_ASSERT(index<m_series->count());
|
|
124
|
Q_ASSERT(index<m_series->count());
|
|
119
|
Q_ASSERT(index>=0);
|
|
125
|
Q_ASSERT(index>=0);
|
|
120
|
point = calculateGeometryPoint(index);
|
|
126
|
point = calculateGeometryPoint(index);
|
|
121
|
points.insert(index, point);
|
|
127
|
points.insert(index, point);
|
|
122
|
updateLayout(m_points, points, index);
|
|
128
|
updateLayout(m_points, points, index);
|
|
123
|
}
|
|
129
|
}
|
|
124
|
update();
|
|
130
|
update();
|
|
125
|
}
|
|
131
|
}
|
|
126
|
void XYChartItem::handlePointRemoved(int index)
|
|
132
|
void XYChartItem::handlePointRemoved(int index)
|
|
127
|
{
|
|
133
|
{
|
|
128
|
QVector<QPointF> points = m_points;
|
|
134
|
QVector<QPointF> points = m_points;
|
|
129
|
if (m_series->model()) {
|
|
135
|
if (m_series->model()) {
|
|
130
|
if (index < m_series->mapFirst())
|
|
136
|
if (index < m_series->mapFirst())
|
|
131
|
points.remove(0);
|
|
137
|
points.remove(0);
|
|
132
|
else
|
|
138
|
else
|
|
133
|
points.remove(index - m_series->mapFirst());
|
|
139
|
points.remove(index - m_series->mapFirst());
|
|
134
|
updateLayout(m_points, points, index - m_series->mapFirst());
|
|
140
|
updateLayout(m_points, points, index - m_series->mapFirst());
|
|
135
|
}
|
|
141
|
}
|
|
136
|
else {
|
|
142
|
else {
|
|
137
|
// this checks do not work correctly if model is set
|
|
143
|
// this checks do not work correctly if model is set
|
|
138
|
Q_ASSERT(index<m_series->count() + 1);
|
|
144
|
Q_ASSERT(index<m_series->count() + 1);
|
|
139
|
Q_ASSERT(index>=0);
|
|
145
|
Q_ASSERT(index>=0);
|
|
140
|
points.remove(index);
|
|
146
|
points.remove(index);
|
|
141
|
updateLayout(m_points, points, index);
|
|
147
|
updateLayout(m_points, points, index);
|
|
142
|
}
|
|
148
|
}
|
|
143
|
update();
|
|
149
|
update();
|
|
144
|
}
|
|
150
|
}
|
|
145
|
|
|
151
|
|
|
146
|
void XYChartItem::handlePointReplaced(int index)
|
|
152
|
void XYChartItem::handlePointReplaced(int index)
|
|
147
|
{
|
|
153
|
{
|
|
148
|
Q_ASSERT(index<m_series->count());
|
|
154
|
Q_ASSERT(index<m_series->count());
|
|
149
|
Q_ASSERT(index>=0);
|
|
155
|
Q_ASSERT(index>=0);
|
|
150
|
QPointF point = calculateGeometryPoint(index);
|
|
156
|
QPointF point = calculateGeometryPoint(index);
|
|
151
|
QVector<QPointF> points = m_points;
|
|
157
|
QVector<QPointF> points = m_points;
|
|
152
|
points.replace(index,point);
|
|
158
|
points.replace(index,point);
|
|
153
|
updateLayout(m_points,points,index);
|
|
159
|
updateLayout(m_points,points,index);
|
|
154
|
update();
|
|
160
|
update();
|
|
155
|
}
|
|
161
|
}
|
|
156
|
|
|
162
|
|
|
157
|
void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
|
|
163
|
void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
|
|
158
|
{
|
|
164
|
{
|
|
159
|
m_minX=minX;
|
|
165
|
m_minX=minX;
|
|
160
|
m_maxX=maxX;
|
|
166
|
m_maxX=maxX;
|
|
161
|
m_minY=minY;
|
|
167
|
m_minY=minY;
|
|
162
|
m_maxY=maxY;
|
|
168
|
m_maxY=maxY;
|
|
163
|
|
|
169
|
|
|
164
|
if (isEmpty()) return;
|
|
170
|
if (isEmpty()) return;
|
|
165
|
QVector<QPointF> points = calculateGeometryPoints();
|
|
171
|
QVector<QPointF> points = calculateGeometryPoints();
|
|
166
|
updateLayout(m_points,points);
|
|
172
|
updateLayout(m_points,points);
|
|
167
|
update();
|
|
173
|
update();
|
|
168
|
}
|
|
174
|
}
|
|
169
|
|
|
175
|
|
|
170
|
void XYChartItem::handleGeometryChanged(const QRectF &rect)
|
|
176
|
void XYChartItem::handleGeometryChanged(const QRectF &rect)
|
|
171
|
{
|
|
177
|
{
|
|
172
|
Q_ASSERT(rect.isValid());
|
|
178
|
Q_ASSERT(rect.isValid());
|
|
173
|
m_size=rect.size();
|
|
179
|
m_size=rect.size();
|
|
174
|
m_clipRect=rect.translated(-rect.topLeft());
|
|
180
|
m_clipRect=rect.translated(-rect.topLeft());
|
|
175
|
setPos(rect.topLeft());
|
|
181
|
setPos(rect.topLeft());
|
|
176
|
|
|
182
|
|
|
177
|
if (isEmpty()) return;
|
|
183
|
if (isEmpty()) return;
|
|
178
|
QVector<QPointF> points = calculateGeometryPoints();
|
|
184
|
QVector<QPointF> points = calculateGeometryPoints();
|
|
179
|
updateLayout(m_points,points);
|
|
185
|
updateLayout(m_points,points);
|
|
180
|
update();
|
|
186
|
update();
|
|
181
|
}
|
|
187
|
}
|
|
182
|
|
|
188
|
|
|
183
|
|
|
189
|
|
|
184
|
bool XYChartItem::isEmpty()
|
|
190
|
bool XYChartItem::isEmpty()
|
|
185
|
{
|
|
191
|
{
|
|
186
|
return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY);
|
|
192
|
return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY);
|
|
187
|
}
|
|
193
|
}
|
|
188
|
|
|
194
|
|
|
189
|
void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
195
|
void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
190
|
{
|
|
196
|
{
|
|
191
|
emit clicked(calculateDomainPoint(event->pos()));
|
|
197
|
emit clicked(calculateDomainPoint(event->pos()));
|
|
192
|
}
|
|
198
|
}
|
|
193
|
|
|
199
|
|
|
194
|
#include "moc_xychartitem_p.cpp"
|
|
200
|
#include "moc_xychartitem_p.cpp"
|
|
195
|
|
|
201
|
|
|
196
|
QTCOMMERCIALCHART_END_NAMESPACE
|
|
202
|
QTCOMMERCIALCHART_END_NAMESPACE
|