##// END OF EJS Templates
Rewrite animation hadnling in line series...
Michal Klocek -
r389:7b90ec69ce9a
parent child
Show More
@@ -29,7 +29,7 class AxisAnimator: public QVariantAnimation
29 {
29 {
30 public:
30 public:
31 AxisAnimator(AxisItem *axis,QObject *parent = 0);
31 AxisAnimator(AxisItem *axis,QObject *parent = 0);
32 virtual ~AxisAnimator();
32 ~AxisAnimator();
33 protected:
33 protected:
34 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
34 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
35 virtual void updateCurrentValue (const QVariant & value );
35 virtual void updateCurrentValue (const QVariant & value );
@@ -58,9 +58,6 void AxisItem::clear(int count)
58 if(lines.size()%2) delete(shades.takeLast());
58 if(lines.size()%2) delete(shades.takeLast());
59 delete(axis.takeLast());
59 delete(axis.takeLast());
60 }
60 }
61
62 m_thicksList.clear();
63
64 }
61 }
65
62
66 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
63 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -35,7 +35,7 public:
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
37
37
38 protected slots:
38 public slots:
39 void handleModelChanged(int index);
39 void handleModelChanged(int index);
40 void handleDomainChanged(const Domain& domain);
40 void handleDomainChanged(const Domain& domain);
41 void handleGeometryChanged(const QRectF& size);
41 void handleGeometryChanged(const QRectF& size);
@@ -150,12 +150,11 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
150 m_axisXInitialized=true;
150 m_axisXInitialized=true;
151 }
151 }
152
152
153
154 emit seriesAdded(series);
155 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
153 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
156 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
154 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
157 emit axisLabelsChanged(axisY,ylabels);
155 emit axisLabelsChanged(axisY,ylabels);
158 emit axisLabelsChanged(axisX(),xlabels);
156 emit axisLabelsChanged(axisX(),xlabels);
157 emit seriesAdded(series);
159 emit seriesDomainChanged(series,domain);
158 emit seriesDomainChanged(series,domain);
160
159
161 }
160 }
@@ -118,8 +118,11 void ChartPresenter::handleSeriesAdded(QSeries* series)
118 }
118 }
119 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
119 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 QObject::connect(lineSeries,SIGNAL(pointChanged(int)),item,SLOT(handleModelChanged(int)));
121 QObject::connect(lineSeries,SIGNAL(pointReplaced(int)),item,SLOT(handlePointReplaced(int)));
122 QObject::connect(lineSeries,SIGNAL(pointAdded(int)),item,SLOT(handlePointAdded(int)));
123 QObject::connect(lineSeries,SIGNAL(pointRemoved(int)),item,SLOT(handlePointRemoved(int)));
122 m_chartItems.insert(series,item);
124 m_chartItems.insert(series,item);
125 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
123 break;
126 break;
124 }
127 }
125
128
@@ -131,6 +134,7 void ChartPresenter::handleSeriesAdded(QSeries* series)
131 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
134 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
132 m_chartItems.insert(series,item);
135 m_chartItems.insert(series,item);
133 // m_axisXItem->setVisible(false);
136 // m_axisXItem->setVisible(false);
137 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
134 break;
138 break;
135 }
139 }
136
140
@@ -142,6 +146,7 void ChartPresenter::handleSeriesAdded(QSeries* series)
142 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
146 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
143 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
147 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
144 m_chartItems.insert(series,item);
148 m_chartItems.insert(series,item);
149 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
145 break;
150 break;
146 }
151 }
147
152
@@ -153,6 +158,7 void ChartPresenter::handleSeriesAdded(QSeries* series)
153 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
158 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
154 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
159 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
155 m_chartItems.insert(series,item);
160 m_chartItems.insert(series,item);
161 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
156 break;
162 break;
157 }
163 }
158 case QSeries::SeriesTypeScatter: {
164 case QSeries::SeriesTypeScatter: {
@@ -163,6 +169,7 void ChartPresenter::handleSeriesAdded(QSeries* series)
163 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
169 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
164 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
170 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
165 m_chartItems.insert(scatterSeries, scatterPresenter);
171 m_chartItems.insert(scatterSeries, scatterPresenter);
172 if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect);
166 break;
173 break;
167 }
174 }
168 case QSeries::SeriesTypePie: {
175 case QSeries::SeriesTypePie: {
@@ -186,6 +193,7 void ChartPresenter::handleSeriesAdded(QSeries* series)
186 }
193 }
187
194
188 m_chartItems.insert(series, pie);
195 m_chartItems.insert(series, pie);
196 pie->handleGeometryChanged(m_rect);
189 break;
197 break;
190 }
198 }
191 default: {
199 default: {
@@ -193,8 +201,6 void ChartPresenter::handleSeriesAdded(QSeries* series)
193 break;
201 break;
194 }
202 }
195 }
203 }
196
197 if(m_rect.isValid()) emit geometryChanged(m_rect);
198 }
204 }
199
205
200 void ChartPresenter::handleSeriesRemoved(QSeries* series)
206 void ChartPresenter::handleSeriesRemoved(QSeries* series)
@@ -203,11 +209,6 void ChartPresenter::handleSeriesRemoved(QSeries* series)
203 delete item;
209 delete item;
204 }
210 }
205
211
206 void ChartPresenter::handleSeriesChanged(QSeries* series)
207 {
208 //TODO:
209 }
210
211 void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain)
212 void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain)
212 {
213 {
213 m_chartItems.value(series)->handleDomainChanged(domain);
214 m_chartItems.value(series)->handleDomainChanged(domain);
@@ -53,7 +53,6 public slots:
53 void handleAxisRemoved(QChartAxis* axis);
53 void handleAxisRemoved(QChartAxis* axis);
54 void handleSeriesDomainChanged(QSeries* series, const Domain& domain);
54 void handleSeriesDomainChanged(QSeries* series, const Domain& domain);
55 void handleAxisLabelsChanged(QChartAxis* axis, const QStringList& labels);
55 void handleAxisLabelsChanged(QChartAxis* axis, const QStringList& labels);
56 void handleSeriesChanged(QSeries* series);
57 void handleGeometryChanged();
56 void handleGeometryChanged();
58 signals:
57 signals:
59 void geometryChanged(const QRectF& rect);
58 void geometryChanged(const QRectF& rect);
@@ -19,8 +19,19 LineChartAnimationItem::~LineChartAnimationItem()
19
19
20 void LineChartAnimationItem::addPoints(const QVector<QPointF>& points)
20 void LineChartAnimationItem::addPoints(const QVector<QPointF>& points)
21 {
21 {
22 m_data=points;
22 QVector<qreal> vector0 = vector1;
23 clearView();
23 calculateLayout(vector1);
24 if(vector1.count()==0) return;
25 vector0.resize(vector1.size());
26
27
28 LineChartAnimatator *animation = new LineChartAnimatator(this,this);
29 animation->setDuration(duration);
30 animation->setEasingCurve(QEasingCurve::InOutBack);
31 animation->setKeyValueAt(0.0, qVariantFromValue(vector0));
32 animation->setKeyValueAt(1.0, qVariantFromValue(vector1));
33 animation->start(QAbstractAnimation::DeleteWhenStopped);
34
24 QPropertyAnimation *animation = new QPropertyAnimation(this, "a_addPoints", parent());
35 QPropertyAnimation *animation = new QPropertyAnimation(this, "a_addPoints", parent());
25 animation->setDuration(duration);
36 animation->setDuration(duration);
26 //animation->setEasingCurve(QEasingCurve::InOutBack);
37 //animation->setEasingCurve(QEasingCurve::InOutBack);
@@ -29,7 +40,7 void LineChartAnimationItem::addPoints(const QVector<QPointF>& points)
29 animation->start(QAbstractAnimation::DeleteWhenStopped);
40 animation->start(QAbstractAnimation::DeleteWhenStopped);
30 }
41 }
31
42
32 void LineChartAnimationItem::setPoint(int index,const QPointF& point)
43 void LineChartAnimationItem::replacePoint(int index,const QPointF& point)
33 {
44 {
34 AnimationHelper* helper = new AnimationHelper(this,index);
45 AnimationHelper* helper = new AnimationHelper(this,index);
35 QPropertyAnimation *animation = new QPropertyAnimation(helper, "point", parent());
46 QPropertyAnimation *animation = new QPropertyAnimation(helper, "point", parent());
@@ -52,11 +63,42 void LineChartAnimationItem::aw_addPoints(int points)
52
63
53 void LineChartAnimationItem::aw_setPoint(int index,const QPointF& point)
64 void LineChartAnimationItem::aw_setPoint(int index,const QPointF& point)
54 {
65 {
55 LineChartItem::setPoint(index,point);
66 LineChartItem::replacePoint(index,point);
56 updateGeometry();
67 updateGeometry();
57 update();
68 update();
58 }
69 }
59
70
71 LineChartAnimatator::LineChartAnimatator(LineChartItem *item, int index , QObject *parent = 0 ):QVariantAnimation(parent),
72 m_item(item),
73 m_index(index)
74 {
75 };
76
77 LineChartAnimatator::~LineChartAnimatator()
78 {
79 }
80
81 QVariant LineChartAnimatator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
82 {
83 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
84 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
85 QVector<qreal> result;
86 Q_ASSERT(startVector.count() == endVecotr.count());
87
88 for(int i =0 ;i< startVector.count();i++){
89 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
90 result << value;
91 }
92 return qVariantFromValue(result);
93 }
94
95
96 void LineChartAnimatator::updateCurrentValue (const QVariant & value )
97 {
98 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
99 m_axis->applyLayout(vector);
100 }
101
60
102
61 #include "moc_linechartanimationitem_p.cpp"
103 #include "moc_linechartanimationitem_p.cpp"
62
104
@@ -10,22 +10,13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 class LineChartItem;
10 class LineChartItem;
11
11
12 class LineChartAnimationItem : public LineChartItem {
12 class LineChartAnimationItem : public LineChartItem {
13 Q_OBJECT
13
14 Q_PROPERTY(int a_addPoints READ ar_addPoints WRITE aw_addPoints);
15 // Q_PROPERTY(QPointF a_setPoint READ ar_setPoint WRITE aw_setPoint);
16 public:
14 public:
17 LineChartAnimationItem(ChartPresenter* presenter, QLineSeries *series, QGraphicsItem *parent = 0);
15 LineChartAnimationItem(ChartPresenter* presenter, QLineSeries *series, QGraphicsItem *parent = 0);
18 virtual ~LineChartAnimationItem();
16 virtual ~LineChartAnimationItem();
19
17
20 void addPoints(const QVector<QPointF>& points);
18 void addPoints(const QVector<QPointF>& points);
21 void setPoint(int index,const QPointF& point);
19 void replacePoint(int index,const QPointF& point);
22 //void removePoint(const QPointF& point){};
23 //void setPoint(const QPointF& oldPoint, const QPointF& newPoint){};
24
25 int ar_addPoints() const { return m_addPoints;}
26 void aw_addPoints(int points);
27 const QPointF& ar_setPoint() const { return m_setPoint;}
28 void aw_setPoint(int index,const QPointF& point);
29
20
30 private:
21 private:
31 QVector<QPointF> m_data;
22 QVector<QPointF> m_data;
@@ -35,16 +26,12 private:
35 int m_setPoint_index;
26 int m_setPoint_index;
36 };
27 };
37
28
38 class AnimationHelper: public QObject
29 class LineChartAnimatator: public QVariantAnimation
39 {
30 {
40 Q_OBJECT
31
41 Q_PROPERTY(QPointF point READ point WRITE setPoint);
42 public:
32 public:
43 AnimationHelper(LineChartAnimationItem* item,int index):m_item(item),m_index(index){};
33 LineChartAnimatator(LineChartItem *item, int index , QObject *parent = 0 ):m_item(item),m_index(index){};
44 void setPoint(const QPointF& point){
34
45 m_item->aw_setPoint(m_index,point);
46 }
47 QPointF point(){return m_point;}
48 QPointF m_point;
35 QPointF m_point;
49 LineChartAnimationItem* m_item;
36 LineChartAnimationItem* m_item;
50 int m_index;
37 int m_index;
@@ -10,10 +10,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 LineChartItem::LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent):ChartItem(parent),
11 LineChartItem::LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent):ChartItem(parent),
12 m_presenter(presenter),
12 m_presenter(presenter),
13 m_series(series),
13 m_series(series)
14 m_dirtyData(false),
15 m_dirtyGeometry(false),
16 m_dirtyDomain(false)
17 {
14 {
18 setZValue(ChartPresenter::LineChartZValue);
15 setZValue(ChartPresenter::LineChartZValue);
19 }
16 }
@@ -28,78 +25,23 QPainterPath LineChartItem::shape() const
28 return m_path;
25 return m_path;
29 }
26 }
30
27
31
28 void LineChartItem::createPoints(int count)
32 void LineChartItem::addPoints(const QVector<QPointF>& points)
33 {
29 {
34 m_data = points;
30 for (int i = 0; i < count; ++i) {
35 for(int i=0; i<m_data.size();i++){
31 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
36 const QPointF& point =m_data[i];
32 m_items.addToGroup(item);
37 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
38 item->setPos(point.x()-1,point.y()-1);;
39 if(!m_clipRect.contains(point) || !m_series->pointsVisible()) item->setVisible(false);
40 m_points << item;
41 }
33 }
42 }
34 }
43
35
44 void LineChartItem::addPoint(const QPointF& point)
36 void LineChartItem::clearPoints(int count)
45 {
37 {
46 m_data << point;
38 QList<QGraphicsItem *> items = m_items.childItems();
47 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
48 m_clipRect.contains(point);
49 item->setPos(point.x()-1,point.y()-1);
50 if(!m_clipRect.contains(point) || !m_series->pointsVisible()) item->setVisible(false);
51 m_points << item;
52 }
53
54 void LineChartItem::removePoint(const QPointF& point)
55 {
56 Q_ASSERT(m_data.count() == m_points.count());
57 int index = m_data.lastIndexOf(point,0);
58 m_data.remove(index);
59 delete(m_points.takeAt(index));
60 }
61
39
62 void LineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint)
40 for (int i = 0; i < count; ++i) {
63 {
41 delete(items.takeLast());
64 Q_ASSERT(m_data.count() == m_points.count());
65 int index = m_data.lastIndexOf(oldPoint,0);
66
67 if(index > -1){
68 m_data.replace(index,newPoint);
69 QGraphicsItem* item = m_points.at(index);
70 item->setPos(newPoint.x()-1,newPoint.y()-1);
71 }
42 }
72 }
43 }
73
44
74 void LineChartItem::setPoint(int index,const QPointF& point)
75 {
76 Q_ASSERT(m_data.count() == m_points.count());
77 Q_ASSERT(index>=0);
78
79 m_data.replace(index,point);
80 QGraphicsItem* item = m_points.at(index);
81 item->setPos(point.x()-1,point.y()-1);
82 }
83
84 void LineChartItem::clear()
85 {
86 qDeleteAll(m_points);
87 m_points.clear();
88 m_hash.clear();
89 m_path = QPainterPath();
90 m_rect = QRect();
91 m_data.clear();
92 }
93
94 void LineChartItem::clearView()
95 {
96 qDeleteAll(m_points);
97 m_points.clear();
98 m_path = QPainterPath();
99 m_rect = QRect();
100 m_data.clear();
101 }
102
103 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
45 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
104 {
46 {
105 Q_UNUSED(widget);
47 Q_UNUSED(widget);
@@ -111,58 +53,66 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
111 painter->restore();
53 painter->restore();
112 }
54 }
113
55
114 void LineChartItem::calculatePoint(QPointF& point, int index, const QLineSeries* series,const QSizeF& size, const Domain& domain) const
56 QPointF LineChartItem::calculatePoint(int index) const
115 {
57 {
116 const qreal deltaX = size.width()/domain.spanX();
58 const qreal deltaX = m_size.width()/m_domain.spanX();
117 const qreal deltaY = size.height()/domain.spanY();
59 const qreal deltaY = m_size.height()/m_domain.spanY();
118 qreal x = (series->x(index) - domain.m_minX)* deltaX;
60 qreal x = (m_series->x(index) - m_domain.m_minX)* deltaX;
119 qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height();
61 qreal y = (m_series->y(index) - m_domain.m_minY)*-deltaY + m_size.height();
120 point.setX(x);
62 return QPointF(x,y);
121 point.setY(y);
122 }
63 }
123
64
124
65
125 void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QLineSeries* series,const QSizeF& size, const Domain& domain) const
66 void LineChartItem::calculatePoints(QVector<QPointF>& points, QVector<int>& map) const
126 {
67 {
127 const qreal deltaX = size.width()/domain.spanX();
68 const qreal deltaX = m_size.width()/m_domain.spanX();
128 const qreal deltaY = size.height()/domain.spanY();
69 const qreal deltaY = m_size.height()/m_domain.spanY();
129
70
130 for (int i = 0; i < series->count(); ++i) {
71 for (int i = 0; i < m_series->count(); ++i) {
131 qreal x = (series->x(i) - domain.m_minX)* deltaX;
72 qreal x = (m_series->x(i) - m_domain.m_minX)* deltaX;
132 qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height();
73 qreal y = (m_series->y(i) - m_domain.m_minY)*-deltaY + m_size.height();
133 hash[i] = points.size();
74 map << i;
134 points << QPointF(x,y);
75 points << QPointF(x,y);
135 }
76 }
136 }
77 }
137
78
138 void LineChartItem::updateDomain()
79 void LineChartItem::updateItem(QVector<QPointF>& points,QVector<int>& map)
139 {
80 {
140 clear();
81 int diff = m_points.size() - points.size();
141 prepareGeometryChange();
142 calculatePoints(m_data,m_hash,m_series,m_size, m_domain);
143 addPoints(m_data);
144 }
145
82
146 void LineChartItem::updateData()
83 if(diff>0) {
147 {
84 clearPoints(diff);
148 //for now the same
85 }
149 updateDomain();
86 else if(diff<0) {
87 createPoints(-diff);
88 }
89
90 applyGeometry(points);
91 m_points=points;
92 m_map=map;
150 }
93 }
151
94
152 void LineChartItem::updateGeometry()
95 void LineChartItem::applyGeometry(QVector<QPointF>& points)
153 {
96 {
97 if(points.size()==0) return;
154
98
155 if(m_data.size()==0) return;
99 QList<QGraphicsItem*> items = m_items.childItems();
156
100
157 prepareGeometryChange();
158 QPainterPath path;
101 QPainterPath path;
159 const QPointF& point = m_data.at(0);
102 const QPointF& point = points.at(0);
160 path.moveTo(point);
103 path.moveTo(point);
161
104 QGraphicsItem* item = items.at(0);
162 foreach( const QPointF& point , m_data) {
105 item->setPos(point.x()-1,point.y()-1);
106
107 for(int i=1 ; i< points.size();i++) {
108 QGraphicsItem* item = items.at(i);
109 const QPointF& point = points.at(i);
110 item->setPos(point.x()-1,point.y()-1);
111 if(!m_clipRect.contains(point)) item->setVisible(false);
163 path.lineTo(point);
112 path.lineTo(point);
164 }
113 }
165
114
115 prepareGeometryChange();
166 m_path = path;
116 m_path = path;
167 m_rect = path.boundingRect();
117 m_rect = path.boundingRect();
168 }
118 }
@@ -174,14 +124,78 void LineChartItem::setPen(const QPen& pen)
174
124
175 //handlers
125 //handlers
176
126
177 void LineChartItem::handleModelChanged(int index)
127 void LineChartItem::handlePointAdded(int index)
178 {
128 {
179 Q_ASSERT(index<m_series->count());
129 Q_ASSERT(index<m_series->count());
180 if(m_hash.contains(index)){
130 Q_ASSERT(index>=0);
181 int i = m_hash.value(index);
131
182 QPointF point;
132 if(m_map.contains(index)){
183 calculatePoint(point,index,m_series,m_size,m_domain);
133
184 setPoint(i,point);
134 int i = m_map.indexOf(index);
135
136 QPointF point = calculatePoint(index);
137
138 if(point.isNull()) return;
139
140 QVector<QPointF> points = m_points;
141 points.insert(i,point);
142
143 for(int j=i;j<m_map.count();j++)
144 m_map[j]-=1;
145
146 updateItem(points,m_map);
147
148 }else{
149 //ignore
150 return;
151 }
152 update();
153 }
154 void LineChartItem::handlePointRemoved(int index)
155 {
156 Q_ASSERT(index<m_series->count());
157 Q_ASSERT(index>=0);
158
159 if(m_map.contains(index)){
160
161 int i = m_map.value(index);
162
163 QVector<QPointF> points = m_points;
164 points.remove(i);
165
166 for(int j=i;j<m_map.count();j++)
167 m_map[j]+=1;
168
169 m_map<<
170
171 updateItem(points,m_map);
172
173 }else{
174 //ignore
175 return;
176 }
177 update();
178 }
179
180 void LineChartItem::handlePointReplaced(int index)
181 {
182 Q_ASSERT(index<m_series->count());
183 Q_ASSERT(index>=0);
184
185 if(m_map.contains(index)){
186
187 int i = m_map.indexOf(index);
188
189 QPointF point = calculatePoint(index);
190
191 QVector<QPointF> points = m_points;
192 points.replace(i,point);
193
194 updateItem(points,m_map);
195
196 }else{
197 //ignore
198 return;
185 }
199 }
186 update();
200 update();
187 }
201 }
@@ -189,7 +203,12 void LineChartItem::handleModelChanged(int index)
189 void LineChartItem::handleDomainChanged(const Domain& domain)
203 void LineChartItem::handleDomainChanged(const Domain& domain)
190 {
204 {
191 m_domain = domain;
205 m_domain = domain;
192 updateDomain();
206 if(m_domain.isEmpty()) return;
207
208 QVector<QPointF> points;
209 QVector<int> map;
210 calculatePoints(points,map);
211 updateItem(points,map);
193 update();
212 update();
194 }
213 }
195
214
@@ -198,9 +217,24 void LineChartItem::handleGeometryChanged(const QRectF& rect)
198 Q_ASSERT(rect.isValid());
217 Q_ASSERT(rect.isValid());
199 m_size=rect.size();
218 m_size=rect.size();
200 m_clipRect=rect.translated(-rect.topLeft());
219 m_clipRect=rect.translated(-rect.topLeft());
201 updateDomain();
202 updateGeometry();
203 setPos(rect.topLeft());
220 setPos(rect.topLeft());
221
222 if(m_domain.isEmpty()) return;
223 if(m_points.isEmpty()) return;
224
225 QVector<QPointF> points;
226 QVector<int> map;
227 calculatePoints(points,map);
228 updateItem(points,map);
229 update();
230 }
231
232 void LineChartItem::handleSeriesUpdated()
233 {
234 if(m_points.count()==0) return;
235
236 m_items.setVisible(m_series->pointsVisible());
237 setPen(m_series->pen());
204 update();
238 update();
205 }
239 }
206
240
@@ -9,7 +9,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartPresenter;
10 class ChartPresenter;
11 class QLineSeries;
11 class QLineSeries;
12 class LineChartAnimationItem;
13
12
14 class LineChartItem : public QObject , public ChartItem
13 class LineChartItem : public QObject , public ChartItem
15 {
14 {
@@ -24,32 +23,28 public:
24 QPainterPath shape() const;
23 QPainterPath shape() const;
25
24
26 void setPen(const QPen& pen);
25 void setPen(const QPen& pen);
27 const Domain& domain() const { return m_domain;}
26 void setPointsVisible(bool visible);
27
28 public slots:
29 void handlePointAdded(int index);
30 void handlePointRemoved(int index);
31 void handlePointReplaced(int index);
32 void handleDomainChanged(const Domain& domain);
33 void handleGeometryChanged(const QRectF& size);
28
34
35 protected:
29 virtual void addPoint(const QPointF& );
36 virtual void addPoint(const QPointF& );
30 virtual void addPoints(const QVector<QPointF>& points);
37 virtual void addPoints(const QVector<QPointF>& points);
31 virtual void removePoint(const QPointF& point);
38 virtual void removePoint(const QPointF& point);
32 virtual void setPoint(const QPointF& oldPoint, const QPointF& newPoint);
39 virtual void replacePoint(const QPointF& oldPoint, const QPointF& newPoint);
33 virtual void setPoint(int index,const QPointF& point);
40 virtual void replacePoint(int index,const QPointF& point);
34 void setPointsVisible(bool visible);
41 virtual void updateItem(QVector<QPointF>& points,QVector<int>& map);
35 void clear();
42 virtual void applyGeometry(QVector<QPointF>& points);
36 void clearView();
37 int count() const { return m_data.size();}
38
39 const QVector<QPointF>& points(){ return m_data;}
40
43
41 protected:
44 void clear();
42 virtual void updateGeometry();
43 virtual void updateData();
44 virtual void updateDomain();
45 //refactor
46 void calculatePoint(QPointF& point, int index, const QLineSeries* series,const QSizeF& size, const Domain& domain) const;
47 void calculatePoints(QVector<QPointF>& points,QHash<int,int>& hash,const QLineSeries* series, const QSizeF& size, const Domain& domain) const;
48
45
49 protected slots:
46 QPointF calculatePoint(int index) const;
50 void handleModelChanged(int index);
47 void calculatePoints(QVector<QPointF>& points,QVector<int>& map) const;
51 void handleDomainChanged(const Domain& domain);
52 void handleGeometryChanged(const QRectF& size);
53
48
54 private:
49 private:
55 ChartPresenter* m_presenter;
50 ChartPresenter* m_presenter;
@@ -58,14 +53,11 private:
58 QRectF m_rect;
53 QRectF m_rect;
59 QRectF m_clipRect;
54 QRectF m_clipRect;
60 Domain m_domain;
55 Domain m_domain;
61 QList<QGraphicsItem*> m_points;
56 QList<QGraphicsItem*> m_items;
62 QVector<QPointF> m_data;
57 QVector<QPointF> m_points;
63 QHash<int,int> m_hash;
58 QVector<int> m_map;
64 QLineSeries* m_series;
59 QLineSeries* m_series;
65 QPen m_pen;
60 QPen m_pen;
66 bool m_dirtyData;
67 bool m_dirtyGeometry;
68 bool m_dirtyDomain;
69
61
70 };
62 };
71
63
@@ -43,7 +43,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
43
43
44
44
45 /*!
45 /*!
46 \fn void QLineSeries::pointChanged(int index)
46 \fn void QLineSeries::pointReplaced(int index)
47 \brief \internal \a index
47 \brief \internal \a index
48 */
48 */
49
49
@@ -101,7 +101,7 void QLineSeries::replace(qreal x,qreal y)
101 int index = m_x.indexOf(x);
101 int index = m_x.indexOf(x);
102 m_x[index]=x;
102 m_x[index]=x;
103 m_y[index]=y;
103 m_y[index]=y;
104 emit pointChanged(index);
104 emit pointReplaced(index);
105 }
105 }
106
106
107 /*!
107 /*!
@@ -40,7 +40,7 public: // from QChartSeries
40 friend QDebug operator<< (QDebug d, const QLineSeries series);
40 friend QDebug operator<< (QDebug d, const QLineSeries series);
41
41
42 signals:
42 signals:
43 void pointChanged(int index);
43 void pointReplaced(int index);
44 void pointRemoved(int index);
44 void pointRemoved(int index);
45 void pointAdded(int index);
45 void pointAdded(int index);
46
46
General Comments 0
You need to be logged in to leave comments. Login now