##// END OF EJS Templates
Refactors linechart update calls
Michal Klocek -
r464:407de0c5cf5b
parent child
Show More
@@ -1,66 +1,66
1 #ifndef AREACHARTITEM_H
1 #ifndef AREACHARTITEM_H
2 #define AREACHARTITEM_H
2 #define AREACHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "linechartitem_p.h"
5 #include "linechartitem_p.h"
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartPresenter;
10 class ChartPresenter;
11 class QAreaSeries;
11 class QAreaSeries;
12 class AreaChartItem;
12 class AreaChartItem;
13
13
14 class AreaChartItem : public QObject ,public ChartItem
14 class AreaChartItem : public QObject ,public ChartItem
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 AreaChartItem(QAreaSeries* areaSeries, QGraphicsItem *parent = 0);
18 AreaChartItem(QAreaSeries* areaSeries, QGraphicsItem *parent = 0);
19 ~ AreaChartItem();
19 ~ AreaChartItem();
20
20
21 //from QGraphicsItem
21 //from QGraphicsItem
22 QRectF boundingRect() const;
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24 QPainterPath shape() const;
24 QPainterPath shape() const;
25
25
26 void setPen(const QPen& pen);
26 void setPen(const QPen& pen);
27 void setBrush(const QBrush& brush);
27 void setBrush(const QBrush& brush);
28 void setPointsVisible(bool visible);
28 void setPointsVisible(bool visible);
29 void updatePath();
29 void updatePath();
30 public slots:
30 public slots:
31 void handleUpdated();
31 void handleUpdated();
32 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
32 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
33 void handleGeometryChanged(const QRectF& size);
33 void handleGeometryChanged(const QRectF& size);
34
34
35 private:
35 private:
36 QAreaSeries* m_series;
36 QAreaSeries* m_series;
37 LineChartItem* m_upper;
37 LineChartItem* m_upper;
38 LineChartItem* m_lower;
38 LineChartItem* m_lower;
39 QPainterPath m_path;
39 QPainterPath m_path;
40 QRectF m_rect;
40 QRectF m_rect;
41 QRectF m_clipRect;
41 QRectF m_clipRect;
42 QPen m_pen;
42 QPen m_pen;
43 QBrush m_brush;
43 QBrush m_brush;
44 };
44 };
45
45
46 class AreaBoundItem : public LineChartItem
46 class AreaBoundItem : public LineChartItem
47 {
47 {
48 public:
48 public:
49 AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries),
49 AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries),
50 m_item(item){};
50 m_item(item){};
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
59 private:
59 private:
60 AreaChartItem* m_item;
60 AreaChartItem* m_item;
61
61
62 };
62 };
63
63
64 QTCOMMERCIALCHART_END_NAMESPACE
64 QTCOMMERCIALCHART_END_NAMESPACE
65
65
66 #endif
66 #endif
@@ -1,103 +1,113
1 #include "linechartanimationitem_p.h"
1 #include "linechartanimationitem_p.h"
2 #include "linechartitem_p.h"
2 #include "linechartitem_p.h"
3 #include <QPropertyAnimation>
3 #include <QPropertyAnimation>
4 #include <QTimer>
4 #include <QTimer>
5
5
6 Q_DECLARE_METATYPE(QVector<QPointF>)
6 Q_DECLARE_METATYPE(QVector<QPointF>)
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 const static int duration = 500;
10 const static int duration = 500;
11
11
12 LineChartAnimationItem::LineChartAnimationItem(QLineSeries* series,QGraphicsItem *parent):
12 LineChartAnimationItem::LineChartAnimationItem(QLineSeries* series,QGraphicsItem *parent):
13 LineChartItem(series,parent),
13 LineChartItem(series,parent),
14 m_animation(new LineChartAnimatator(this,this)),
14 m_animation(new LineChartAnimatator(this,this)),
15 m_dirty(false)
15 m_dirty(false)
16 {
16 {
17 }
17 }
18
18
19 LineChartAnimationItem::~LineChartAnimationItem()
19 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
28 if(m_animation->state()!=QAbstractAnimation::Stopped){
32 if(m_animation->state()!=QAbstractAnimation::Stopped){
29 m_animation->stop();
33 m_animation->stop();
30 }
34 }
31
35
32 m_animation->setDuration(duration);
36 m_animation->setDuration(duration);
33 m_animation->setEasingCurve(QEasingCurve::InOutBack);
37 m_animation->setEasingCurve(QEasingCurve::InOutBack);
34 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints));
38 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints));
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()
66 {
74 {
67 m_dirty=true;
75 m_dirty=true;
68 m_animation->start();
76 m_animation->start();
69 }
77 }
70
78
71 LineChartAnimatator::LineChartAnimatator(LineChartAnimationItem *item , QObject *parent):QVariantAnimation(parent),
79 LineChartAnimatator::LineChartAnimatator(LineChartAnimationItem *item , QObject *parent):QVariantAnimation(parent),
72 m_item(item)
80 m_item(item)
73 {
81 {
74 }
82 }
75
83
76 LineChartAnimatator::~LineChartAnimatator()
84 LineChartAnimatator::~LineChartAnimatator()
77 {
85 {
78 }
86 }
79
87
80 QVariant LineChartAnimatator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
88 QVariant LineChartAnimatator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
81 {
89 {
82 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
90 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
83 QVector<QPointF> endVecotr = qVariantValue<QVector<QPointF> >(end);
91 QVector<QPointF> endVecotr = qVariantValue<QVector<QPointF> >(end);
84 QVector<QPointF> result;
92 QVector<QPointF> result;
85 Q_ASSERT(startVector.count() == endVecotr.count());
93 Q_ASSERT(startVector.count() == endVecotr.count());
86
94
87 for(int i =0 ;i< startVector.count();i++){
95 for(int i =0 ;i< startVector.count();i++){
88 qreal x = startVector[i].x() + ((endVecotr[i].x()- startVector[i].x()) * progress);//qBound(0.0, progress, 1.0));
96 qreal x = startVector[i].x() + ((endVecotr[i].x()- startVector[i].x()) * progress);//qBound(0.0, progress, 1.0));
89 qreal y = startVector[i].y() + ((endVecotr[i].y()- startVector[i].y()) * progress);//qBound(0.0, progress, 1.0));
97 qreal y = startVector[i].y() + ((endVecotr[i].y()- startVector[i].y()) * progress);//qBound(0.0, progress, 1.0));
90 result << QPointF(x,y);
98 result << QPointF(x,y);
91 }
99 }
92 return qVariantFromValue(result);
100 return qVariantFromValue(result);
93 }
101 }
94
102
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"
102
112
103 QTCOMMERCIALCHART_END_NAMESPACE
113 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,50 +1,50
1 #ifndef LINECHARTANIMATIONITEM_P_H_
1 #ifndef LINECHARTANIMATIONITEM_P_H_
2 #define LINECHARTANIMATIONITEM_P_H_
2 #define LINECHARTANIMATIONITEM_P_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "linechartitem_p.h"
5 #include "linechartitem_p.h"
6 #include "domain_p.h"
6 #include "domain_p.h"
7 #include <QVariantAnimation>
7 #include <QVariantAnimation>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class LineChartAnimatator;
11 class LineChartAnimatator;
12
12
13 class LineChartAnimationItem : public LineChartItem {
13 class LineChartAnimationItem : public LineChartItem {
14
14
15 Q_OBJECT
15 Q_OBJECT
16
16
17 public:
17 public:
18 LineChartAnimationItem(QLineSeries *series, QGraphicsItem *parent = 0);
18 LineChartAnimationItem(QLineSeries *series, QGraphicsItem *parent = 0);
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();
27
27
28 private:
28 private:
29 LineChartAnimatator *m_animation;
29 LineChartAnimatator *m_animation;
30 QVector<QPointF> m_points;
30 QVector<QPointF> m_points;
31 bool m_dirty;
31 bool m_dirty;
32 };
32 };
33
33
34 class LineChartAnimatator: public QVariantAnimation
34 class LineChartAnimatator: public QVariantAnimation
35 {
35 {
36 public:
36 public:
37 LineChartAnimatator(LineChartAnimationItem *item, QObject *parent = 0 );
37 LineChartAnimatator(LineChartAnimationItem *item, QObject *parent = 0 );
38 ~LineChartAnimatator();
38 ~LineChartAnimatator();
39
39
40 protected:
40 protected:
41 QVariant interpolated(const QVariant &start, const QVariant & end, qreal progress ) const;
41 QVariant interpolated(const QVariant &start, const QVariant & end, qreal progress ) const;
42 void updateCurrentValue (const QVariant & value );
42 void updateCurrentValue (const QVariant & value );
43
43
44 private:
44 private:
45 LineChartAnimationItem* m_item;
45 LineChartAnimationItem* m_item;
46 };
46 };
47
47
48 QTCOMMERCIALCHART_END_NAMESPACE
48 QTCOMMERCIALCHART_END_NAMESPACE
49
49
50 #endif
50 #endif
@@ -1,239 +1,245
1 #include "linechartitem_p.h"
1 #include "linechartitem_p.h"
2 #include "qlineseries.h"
2 #include "qlineseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5
5
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 //TODO: optimize : remove points which are not visible
9 //TODO: optimize : remove points which are not visible
10
10
11 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):ChartItem(parent),
11 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):ChartItem(parent),
12 m_minX(0),
12 m_minX(0),
13 m_maxX(0),
13 m_maxX(0),
14 m_minY(0),
14 m_minY(0),
15 m_maxY(0),
15 m_maxY(0),
16 m_series(series),
16 m_series(series),
17 m_items(this)
17 m_items(this)
18 {
18 {
19 //m_items.setZValue(ChartPresenter::LineChartZValue);
19 //m_items.setZValue(ChartPresenter::LineChartZValue);
20 setZValue(ChartPresenter::LineChartZValue);
20 setZValue(ChartPresenter::LineChartZValue);
21
21
22 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
22 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
23 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
23 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
24 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
24 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
25 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
25 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
26
26
27 handleUpdated();
27 handleUpdated();
28 }
28 }
29
29
30 QRectF LineChartItem::boundingRect() const
30 QRectF LineChartItem::boundingRect() const
31 {
31 {
32 return m_rect;
32 return m_rect;
33 }
33 }
34
34
35 QPainterPath LineChartItem::shape() const
35 QPainterPath LineChartItem::shape() const
36 {
36 {
37 return m_path;
37 return m_path;
38 }
38 }
39
39
40 void LineChartItem::createPoints(int count)
40 void LineChartItem::createPoints(int count)
41 {
41 {
42 for (int i = 0; i < count; ++i) {
42 for (int i = 0; i < count; ++i) {
43 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3);
43 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3);
44 m_items.addToGroup(item);
44 m_items.addToGroup(item);
45 }
45 }
46 }
46 }
47
47
48 void LineChartItem::clearPoints(int count)
48 void LineChartItem::clearPoints(int count)
49 {
49 {
50 QList<QGraphicsItem *> items = m_items.childItems();
50 QList<QGraphicsItem *> items = m_items.childItems();
51
51
52 for (int i = 0; i < count; ++i) {
52 for (int i = 0; i < count; ++i) {
53 delete(items.takeLast());
53 delete(items.takeLast());
54 }
54 }
55 }
55 }
56
56
57 QPointF LineChartItem::calculateGeometryPoint(int index) const
57 QPointF LineChartItem::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> LineChartItem::calculateGeometryPoints() const
66 QVector<QPointF> LineChartItem::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 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);
95 }
96
97 void LineChartItem::updatePoints(QVector<QPointF>& points)
98 {
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);
85 }
109 }
86
110
87 void LineChartItem::updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint)
111 void LineChartItem::updatePoint(int index,QPointF& newPoint)
88 {
112 {
89 oldPoints.replace(index,newPoint);
113 m_points.replace(index,newPoint);
90 applyGeometry(oldPoints);
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
97 QList<QGraphicsItem*> items = m_items.childItems();
121 QList<QGraphicsItem*> items = m_items.childItems();
98
122
99 QPainterPath path;
123 QPainterPath path;
100 const QPointF& point = points.at(0);
124 const QPointF& point = points.at(0);
101 path.moveTo(point);
125 path.moveTo(point);
102 QGraphicsItem* item = items.at(0);
126 QGraphicsItem* item = items.at(0);
103 item->setPos(point.x()-1,point.y()-1);
127 item->setPos(point.x()-1,point.y()-1);
104 if(!m_clipRect.contains(point)) {
128 if(!m_clipRect.contains(point)) {
105 item->setVisible(false);
129 item->setVisible(false);
106 }else{
130 }else{
107 item->setVisible(true);
131 item->setVisible(true);
108 }
132 }
109
133
110 for(int i=1 ; i< points.size();i++) {
134 for(int i=1 ; i< points.size();i++) {
111 QGraphicsItem* item = items.at(i);
135 QGraphicsItem* item = items.at(i);
112 const QPointF& point = points.at(i);
136 const QPointF& point = points.at(i);
113 item->setPos(point.x()-1,point.y()-1);
137 item->setPos(point.x()-1,point.y()-1);
114 if(!m_clipRect.contains(point)) {
138 if(!m_clipRect.contains(point)) {
115 item->setVisible(false);
139 item->setVisible(false);
116 }else{
140 }else{
117 item->setVisible(true);
141 item->setVisible(true);
118 }
142 }
119 path.lineTo(point);
143 path.lineTo(point);
120 }
144 }
121
145
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)
128 {
153 {
129 m_pen = pen;
154 m_pen = pen;
130 }
155 }
131
156
132 //handlers
157 //handlers
133
158
134 void LineChartItem::handlePointAdded(int index)
159 void LineChartItem::handlePointAdded(int index)
135 {
160 {
136 Q_ASSERT(index<m_series->count());
161 Q_ASSERT(index<m_series->count());
137 Q_ASSERT(index>=0);
162 Q_ASSERT(index>=0);
138
163
139 QPointF point = calculateGeometryPoint(index);
164 QPointF point = calculateGeometryPoint(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
158 void LineChartItem::handlePointReplaced(int index)
185 void LineChartItem::handlePointReplaced(int index)
159 {
186 {
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
167 void LineChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
194 void LineChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
168 {
195 {
169 m_minX=minX;
196 m_minX=minX;
170 m_maxX=maxX;
197 m_maxX=maxX;
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
192 void LineChartItem::handleGeometryChanged(const QRectF& rect)
206 void LineChartItem::handleGeometryChanged(const QRectF& rect)
193 {
207 {
194 Q_ASSERT(rect.isValid());
208 Q_ASSERT(rect.isValid());
195 m_size=rect.size();
209 m_size=rect.size();
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
216 void LineChartItem::handleUpdated()
218 void LineChartItem::handleUpdated()
217 {
219 {
218 m_items.setVisible(m_series->pointsVisible());
220 m_items.setVisible(m_series->pointsVisible());
219 setPen(m_series->pen());
221 setPen(m_series->pen());
220 update();
222 update();
221 }
223 }
222
224
223 //painter
225 //painter
224
226
225 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
227 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
226 {
228 {
227 Q_UNUSED(widget);
229 Q_UNUSED(widget);
228 Q_UNUSED(option);
230 Q_UNUSED(option);
229 painter->save();
231 painter->save();
230 painter->setPen(m_pen);
232 painter->setPen(m_pen);
231 painter->setClipRect(m_clipRect);
233 painter->setClipRect(m_clipRect);
232 painter->drawPath(m_path);
234 painter->drawPath(m_path);
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
239 QTCOMMERCIALCHART_END_NAMESPACE
245 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,63 +1,69
1 #ifndef LINECHARTITEM_H
1 #ifndef LINECHARTITEM_H
2 #define LINECHARTITEM_H
2 #define LINECHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartPresenter;
10 class ChartPresenter;
11 class QLineSeries;
11 class QLineSeries;
12
12
13 class LineChartItem : public QObject , public ChartItem
13 class LineChartItem : public QObject , public ChartItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 public:
16 public:
17 LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0);
17 LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0);
18 ~ LineChartItem(){};
18 ~ LineChartItem(){};
19
19
20 //from QGraphicsItem
20 //from QGraphicsItem
21 QRectF boundingRect() const;
21 QRectF boundingRect() const;
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 QPainterPath shape() const;
23 QPainterPath shape() const;
24
24
25 void setPen(const QPen& pen);
25 void setPen(const QPen& pen);
26 void setPointsVisible(bool visible);
26 void setPointsVisible(bool visible);
27
27
28 public slots:
28 public slots:
29 void handlePointAdded(int index);
29 void handlePointAdded(int index);
30 void handlePointRemoved(int index);
30 void handlePointRemoved(int index);
31 void handlePointReplaced(int index);
31 void handlePointReplaced(int index);
32 void handleUpdated();
32 void handleUpdated();
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;
47 qreal m_maxX;
51 qreal m_maxX;
48 qreal m_minY;
52 qreal m_minY;
49 qreal m_maxY;
53 qreal m_maxY;
50 QPainterPath m_path;
54 QPainterPath m_path;
51 QRectF m_rect;
55 QRectF m_rect;
52 QLineSeries* m_series;
56 QLineSeries* m_series;
53 QSizeF m_size;
57 QSizeF m_size;
54 QRectF m_clipRect;
58 QRectF m_clipRect;
55 QGraphicsItemGroup m_items;
59 QGraphicsItemGroup m_items;
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
62
68
63 #endif
69 #endif
@@ -1,75 +1,75
1 #include "splinechartitem_p.h"
1 #include "splinechartitem_p.h"
2 #include <QPainter>
2 #include <QPainter>
3
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
5
6 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsObject *parent) :
6 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsObject *parent) :
7 LineChartItem(series, parent)//,m_boundingRect()
7 LineChartItem(series, parent)//,m_boundingRect()
8 {
8 {
9 //
9 //
10 }
10 }
11
11
12
12
13
13
14 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
14 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
15 {
15 {
16 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
16 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
17 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
17 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
18 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
18 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
19 qreal x = (splineSeries->controlPoint(index).x() - m_minX)* deltaX;
19 qreal x = (splineSeries->controlPoint(index).x() - m_minX)* deltaX;
20 qreal y = (splineSeries->controlPoint(index).y() - m_minY)*-deltaY + m_size.height();
20 qreal y = (splineSeries->controlPoint(index).y() - m_minY)*-deltaY + m_size.height();
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
28 QPainterPath splinePath;
28 QPainterPath splinePath;
29 const QPointF& point = points.at(0);
29 const QPointF& point = points.at(0);
30 splinePath.moveTo(point);
30 splinePath.moveTo(point);
31
31
32 // QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
32 // QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
33 for (int i = 0; i < points.size() - 1; i++)
33 for (int i = 0; i < points.size() - 1; i++)
34 {
34 {
35 const QPointF& point = points.at(i + 1);
35 const QPointF& point = points.at(i + 1);
36 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
36 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
37 }
37 }
38
38
39 prepareGeometryChange();
39 prepareGeometryChange();
40 m_path = splinePath;
40 m_path = splinePath;
41 m_rect = splinePath.boundingRect();
41 m_rect = splinePath.boundingRect();
42 }
42 }
43
43
44 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
44 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
45 {
45 {
46 Q_UNUSED(widget);
46 Q_UNUSED(widget);
47 Q_UNUSED(option);
47 Q_UNUSED(option);
48 painter->save();
48 painter->save();
49 painter->setPen(m_pen);
49 painter->setPen(m_pen);
50 painter->setClipRect(m_clipRect);
50 painter->setClipRect(m_clipRect);
51 painter->drawPath(m_path);
51 painter->drawPath(m_path);
52
52
53 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
53 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
54 for (int i = 0; i < m_points.size() - 1; i++)
54 for (int i = 0; i < m_points.size() - 1; i++)
55 {
55 {
56 painter->setPen(Qt::red);
56 painter->setPen(Qt::red);
57 painter->drawEllipse(m_points[i], 2, 2);
57 painter->drawEllipse(m_points[i], 2, 2);
58
58
59 painter->setPen(Qt::blue);
59 painter->setPen(Qt::blue);
60 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
60 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
61 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
61 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
62 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
62 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
63 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
63 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
64 }
64 }
65 if (m_points.count() > 0)
65 if (m_points.count() > 0)
66 {
66 {
67 painter->setPen(Qt::red);
67 painter->setPen(Qt::red);
68 painter->drawEllipse(m_points[m_points.count() - 1], 2, 2);
68 painter->drawEllipse(m_points[m_points.count() - 1], 2, 2);
69 }
69 }
70 painter->restore();
70 painter->restore();
71 }
71 }
72
72
73 #include "moc_splinechartitem_p.cpp"
73 #include "moc_splinechartitem_p.cpp"
74
74
75 QTCOMMERCIALCHART_END_NAMESPACE
75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,28 +1,28
1 #ifndef SPLINECHARTITEM_P_H
1 #ifndef SPLINECHARTITEM_P_H
2 #define SPLINECHARTITEM_P_H
2 #define SPLINECHARTITEM_P_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include <QObject>
5 #include <QObject>
6 #include "qsplineseries.h"
6 #include "qsplineseries.h"
7 #include "linechartitem_p.h"
7 #include "linechartitem_p.h"
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class SplineChartItem : public LineChartItem
11 class SplineChartItem : public LineChartItem
12 {
12 {
13 Q_OBJECT
13 Q_OBJECT
14 public:
14 public:
15 SplineChartItem(QSplineSeries* series, QGraphicsObject *parent = 0);
15 SplineChartItem(QSplineSeries* series, QGraphicsObject *parent = 0);
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
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24 };
24 };
25
25
26 QTCOMMERCIALCHART_END_NAMESPACE
26 QTCOMMERCIALCHART_END_NAMESPACE
27
27
28 #endif // SPLINECHARTITEM_P_H
28 #endif // SPLINECHARTITEM_P_H
General Comments 0
You need to be logged in to leave comments. Login now