@@ -1,251 +1,250 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "xychart_p.h" |
|
22 | 22 | #include "qxyseries.h" |
|
23 | 23 | #include "qxyseries_p.h" |
|
24 | 24 | #include "chartpresenter_p.h" |
|
25 | 25 | #include "chartanimator_p.h" |
|
26 | 26 | #include <QPainter> |
|
27 | 27 | #include <QAbstractItemModel> |
|
28 | 28 | #include "qxymodelmapper.h" |
|
29 | 29 | #include <QDebug> |
|
30 | 30 | |
|
31 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | //TODO: optimize : remove points which are not visible |
|
34 | 34 | |
|
35 | 35 | XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter):Chart(presenter), |
|
36 | 36 | m_minX(0), |
|
37 | 37 | m_maxX(0), |
|
38 | 38 | m_minY(0), |
|
39 | 39 | m_maxY(0), |
|
40 | 40 | m_series(series), |
|
41 | 41 | m_animation(0), |
|
42 | 42 | m_dirty(true) |
|
43 | 43 | { |
|
44 | 44 | QObject::connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int))); |
|
45 | 45 | QObject::connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int))); |
|
46 | 46 | QObject::connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int))); |
|
47 | 47 | QObject::connect(series->d_func(),SIGNAL(reinitialized()),this,SLOT(handleReinitialized())); |
|
48 | 48 | QObject::connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF))); |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | void XYChart::setGeometryPoints(const QVector<QPointF>& points) |
|
52 | 52 | { |
|
53 | 53 | m_points = points; |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | void XYChart::setClipRect(const QRectF &rect) |
|
57 | 57 | { |
|
58 | 58 | m_clipRect = rect; |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | void XYChart::setAnimation(XYAnimation* animation) |
|
62 | 62 | { |
|
63 | 63 | m_animation=animation; |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | void XYChart::setDirty(bool dirty) |
|
67 | 67 | { |
|
68 | 68 | m_dirty=dirty; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | QPointF XYChart::calculateGeometryPoint(const QPointF &point) const |
|
72 | 72 | { |
|
73 | 73 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
74 | 74 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
75 | 75 | qreal x = (point.x() - m_minX)* deltaX; |
|
76 | 76 | qreal y = (point.y() - m_minY)*-deltaY + m_size.height(); |
|
77 | 77 | return QPointF(x,y); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | QPointF XYChart::calculateGeometryPoint(int index) const |
|
81 | 81 | { |
|
82 | 82 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
83 | 83 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
84 | 84 | const QList<QPointF>& vector = m_series->points(); |
|
85 | 85 | qreal x = (vector[index].x() - m_minX)* deltaX; |
|
86 | 86 | qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height(); |
|
87 | 87 | return QPointF(x,y); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | QVector<QPointF> XYChart::calculateGeometryPoints() const |
|
91 | 91 | { |
|
92 | 92 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
93 | 93 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
94 | 94 | |
|
95 | 95 | QVector<QPointF> result; |
|
96 | 96 | result.resize(m_series->count()); |
|
97 | 97 | const QList<QPointF>& vector = m_series->points(); |
|
98 | 98 | for (int i = 0; i < m_series->count(); ++i) { |
|
99 | 99 | qreal x = (vector[i].x() - m_minX)* deltaX; |
|
100 | 100 | qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height(); |
|
101 | 101 | result[i].setX(x); |
|
102 | 102 | result[i].setY(y); |
|
103 | 103 | } |
|
104 | 104 | return result; |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | QPointF XYChart::calculateDomainPoint(const QPointF &point) const |
|
108 | 108 | { |
|
109 | 109 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
110 | 110 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
111 | 111 | qreal x = point.x()/deltaX +m_minX; |
|
112 | 112 | qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY; |
|
113 | 113 | return QPointF(x,y); |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | void XYChart::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index) |
|
117 | 117 | { |
|
118 | 118 | if (m_animation) { |
|
119 | 119 | m_animation->setValues(oldPoints, newPoints, index); |
|
120 | 120 | setGeometryPoints(newPoints); |
|
121 | 121 | setDirty(false); |
|
122 | 122 | presenter()->startAnimation(m_animation); |
|
123 | 123 | } |
|
124 | 124 | else { |
|
125 | 125 | setGeometryPoints(newPoints); |
|
126 | 126 | updateGeometry(); |
|
127 | 127 | } |
|
128 | 128 | } |
|
129 | 129 | |
|
130 | 130 | //handlers |
|
131 | 131 | |
|
132 | 132 | void XYChart::handlePointAdded(int index) |
|
133 | 133 | { |
|
134 | 134 | Q_ASSERT(index<m_series->count()); |
|
135 | 135 | Q_ASSERT(index>=0); |
|
136 | 136 | |
|
137 | 137 | QVector<QPointF> points; |
|
138 | 138 | |
|
139 | 139 | if(m_animation) { |
|
140 | 140 | m_animation->setAnimationType(XYAnimation::AddPointAnimation); |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | if(m_dirty) { |
|
144 | 144 | points = calculateGeometryPoints(); |
|
145 | 145 | } else { |
|
146 | 146 | points = m_points; |
|
147 | 147 | QPointF point = calculateGeometryPoint(index); |
|
148 | 148 | points.insert(index, point); |
|
149 | 149 | } |
|
150 | 150 | |
|
151 | 151 | updateChart(m_points,points,index); |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | void XYChart::handlePointRemoved(int index) |
|
155 | 155 | { |
|
156 | 156 | Q_ASSERT(index<=m_series->count()); |
|
157 | 157 | Q_ASSERT(index>=0); |
|
158 | 158 | |
|
159 | 159 | QVector<QPointF> points; |
|
160 | 160 | |
|
161 | 161 | if(m_animation) { |
|
162 | 162 | m_animation->setAnimationType(XYAnimation::RemovePointAnimation); |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | if(m_dirty) { |
|
166 | 166 | points = calculateGeometryPoints(); |
|
167 | 167 | } else { |
|
168 | 168 | points = m_points; |
|
169 | 169 | points.remove(index); |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | updateChart(m_points,points,index); |
|
173 | 173 | } |
|
174 | 174 | |
|
175 | 175 | void XYChart::handlePointReplaced(int index) |
|
176 | 176 | { |
|
177 | 177 | Q_ASSERT(index<m_series->count()); |
|
178 | 178 | Q_ASSERT(index>=0); |
|
179 | 179 | |
|
180 | 180 | QVector<QPointF> points; |
|
181 | 181 | |
|
182 | 182 | if(m_animation) { |
|
183 | 183 | m_animation->setAnimationType(XYAnimation::ReplacePointAnimation); |
|
184 | 184 | } |
|
185 | 185 | |
|
186 | 186 | if(m_dirty) { |
|
187 | 187 | points = calculateGeometryPoints(); |
|
188 | 188 | } else { |
|
189 | 189 | QPointF point = calculateGeometryPoint(index); |
|
190 | 190 | points = m_points; |
|
191 | 191 | points.replace(index,point); |
|
192 | 192 | } |
|
193 | 193 | |
|
194 | 194 | updateChart(m_points,points,index); |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | void XYChart::handleReinitialized() |
|
198 | 198 | { |
|
199 | 199 | QVector<QPointF> points = calculateGeometryPoints(); |
|
200 | 200 | |
|
201 | 201 | if(m_animation) { |
|
202 | 202 | m_animation->setAnimationType(XYAnimation::NewAnimation); |
|
203 | 203 | } |
|
204 | 204 | |
|
205 | 205 | updateChart(m_points,points); |
|
206 | 206 | } |
|
207 | 207 | |
|
208 | 208 | void XYChart::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
209 | 209 | { |
|
210 | qDebug()<<__FUNCTION__; | |
|
211 | 210 | m_minX=minX; |
|
212 | 211 | m_maxX=maxX; |
|
213 | 212 | m_minY=minY; |
|
214 | 213 | m_maxY=maxY; |
|
215 | 214 | if (isEmpty()) return; |
|
216 | 215 | |
|
217 | 216 | QVector<QPointF> points = calculateGeometryPoints(); |
|
218 | 217 | |
|
219 | 218 | if(m_animation) { |
|
220 | 219 | m_animation->setAnimationType(XYAnimation::ReplacePointAnimation); |
|
221 | 220 | } |
|
222 | 221 | |
|
223 | 222 | updateChart(m_points,points); |
|
224 | 223 | } |
|
225 | 224 | |
|
226 | 225 | void XYChart::handleGeometryChanged(const QRectF &rect) |
|
227 | 226 | { |
|
228 | 227 | Q_ASSERT(rect.isValid()); |
|
229 | 228 | m_size=rect.size(); |
|
230 | 229 | m_clipRect=rect.translated(-rect.topLeft()); |
|
231 | 230 | m_origin=rect.topLeft(); |
|
232 | 231 | |
|
233 | 232 | if (isEmpty()) return; |
|
234 | 233 | |
|
235 | 234 | QVector<QPointF> points = calculateGeometryPoints(); |
|
236 | 235 | |
|
237 | 236 | if(m_animation) { |
|
238 | 237 | m_animation->setAnimationType(XYAnimation::NewAnimation); |
|
239 | 238 | } |
|
240 | 239 | |
|
241 | 240 | updateChart(m_points,points); |
|
242 | 241 | } |
|
243 | 242 | |
|
244 | 243 | bool XYChart::isEmpty() |
|
245 | 244 | { |
|
246 | 245 | return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY) || m_series->points().isEmpty(); |
|
247 | 246 | } |
|
248 | 247 | |
|
249 | 248 | #include "moc_xychart_p.cpp" |
|
250 | 249 | |
|
251 | 250 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now