##// END OF EJS Templates
Add pointsVisible setting for line series
Michal Klocek -
r185:71bb456d2e5e
parent child
Show More
@@ -1,207 +1,208
1 #include "linechartitem_p.h"
1 #include "linechartitem_p.h"
2 #include "qlinechartseries.h"
2 #include "qlinechartseries.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: optimazie : remove points which are not visible
9 //TODO: optimazie : remove points which are not visible
10
10
11 LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
11 LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* 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),
14 m_dirtyData(false),
15 m_dirtyGeometry(false),
15 m_dirtyGeometry(false),
16 m_dirtyDomain(false)
16 m_dirtyDomain(false)
17 {
17 {
18
18 }
19 }
19
20
20 QRectF LineChartItem::boundingRect() const
21 QRectF LineChartItem::boundingRect() const
21 {
22 {
22 return m_rect;
23 return m_rect;
23 }
24 }
24
25
25 QPainterPath LineChartItem::shape() const
26 QPainterPath LineChartItem::shape() const
26 {
27 {
27 return m_path;
28 return m_path;
28 }
29 }
29
30
30
31
31 void LineChartItem::addPoints(const QVector<QPointF>& points)
32 void LineChartItem::addPoints(const QVector<QPointF>& points)
32 {
33 {
33 m_data = points;
34 m_data = points;
34 for(int i=0; i<m_data.size();i++){
35 for(int i=0; i<m_data.size();i++){
35 const QPointF& point =m_data[i];
36 const QPointF& point =m_data[i];
36 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
37 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
37 item->setPos(point.x()-1,point.y()-1);;
38 item->setPos(point.x()-1,point.y()-1);;
38 if(!m_clipRect.contains(point)) item->setVisible(false);
39 if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false);
39 m_points << item;
40 m_points << item;
40 }
41 }
41 }
42 }
42
43
43 void LineChartItem::addPoint(const QPointF& point)
44 void LineChartItem::addPoint(const QPointF& point)
44 {
45 {
45 m_data << point;
46 m_data << point;
46 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
47 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
47 m_clipRect.contains(point);
48 m_clipRect.contains(point);
48 item->setPos(point.x()-1,point.y()-1);
49 item->setPos(point.x()-1,point.y()-1);
49 if(!m_clipRect.contains(point)) item->setVisible(false);
50 if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false);
50 m_points << item;
51 m_points << item;
51 }
52 }
52
53
53 void LineChartItem::removePoint(const QPointF& point)
54 void LineChartItem::removePoint(const QPointF& point)
54 {
55 {
55 Q_ASSERT(m_data.count() == m_points.count());
56 Q_ASSERT(m_data.count() == m_points.count());
56 int index = m_data.lastIndexOf(point,0);
57 int index = m_data.lastIndexOf(point,0);
57 m_data.remove(index);
58 m_data.remove(index);
58 delete(m_points.takeAt(index));
59 delete(m_points.takeAt(index));
59 }
60 }
60
61
61 void LineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint)
62 void LineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint)
62 {
63 {
63 Q_ASSERT(m_data.count() == m_points.count());
64 Q_ASSERT(m_data.count() == m_points.count());
64 int index = m_data.lastIndexOf(oldPoint,0);
65 int index = m_data.lastIndexOf(oldPoint,0);
65
66
66 if(index > -1){
67 if(index > -1){
67 m_data.replace(index,newPoint);
68 m_data.replace(index,newPoint);
68 QGraphicsItem* item = m_points.at(index);
69 QGraphicsItem* item = m_points.at(index);
69 item->setPos(newPoint.x()-1,newPoint.y()-1);
70 item->setPos(newPoint.x()-1,newPoint.y()-1);
70 }
71 }
71 }
72 }
72
73
73 void LineChartItem::setPoint(int index,const QPointF& point)
74 void LineChartItem::setPoint(int index,const QPointF& point)
74 {
75 {
75 Q_ASSERT(m_data.count() == m_points.count());
76 Q_ASSERT(m_data.count() == m_points.count());
76 Q_ASSERT(index>=0);
77 Q_ASSERT(index>=0);
77
78
78 m_data.replace(index,point);
79 m_data.replace(index,point);
79 QGraphicsItem* item = m_points.at(index);
80 QGraphicsItem* item = m_points.at(index);
80 item->setPos(point.x()-1,point.y()-1);
81 item->setPos(point.x()-1,point.y()-1);
81 }
82 }
82
83
83 void LineChartItem::clear()
84 void LineChartItem::clear()
84 {
85 {
85 qDeleteAll(m_points);
86 qDeleteAll(m_points);
86 m_points.clear();
87 m_points.clear();
87 m_hash.clear();
88 m_hash.clear();
88 m_path = QPainterPath();
89 m_path = QPainterPath();
89 m_rect = QRect();
90 m_rect = QRect();
90 m_data.clear();
91 m_data.clear();
91 }
92 }
92
93
93 void LineChartItem::clearView()
94 void LineChartItem::clearView()
94 {
95 {
95 qDeleteAll(m_points);
96 qDeleteAll(m_points);
96 m_points.clear();
97 m_points.clear();
97 m_path = QPainterPath();
98 m_path = QPainterPath();
98 m_rect = QRect();
99 m_rect = QRect();
99 m_data.clear();
100 m_data.clear();
100 }
101 }
101
102
102 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 {
104 {
104 Q_UNUSED(widget);
105 Q_UNUSED(widget);
105 Q_UNUSED(option);
106 Q_UNUSED(option);
106 painter->setPen(m_pen);
107 painter->setPen(m_pen);
107 painter->setClipRect(m_clipRect);
108 painter->setClipRect(m_clipRect);
108 painter->drawPath(m_path);
109 painter->drawPath(m_path);
109 }
110 }
110
111
111 void LineChartItem::calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const
112 void LineChartItem::calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const
112 {
113 {
113 const qreal deltaX = size.width()/domain.spanX();
114 const qreal deltaX = size.width()/domain.spanX();
114 const qreal deltaY = size.height()/domain.spanY();
115 const qreal deltaY = size.height()/domain.spanY();
115 qreal x = (series->x(index) - domain.m_minX)* deltaX;
116 qreal x = (series->x(index) - domain.m_minX)* deltaX;
116 qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height();
117 qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height();
117 point.setX(x);
118 point.setX(x);
118 point.setY(y);
119 point.setY(y);
119 }
120 }
120
121
121
122
122 void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const
123 void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const
123 {
124 {
124 const qreal deltaX = size.width()/domain.spanX();
125 const qreal deltaX = size.width()/domain.spanX();
125 const qreal deltaY = size.height()/domain.spanY();
126 const qreal deltaY = size.height()/domain.spanY();
126
127
127 for (int i = 0; i < series->count(); ++i) {
128 for (int i = 0; i < series->count(); ++i) {
128 qreal x = (series->x(i) - domain.m_minX)* deltaX;
129 qreal x = (series->x(i) - domain.m_minX)* deltaX;
129 qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height();
130 qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height();
130 hash[i] = points.size();
131 hash[i] = points.size();
131 points << QPointF(x,y);
132 points << QPointF(x,y);
132 }
133 }
133 }
134 }
134
135
135 void LineChartItem::updateDomain()
136 void LineChartItem::updateDomain()
136 {
137 {
137 clear();
138 clear();
138 prepareGeometryChange();
139 prepareGeometryChange();
139 calculatePoints(m_data,m_hash,m_series,m_size, m_domain);
140 calculatePoints(m_data,m_hash,m_series,m_size, m_domain);
140 addPoints(m_data);
141 addPoints(m_data);
141 }
142 }
142
143
143 void LineChartItem::updateData()
144 void LineChartItem::updateData()
144 {
145 {
145 //for now the same
146 //for now the same
146 updateDomain();
147 updateDomain();
147 }
148 }
148
149
149 void LineChartItem::updateGeometry()
150 void LineChartItem::updateGeometry()
150 {
151 {
151
152
152 if(m_data.size()==0) return;
153 if(m_data.size()==0) return;
153
154
154 prepareGeometryChange();
155 prepareGeometryChange();
155 QPainterPath path;
156 QPainterPath path;
156 const QPointF& point = m_data.at(0);
157 const QPointF& point = m_data.at(0);
157 path.moveTo(point);
158 path.moveTo(point);
158
159
159 foreach( const QPointF& point , m_data) {
160 foreach( const QPointF& point , m_data) {
160 path.lineTo(point);
161 path.lineTo(point);
161 }
162 }
162
163
163 m_path = path;
164 m_path = path;
164 m_rect = path.boundingRect();
165 m_rect = path.boundingRect();
165 }
166 }
166
167
167 void LineChartItem::setPen(const QPen& pen)
168 void LineChartItem::setPen(const QPen& pen)
168 {
169 {
169 m_pen = pen;
170 m_pen = pen;
170 }
171 }
171
172
172 //handlers
173 //handlers
173
174
174 void LineChartItem::handleModelChanged(int index)
175 void LineChartItem::handleModelChanged(int index)
175 {
176 {
176 Q_ASSERT(index<m_series->count());
177 Q_ASSERT(index<m_series->count());
177 if(m_hash.contains(index)){
178 if(m_hash.contains(index)){
178 int i = m_hash.value(index);
179 int i = m_hash.value(index);
179 QPointF point;
180 QPointF point;
180 calculatePoint(point,index,m_series,m_size,m_domain);
181 calculatePoint(point,index,m_series,m_size,m_domain);
181 setPoint(i,point);
182 setPoint(i,point);
182 }
183 }
183 update();
184 update();
184 }
185 }
185
186
186 void LineChartItem::handleDomainChanged(const Domain& domain)
187 void LineChartItem::handleDomainChanged(const Domain& domain)
187 {
188 {
188 m_domain = domain;
189 m_domain = domain;
189 updateDomain();
190 updateDomain();
190 update();
191 update();
191 }
192 }
192
193
193 void LineChartItem::handleGeometryChanged(const QRectF& rect)
194 void LineChartItem::handleGeometryChanged(const QRectF& rect)
194 {
195 {
195 Q_ASSERT(rect.isValid());
196 Q_ASSERT(rect.isValid());
196 m_size=rect.size();
197 m_size=rect.size();
197 m_clipRect=rect.translated(-rect.topLeft());
198 m_clipRect=rect.translated(-rect.topLeft());
198 updateDomain();
199 updateDomain();
199 updateGeometry();
200 updateGeometry();
200 setPos(rect.topLeft());
201 setPos(rect.topLeft());
201 update();
202 update();
202 }
203 }
203
204
204
205
205 #include "moc_linechartitem_p.cpp"
206 #include "moc_linechartitem_p.cpp"
206
207
207 QTCOMMERCIALCHART_END_NAMESPACE
208 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,73 +1,74
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 QLineChartSeries;
11 class QLineChartSeries;
12 class LineChartAnimationItem;
12 class LineChartAnimationItem;
13
13
14 class LineChartItem : public QObject , public ChartItem
14 class LineChartItem : public QObject , public ChartItem
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent = 0);
18 LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent = 0);
19 ~ LineChartItem(){};
19 ~ LineChartItem(){};
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 const Domain& domain() const { return m_domain;}
27 const Domain& domain() const { return m_domain;}
28
28
29 virtual void addPoint(const QPointF& );
29 virtual void addPoint(const QPointF& );
30 virtual void addPoints(const QVector<QPointF>& points);
30 virtual void addPoints(const QVector<QPointF>& points);
31 virtual void removePoint(const QPointF& point);
31 virtual void removePoint(const QPointF& point);
32 virtual void setPoint(const QPointF& oldPoint, const QPointF& newPoint);
32 virtual void setPoint(const QPointF& oldPoint, const QPointF& newPoint);
33 virtual void setPoint(int index,const QPointF& point);
33 virtual void setPoint(int index,const QPointF& point);
34 void setPointsVisible(bool visible);
34 void clear();
35 void clear();
35 void clearView();
36 void clearView();
36 int count() const { return m_data.size();}
37 int count() const { return m_data.size();}
37
38
38 const QVector<QPointF>& points(){ return m_data;}
39 const QVector<QPointF>& points(){ return m_data;}
39
40
40 protected:
41 protected:
41 virtual void updateGeometry();
42 virtual void updateGeometry();
42 virtual void updateData();
43 virtual void updateData();
43 virtual void updateDomain();
44 virtual void updateDomain();
44 //refactor
45 //refactor
45 void calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const;
46 void calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const;
46 void calculatePoints(QVector<QPointF>& points,QHash<int,int>& hash,const QLineChartSeries* series, const QSizeF& size, const Domain& domain) const;
47 void calculatePoints(QVector<QPointF>& points,QHash<int,int>& hash,const QLineChartSeries* series, const QSizeF& size, const Domain& domain) const;
47
48
48 protected slots:
49 protected slots:
49 void handleModelChanged(int index);
50 void handleModelChanged(int index);
50 void handleDomainChanged(const Domain& domain);
51 void handleDomainChanged(const Domain& domain);
51 void handleGeometryChanged(const QRectF& size);
52 void handleGeometryChanged(const QRectF& size);
52
53
53 private:
54 private:
54 ChartPresenter* m_presenter;
55 ChartPresenter* m_presenter;
55 QPainterPath m_path;
56 QPainterPath m_path;
56 QSizeF m_size;
57 QSizeF m_size;
57 QRectF m_rect;
58 QRectF m_rect;
58 QRectF m_clipRect;
59 QRectF m_clipRect;
59 Domain m_domain;
60 Domain m_domain;
60 QList<QGraphicsItem*> m_points;
61 QList<QGraphicsItem*> m_points;
61 QVector<QPointF> m_data;
62 QVector<QPointF> m_data;
62 QHash<int,int> m_hash;
63 QHash<int,int> m_hash;
63 QLineChartSeries* m_series;
64 QLineChartSeries* m_series;
64 QPen m_pen;
65 QPen m_pen;
65 bool m_dirtyData;
66 bool m_dirtyData;
66 bool m_dirtyGeometry;
67 bool m_dirtyGeometry;
67 bool m_dirtyDomain;
68 bool m_dirtyDomain;
68
69
69 };
70 };
70
71
71 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
72
73
73 #endif
74 #endif
@@ -1,70 +1,71
1 #include "qlinechartseries.h"
1 #include "qlinechartseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 QLineChartSeries::QLineChartSeries(QObject* parent):QChartSeries(parent)
5 QLineChartSeries::QLineChartSeries(QObject* parent):QChartSeries(parent),
6 m_pointsVisible(false)
6 {
7 {
7 }
8 }
8
9
9 QLineChartSeries::~QLineChartSeries()
10 QLineChartSeries::~QLineChartSeries()
10 {
11 {
11 }
12 }
12
13
13 int QLineChartSeries::add(qreal x,qreal y)
14 int QLineChartSeries::add(qreal x,qreal y)
14 {
15 {
15 m_x<<x;
16 m_x<<x;
16 m_y<<y;
17 m_y<<y;
17 return m_x.size()-1;
18 return m_x.size()-1;
18 }
19 }
19
20
20 void QLineChartSeries::set(int index,qreal x,qreal y)
21 void QLineChartSeries::set(int index,qreal x,qreal y)
21 {
22 {
22 m_x[index]=x;
23 m_x[index]=x;
23 m_y[index]=y;
24 m_y[index]=y;
24 emit changed(index);
25 emit changed(index);
25 }
26 }
26
27
27 void QLineChartSeries::clear()
28 void QLineChartSeries::clear()
28 {
29 {
29 m_x.clear();
30 m_x.clear();
30 m_y.clear();
31 m_y.clear();
31 }
32 }
32
33
33 qreal QLineChartSeries::x(int pos) const
34 qreal QLineChartSeries::x(int pos) const
34 {
35 {
35 return m_x.at(pos);
36 return m_x.at(pos);
36 }
37 }
37
38
38 qreal QLineChartSeries::y(int pos) const
39 qreal QLineChartSeries::y(int pos) const
39 {
40 {
40 return m_y.at(pos);
41 return m_y.at(pos);
41 }
42 }
42
43
43 int QLineChartSeries::count() const
44 int QLineChartSeries::count() const
44 {
45 {
45 Q_ASSERT(m_x.size() == m_y.size());
46 Q_ASSERT(m_x.size() == m_y.size());
46
47
47 return m_x.size();
48 return m_x.size();
48
49
49 }
50 }
50
51
51 void QLineChartSeries::setPen(const QPen& pen)
52 void QLineChartSeries::setPen(const QPen& pen)
52 {
53 {
53 m_pen=pen;
54 m_pen=pen;
54 }
55 }
55
56
56 QDebug operator<< (QDebug debug, const QLineChartSeries series)
57 QDebug operator<< (QDebug debug, const QLineChartSeries series)
57 {
58 {
58 Q_ASSERT(series.m_x.size() == series.m_y.size());
59 Q_ASSERT(series.m_x.size() == series.m_y.size());
59
60
60 int size = series.m_x.size();
61 int size = series.m_x.size();
61
62
62 for (int i=0;i<size;i++) {
63 for (int i=0;i<size;i++) {
63 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
64 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
64 }
65 }
65 return debug.space();
66 return debug.space();
66 }
67 }
67
68
68 #include "moc_qlinechartseries.cpp"
69 #include "moc_qlinechartseries.cpp"
69
70
70 QTCOMMERCIALCHART_END_NAMESPACE
71 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,48
1 #ifndef QLINECHARTSERIES_H_
1 #ifndef QLINECHARTSERIES_H_
2 #define QLINECHARTSERIES_H_
2 #define QLINECHARTSERIES_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchartseries.h"
5 #include "qchartseries.h"
6 #include <QDebug>
6 #include <QDebug>
7 #include <QPen>
7 #include <QPen>
8 #include <QBrush>
8 #include <QBrush>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QLineChartSeries : public QChartSeries
12 class QTCOMMERCIALCHART_EXPORT QLineChartSeries : public QChartSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 QLineChartSeries(QObject* parent=0);
16 QLineChartSeries(QObject* parent=0);
17 virtual ~QLineChartSeries();
17 virtual ~QLineChartSeries();
18
18
19 public: // from QChartSeries
19 public: // from QChartSeries
20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;}
20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;}
21
22 public:
23 int add(qreal x, qreal y);
21 int add(qreal x, qreal y);
24 void set(int index,qreal x,qreal y);
22 void set(int index,qreal x,qreal y);
25 void clear();
23 void clear();
26
24
27 void setPen(const QPen& pen);
25 void setPen(const QPen& pen);
28 const QPen& pen() const { return m_pen;}
26 const QPen& pen() const { return m_pen;}
29
27
28 void pointsVisible(bool visible);
29 bool isPointsVisible() const {return m_pointsVisible;}
30
30 int count() const;
31 int count() const;
31 qreal x(int pos) const;
32 qreal x(int pos) const;
32 qreal y(int pos) const;
33 qreal y(int pos) const;
33 friend QDebug operator<< (QDebug d, const QLineChartSeries series);
34 friend QDebug operator<< (QDebug d, const QLineChartSeries series);
34
35
35 signals:
36 signals:
36 void changed(int index);
37 void changed(int index);
37
38
38 private:
39 private:
39 QVector<qreal> m_x;
40 QVector<qreal> m_x;
40 QVector<qreal> m_y;
41 QVector<qreal> m_y;
41 QPen m_pen;
42 QPen m_pen;
43 bool m_pointsVisible;
42 };
44 };
43
45
44 QTCOMMERCIALCHART_END_NAMESPACE
46 QTCOMMERCIALCHART_END_NAMESPACE
45
47
46 #endif
48 #endif
General Comments 0
You need to be logged in to leave comments. Login now