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