##// END OF EJS Templates
Refactors linechart update calls
Michal Klocek -
r464:407de0c5cf5b
parent child
Show More
@@ -51,8 +51,8 public:
51
51
52 ~AreaBoundItem(){};
52 ~AreaBoundItem(){};
53
53
54 void applyGeometry(QVector<QPointF>& points){
54 void setGeometry(QVector<QPointF>& points){
55 LineChartItem::applyGeometry(points);
55 LineChartItem::setGeometry(points);
56 m_item->updatePath();
56 m_item->updatePath();
57 }
57 }
58
58
@@ -20,8 +20,12 LineChartAnimationItem::~LineChartAnimationItem()
20 {
20 {
21 }
21 }
22
22
23 void LineChartAnimationItem::updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints)
23 void LineChartAnimationItem::updateAllPoints()
24 {
24 {
25 QVector<QPointF> oldPoints = points();
26 LineChartItem::updateAllPoints();
27 QVector<QPointF> newPoints = points();
28
25 if(newPoints.count()==0) return;
29 if(newPoints.count()==0) return;
26 oldPoints.resize(newPoints.size());
30 oldPoints.resize(newPoints.size());
27
31
@@ -35,31 +39,35 void LineChartAnimationItem::updateItem(QVector<QPointF>& oldPoints,QVector<QPoi
35 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
39 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
36 QTimer::singleShot(0,m_animation,SLOT(start()));
40 QTimer::singleShot(0,m_animation,SLOT(start()));
37
41
38 oldPoints = newPoints;
42
39 m_points = newPoints;
43 m_points = newPoints;
40 m_dirty=false;
44 m_dirty=false;
45
41 }
46 }
42
47
43 void LineChartAnimationItem::updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint)
48 void LineChartAnimationItem::updatePoint(int index,QPointF& newPoint)
44 {
49 {
50
45 if(m_animation->state()!=QAbstractAnimation::Stopped){
51 if(m_animation->state()!=QAbstractAnimation::Stopped){
46 m_animation->stop();
52 m_animation->stop();
47 m_dirty=true;
53 m_dirty=true;
48 }
54 }
49
55
50 if(m_dirty){
56 if(m_dirty){
51 m_points=oldPoints;
57 m_points=points();
52 m_dirty=false;
58 m_dirty=false;
53 }
59 }
54
60
55 oldPoints.replace(index,newPoint);
61 LineChartItem::updatePoint(index,newPoint);
56
62
57 m_animation->setDuration(duration);
63 m_animation->setDuration(duration);
58 m_animation->setEasingCurve(QEasingCurve::InOutBack);
64 m_animation->setEasingCurve(QEasingCurve::InOutBack);
59 m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points));
65 m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points));
60 m_animation->setKeyValueAt(1.0, qVariantFromValue(oldPoints));
66 m_animation->setKeyValueAt(1.0, qVariantFromValue( points()));
67
61 QTimer::singleShot(0,this,SLOT(startAnimation()));
68 QTimer::singleShot(0,this,SLOT(startAnimation()));
62
69
70
63 }
71 }
64
72
65 void LineChartAnimationItem::startAnimation()
73 void LineChartAnimationItem::startAnimation()
@@ -95,7 +103,9 QVariant LineChartAnimatator::interpolated(const QVariant &start, const QVariant
95 void LineChartAnimatator::updateCurrentValue (const QVariant & value )
103 void LineChartAnimatator::updateCurrentValue (const QVariant & value )
96 {
104 {
97 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
105 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
98 m_item->applyGeometry(vector);
106 if(state()!=QAbstractAnimation::Stopped){ //workaround
107 m_item->setGeometry(vector);
108 }
99 }
109 }
100
110
101 #include "moc_linechartanimationitem_p.cpp"
111 #include "moc_linechartanimationitem_p.cpp"
@@ -19,8 +19,8 public:
19 virtual ~LineChartAnimationItem();
19 virtual ~LineChartAnimationItem();
20
20
21 protected:
21 protected:
22 virtual void updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints);
22 virtual void updateAllPoints();
23 virtual void updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint);
23 virtual void updatePoint(int index,QPointF& newPoint);
24
24
25 private slots:
25 private slots:
26 void startAnimation();
26 void startAnimation();
@@ -78,19 +78,43 QVector<QPointF> LineChartItem::calculateGeometryPoints() const
78 return points;
78 return points;
79 }
79 }
80
80
81 void LineChartItem::updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints)
81 void LineChartItem::updateAllPoints()
82 {
82 {
83 applyGeometry(newPoints);
83 QVector<QPointF> points = calculateGeometryPoints();
84 oldPoints = newPoints;
84
85 int diff = m_points.size() - points.size();
86
87 if(diff>0) {
88 clearPoints(diff);
89 }
90 else if(diff<0) {
91 createPoints(-diff);
92 }
93
94 setGeometry(points);
85 }
95 }
86
96
87 void LineChartItem::updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint)
97 void LineChartItem::updatePoints(QVector<QPointF>& points)
88 {
98 {
89 oldPoints.replace(index,newPoint);
99 int diff = m_points.size() - points.size();
90 applyGeometry(oldPoints);
100
101 if(diff>0) {
102 clearPoints(diff);
103 }
104 else if(diff<0) {
105 createPoints(-diff);
106 }
107
108 setGeometry(points);
109 }
110
111 void LineChartItem::updatePoint(int index,QPointF& newPoint)
112 {
113 m_points.replace(index,newPoint);
114 setGeometry(m_points);
91 }
115 }
92
116
93 void LineChartItem::applyGeometry(QVector<QPointF>& points)
117 void LineChartItem::setGeometry(QVector<QPointF>& points)
94 {
118 {
95 if(points.size()==0) return;
119 if(points.size()==0) return;
96
120
@@ -122,6 +146,7 void LineChartItem::applyGeometry(QVector<QPointF>& points)
122 prepareGeometryChange();
146 prepareGeometryChange();
123 m_path = path;
147 m_path = path;
124 m_rect = path.boundingRect();
148 m_rect = path.boundingRect();
149 m_points = points;
125 }
150 }
126
151
127 void LineChartItem::setPen(const QPen& pen)
152 void LineChartItem::setPen(const QPen& pen)
@@ -140,18 +165,20 void LineChartItem::handlePointAdded(int index)
140 createPoints(1);
165 createPoints(1);
141 QVector<QPointF> points = m_points;
166 QVector<QPointF> points = m_points;
142 points.insert(index,point);
167 points.insert(index,point);
143 updateItem(m_points,points);
168
169 updatePoints(points);
144 update();
170 update();
145 }
171 }
146 void LineChartItem::handlePointRemoved(int index)
172 void LineChartItem::handlePointRemoved(int index)
147 {
173 {
148 Q_ASSERT(index<m_series->count());
174 Q_ASSERT(index<m_series->count());
149 Q_ASSERT(index>=0);
175 Q_ASSERT(index>=0);
176
150 QPointF point = calculateGeometryPoint(index);
177 QPointF point = calculateGeometryPoint(index);
151 clearPoints(1);
178 clearPoints(1);
152 QVector<QPointF> points = m_points;
179 QVector<QPointF> points = m_points;
153 points.remove(index);
180 points.remove(index);
154 updateItem(m_points,points);
181 updatePoints(points);
155 update();
182 update();
156 }
183 }
157
184
@@ -160,7 +187,7 void LineChartItem::handlePointReplaced(int index)
160 Q_ASSERT(index<m_series->count());
187 Q_ASSERT(index<m_series->count());
161 Q_ASSERT(index>=0);
188 Q_ASSERT(index>=0);
162 QPointF point = calculateGeometryPoint(index);
189 QPointF point = calculateGeometryPoint(index);
163 updateItem(m_points,index,point);
190 updatePoint(index,point);
164 update();
191 update();
165 }
192 }
166
193
@@ -171,21 +198,8 void LineChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qrea
171 m_minY=minY;
198 m_minY=minY;
172 m_maxY=maxY;
199 m_maxY=maxY;
173
200
174 if( (m_maxX - m_minX) == 0|| (m_maxY - m_minY) == 0) return;
201 if(isEmpty()) return;
175 if(!m_clipRect.isValid()) return;
202 updateAllPoints();
176
177 QVector<QPointF> points = calculateGeometryPoints();
178
179 int diff = m_points.size() - points.size();
180
181 if(diff>0) {
182 clearPoints(diff);
183 }
184 else if(diff<0) {
185 createPoints(-diff);
186 }
187
188 updateItem(m_points,points);
189 update();
203 update();
190 }
204 }
191
205
@@ -196,20 +210,8 void LineChartItem::handleGeometryChanged(const QRectF& rect)
196 m_clipRect=rect.translated(-rect.topLeft());
210 m_clipRect=rect.translated(-rect.topLeft());
197 setPos(rect.topLeft());
211 setPos(rect.topLeft());
198
212
199 if( (m_maxX - m_minX) == 0|| (m_maxY - m_minY) == 0) return;
213 if(isEmpty()) return;
200
214 updateAllPoints();
201 QVector<QPointF> points = calculateGeometryPoints();
202
203 int diff = m_points.size() - points.size();
204
205 if(diff>0) {
206 clearPoints(diff);
207 }
208 else if(diff<0) {
209 createPoints(-diff);
210 }
211
212 updateItem(m_points,points);
213 update();
215 update();
214 }
216 }
215
217
@@ -233,6 +235,10 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
233 painter->restore();
235 painter->restore();
234 }
236 }
235
237
238 bool LineChartItem::isEmpty()
239 {
240 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
241 }
236
242
237 #include "moc_linechartitem_p.cpp"
243 #include "moc_linechartitem_p.cpp"
238
244
@@ -33,14 +33,18 public slots:
33 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
33 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
34 void handleGeometryChanged(const QRectF& size);
34 void handleGeometryChanged(const QRectF& size);
35
35
36 public:
36 protected:
37 virtual void updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints);
37 virtual void updateAllPoints();
38 virtual void updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint);
38 virtual void updatePoints(QVector<QPointF>& points);
39 virtual void applyGeometry(QVector<QPointF>& points);
39 virtual void updatePoint(int index,QPointF& newPoint);
40 virtual void setGeometry(QVector<QPointF>& points);
41
42 QVector<QPointF> points() {return m_points;}
40 void createPoints(int count);
43 void createPoints(int count);
41 void clearPoints(int count);
44 void clearPoints(int count);
42 QPointF calculateGeometryPoint(int index) const;
45 QPointF calculateGeometryPoint(int index) const;
43 QVector<QPointF> calculateGeometryPoints() const;
46 QVector<QPointF> calculateGeometryPoints() const;
47 inline bool isEmpty();
44
48
45 protected:
49 protected:
46 qreal m_minX;
50 qreal m_minX;
@@ -56,6 +60,8 protected:
56 QVector<QPointF> m_points;
60 QVector<QPointF> m_points;
57 QPen m_pen;
61 QPen m_pen;
58
62
63 friend class LineChartAnimatator;
64
59 };
65 };
60
66
61 QTCOMMERCIALCHART_END_NAMESPACE
67 QTCOMMERCIALCHART_END_NAMESPACE
@@ -21,7 +21,7 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
21 return QPointF(x,y);
21 return QPointF(x,y);
22 }
22 }
23
23
24 void SplineChartItem::applyGeometry(QVector<QPointF>& points)
24 void SplineChartItem::setGeometry(QVector<QPointF>& points)
25 {
25 {
26 if(points.size()==0) return;
26 if(points.size()==0) return;
27
27
@@ -16,7 +16,7 public:
16
16
17 void updateGeometry();
17 void updateGeometry();
18
18
19 void applyGeometry(QVector<QPointF>& points);
19 void setGeometry(QVector<QPointF>& points);
20
20
21 QPointF calculateGeometryControlPoint(int index) const;
21 QPointF calculateGeometryControlPoint(int index) const;
22
22
General Comments 0
You need to be logged in to leave comments. Login now