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