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