@@ -1,269 +1,270 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include "qsplineseries.h" |
|
21 | #include "qsplineseries.h" | |
22 | #include "qsplineseries_p.h" |
|
22 | #include "qsplineseries_p.h" | |
23 | #include "splinechartitem_p.h" |
|
23 | #include "splinechartitem_p.h" | |
24 | #include "chartdataset_p.h" |
|
24 | #include "chartdataset_p.h" | |
25 | #include "charttheme_p.h" |
|
25 | #include "charttheme_p.h" | |
26 | #include "chartanimator_p.h" |
|
26 | #include "chartanimator_p.h" | |
27 | #include <QAbstractItemModel> |
|
27 | #include <QAbstractItemModel> | |
28 |
|
28 | |||
29 | /*! |
|
29 | /*! | |
30 | \class QSplineSeries |
|
30 | \class QSplineSeries | |
31 | \brief Series type used to store data needed to draw a spline. |
|
31 | \brief Series type used to store data needed to draw a spline. | |
32 |
|
32 | |||
33 | QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline |
|
33 | QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline | |
34 | Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn. |
|
34 | Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn. | |
35 |
|
35 | |||
36 | \image examples_splinechart.png |
|
36 | \image examples_splinechart.png | |
37 |
|
37 | |||
38 | Creating basic spline chart is simple: |
|
38 | Creating basic spline chart is simple: | |
39 | \code |
|
39 | \code | |
40 | QSplineSeries* series = new QSplineSeries(); |
|
40 | QSplineSeries* series = new QSplineSeries(); | |
41 | series->append(0, 6); |
|
41 | series->append(0, 6); | |
42 | series->append(2, 4); |
|
42 | series->append(2, 4); | |
43 | ... |
|
43 | ... | |
44 | chart->addSeries(series); |
|
44 | chart->addSeries(series); | |
45 | \endcode |
|
45 | \endcode | |
46 | */ |
|
46 | */ | |
47 |
|
47 | |||
48 | /*! |
|
48 | /*! | |
49 | \fn QSeriesType QSplineSeries::type() const |
|
49 | \fn QSeriesType QSplineSeries::type() const | |
50 | Returns the type of the series |
|
50 | Returns the type of the series | |
51 | */ |
|
51 | */ | |
52 |
|
52 | |||
53 | /*! |
|
53 | /*! | |
54 | \fn QSeriesType QSplineSeries::controlPoint(int index) const |
|
54 | \fn QSeriesType QSplineSeries::controlPoint(int index) const | |
55 | Returns the control point specified by \a index |
|
55 | Returns the control point specified by \a index | |
56 | */ |
|
56 | */ | |
57 |
|
57 | |||
58 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
58 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
59 |
|
59 | |||
60 | /*! |
|
60 | /*! | |
61 | Constructs empty series object which is a child of \a parent. |
|
61 | Constructs empty series object which is a child of \a parent. | |
62 | When series object is added to QChartView or QChart instance then the ownerships is transferred. |
|
62 | When series object is added to QChartView or QChart instance then the ownerships is transferred. | |
63 | */ |
|
63 | */ | |
64 |
|
64 | |||
65 | QSplineSeries::QSplineSeries(QObject *parent) : |
|
65 | QSplineSeries::QSplineSeries(QObject *parent) : | |
66 | QLineSeries(*new QSplineSeriesPrivate(this),parent) |
|
66 | QLineSeries(*new QSplineSeriesPrivate(this),parent) | |
67 | { |
|
67 | { | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 | QSplineSeries::~QSplineSeries() |
|
70 | QSplineSeries::~QSplineSeries() | |
71 | { |
|
71 | { | |
72 | Q_D(QSplineSeries); |
|
72 | Q_D(QSplineSeries); | |
73 | if(d->m_dataset){ |
|
73 | if(d->m_dataset){ | |
74 |
|
|
74 | d->m_dataset->removeSeries(this); | |
75 | } |
|
75 | } | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | QAbstractSeries::SeriesType QSplineSeries::type() const |
|
78 | QAbstractSeries::SeriesType QSplineSeries::type() const | |
79 | { |
|
79 | { | |
80 | return QAbstractSeries::SeriesTypeSpline; |
|
80 | return QAbstractSeries::SeriesTypeSpline; | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | QPointF QSplineSeries::controlPoint(int index) const |
|
|||
84 | { |
|
|||
85 | Q_D(const QSplineSeries); |
|
|||
86 | return d->m_controlPoints[index]; |
|
|||
87 | } |
|
|||
88 |
|
||||
89 | /*! |
|
83 | /*! | |
90 | Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used |
|
84 | Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used | |
91 | as a data source for y coordinate. The \a orientation parameter specifies whether the data |
|
85 | as a data source for y coordinate. The \a orientation parameter specifies whether the data | |
92 | is in columns or in rows. |
|
86 | is in columns or in rows. | |
93 | \sa setModel() |
|
87 | \sa setModel() | |
94 | */ |
|
88 | */ | |
95 | void QSplineSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation) |
|
89 | void QSplineSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation) | |
96 | { |
|
90 | { | |
97 | Q_D(QSplineSeries); |
|
91 | Q_D(QSplineSeries); | |
98 | QXYSeries::setModelMapping(modelX, modelY, orientation); |
|
92 | QXYSeries::setModelMapping(modelX, modelY, orientation); | |
99 | d->updateControlPoints(); |
|
93 | d->updateControlPoints(); | |
100 | } |
|
94 | } | |
101 |
|
95 | |||
102 | void QSplineSeries::setModelMappingRange(int first, int count) |
|
96 | void QSplineSeries::setModelMappingRange(int first, int count) | |
103 | { |
|
97 | { | |
104 | Q_D(QSplineSeries); |
|
98 | Q_D(QSplineSeries); | |
105 | QXYSeries::setModelMappingRange(first, count); |
|
99 | QXYSeries::setModelMappingRange(first, count); | |
106 | d->updateControlPoints(); |
|
100 | d->updateControlPoints(); | |
107 |
|
101 | |||
108 | } |
|
102 | } | |
109 |
|
103 | |||
110 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
104 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
111 |
|
105 | |||
112 | QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q) |
|
106 | QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q) | |
113 | { |
|
107 | { | |
114 | QObject::connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints())); |
|
108 | QObject::connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints())); | |
115 | QObject::connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints())); |
|
109 | QObject::connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints())); | |
116 | QObject::connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints())); |
|
110 | QObject::connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints())); | |
117 | }; |
|
111 | }; | |
118 |
|
112 | |||
119 | /*! |
|
113 | /*! | |
120 | Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points. |
|
114 | Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points. | |
121 | */ |
|
115 | */ | |
122 | void QSplineSeriesPrivate::calculateControlPoints() |
|
116 | void QSplineSeriesPrivate::calculateControlPoints() | |
123 | { |
|
117 | { | |
124 | Q_Q(QSplineSeries); |
|
118 | Q_Q(QSplineSeries); | |
125 |
|
119 | |||
126 | const QList<QPointF>& points = q->points(); |
|
120 | const QList<QPointF>& points = q->points(); | |
127 |
|
121 | |||
128 | int n = points.count() - 1; |
|
122 | int n = points.count() - 1; | |
129 |
|
123 | |||
130 | if (n == 1) |
|
124 | if (n == 1) | |
131 | { |
|
125 | { | |
132 | //for n==1 |
|
126 | //for n==1 | |
133 | m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3); |
|
127 | m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3); | |
134 | m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3); |
|
128 | m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3); | |
135 | m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x()); |
|
129 | m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x()); | |
136 | m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y()); |
|
130 | m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y()); | |
137 | return; |
|
131 | return; | |
138 | } |
|
132 | } | |
139 |
|
133 | |||
140 | // Calculate first Bezier control points |
|
134 | // Calculate first Bezier control points | |
141 | // Right hand side vector |
|
135 | // Right hand side vector | |
142 | // Set of equations for P0 to Pn points. |
|
136 | // Set of equations for P0 to Pn points. | |
143 | // |
|
137 | // | |
144 | // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 | |
|
138 | // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 | | |
145 | // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 | |
|
139 | // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 | | |
146 | // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 | |
|
140 | // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 | | |
147 | // | . . . . . . . . . . . . | | ... | | ... | |
|
141 | // | . . . . . . . . . . . . | | ... | | ... | | |
148 | // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi | |
|
142 | // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi | | |
149 | // | . . . . . . . . . . . . | | ... | | ... | |
|
143 | // | . . . . . . . . . . . . | | ... | | ... | | |
150 | // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) | |
|
144 | // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) | | |
151 | // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn | |
|
145 | // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn | | |
152 | // |
|
146 | // | |
153 | QVector<qreal> vector; |
|
147 | QVector<qreal> vector; | |
154 | vector.resize(n); |
|
148 | vector.resize(n); | |
155 |
|
149 | |||
156 | vector[0] = points[0].x() + 2 * points[1].x(); |
|
150 | vector[0] = points[0].x() + 2 * points[1].x(); | |
157 |
|
151 | |||
158 |
|
152 | |||
159 | for (int i = 1; i < n - 1; ++i){ |
|
153 | for (int i = 1; i < n - 1; ++i){ | |
160 | vector[i] = 4 * points[i].x() + 2 * points[i + 1].x(); |
|
154 | vector[i] = 4 * points[i].x() + 2 * points[i + 1].x(); | |
161 | } |
|
155 | } | |
162 |
|
156 | |||
163 | vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0; |
|
157 | vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0; | |
164 |
|
158 | |||
165 | QVector<qreal> xControl = firstControlPoints(vector); |
|
159 | QVector<qreal> xControl = firstControlPoints(vector); | |
166 |
|
160 | |||
167 | vector[0] = points[0].y() + 2 * points[1].y(); |
|
161 | vector[0] = points[0].y() + 2 * points[1].y(); | |
168 |
|
162 | |||
169 | for (int i = 1; i < n - 1; ++i) { |
|
163 | for (int i = 1; i < n - 1; ++i) { | |
170 | vector[i] = 4 * points[i].y() + 2 * points[i + 1].y(); |
|
164 | vector[i] = 4 * points[i].y() + 2 * points[i + 1].y(); | |
171 | } |
|
165 | } | |
172 |
|
166 | |||
173 | vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0; |
|
167 | vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0; | |
174 |
|
168 | |||
175 | QVector<qreal> yControl = firstControlPoints(vector); |
|
169 | QVector<qreal> yControl = firstControlPoints(vector); | |
176 |
|
170 | |||
177 | for (int i = 0,j =0; i < n; ++i, ++j) { |
|
171 | for (int i = 0,j =0; i < n; ++i, ++j) { | |
178 |
|
172 | |||
179 | m_controlPoints[j].setX(xControl[i]); |
|
173 | m_controlPoints[j].setX(xControl[i]); | |
180 | m_controlPoints[j].setY(yControl[i]); |
|
174 | m_controlPoints[j].setY(yControl[i]); | |
181 |
|
175 | |||
182 | j++; |
|
176 | j++; | |
183 |
|
177 | |||
184 | if (i < n - 1){ |
|
178 | if (i < n - 1){ | |
185 | m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]); |
|
179 | m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]); | |
186 | m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]); |
|
180 | m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]); | |
187 | }else{ |
|
181 | }else{ | |
188 | m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2); |
|
182 | m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2); | |
189 | m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2); |
|
183 | m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2); | |
190 | } |
|
184 | } | |
191 | } |
|
185 | } | |
192 | } |
|
186 | } | |
193 |
|
187 | |||
194 | QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector) |
|
188 | QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector) | |
195 | { |
|
189 | { | |
196 | QVector<qreal> result; |
|
190 | QVector<qreal> result; | |
197 |
|
191 | |||
198 | int count = vector.count(); |
|
192 | int count = vector.count(); | |
199 | result.resize(count); |
|
193 | result.resize(count); | |
200 | result[0] = vector[0] / 2.0; |
|
194 | result[0] = vector[0] / 2.0; | |
201 |
|
195 | |||
202 | QVector<qreal> temp; |
|
196 | QVector<qreal> temp; | |
203 | temp.resize(count); |
|
197 | temp.resize(count); | |
204 | temp[0] = 0; |
|
198 | temp[0] = 0; | |
205 |
|
199 | |||
206 | qreal b = 2.0; |
|
200 | qreal b = 2.0; | |
207 |
|
201 | |||
208 | for (int i = 1; i < count; i++) { |
|
202 | for (int i = 1; i < count; i++) { | |
209 | temp[i] = 1 / b; |
|
203 | temp[i] = 1 / b; | |
210 | b = (i < count - 1 ? 4.0 : 3.5) - temp[i]; |
|
204 | b = (i < count - 1 ? 4.0 : 3.5) - temp[i]; | |
211 | result[i]=(vector[i] - result[i - 1]) / b; |
|
205 | result[i]=(vector[i] - result[i - 1]) / b; | |
212 | } |
|
206 | } | |
213 | for (int i = 1; i < count; i++) |
|
207 | for (int i = 1; i < count; i++) | |
214 | result[count - i - 1] -= temp[count - i] * result[count - i]; |
|
208 | result[count - i - 1] -= temp[count - i] * result[count - i]; | |
215 |
|
209 | |||
216 | return result; |
|
210 | return result; | |
217 | } |
|
211 | } | |
218 |
|
212 | |||
|
213 | QPointF QSplineSeriesPrivate::controlPoint(int index) const | |||
|
214 | { | |||
|
215 | // Q_D(const QSplineSeries); | |||
|
216 | // return d->m_controlPoints[index]; | |||
|
217 | return m_controlPoints[index]; | |||
|
218 | } | |||
|
219 | ||||
219 | /*! |
|
220 | /*! | |
220 | Updates the control points, besed on currently avaiable knots. |
|
221 | Updates the control points, besed on currently avaiable knots. | |
221 | */ |
|
222 | */ | |
222 | void QSplineSeriesPrivate::updateControlPoints() |
|
223 | void QSplineSeriesPrivate::updateControlPoints() | |
223 | { |
|
224 | { | |
224 | Q_Q(QSplineSeries); |
|
225 | Q_Q(QSplineSeries); | |
225 | if (q->count() > 1) { |
|
226 | if (q->count() > 1) { | |
226 | m_controlPoints.resize(2*q->count()-2); |
|
227 | m_controlPoints.resize(2*q->count()-2); | |
227 | calculateControlPoints(); |
|
228 | calculateControlPoints(); | |
228 | } |
|
229 | } | |
229 | } |
|
230 | } | |
230 |
|
231 | |||
231 | void QSplineSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end) |
|
232 | void QSplineSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end) | |
232 | { |
|
233 | { | |
233 | updateControlPoints(); |
|
234 | updateControlPoints(); | |
234 | QXYSeriesPrivate::modelRowsAdded(parent, start, end); |
|
235 | QXYSeriesPrivate::modelRowsAdded(parent, start, end); | |
235 | } |
|
236 | } | |
236 |
|
237 | |||
237 | void QSplineSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) |
|
238 | void QSplineSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) | |
238 | { |
|
239 | { | |
239 | updateControlPoints(); |
|
240 | updateControlPoints(); | |
240 | QXYSeriesPrivate::modelRowsRemoved(parent, start, end); |
|
241 | QXYSeriesPrivate::modelRowsRemoved(parent, start, end); | |
241 | } |
|
242 | } | |
242 |
|
243 | |||
243 | void QSplineSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) |
|
244 | void QSplineSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) | |
244 | { |
|
245 | { | |
245 | updateControlPoints(); |
|
246 | updateControlPoints(); | |
246 | QXYSeriesPrivate::modelColumnsAdded(parent, start, end); |
|
247 | QXYSeriesPrivate::modelColumnsAdded(parent, start, end); | |
247 | } |
|
248 | } | |
248 |
|
249 | |||
249 | void QSplineSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) |
|
250 | void QSplineSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) | |
250 | { |
|
251 | { | |
251 | updateControlPoints(); |
|
252 | updateControlPoints(); | |
252 | QXYSeriesPrivate::modelColumnsRemoved(parent, start, end); |
|
253 | QXYSeriesPrivate::modelColumnsRemoved(parent, start, end); | |
253 | } |
|
254 | } | |
254 |
|
255 | |||
255 | Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter) |
|
256 | Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter) | |
256 | { |
|
257 | { | |
257 | Q_Q(QSplineSeries); |
|
258 | Q_Q(QSplineSeries); | |
258 | SplineChartItem* spline = new SplineChartItem(q,presenter); |
|
259 | SplineChartItem* spline = new SplineChartItem(q,presenter); | |
259 | if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { |
|
260 | if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { | |
260 | presenter->animator()->addAnimation(spline); |
|
261 | presenter->animator()->addAnimation(spline); | |
261 | } |
|
262 | } | |
262 | presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); |
|
263 | presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); | |
263 | return spline; |
|
264 | return spline; | |
264 | } |
|
265 | } | |
265 |
|
266 | |||
266 | #include "moc_qsplineseries.cpp" |
|
267 | #include "moc_qsplineseries.cpp" | |
267 | #include "moc_qsplineseries_p.cpp" |
|
268 | #include "moc_qsplineseries_p.cpp" | |
268 |
|
269 | |||
269 | QTCOMMERCIALCHART_END_NAMESPACE |
|
270 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,56 +1,56 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #ifndef QSPLINESERIES_H |
|
21 | #ifndef QSPLINESERIES_H | |
22 | #define QSPLINESERIES_H |
|
22 | #define QSPLINESERIES_H | |
23 |
|
23 | |||
24 | #include <qchartglobal.h> |
|
24 | #include <qchartglobal.h> | |
25 | #include <qlineseries.h> |
|
25 | #include <qlineseries.h> | |
26 | #include <QList> |
|
26 | #include <QList> | |
27 | #include <QPointF> |
|
27 | #include <QPointF> | |
28 | #include <QtGlobal> |
|
28 | #include <QtGlobal> | |
29 |
|
29 | |||
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
31 |
|
31 | |||
32 | class QSplineSeriesPrivate; |
|
32 | class QSplineSeriesPrivate; | |
33 |
|
33 | |||
34 | class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QLineSeries |
|
34 | class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QLineSeries | |
35 | { |
|
35 | { | |
36 | Q_OBJECT |
|
36 | Q_OBJECT | |
37 | public: |
|
37 | public: | |
38 |
|
38 | |||
39 | explicit QSplineSeries(QObject *parent = 0); |
|
39 | explicit QSplineSeries(QObject *parent = 0); | |
40 | ~QSplineSeries(); |
|
40 | ~QSplineSeries(); | |
41 | QAbstractSeries::SeriesType type() const; |
|
41 | QAbstractSeries::SeriesType type() const; | |
42 |
|
42 | |||
43 | QPointF controlPoint(int index) const; |
|
43 | // QPointF controlPoint(int index) const; | |
44 |
|
44 | |||
45 | void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical); |
|
45 | void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical); | |
46 | void setModelMappingRange(int first, int count = -1); |
|
46 | void setModelMappingRange(int first, int count = -1); | |
47 |
|
47 | |||
48 | private: |
|
48 | private: | |
49 | Q_DECLARE_PRIVATE(QSplineSeries); |
|
49 | Q_DECLARE_PRIVATE(QSplineSeries); | |
50 | Q_DISABLE_COPY(QSplineSeries); |
|
50 | Q_DISABLE_COPY(QSplineSeries); | |
51 | friend class SplineChartItem; |
|
51 | friend class SplineChartItem; | |
52 | }; |
|
52 | }; | |
53 |
|
53 | |||
54 | QTCOMMERCIALCHART_END_NAMESPACE |
|
54 | QTCOMMERCIALCHART_END_NAMESPACE | |
55 |
|
55 | |||
56 | #endif // QSPLINESERIES_H |
|
56 | #endif // QSPLINESERIES_H |
@@ -1,66 +1,68 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | // W A R N I N G |
|
21 | // W A R N I N G | |
22 | // ------------- |
|
22 | // ------------- | |
23 | // |
|
23 | // | |
24 | // This file is not part of the QtCommercial Chart API. It exists purely as an |
|
24 | // This file is not part of the QtCommercial Chart API. It exists purely as an | |
25 | // implementation detail. This header file may change from version to |
|
25 | // implementation detail. This header file may change from version to | |
26 | // version without notice, or even be removed. |
|
26 | // version without notice, or even be removed. | |
27 | // |
|
27 | // | |
28 | // We mean it. |
|
28 | // We mean it. | |
29 |
|
29 | |||
30 | #ifndef QSPLINESERIES_P_H |
|
30 | #ifndef QSPLINESERIES_P_H | |
31 | #define QSPLINESERIES_P_H |
|
31 | #define QSPLINESERIES_P_H | |
32 |
|
32 | |||
33 | #include "qlineseries_p.h" |
|
33 | #include "qlineseries_p.h" | |
34 |
|
34 | |||
35 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
35 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | class QSplineSeriesPrivate: public QLineSeriesPrivate |
|
38 | class QSplineSeriesPrivate: public QLineSeriesPrivate | |
39 | { |
|
39 | { | |
40 | Q_OBJECT |
|
40 | Q_OBJECT | |
41 | public: |
|
41 | public: | |
42 | Chart* createGraphics(ChartPresenter* presenter); |
|
42 | Chart* createGraphics(ChartPresenter* presenter); | |
43 | QSplineSeriesPrivate(QSplineSeries* q); |
|
43 | QSplineSeriesPrivate(QSplineSeries* q); | |
44 |
|
44 | |||
|
45 | QPointF controlPoint(int index) const; | |||
|
46 | ||||
45 | public Q_SLOTS: |
|
47 | public Q_SLOTS: | |
46 | void updateControlPoints(); |
|
48 | void updateControlPoints(); | |
47 |
|
49 | |||
48 | protected Q_SLOTS: |
|
50 | protected Q_SLOTS: | |
49 | void modelRowsAdded(QModelIndex parent, int start, int end); |
|
51 | void modelRowsAdded(QModelIndex parent, int start, int end); | |
50 | void modelRowsRemoved(QModelIndex parent, int start, int end); |
|
52 | void modelRowsRemoved(QModelIndex parent, int start, int end); | |
51 | void modelColumnsAdded(QModelIndex parent, int start, int end); |
|
53 | void modelColumnsAdded(QModelIndex parent, int start, int end); | |
52 | void modelColumnsRemoved(QModelIndex parent, int start, int end); |
|
54 | void modelColumnsRemoved(QModelIndex parent, int start, int end); | |
53 |
|
55 | |||
54 | private: |
|
56 | private: | |
55 | void calculateControlPoints(); |
|
57 | void calculateControlPoints(); | |
56 | QVector<qreal> firstControlPoints(const QVector<qreal>& vector); |
|
58 | QVector<qreal> firstControlPoints(const QVector<qreal>& vector); | |
57 |
|
59 | |||
58 | public: |
|
60 | public: | |
59 | QVector<QPointF> m_controlPoints; |
|
61 | QVector<QPointF> m_controlPoints; | |
60 | private: |
|
62 | private: | |
61 | Q_DECLARE_PUBLIC(QSplineSeries) |
|
63 | Q_DECLARE_PUBLIC(QSplineSeries) | |
62 | }; |
|
64 | }; | |
63 |
|
65 | |||
64 | QTCOMMERCIALCHART_END_NAMESPACE |
|
66 | QTCOMMERCIALCHART_END_NAMESPACE | |
65 |
|
67 | |||
66 | #endif |
|
68 | #endif |
@@ -1,148 +1,149 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include "splinechartitem_p.h" |
|
21 | #include "splinechartitem_p.h" | |
22 | #include "qsplineseries_p.h" |
|
22 | #include "qsplineseries_p.h" | |
23 | #include "chartpresenter_p.h" |
|
23 | #include "chartpresenter_p.h" | |
24 | #include "chartanimator_p.h" |
|
24 | #include "chartanimator_p.h" | |
25 | #include <QPainter> |
|
25 | #include <QPainter> | |
26 | #include <QDebug> |
|
26 | #include <QDebug> | |
27 |
|
27 | |||
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
29 |
|
29 | |||
30 | SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) : |
|
30 | SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) : | |
31 | XYChartItem(series, presenter), |
|
31 | XYChartItem(series, presenter), | |
32 | m_series(series), |
|
32 | m_series(series), | |
33 | m_pointsVisible(false) |
|
33 | m_pointsVisible(false) | |
34 | { |
|
34 | { | |
35 | setZValue(ChartPresenter::LineChartZValue); |
|
35 | setZValue(ChartPresenter::LineChartZValue); | |
36 | QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); |
|
36 | QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); | |
37 | handleUpdated(); |
|
37 | handleUpdated(); | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | QRectF SplineChartItem::boundingRect() const |
|
40 | QRectF SplineChartItem::boundingRect() const | |
41 | { |
|
41 | { | |
42 | return m_rect; |
|
42 | return m_rect; | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | QPainterPath SplineChartItem::shape() const |
|
45 | QPainterPath SplineChartItem::shape() const | |
46 | { |
|
46 | { | |
47 | return m_path; |
|
47 | return m_path; | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | void SplineChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index) |
|
50 | void SplineChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index) | |
51 | { |
|
51 | { | |
52 | QVector<QPointF> controlPoints; |
|
52 | QVector<QPointF> controlPoints; | |
53 |
|
53 | |||
54 | if(newPoints.count()>=2){ |
|
54 | if(newPoints.count()>=2){ | |
55 | controlPoints.resize(newPoints.count()*2-2); |
|
55 | controlPoints.resize(newPoints.count()*2-2); | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | for (int i = 0; i < newPoints.size() - 1; i++) { |
|
58 | for (int i = 0; i < newPoints.size() - 1; i++) { | |
59 | controlPoints[2*i] = calculateGeometryControlPoint(2 * i); |
|
59 | controlPoints[2*i] = calculateGeometryControlPoint(2 * i); | |
60 | controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1); |
|
60 | controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1); | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | if (controlPoints.count()<2) { |
|
63 | if (controlPoints.count()<2) { | |
64 | setLayout(newPoints,controlPoints); |
|
64 | setLayout(newPoints,controlPoints); | |
65 | return; |
|
65 | return; | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | if (animator()) { |
|
68 | if (animator()) { | |
69 | animator()->updateLayout(this,oldPoints,newPoints,m_controlPoints,controlPoints,index); |
|
69 | animator()->updateLayout(this,oldPoints,newPoints,m_controlPoints,controlPoints,index); | |
70 | } else { |
|
70 | } else { | |
71 | setLayout(newPoints,controlPoints); |
|
71 | setLayout(newPoints,controlPoints); | |
72 | } |
|
72 | } | |
73 | } |
|
73 | } | |
74 |
|
74 | |||
75 | QPointF SplineChartItem::calculateGeometryControlPoint(int index) const |
|
75 | QPointF SplineChartItem::calculateGeometryControlPoint(int index) const | |
76 | { |
|
76 | { | |
77 | return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index)); |
|
77 | // return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index)); | |
|
78 | return XYChartItem::calculateGeometryPoint(m_series->d_func()->controlPoint(index)); | |||
78 | } |
|
79 | } | |
79 |
|
80 | |||
80 | void SplineChartItem::setLayout(QVector<QPointF> &points) |
|
81 | void SplineChartItem::setLayout(QVector<QPointF> &points) | |
81 | { |
|
82 | { | |
82 | // Dummy implementation because of a bug in Clang compiler |
|
83 | // Dummy implementation because of a bug in Clang compiler | |
83 | if (points.size() < 2) { |
|
84 | if (points.size() < 2) { | |
84 | m_path = QPainterPath(); |
|
85 | m_path = QPainterPath(); | |
85 | } |
|
86 | } | |
86 | XYChartItem::setLayout(points); |
|
87 | XYChartItem::setLayout(points); | |
87 | } |
|
88 | } | |
88 |
|
89 | |||
89 | void SplineChartItem::setLayout(QVector<QPointF> &points, QVector<QPointF> &controlPoints) |
|
90 | void SplineChartItem::setLayout(QVector<QPointF> &points, QVector<QPointF> &controlPoints) | |
90 | { |
|
91 | { | |
91 | if ((points.size()<2) || (controlPoints.size()<2)) { |
|
92 | if ((points.size()<2) || (controlPoints.size()<2)) { | |
92 | m_path = QPainterPath(); |
|
93 | m_path = QPainterPath(); | |
93 | XYChartItem::setLayout(points); |
|
94 | XYChartItem::setLayout(points); | |
94 | m_controlPoints=controlPoints; |
|
95 | m_controlPoints=controlPoints; | |
95 | return; |
|
96 | return; | |
96 | } |
|
97 | } | |
97 |
|
98 | |||
98 | Q_ASSERT(points.count()*2-2 == controlPoints.count()); |
|
99 | Q_ASSERT(points.count()*2-2 == controlPoints.count()); | |
99 |
|
100 | |||
100 | QPainterPath splinePath(points.at(0)); |
|
101 | QPainterPath splinePath(points.at(0)); | |
101 |
|
102 | |||
102 | for (int i = 0; i < points.size() - 1; i++) { |
|
103 | for (int i = 0; i < points.size() - 1; i++) { | |
103 | const QPointF& point = points.at(i + 1); |
|
104 | const QPointF& point = points.at(i + 1); | |
104 | splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point); |
|
105 | splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point); | |
105 | } |
|
106 | } | |
106 |
|
107 | |||
107 | prepareGeometryChange(); |
|
108 | prepareGeometryChange(); | |
108 | m_path = splinePath; |
|
109 | m_path = splinePath; | |
109 | m_rect = splinePath.boundingRect(); |
|
110 | m_rect = splinePath.boundingRect(); | |
110 | XYChartItem::setLayout(points); |
|
111 | XYChartItem::setLayout(points); | |
111 | m_controlPoints=controlPoints; |
|
112 | m_controlPoints=controlPoints; | |
112 |
|
113 | |||
113 | } |
|
114 | } | |
114 |
|
115 | |||
115 | //handlers |
|
116 | //handlers | |
116 |
|
117 | |||
117 | void SplineChartItem::handleUpdated() |
|
118 | void SplineChartItem::handleUpdated() | |
118 | { |
|
119 | { | |
119 | m_pointsVisible = m_series->pointsVisible(); |
|
120 | m_pointsVisible = m_series->pointsVisible(); | |
120 | m_linePen = m_series->pen(); |
|
121 | m_linePen = m_series->pen(); | |
121 | m_pointPen = m_series->pen(); |
|
122 | m_pointPen = m_series->pen(); | |
122 | m_pointPen.setWidthF(2*m_pointPen.width()); |
|
123 | m_pointPen.setWidthF(2*m_pointPen.width()); | |
123 | update(); |
|
124 | update(); | |
124 | } |
|
125 | } | |
125 |
|
126 | |||
126 | //painter |
|
127 | //painter | |
127 |
|
128 | |||
128 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
129 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
129 | { |
|
130 | { | |
130 | Q_UNUSED(widget) |
|
131 | Q_UNUSED(widget) | |
131 | Q_UNUSED(option) |
|
132 | Q_UNUSED(option) | |
132 | qDebug()<<__FUNCTION__; |
|
133 | qDebug()<<__FUNCTION__; | |
133 | painter->save(); |
|
134 | painter->save(); | |
134 | painter->setClipRect(clipRect()); |
|
135 | painter->setClipRect(clipRect()); | |
135 | painter->setPen(m_linePen); |
|
136 | painter->setPen(m_linePen); | |
136 | painter->drawPath(m_path); |
|
137 | painter->drawPath(m_path); | |
137 | if (m_pointsVisible) { |
|
138 | if (m_pointsVisible) { | |
138 | painter->setPen(m_pointPen); |
|
139 | painter->setPen(m_pointPen); | |
139 | painter->drawPoints(points()); |
|
140 | painter->drawPoints(points()); | |
140 | } |
|
141 | } | |
141 | painter->restore(); |
|
142 | painter->restore(); | |
142 | } |
|
143 | } | |
143 |
|
144 | |||
144 |
|
145 | |||
145 |
|
146 | |||
146 | #include "moc_splinechartitem_p.cpp" |
|
147 | #include "moc_splinechartitem_p.cpp" | |
147 |
|
148 | |||
148 | QTCOMMERCIALCHART_END_NAMESPACE |
|
149 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now