##// END OF EJS Templates
Fixes to model support
Marek Rosa -
r1060:9efd7b220dc4
parent child
Show More
@@ -1,863 +1,870
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 "qpieseries.h"
21 #include "qpieseries.h"
22 #include "qpieseries_p.h"
22 #include "qpieseries_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "pieslicedata_p.h"
24 #include "pieslicedata_p.h"
25 #include "chartdataset_p.h"
25 #include "chartdataset_p.h"
26 #include "charttheme_p.h"
26 #include "charttheme_p.h"
27 #include "chartanimator_p.h"
27 #include "chartanimator_p.h"
28 #include "legendmarker_p.h"
28 #include "legendmarker_p.h"
29 #include <QAbstractItemModel>
29 #include <QAbstractItemModel>
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QPieSeries
34 \class QPieSeries
35 \brief Pie series API for QtCommercial Charts
35 \brief Pie series API for QtCommercial Charts
36
36
37 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
37 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
38 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
38 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
39 The actual slice size is determined by that relative value.
39 The actual slice size is determined by that relative value.
40
40
41 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
41 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
42 These relate to the actual chart rectangle.
42 These relate to the actual chart rectangle.
43
43
44 By default the pie is defined as a full pie but it can also be a partial pie.
44 By default the pie is defined as a full pie but it can also be a partial pie.
45 This can be done by setting a starting angle and angle span to the series.
45 This can be done by setting a starting angle and angle span to the series.
46 Full pie is 360 degrees where 0 is at 12 a'clock.
46 Full pie is 360 degrees where 0 is at 12 a'clock.
47
47
48 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
48 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
49 \image examples_piechart.png
49 \image examples_piechart.png
50 */
50 */
51
51
52 /*!
52 /*!
53 \property QPieSeries::horizontalPosition
53 \property QPieSeries::horizontalPosition
54 \brief Defines the horizontal position of the pie.
54 \brief Defines the horizontal position of the pie.
55
55
56 The value is a relative value to the chart rectangle where:
56 The value is a relative value to the chart rectangle where:
57
57
58 \list
58 \list
59 \o 0.0 is the absolute left.
59 \o 0.0 is the absolute left.
60 \o 1.0 is the absolute right.
60 \o 1.0 is the absolute right.
61 \endlist
61 \endlist
62
62
63 Default value is 0.5 (center).
63 Default value is 0.5 (center).
64 */
64 */
65
65
66 /*!
66 /*!
67 \property QPieSeries::verticalPosition
67 \property QPieSeries::verticalPosition
68 \brief Defines the vertical position of the pie.
68 \brief Defines the vertical position of the pie.
69
69
70 The value is a relative value to the chart rectangle where:
70 The value is a relative value to the chart rectangle where:
71
71
72 \list
72 \list
73 \o 0.0 is the absolute top.
73 \o 0.0 is the absolute top.
74 \o 1.0 is the absolute bottom.
74 \o 1.0 is the absolute bottom.
75 \endlist
75 \endlist
76
76
77 Default value is 0.5 (center).
77 Default value is 0.5 (center).
78 */
78 */
79
79
80 /*!
80 /*!
81 \property QPieSeries::size
81 \property QPieSeries::size
82 \brief Defines the pie size.
82 \brief Defines the pie size.
83
83
84 The value is a relative value to the chart rectangle where:
84 The value is a relative value to the chart rectangle where:
85
85
86 \list
86 \list
87 \o 0.0 is the minimum size (pie not drawn).
87 \o 0.0 is the minimum size (pie not drawn).
88 \o 1.0 is the maximum size that can fit the chart.
88 \o 1.0 is the maximum size that can fit the chart.
89 \endlist
89 \endlist
90
90
91 Default value is 0.7.
91 Default value is 0.7.
92 */
92 */
93
93
94 /*!
94 /*!
95 \property QPieSeries::startAngle
95 \property QPieSeries::startAngle
96 \brief Defines the starting angle of the pie.
96 \brief Defines the starting angle of the pie.
97
97
98 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
98 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
99
99
100 Default is value is 0.
100 Default is value is 0.
101 */
101 */
102
102
103 /*!
103 /*!
104 \property QPieSeries::endAngle
104 \property QPieSeries::endAngle
105 \brief Defines the ending angle of the pie.
105 \brief Defines the ending angle of the pie.
106
106
107 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
107 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
108
108
109 Default is value is 360.
109 Default is value is 360.
110 */
110 */
111
111
112
112
113 /*!
113 /*!
114 Constructs a series object which is a child of \a parent.
114 Constructs a series object which is a child of \a parent.
115 */
115 */
116 QPieSeries::QPieSeries(QObject *parent) :
116 QPieSeries::QPieSeries(QObject *parent) :
117 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
117 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
118 {
118 {
119
119
120 }
120 }
121
121
122 /*!
122 /*!
123 Destroys the series and its slices.
123 Destroys the series and its slices.
124 */
124 */
125 QPieSeries::~QPieSeries()
125 QPieSeries::~QPieSeries()
126 {
126 {
127 // NOTE: d_prt destroyed by QObject
127 // NOTE: d_prt destroyed by QObject
128 }
128 }
129
129
130 /*!
130 /*!
131 Returns QChartSeries::SeriesTypePie.
131 Returns QChartSeries::SeriesTypePie.
132 */
132 */
133 QAbstractSeries::QSeriesType QPieSeries::type() const
133 QAbstractSeries::QSeriesType QPieSeries::type() const
134 {
134 {
135 return QAbstractSeries::SeriesTypePie;
135 return QAbstractSeries::SeriesTypePie;
136 }
136 }
137
137
138 /*!
138 /*!
139 Appends an array of \a slices to the series.
139 Appends an array of \a slices to the series.
140 Slice ownership is passed to the series.
140 Slice ownership is passed to the series.
141 */
141 */
142 void QPieSeries::append(QList<QPieSlice*> slices)
142 void QPieSeries::append(QList<QPieSlice*> slices)
143 {
143 {
144 Q_D(QPieSeries);
144 Q_D(QPieSeries);
145
145
146 foreach (QPieSlice* s, slices) {
146 foreach (QPieSlice* s, slices) {
147 s->setParent(this);
147 s->setParent(this);
148 d->m_slices << s;
148 d->m_slices << s;
149 }
149 }
150
150
151 d->updateDerivativeData();
151 d->updateDerivativeData();
152
152
153 foreach (QPieSlice* s, slices) {
153 foreach (QPieSlice* s, slices) {
154 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
154 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
155 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
155 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
156 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
156 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
157 }
157 }
158
158
159 emit d->added(slices);
159 emit d->added(slices);
160 }
160 }
161
161
162 /*!
162 /*!
163 Appends a single \a slice to the series.
163 Appends a single \a slice to the series.
164 Slice ownership is passed to the series.
164 Slice ownership is passed to the series.
165 */
165 */
166 void QPieSeries::append(QPieSlice* slice)
166 void QPieSeries::append(QPieSlice* slice)
167 {
167 {
168 append(QList<QPieSlice*>() << slice);
168 append(QList<QPieSlice*>() << slice);
169 }
169 }
170
170
171 /*!
171 /*!
172 Appends a single \a slice to the series and returns a reference to the series.
172 Appends a single \a slice to the series and returns a reference to the series.
173 Slice ownership is passed to the series.
173 Slice ownership is passed to the series.
174 */
174 */
175 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
175 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
176 {
176 {
177 append(slice);
177 append(slice);
178 return *this;
178 return *this;
179 }
179 }
180
180
181
181
182 /*!
182 /*!
183 Appends a single slice to the series with give \a value and \a name.
183 Appends a single slice to the series with give \a value and \a name.
184 Slice ownership is passed to the series.
184 Slice ownership is passed to the series.
185 */
185 */
186 QPieSlice* QPieSeries::append(qreal value, QString name)
186 QPieSlice* QPieSeries::append(qreal value, QString name)
187 {
187 {
188 QPieSlice* slice = new QPieSlice(value, name);
188 QPieSlice* slice = new QPieSlice(value, name);
189 append(slice);
189 append(slice);
190 return slice;
190 return slice;
191 }
191 }
192
192
193 /*!
193 /*!
194 Inserts a single \a slice to the series before the slice at \a index position.
194 Inserts a single \a slice to the series before the slice at \a index position.
195 Slice ownership is passed to the series.
195 Slice ownership is passed to the series.
196 */
196 */
197 void QPieSeries::insert(int index, QPieSlice* slice)
197 void QPieSeries::insert(int index, QPieSlice* slice)
198 {
198 {
199 Q_D(QPieSeries);
199 Q_D(QPieSeries);
200 Q_ASSERT(index <= d->m_slices.count());
200 Q_ASSERT(index <= d->m_slices.count());
201 slice->setParent(this);
201 slice->setParent(this);
202 d->m_slices.insert(index, slice);
202 d->m_slices.insert(index, slice);
203
203
204 d->updateDerivativeData();
204 d->updateDerivativeData();
205
205
206 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
206 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
207 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
207 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
208 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
208 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
209
209
210 emit d->added(QList<QPieSlice*>() << slice);
210 emit d->added(QList<QPieSlice*>() << slice);
211 }
211 }
212
212
213 /*!
213 /*!
214 Removes a single \a slice from the series and deletes the slice.
214 Removes a single \a slice from the series and deletes the slice.
215
215
216 Do not reference the pointer after this call.
216 Do not reference the pointer after this call.
217 */
217 */
218 void QPieSeries::remove(QPieSlice* slice)
218 void QPieSeries::remove(QPieSlice* slice)
219 {
219 {
220 Q_D(QPieSeries);
220 Q_D(QPieSeries);
221 if (!d->m_slices.removeOne(slice)) {
221 if (!d->m_slices.removeOne(slice)) {
222 Q_ASSERT(0); // TODO: how should this be reported?
222 Q_ASSERT(0); // TODO: how should this be reported?
223 return;
223 return;
224 }
224 }
225
225
226 d->updateDerivativeData();
226 d->updateDerivativeData();
227
227
228 emit d->removed(QList<QPieSlice*>() << slice);
228 emit d->removed(QList<QPieSlice*>() << slice);
229
229
230 delete slice;
230 delete slice;
231 slice = 0;
231 slice = 0;
232 }
232 }
233
233
234 /*!
234 /*!
235 Clears all slices from the series.
235 Clears all slices from the series.
236 */
236 */
237 void QPieSeries::clear()
237 void QPieSeries::clear()
238 {
238 {
239 Q_D(QPieSeries);
239 Q_D(QPieSeries);
240 if (d->m_slices.count() == 0)
240 if (d->m_slices.count() == 0)
241 return;
241 return;
242
242
243 QList<QPieSlice*> slices = d->m_slices;
243 QList<QPieSlice*> slices = d->m_slices;
244 foreach (QPieSlice* s, d->m_slices) {
244 foreach (QPieSlice* s, d->m_slices) {
245 d->m_slices.removeOne(s);
245 d->m_slices.removeOne(s);
246 delete s;
246 delete s;
247 }
247 }
248
248
249 d->updateDerivativeData();
249 d->updateDerivativeData();
250
250
251 emit d->removed(slices);
251 emit d->removed(slices);
252 }
252 }
253
253
254 /*!
254 /*!
255 returns the number of the slices in this series.
255 returns the number of the slices in this series.
256 */
256 */
257 int QPieSeries::count() const
257 int QPieSeries::count() const
258 {
258 {
259 Q_D(const QPieSeries);
259 Q_D(const QPieSeries);
260 return d->m_slices.count();
260 return d->m_slices.count();
261 }
261 }
262
262
263 /*!
263 /*!
264 Returns true is the series is empty.
264 Returns true is the series is empty.
265 */
265 */
266 bool QPieSeries::isEmpty() const
266 bool QPieSeries::isEmpty() const
267 {
267 {
268 Q_D(const QPieSeries);
268 Q_D(const QPieSeries);
269 return d->m_slices.isEmpty();
269 return d->m_slices.isEmpty();
270 }
270 }
271
271
272 /*!
272 /*!
273 Returns a list of slices that belong to this series.
273 Returns a list of slices that belong to this series.
274 */
274 */
275 QList<QPieSlice*> QPieSeries::slices() const
275 QList<QPieSlice*> QPieSeries::slices() const
276 {
276 {
277 Q_D(const QPieSeries);
277 Q_D(const QPieSeries);
278 return d->m_slices;
278 return d->m_slices;
279 }
279 }
280
280
281 void QPieSeries::setHorizontalPosition(qreal relativePosition)
281 void QPieSeries::setHorizontalPosition(qreal relativePosition)
282 {
282 {
283 Q_D(QPieSeries);
283 Q_D(QPieSeries);
284 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
284 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
285 emit d->piePositionChanged();
285 emit d->piePositionChanged();
286 }
286 }
287
287
288 void QPieSeries::setVerticalPosition(qreal relativePosition)
288 void QPieSeries::setVerticalPosition(qreal relativePosition)
289 {
289 {
290 Q_D(QPieSeries);
290 Q_D(QPieSeries);
291 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
291 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
292 emit d->piePositionChanged();
292 emit d->piePositionChanged();
293 }
293 }
294
294
295 qreal QPieSeries::horizontalPosition() const
295 qreal QPieSeries::horizontalPosition() const
296 {
296 {
297 Q_D(const QPieSeries);
297 Q_D(const QPieSeries);
298 return d->m_pieRelativeHorPos;
298 return d->m_pieRelativeHorPos;
299 }
299 }
300
300
301 qreal QPieSeries::verticalPosition() const
301 qreal QPieSeries::verticalPosition() const
302 {
302 {
303 Q_D(const QPieSeries);
303 Q_D(const QPieSeries);
304 return d->m_pieRelativeVerPos;
304 return d->m_pieRelativeVerPos;
305 }
305 }
306
306
307 void QPieSeries::setPieSize(qreal relativeSize)
307 void QPieSeries::setPieSize(qreal relativeSize)
308 {
308 {
309 Q_D(QPieSeries);
309 Q_D(QPieSeries);
310 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
310 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
311 emit d->pieSizeChanged();
311 emit d->pieSizeChanged();
312 }
312 }
313
313
314 qreal QPieSeries::pieSize() const
314 qreal QPieSeries::pieSize() const
315 {
315 {
316 Q_D(const QPieSeries);
316 Q_D(const QPieSeries);
317 return d->m_pieRelativeSize;
317 return d->m_pieRelativeSize;
318 }
318 }
319
319
320
320
321 void QPieSeries::setPieStartAngle(qreal angle)
321 void QPieSeries::setPieStartAngle(qreal angle)
322 {
322 {
323 Q_D(QPieSeries);
323 Q_D(QPieSeries);
324 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
324 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
325 d->updateDerivativeData();
325 d->updateDerivativeData();
326 }
326 }
327
327
328 qreal QPieSeries::pieStartAngle() const
328 qreal QPieSeries::pieStartAngle() const
329 {
329 {
330 Q_D(const QPieSeries);
330 Q_D(const QPieSeries);
331 return d->m_pieStartAngle;
331 return d->m_pieStartAngle;
332 }
332 }
333
333
334 /*!
334 /*!
335 Sets the end angle of the pie.
335 Sets the end angle of the pie.
336
336
337 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
337 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
338
338
339 \a angle must be greater than start angle.
339 \a angle must be greater than start angle.
340
340
341 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
341 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
342 */
342 */
343 void QPieSeries::setPieEndAngle(qreal angle)
343 void QPieSeries::setPieEndAngle(qreal angle)
344 {
344 {
345 Q_D(QPieSeries);
345 Q_D(QPieSeries);
346
346
347 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
347 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
348 d->updateDerivativeData();
348 d->updateDerivativeData();
349 }
349 }
350
350
351 /*!
351 /*!
352 Returns the end angle of the pie.
352 Returns the end angle of the pie.
353
353
354 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
354 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
355
355
356 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
356 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
357 */
357 */
358 qreal QPieSeries::pieEndAngle() const
358 qreal QPieSeries::pieEndAngle() const
359 {
359 {
360 Q_D(const QPieSeries);
360 Q_D(const QPieSeries);
361 return d->m_pieEndAngle;
361 return d->m_pieEndAngle;
362 }
362 }
363
363
364 /*!
364 /*!
365 Sets the all the slice labels \a visible or invisible.
365 Sets the all the slice labels \a visible or invisible.
366
366
367 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
367 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
368 */
368 */
369 void QPieSeries::setLabelsVisible(bool visible)
369 void QPieSeries::setLabelsVisible(bool visible)
370 {
370 {
371 Q_D(QPieSeries);
371 Q_D(QPieSeries);
372 foreach (QPieSlice* s, d->m_slices)
372 foreach (QPieSlice* s, d->m_slices)
373 s->setLabelVisible(visible);
373 s->setLabelVisible(visible);
374 }
374 }
375
375
376 /*!
376 /*!
377 Returns the sum of all slice values in this series.
377 Returns the sum of all slice values in this series.
378
378
379 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
379 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
380 */
380 */
381 qreal QPieSeries::sum() const
381 qreal QPieSeries::sum() const
382 {
382 {
383 Q_D(const QPieSeries);
383 Q_D(const QPieSeries);
384 return d->m_sum;
384 return d->m_sum;
385 }
385 }
386
386
387 /*!
387 /*!
388 \fn void QPieSeries::clicked(QPieSlice* slice)
388 \fn void QPieSeries::clicked(QPieSlice* slice)
389
389
390 This signal is emitted when a \a slice has been clicked.
390 This signal is emitted when a \a slice has been clicked.
391
391
392 \sa QPieSlice::clicked()
392 \sa QPieSlice::clicked()
393 */
393 */
394
394
395 /*!
395 /*!
396 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
396 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
397
397
398 This signal is emitted when user has hovered over or away from the \a slice.
398 This signal is emitted when user has hovered over or away from the \a slice.
399
399
400 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
400 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
401
401
402 \sa QPieSlice::hovered()
402 \sa QPieSlice::hovered()
403 */
403 */
404
404
405 /*!
405 /*!
406 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
406 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
407 Sets the \a model to be used as a data source
407 Sets the \a model to be used as a data source
408 */
408 */
409 bool QPieSeries::setModel(QAbstractItemModel* model)
409 bool QPieSeries::setModel(QAbstractItemModel* model)
410 {
410 {
411 Q_D(QPieSeries);
411 Q_D(QPieSeries);
412 // disconnect signals from old model
412 // disconnect signals from old model
413 if(d->m_model)
413 if(d->m_model)
414 {
414 {
415 disconnect(d->m_model, 0, this, 0);
415 disconnect(d->m_model, 0, this, 0);
416 d->m_mapValues = -1;
416 d->m_mapValues = -1;
417 d->m_mapLabels = -1;
417 d->m_mapLabels = -1;
418 d->m_mapOrientation = Qt::Vertical;
418 d->m_mapOrientation = Qt::Vertical;
419 }
419 }
420
420
421 // set new model
421 // set new model
422 if(model)
422 if(model)
423 {
423 {
424 d->m_model = model;
424 d->m_model = model;
425 return true;
425 return true;
426 }
426 }
427 else
427 else
428 {
428 {
429 d->m_model = 0;
429 d->m_model = 0;
430 return false;
430 return false;
431 }
431 }
432 }
432 }
433
433
434 /*!
434 /*!
435 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
435 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
436 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
436 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
437 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
437 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
438 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
438 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
439 The \a orientation parameter specifies whether the data is in columns or in rows.
439 The \a orientation parameter specifies whether the data is in columns or in rows.
440 */
440 */
441 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
441 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
442 {
442 {
443 Q_D(QPieSeries);
443 Q_D(QPieSeries);
444
444
445 if (d->m_model == 0)
445 if (d->m_model == 0)
446 return;
446 return;
447
447
448 d->m_mapValues = modelValuesLine;
448 d->m_mapValues = modelValuesLine;
449 d->m_mapLabels = modelLabelsLine;
449 d->m_mapLabels = modelLabelsLine;
450 d->m_mapOrientation = orientation;
450 d->m_mapOrientation = orientation;
451
451
452 d->initializePieFromModel();
452 d->initializePieFromModel();
453 // // connect the signals
453 // // connect the signals
454 // connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
454 // connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
455
455
456
456
457 // // create the initial slices set
457 // // create the initial slices set
458 // if (d->m_mapOrientation == Qt::Vertical) {
458 // if (d->m_mapOrientation == Qt::Vertical) {
459 // for (int i = 0; i < d->m_model->rowCount(); i++)
459 // for (int i = 0; i < d->m_model->rowCount(); i++)
460 // append(d->m_model->data(d->m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
460 // append(d->m_model->data(d->m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
461 // } else {
461 // } else {
462 // for (int i = 0; i < d->m_model->columnCount(); i++)
462 // for (int i = 0; i < d->m_model->columnCount(); i++)
463 // append(d->m_model->data(d->m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
463 // append(d->m_model->data(d->m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
464 // }
464 // }
465 }
465 }
466
466
467 void QPieSeries::setModelMappingRange(int first, int count)
467 void QPieSeries::setModelMappingRange(int first, int count)
468 {
468 {
469 Q_D(QPieSeries);
469 Q_D(QPieSeries);
470 d->m_mapFirst = first;
470 d->m_mapFirst = first;
471 d->m_mapCount = count;
471 d->m_mapCount = count;
472 }
472 }
473
473
474 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
474 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
475
475
476
476
477 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
477 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
478 QAbstractSeriesPrivate(parent),
478 QAbstractSeriesPrivate(parent),
479 m_pieRelativeHorPos(0.5),
479 m_pieRelativeHorPos(0.5),
480 m_pieRelativeVerPos(0.5),
480 m_pieRelativeVerPos(0.5),
481 m_pieRelativeSize(0.7),
481 m_pieRelativeSize(0.7),
482 m_pieStartAngle(0),
482 m_pieStartAngle(0),
483 m_pieEndAngle(360),
483 m_pieEndAngle(360),
484 m_sum(0),
484 m_sum(0),
485 m_mapValues(0),
485 m_mapValues(0),
486 m_mapLabels(0)
486 m_mapLabels(0)
487 {
487 {
488
488
489 }
489 }
490
490
491 QPieSeriesPrivate::~QPieSeriesPrivate()
491 QPieSeriesPrivate::~QPieSeriesPrivate()
492 {
492 {
493
493
494 }
494 }
495
495
496 void QPieSeriesPrivate::updateDerivativeData()
496 void QPieSeriesPrivate::updateDerivativeData()
497 {
497 {
498 m_sum = 0;
498 m_sum = 0;
499
499
500 // nothing to do?
500 // nothing to do?
501 if (m_slices.count() == 0)
501 if (m_slices.count() == 0)
502 return;
502 return;
503
503
504 // calculate sum of all slices
504 // calculate sum of all slices
505 foreach (QPieSlice* s, m_slices)
505 foreach (QPieSlice* s, m_slices)
506 m_sum += s->value();
506 m_sum += s->value();
507
507
508 // nothing to show..
508 // nothing to show..
509 if (qFuzzyIsNull(m_sum))
509 if (qFuzzyIsNull(m_sum))
510 return;
510 return;
511
511
512 // update slice attributes
512 // update slice attributes
513 qreal sliceAngle = m_pieStartAngle;
513 qreal sliceAngle = m_pieStartAngle;
514 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
514 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
515 QVector<QPieSlice*> changed;
515 QVector<QPieSlice*> changed;
516 foreach (QPieSlice* s, m_slices) {
516 foreach (QPieSlice* s, m_slices) {
517
517
518 PieSliceData data = PieSliceData::data(s);
518 PieSliceData data = PieSliceData::data(s);
519 data.m_percentage = s->value() / m_sum;
519 data.m_percentage = s->value() / m_sum;
520 data.m_angleSpan = pieSpan * data.m_percentage;
520 data.m_angleSpan = pieSpan * data.m_percentage;
521 data.m_startAngle = sliceAngle;
521 data.m_startAngle = sliceAngle;
522 sliceAngle += data.m_angleSpan;
522 sliceAngle += data.m_angleSpan;
523
523
524 if (PieSliceData::data(s) != data) {
524 if (PieSliceData::data(s) != data) {
525 PieSliceData::data(s) = data;
525 PieSliceData::data(s) = data;
526 changed << s;
526 changed << s;
527 }
527 }
528 }
528 }
529
529
530 // emit signals
530 // emit signals
531 foreach (QPieSlice* s, changed)
531 foreach (QPieSlice* s, changed)
532 PieSliceData::data(s).emitChangedSignal(s);
532 PieSliceData::data(s).emitChangedSignal(s);
533 }
533 }
534
534
535 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
535 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
536 {
536 {
537 return series.d_func();
537 return series.d_func();
538 }
538 }
539
539
540 void QPieSeriesPrivate::sliceChanged()
540 void QPieSeriesPrivate::sliceChanged()
541 {
541 {
542 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
542 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
543 updateDerivativeData();
543 updateDerivativeData();
544 }
544 }
545
545
546 void QPieSeriesPrivate::sliceClicked()
546 void QPieSeriesPrivate::sliceClicked()
547 {
547 {
548 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
548 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
549 Q_ASSERT(m_slices.contains(slice));
549 Q_ASSERT(m_slices.contains(slice));
550 Q_Q(QPieSeries);
550 Q_Q(QPieSeries);
551 emit q->clicked(slice);
551 emit q->clicked(slice);
552 }
552 }
553
553
554 void QPieSeriesPrivate::sliceHovered(bool state)
554 void QPieSeriesPrivate::sliceHovered(bool state)
555 {
555 {
556 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
556 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
557 Q_ASSERT(m_slices.contains(slice));
557 Q_ASSERT(m_slices.contains(slice));
558 Q_Q(QPieSeries);
558 Q_Q(QPieSeries);
559 emit q->hovered(slice, state);
559 emit q->hovered(slice, state);
560 }
560 }
561
561
562 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
562 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
563 {
563 {
564 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
564 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
565 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
565 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
566 if (m_mapOrientation == Qt::Vertical)
566 if (m_mapOrientation == Qt::Vertical)
567 {
567 {
568 if ( topLeft.row() >= m_mapFirst && (m_mapCount == - 1 || topLeft.row() < m_mapFirst + m_mapCount)) {
568 if ( topLeft.row() >= m_mapFirst && (m_mapCount == - 1 || topLeft.row() < m_mapFirst + m_mapCount)) {
569 if (topLeft.column() == m_mapValues)
569 if (topLeft.column() == m_mapValues)
570 m_slices.at(topLeft.row() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
570 m_slices.at(topLeft.row() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
571 if (topLeft.column() == m_mapLabels)
571 if (topLeft.column() == m_mapLabels)
572 m_slices.at(topLeft.row() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
572 m_slices.at(topLeft.row() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
573 }
573 }
574 }
574 }
575 else
575 else
576 {
576 {
577 if (topLeft.column() >= m_mapFirst && (m_mapCount == - 1 || topLeft.column() < m_mapFirst + m_mapCount)) {
577 if (topLeft.column() >= m_mapFirst && (m_mapCount == - 1 || topLeft.column() < m_mapFirst + m_mapCount)) {
578 if (topLeft.row() == m_mapValues)
578 if (topLeft.row() == m_mapValues)
579 m_slices.at(topLeft.column() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
579 m_slices.at(topLeft.column() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
580 if (topLeft.row() == m_mapLabels)
580 if (topLeft.row() == m_mapLabels)
581 m_slices.at(topLeft.column() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
581 m_slices.at(topLeft.column() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
582 }
582 }
583 }
583 }
584 }
584 }
585 }
585 }
586 }
586 }
587
587
588
588
589 void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
589 void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
590 {
590 {
591 Q_UNUSED(parent);
591 Q_UNUSED(parent);
592 Q_Q(QPieSeries);
592 Q_Q(QPieSeries);
593 // series uses model as a data sourceupda
593 // series uses model as a data sourceupda
594 int addedCount = end - start + 1;
595 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
594 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
596 return;
595 return;
597 } else {
596 } else {
597 int addedCount = end - start + 1;
598 if (m_mapCount != -1 && addedCount > m_mapCount)
599 addedCount = m_mapCount;
598 int first = qMax(start, m_mapFirst);
600 int first = qMax(start, m_mapFirst);
599 int last = qMin(first + addedCount - 1, end);
601 int last = qMin(first + addedCount - 1, m_mapOrientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
600 for (int i = first; i <= last; i++) {
602 for (int i = first; i <= last; i++) {
601 QPieSlice *slice = new QPieSlice;
603 QPieSlice *slice = new QPieSlice;
602 if (m_mapOrientation == Qt::Vertical) {
604 if (m_mapOrientation == Qt::Vertical) {
603 slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
605 slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
604 slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
606 slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
605 } else {
607 } else {
606 slice->setValue(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble());
608 slice->setValue(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble());
607 slice->setLabel(m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
609 slice->setLabel(m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
608 }
610 }
609 slice->setLabelVisible();
611 slice->setLabelVisible();
610 q->insert(i - m_mapFirst, slice);
612 q->insert(i - m_mapFirst, slice);
611 }
613 }
612 if (m_mapCount != -1 && m_slices.size() > m_mapCount)
614 if (m_mapCount != -1 && m_slices.size() > m_mapCount)
613 for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
615 for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
614 q->remove(q->slices().at(i));
616 q->remove(q->slices().at(i));
615 }
617 }
616
618
617 // // adding items to unlimited map
619 // // adding items to unlimited map
618 // else if (m_mapCount == -1 && start >= m_mapFirst) {
620 // else if (m_mapCount == -1 && start >= m_mapFirst) {
619 // for (int i = start; i <= end; i++) {
621 // for (int i = start; i <= end; i++) {
620 // QPieSlice *slice = new QPieSlice;
622 // QPieSlice *slice = new QPieSlice;
621 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
623 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
622 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
624 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
623 // slice->setLabelVisible(true);
625 // slice->setLabelVisible(true);
624 // q->insert(i - m_mapFirst, slice);
626 // q->insert(i - m_mapFirst, slice);
625 // // handlePointAdded(i - first);
627 // // handlePointAdded(i - first);
626 // }
628 // }
627 // } else if (m_mapCount == - 1 && start < m_mapFirst) {
629 // } else if (m_mapCount == - 1 && start < m_mapFirst) {
628 // // not all newly added items
630 // // not all newly added items
629 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
631 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
630 // QPieSlice *slice = new QPieSlice;
632 // QPieSlice *slice = new QPieSlice;
631 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
633 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
632 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
634 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
633 // slice->setLabelVisible(true);
635 // slice->setLabelVisible(true);
634 // q->insert(i - m_mapFirst, slice);
636 // q->insert(i - m_mapFirst, slice);
635 // // handlePointAdded(i - first);
637 // // handlePointAdded(i - first);
636 // }
638 // }
637 // }
639 // }
638
640
639 // // adding items to limited map
641 // // adding items to limited map
640 // else if (start >= m_mapFirst) {
642 // else if (start >= m_mapFirst) {
641 // // remove the items that will no longer fit into the map
643 // // remove the items that will no longer fit into the map
642 // // int toRemove = addedCount - (count - points().size());
644 // // int toRemove = addedCount - (count - points().size());
643 // for (int i = start; i <= end; i++) {
645 // for (int i = start; i <= end; i++) {
644 // QPieSlice *slice = new QPieSlice;
646 // QPieSlice *slice = new QPieSlice;
645 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
647 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
646 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
648 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
647 // slice->setLabelVisible(true);
649 // slice->setLabelVisible(true);
648 // q->insert(i - m_mapFirst, slice);
650 // q->insert(i - m_mapFirst, slice);
649 // }
651 // }
650 // if (m_slices.size() > m_mapCount)
652 // if (m_slices.size() > m_mapCount)
651 // for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
653 // for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
652 // q->remove(q->slices().at(i));
654 // q->remove(q->slices().at(i));
653 // // handlePointRemoved(i);
655 // // handlePointRemoved(i);
654 // // update();
656 // // update();
655 // } else {
657 // } else {
656 // //
658 // //
657 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
659 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
658 // QPieSlice *slice = new QPieSlice;
660 // QPieSlice *slice = new QPieSlice;
659 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
661 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
660 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
662 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
661 // slice->setLabelVisible(true);
663 // slice->setLabelVisible(true);
662 // q->insert(i - m_mapFirst, slice);
664 // q->insert(i - m_mapFirst, slice);
663 // // handlePointAdded(i - first);
665 // // handlePointAdded(i - first);
664 // }
666 // }
665 // if (m_slices.size() > m_mapCount)
667 // if (m_slices.size() > m_mapCount)
666 // for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
668 // for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
667 // q->remove(q->slices().at(i));
669 // q->remove(q->slices().at(i));
668 // // handlePointRemoved(i);
670 // // handlePointRemoved(i);
669 // }
671 // }
670 }
672 }
671
673
672 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
674 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
673 {
675 {
674 Q_UNUSED(parent);
676 Q_UNUSED(parent);
675 Q_Q(QPieSeries);
677 Q_Q(QPieSeries);
676 int removedCount = end - start + 1;
678 int removedCount = end - start + 1;
677 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
679 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
678 return;
680 return;
679 }
681 }
680
682
681 // else if (m_mapCount == -1) {
683 // else if (m_mapCount == -1) {
684 // // first find how many items can actually be removed
685 // int toRemove = qMin(m_slices.size(), removedCount);
686 // // get the index of the first item that will be removed.
682 // int first = qMax(start, m_mapFirst);
687 // int first = qMax(start, m_mapFirst);
683 // int last = qMax(end, m_mapFirst + removedCount - 1);
688 // // get the index of the last item that will be removed.
684 // for (int i = last; i >= first; i--)
689 // int last = first + toRemove;
690 // for (int i = last - 1; i >= first; i--)
685 // q->remove(q->slices().at(i - m_mapFirst));
691 // q->remove(q->slices().at(i - m_mapFirst));
686 // }
692 // }
687 // else {
693 // else {
688 // int toRemove = qMin(m_slices.size() - 1, removedCount);
694 // int toRemove = qMin(m_slices.size() - 1, removedCount);
689 // int first = qMax(start, m_mapFirst);
695 // int first = qMax(start, m_mapFirst);
690 // int last = qMax(end, m_mapFirst + toRemove - 1);
696 // int last = qMax(end, m_mapFirst + toRemove - 1);
691
697
692 // }
698 // }
693
699
694 // removing items from unlimited map
700 // removing items from unlimited map
695 else if (m_mapCount == -1 && start >= m_mapFirst) {
701 else if (m_mapCount == -1 && start >= m_mapFirst) {
696 for (int i = end; i >= start; i--)
702 for (int i = end; i >= start; i--)
697 q->remove(q->slices().at(i - m_mapFirst));
703 q->remove(q->slices().at(i - m_mapFirst));
698 // handlePointRemoved(i - m_mapFirst);
704 // handlePointRemoved(i - m_mapFirst);
699 } else if (m_mapCount == - 1 && start < m_mapFirst) {
705 } else if (m_mapCount == - 1 && start < m_mapFirst) {
700 // not all removed items
706 // not all removed items
701 for (int i = m_mapFirst + removedCount - 1; i >= m_mapFirst; i--)
707 int lastExisting = qMin(m_mapFirst + m_slices.size() - 1, m_mapFirst + removedCount - 1);
708 for (int i = lastExisting; i >= m_mapFirst; i--)
702 q->remove(q->slices().at(i - m_mapFirst));
709 q->remove(q->slices().at(i - m_mapFirst));
703 // handlePointRemoved(i - m_mapFirst);
710 // handlePointRemoved(i - m_mapFirst);
704 }
711 }
705
712
706 // removing items from limited map
713 // removing items from limited map
707 else if (start >= m_mapFirst) {
714 else if (start >= m_mapFirst) {
708 //
715 //
709 // int sizeAfter = m_slices.size();
716 // int sizeAfter = m_slices.size();
710 int lastExisting = qMin(m_mapFirst + m_slices.size() - 1, end);
717 int lastExisting = qMin(m_mapFirst + m_slices.size() - 1, end);
711 for (int i = lastExisting; i >= start; i--) {
718 for (int i = lastExisting; i >= start; i--) {
712 q->remove(q->slices().at(i - m_mapFirst));
719 q->remove(q->slices().at(i - m_mapFirst));
713 // sizeAfter--;
720 // sizeAfter--;
714 // handlePointRemoved(i - m_mapFirst);
721 // handlePointRemoved(i - m_mapFirst);
715 }
722 }
716
723
717 // the map is limited, so after removing the items some new items may have fall within the mapped area
724 // the map is limited, so after removing the items some new items may have fall within the mapped area
718 int itemsAvailable;
725 int itemsAvailable;
719 if (m_mapOrientation == Qt::Vertical)
726 if (m_mapOrientation == Qt::Vertical)
720 itemsAvailable = m_model->rowCount() - m_mapFirst - m_slices.size();
727 itemsAvailable = m_model->rowCount() - m_mapFirst - m_slices.size();
721 else
728 else
722 itemsAvailable = m_model->columnCount() - m_mapFirst - m_slices.size();
729 itemsAvailable = m_model->columnCount() - m_mapFirst - m_slices.size();
723 int toBeAdded = qMin(itemsAvailable, m_mapCount - m_slices.size());
730 int toBeAdded = qMin(itemsAvailable, m_mapCount - m_slices.size());
724 int currentSize = m_slices.size();
731 int currentSize = m_slices.size();
725 if (itemsAvailable > 0)
732 if (itemsAvailable > 0)
726 for (int i = m_slices.size() + m_mapFirst; i < currentSize + toBeAdded + m_mapFirst; i++) {
733 for (int i = m_slices.size() + m_mapFirst; i < currentSize + toBeAdded + m_mapFirst; i++) {
727 // handlePointAdded(i);
734 // handlePointAdded(i);
728 QPieSlice *slice = new QPieSlice;
735 QPieSlice *slice = new QPieSlice;
729 slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
736 slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
730 slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
737 slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
731 slice->setLabelVisible(true);
738 slice->setLabelVisible(true);
732 q->insert(i - m_mapFirst, slice);
739 q->insert(i - m_mapFirst, slice);
733 }
740 }
734
741
735 // for (int i = lastExisting; i >= start; i--) {
742 // for (int i = lastExisting; i >= start; i--) {
736 // q->remove(q->slices().at(i - m_mapFirst));
743 // q->remove(q->slices().at(i - m_mapFirst));
737 //// sizeAfter--;
744 //// sizeAfter--;
738 //// handlePointRemoved(i - m_mapFirst);
745 //// handlePointRemoved(i - m_mapFirst);
739 // }
746 // }
740 } else {
747 } else {
741 // first remove item lays before the mapped area
748 // first remove item lays before the mapped area
742 int toRemove = qMin(m_slices.size(), removedCount);
749 int toRemove = qMin(m_slices.size(), removedCount);
743 for (int i = m_mapFirst; i < m_mapFirst + toRemove; i++)
750 for (int i = m_mapFirst; i < m_mapFirst + toRemove; i++)
744 q->remove(q->slices().at(0));
751 q->remove(q->slices().at(0));
745 // handlePointRemoved(0);
752 // handlePointRemoved(0);
746
753
747 // the map is limited, so after removing the items some new items may have fall into the map
754 // the map is limited, so after removing the items some new items may have fall into the map
748 int itemsAvailable;
755 int itemsAvailable;
749 if (m_mapOrientation == Qt::Vertical)
756 if (m_mapOrientation == Qt::Vertical)
750 itemsAvailable = m_model->rowCount() - m_mapFirst - m_slices.size();
757 itemsAvailable = m_model->rowCount() - m_mapFirst - m_slices.size();
751 else
758 else
752 itemsAvailable = m_model->columnCount() - m_mapFirst - m_slices.size();
759 itemsAvailable = m_model->columnCount() - m_mapFirst - m_slices.size();
753 int toBeAdded = qMin(itemsAvailable, m_mapCount - m_slices.size());
760 int toBeAdded = qMin(itemsAvailable, m_mapCount - m_slices.size());
754 int currentSize = m_slices.size();
761 int currentSize = m_slices.size();
755 if (itemsAvailable > 0)
762 if (itemsAvailable > 0)
756 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
763 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
757 QPieSlice *slice = new QPieSlice;
764 QPieSlice *slice = new QPieSlice;
758 slice->setValue(m_model->data(m_model->index(i + m_mapFirst, m_mapValues), Qt::DisplayRole).toDouble());
765 slice->setValue(m_model->data(m_model->index(i + m_mapFirst, m_mapValues), Qt::DisplayRole).toDouble());
759 slice->setLabel(m_model->data(m_model->index(i + m_mapFirst, m_mapLabels), Qt::DisplayRole).toString());
766 slice->setLabel(m_model->data(m_model->index(i + m_mapFirst, m_mapLabels), Qt::DisplayRole).toString());
760 slice->setLabelVisible(true);
767 slice->setLabelVisible(true);
761 q->insert(i, slice);
768 q->insert(i, slice);
762 // handlePointAdded(i);
769 // handlePointAdded(i);
763 }
770 }
764 }
771 }
765
772
766 }
773 }
767
774
768 void QPieSeriesPrivate::initializePieFromModel()
775 void QPieSeriesPrivate::initializePieFromModel()
769 {
776 {
770 Q_Q(QPieSeries);
777 Q_Q(QPieSeries);
771 // clear current content
778 // clear current content
772 q->clear();
779 q->clear();
773
780
774 // connect the signals
781 // connect the signals
775 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
782 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
776 if (m_mapOrientation == Qt::Vertical) {
783 if (m_mapOrientation == Qt::Vertical) {
777 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
784 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
778 connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
785 connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
779 } else {
786 } else {
780 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
787 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
781 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
788 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
782 }
789 }
783
790
784 // create the initial slices set
791 // create the initial slices set
785 if (m_mapOrientation == Qt::Vertical) {
792 if (m_mapOrientation == Qt::Vertical) {
786 int sliceCount = 0;
793 int sliceCount = 0;
787 if(m_mapCount == -1)
794 if(m_mapCount == -1)
788 sliceCount = m_model->rowCount() - m_mapFirst;
795 sliceCount = m_model->rowCount() - m_mapFirst;
789 else
796 else
790 sliceCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
797 sliceCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
791 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
798 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
792 q->append(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
799 q->append(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
793 } else {
800 } else {
794 int sliceCount = 0;
801 int sliceCount = 0;
795 if(m_mapCount == -1)
802 if(m_mapCount == -1)
796 sliceCount = m_model->columnCount() - m_mapFirst;
803 sliceCount = m_model->columnCount() - m_mapFirst;
797 else
804 else
798 sliceCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
805 sliceCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
799 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
806 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
800 q->append(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
807 q->append(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
801 }
808 }
802 }
809 }
803
810
804 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
811 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
805 {
812 {
806 // Remove rounding errors
813 // Remove rounding errors
807 qreal roundedValue = newValue;
814 qreal roundedValue = newValue;
808 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
815 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
809 roundedValue = 0.0;
816 roundedValue = 0.0;
810 else if (qFuzzyCompare(newValue, max))
817 else if (qFuzzyCompare(newValue, max))
811 roundedValue = max;
818 roundedValue = max;
812 else if (qFuzzyCompare(newValue, min))
819 else if (qFuzzyCompare(newValue, min))
813 roundedValue = min;
820 roundedValue = min;
814
821
815 // Check if the position is valid after removing the rounding errors
822 // Check if the position is valid after removing the rounding errors
816 if (roundedValue < min || roundedValue > max) {
823 if (roundedValue < min || roundedValue > max) {
817 qWarning("QPieSeries: Illegal value");
824 qWarning("QPieSeries: Illegal value");
818 return false;
825 return false;
819 }
826 }
820
827
821 if (!qFuzzyIsNull(value - roundedValue)) {
828 if (!qFuzzyIsNull(value - roundedValue)) {
822 value = roundedValue;
829 value = roundedValue;
823 return true;
830 return true;
824 }
831 }
825
832
826 // The change was so small it is considered a rounding error
833 // The change was so small it is considered a rounding error
827 return false;
834 return false;
828 }
835 }
829
836
830 void QPieSeriesPrivate::scaleDomain(Domain& domain)
837 void QPieSeriesPrivate::scaleDomain(Domain& domain)
831 {
838 {
832 Q_UNUSED(domain);
839 Q_UNUSED(domain);
833 #ifndef QT_NO_DEBUG
840 #ifndef QT_NO_DEBUG
834 qWarning() << __FILE__<<__FUNCTION__<<"not implemented";
841 qWarning() << __FILE__<<__FUNCTION__<<"not implemented";
835 #endif
842 #endif
836 }
843 }
837
844
838 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
845 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
839 {
846 {
840 Q_Q(QPieSeries);
847 Q_Q(QPieSeries);
841 PieChartItem* pie = new PieChartItem(q,presenter);
848 PieChartItem* pie = new PieChartItem(q,presenter);
842 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
849 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
843 presenter->animator()->addAnimation(pie);
850 presenter->animator()->addAnimation(pie);
844 }
851 }
845 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
852 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
846 return pie;
853 return pie;
847 }
854 }
848
855
849 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
856 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
850 {
857 {
851 Q_Q(QPieSeries);
858 Q_Q(QPieSeries);
852 QList<LegendMarker*> markers;
859 QList<LegendMarker*> markers;
853 foreach(QPieSlice* slice, q->slices()) {
860 foreach(QPieSlice* slice, q->slices()) {
854 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
861 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
855 markers << marker;
862 markers << marker;
856 }
863 }
857 return markers;
864 return markers;
858 }
865 }
859
866
860 #include "moc_qpieseries.cpp"
867 #include "moc_qpieseries.cpp"
861 #include "moc_qpieseries_p.cpp"
868 #include "moc_qpieseries_p.cpp"
862
869
863 QTCOMMERCIALCHART_END_NAMESPACE
870 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,457 +1,458
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 "qxyseries.h"
21 #include "qxyseries.h"
22 #include "qxyseries_p.h"
22 #include "qxyseries_p.h"
23 #include "domain_p.h"
23 #include "domain_p.h"
24 #include "legendmarker_p.h"
24 #include "legendmarker_p.h"
25 #include <QAbstractItemModel>
25 #include <QAbstractItemModel>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 /*!
29 /*!
30 \class QXYSeries
30 \class QXYSeries
31 \brief The QXYSeries class is a base class for line, spline and scatter series.
31 \brief The QXYSeries class is a base class for line, spline and scatter series.
32 */
32 */
33
33
34 /*!
34 /*!
35 \fn QPen QXYSeries::pen() const
35 \fn QPen QXYSeries::pen() const
36 \brief Returns pen used to draw points for series.
36 \brief Returns pen used to draw points for series.
37 \sa setPen()
37 \sa setPen()
38 */
38 */
39
39
40 /*!
40 /*!
41 \fn QBrush QXYSeries::brush() const
41 \fn QBrush QXYSeries::brush() const
42 \brief Returns brush used to draw points for series.
42 \brief Returns brush used to draw points for series.
43 \sa setBrush()
43 \sa setBrush()
44 */
44 */
45
45
46 /*!
46 /*!
47 \fn void QXYSeries::clicked(const QPointF& point)
47 \fn void QXYSeries::clicked(const QPointF& point)
48 \brief Signal is emitted when user clicks the \a point on chart.
48 \brief Signal is emitted when user clicks the \a point on chart.
49 */
49 */
50
50
51 /*!
51 /*!
52 \fn void QXYSeries::selected()
52 \fn void QXYSeries::selected()
53
53
54 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
54 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
55 implemented by the user of QXYSeries API.
55 implemented by the user of QXYSeries API.
56 */
56 */
57
57
58 /*!
58 /*!
59 \fn void QXYSeriesPrivate::pointReplaced(int index)
59 \fn void QXYSeriesPrivate::pointReplaced(int index)
60 \brief \internal \a index
60 \brief \internal \a index
61 */
61 */
62
62
63 /*!
63 /*!
64 \fn void QXYSeriesPrivate::pointAdded(int index)
64 \fn void QXYSeriesPrivate::pointAdded(int index)
65 \brief \internal \a index
65 \brief \internal \a index
66 */
66 */
67
67
68 /*!
68 /*!
69 \fn void QXYSeriesPrivate::pointRemoved(int index)
69 \fn void QXYSeriesPrivate::pointRemoved(int index)
70 \brief \internal \a index
70 \brief \internal \a index
71 */
71 */
72
72
73 /*!
73 /*!
74 \fn void QXYSeriesPrivate::updated()
74 \fn void QXYSeriesPrivate::updated()
75 \brief \internal
75 \brief \internal
76 */
76 */
77
77
78 /*!
78 /*!
79 \internal
79 \internal
80
80
81 Constructs empty series object which is a child of \a parent.
81 Constructs empty series object which is a child of \a parent.
82 When series object is added to QChartView or QChart instance ownerships is transferred.
82 When series object is added to QChartView or QChart instance ownerships is transferred.
83 */
83 */
84 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
84 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
85 {
85 {
86
86
87 }
87 }
88 /*!
88 /*!
89 Destroys the object. Series added to QChartView or QChart instances are owned by those,
89 Destroys the object. Series added to QChartView or QChart instances are owned by those,
90 and are deleted when mentioned object are destroyed.
90 and are deleted when mentioned object are destroyed.
91 */
91 */
92 QXYSeries::~QXYSeries()
92 QXYSeries::~QXYSeries()
93 {
93 {
94 }
94 }
95
95
96 /*!
96 /*!
97 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
97 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
98 */
98 */
99 void QXYSeries::append(qreal x,qreal y)
99 void QXYSeries::append(qreal x,qreal y)
100 {
100 {
101 append(QPointF(x,y));
101 append(QPointF(x,y));
102 }
102 }
103
103
104 /*!
104 /*!
105 This is an overloaded function.
105 This is an overloaded function.
106 Adds data \a point to the series. Points are connected with lines on the chart.
106 Adds data \a point to the series. Points are connected with lines on the chart.
107 */
107 */
108 void QXYSeries::append(const QPointF &point)
108 void QXYSeries::append(const QPointF &point)
109 {
109 {
110 Q_D(QXYSeries);
110 Q_D(QXYSeries);
111 d->m_points<<point;
111 d->m_points<<point;
112 emit d->pointAdded(d->m_points.count()-1);
112 emit d->pointAdded(d->m_points.count()-1);
113 }
113 }
114
114
115 /*!
115 /*!
116 This is an overloaded function.
116 This is an overloaded function.
117 Adds list of data \a points to the series. Points are connected with lines on the chart.
117 Adds list of data \a points to the series. Points are connected with lines on the chart.
118 */
118 */
119 void QXYSeries::append(const QList<QPointF> &points)
119 void QXYSeries::append(const QList<QPointF> &points)
120 {
120 {
121 foreach(const QPointF& point , points) {
121 foreach(const QPointF& point , points) {
122 append(point);
122 append(point);
123 }
123 }
124 }
124 }
125
125
126
126
127 void QXYSeries::replace(qreal oldX,qreal oldY,qreal newX,qreal newY)
127 void QXYSeries::replace(qreal oldX,qreal oldY,qreal newX,qreal newY)
128 {
128 {
129 replace(QPointF(oldX,oldY),QPointF(newX,newY));
129 replace(QPointF(oldX,oldY),QPointF(newX,newY));
130 }
130 }
131
131
132 void QXYSeries::replace(const QPointF &oldPoint,const QPointF &newPoint)
132 void QXYSeries::replace(const QPointF &oldPoint,const QPointF &newPoint)
133 {
133 {
134 Q_D(QXYSeries);
134 Q_D(QXYSeries);
135 int index = d->m_points.indexOf(oldPoint);
135 int index = d->m_points.indexOf(oldPoint);
136 if(index==1) return;
136 if(index==1) return;
137 d->m_points[index] = newPoint;
137 d->m_points[index] = newPoint;
138 emit d->pointReplaced(index);
138 emit d->pointReplaced(index);
139 }
139 }
140
140
141 /*!
141 /*!
142 Removes current \a x and \a y value.
142 Removes current \a x and \a y value.
143 */
143 */
144 void QXYSeries::remove(qreal x,qreal y)
144 void QXYSeries::remove(qreal x,qreal y)
145 {
145 {
146 remove(QPointF(x,y));
146 remove(QPointF(x,y));
147 }
147 }
148
148
149 /*!
149 /*!
150 Removes current \a point x value. Note \a point y value is ignored.
150 Removes current \a point x value. Note \a point y value is ignored.
151 */
151 */
152 void QXYSeries::remove(const QPointF &point)
152 void QXYSeries::remove(const QPointF &point)
153 {
153 {
154 Q_D(QXYSeries);
154 Q_D(QXYSeries);
155 int index = d->m_points.indexOf(point);
155 int index = d->m_points.indexOf(point);
156 if(index==1) return;
156 if(index==1) return;
157 d->m_points.remove(index);
157 d->m_points.remove(index);
158 emit d->pointRemoved(index);
158 emit d->pointRemoved(index);
159 }
159 }
160
160
161 /*!
161 /*!
162 Removes all data points from the series.
162 Removes all data points from the series.
163 */
163 */
164 void QXYSeries::removeAll()
164 void QXYSeries::removeAll()
165 {
165 {
166 Q_D(QXYSeries);
166 Q_D(QXYSeries);
167 foreach(const QPointF& point, d->m_points) {
167 foreach(const QPointF& point, d->m_points) {
168 remove(point);
168 remove(point);
169 }
169 }
170 }
170 }
171
171
172 /*!
172 /*!
173 \internal \a pos
173 \internal \a pos
174 */
174 */
175 QList<QPointF> QXYSeries::points() const
175 QList<QPointF> QXYSeries::points() const
176 {
176 {
177 // Q_ASSERT(false);
177 Q_D(const QXYSeries);
178 Q_D(const QXYSeries);
178 if (d->m_model) {
179 if (d->m_model) {
179 QList<QPointF> result;
180 QList<QPointF> result;
180 if (d->m_mapOrientation == Qt::Vertical){
181 if (d->m_mapOrientation == Qt::Vertical){
181 // consecutive data is read from model's column
182 // consecutive data is read from model's column
182
183
183 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
184 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
184 qreal x = d->m_model->data(d->m_model->index(i, d->m_mapX), Qt::DisplayRole).toReal();
185 qreal x = d->m_model->data(d->m_model->index(i, d->m_mapX), Qt::DisplayRole).toReal();
185 qreal y = d->m_model->data(d->m_model->index(i, d->m_mapY), Qt::DisplayRole).toReal();
186 qreal y = d->m_model->data(d->m_model->index(i, d->m_mapY), Qt::DisplayRole).toReal();
186 result << QPointF(x,y);
187 result << QPointF(x,y);
187 }
188 }
188 return result;
189 return result;
189 }
190 }
190 else{
191 else{
191 // consecutive data is read from model's row
192 // consecutive data is read from model's row
192 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
193 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
193 qreal x = d->m_model->data(d->m_model->index(d->m_mapX, i), Qt::DisplayRole).toReal();
194 qreal x = d->m_model->data(d->m_model->index(d->m_mapX, i), Qt::DisplayRole).toReal();
194 qreal y = d->m_model->data(d->m_model->index(d->m_mapY, i), Qt::DisplayRole).toReal();
195 qreal y = d->m_model->data(d->m_model->index(d->m_mapY, i), Qt::DisplayRole).toReal();
195 result << QPointF(x,y);
196 result << QPointF(x,y);
196 }
197 }
197 return result;
198 return result;
198 }
199 }
199 } else {
200 } else {
200 // model is not specified, return the data from series' internal data store
201 // model is not specified, return the data from series' internal data store
201 return d->m_points.toList();
202 return d->m_points.toList();
202 }
203 }
203 }
204 }
204
205
205 /*!
206 /*!
206 Returns number of data points within series.
207 Returns number of data points within series.
207 */
208 */
208 int QXYSeries::count() const
209 int QXYSeries::count() const
209 {
210 {
210 Q_D(const QXYSeries);
211 Q_D(const QXYSeries);
211
212
212 if (d->m_model) {
213 if (d->m_model) {
213 if (d->m_mapOrientation == Qt::Vertical) {
214 if (d->m_mapOrientation == Qt::Vertical) {
214 // data is in a column. Return the number of mapped items if the model's column have enough items
215 // data is in a column. Return the number of mapped items if the model's column have enough items
215 // or the number of items that can be mapped
216 // or the number of items that can be mapped
216 if (d->m_mapCount != -1)
217 if (d->m_mapCount != -1)
217 return qMin(d->m_mapCount, qMax(d->m_model->rowCount() - d->m_mapFirst, 0));
218 return qMin(d->m_mapCount, qMax(d->m_model->rowCount() - d->m_mapFirst, 0));
218 else
219 else
219 return qMax(d->m_model->rowCount() - d->m_mapFirst, 0);
220 return qMax(d->m_model->rowCount() - d->m_mapFirst, 0);
220 } else {
221 } else {
221 // data is in a row. Return the number of mapped items if the model's row have enough items
222 // data is in a row. Return the number of mapped items if the model's row have enough items
222 // or the number of items that can be mapped
223 // or the number of items that can be mapped
223 if (d->m_mapCount != -1)
224 if (d->m_mapCount != -1)
224 return qMin(d->m_mapCount, qMax(d->m_model->columnCount() - d->m_mapFirst, 0));
225 return qMin(d->m_mapCount, qMax(d->m_model->columnCount() - d->m_mapFirst, 0));
225 else
226 else
226 return qMax(d->m_model->columnCount() - d->m_mapFirst, 0);
227 return qMax(d->m_model->columnCount() - d->m_mapFirst, 0);
227 }
228 }
228 }
229 }
229
230
230 // model is not specified, return the number of points in the series internal data store
231 // model is not specified, return the number of points in the series internal data store
231 return d->m_points.count();
232 return d->m_points.count();
232 }
233 }
233
234
234
235
235 /*!
236 /*!
236 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
237 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
237 pen from chart theme is used.
238 pen from chart theme is used.
238 \sa QChart::setTheme()
239 \sa QChart::setTheme()
239 */
240 */
240 void QXYSeries::setPen(const QPen &pen)
241 void QXYSeries::setPen(const QPen &pen)
241 {
242 {
242 Q_D(QXYSeries);
243 Q_D(QXYSeries);
243 if (d->m_pen!=pen) {
244 if (d->m_pen!=pen) {
244 d->m_pen = pen;
245 d->m_pen = pen;
245 emit d->updated();
246 emit d->updated();
246 }
247 }
247 }
248 }
248
249
249 QPen QXYSeries::pen() const
250 QPen QXYSeries::pen() const
250 {
251 {
251 Q_D(const QXYSeries);
252 Q_D(const QXYSeries);
252 return d->m_pen;
253 return d->m_pen;
253 }
254 }
254
255
255 /*!
256 /*!
256 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
257 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
257 from chart theme setting is used.
258 from chart theme setting is used.
258 \sa QChart::setTheme()
259 \sa QChart::setTheme()
259 */
260 */
260 void QXYSeries::setBrush(const QBrush &brush)
261 void QXYSeries::setBrush(const QBrush &brush)
261 {
262 {
262 Q_D(QXYSeries);
263 Q_D(QXYSeries);
263 if (d->m_brush!=brush) {
264 if (d->m_brush!=brush) {
264 d->m_brush = brush;
265 d->m_brush = brush;
265 emit d->updated();
266 emit d->updated();
266 }
267 }
267 }
268 }
268
269
269 QBrush QXYSeries::brush() const
270 QBrush QXYSeries::brush() const
270 {
271 {
271 Q_D(const QXYSeries);
272 Q_D(const QXYSeries);
272 return d->m_brush;
273 return d->m_brush;
273 }
274 }
274
275
275
276
276 /*!
277 /*!
277 Sets if data points are \a visible and should be drawn on line.
278 Sets if data points are \a visible and should be drawn on line.
278 */
279 */
279 void QXYSeries::setPointsVisible(bool visible)
280 void QXYSeries::setPointsVisible(bool visible)
280 {
281 {
281 Q_D(QXYSeries);
282 Q_D(QXYSeries);
282 if (d->m_pointsVisible != visible){
283 if (d->m_pointsVisible != visible){
283 d->m_pointsVisible = visible;
284 d->m_pointsVisible = visible;
284 emit d->updated();
285 emit d->updated();
285 }
286 }
286 }
287 }
287
288
288 /*!
289 /*!
289 Returns true if drawing the data points of the series is enabled.
290 Returns true if drawing the data points of the series is enabled.
290 */
291 */
291 bool QXYSeries::pointsVisible() const
292 bool QXYSeries::pointsVisible() const
292 {
293 {
293 Q_D(const QXYSeries);
294 Q_D(const QXYSeries);
294 return d->m_pointsVisible;
295 return d->m_pointsVisible;
295 }
296 }
296
297
297
298
298 /*!
299 /*!
299 Stream operator for adding a data \a point to the series.
300 Stream operator for adding a data \a point to the series.
300 \sa append()
301 \sa append()
301 */
302 */
302 QXYSeries& QXYSeries::operator<< (const QPointF &point)
303 QXYSeries& QXYSeries::operator<< (const QPointF &point)
303 {
304 {
304 append(point);
305 append(point);
305 return *this;
306 return *this;
306 }
307 }
307
308
308
309
309 /*!
310 /*!
310 Stream operator for adding a list of \a points to the series.
311 Stream operator for adding a list of \a points to the series.
311 \sa append()
312 \sa append()
312 */
313 */
313
314
314 QXYSeries& QXYSeries::operator<< (const QList<QPointF>& points)
315 QXYSeries& QXYSeries::operator<< (const QList<QPointF>& points)
315 {
316 {
316 append(points);
317 append(points);
317 return *this;
318 return *this;
318 }
319 }
319
320
320 /*!
321 /*!
321 \fn bool QXYSeries::setModel(QAbstractItemModel *model)
322 \fn bool QXYSeries::setModel(QAbstractItemModel *model)
322 Sets the \a model to be used as a data source
323 Sets the \a model to be used as a data source
323 \sa setModelMapping()
324 \sa setModelMapping()
324 */
325 */
325 bool QXYSeries::setModel(QAbstractItemModel *model)
326 bool QXYSeries::setModel(QAbstractItemModel *model)
326 {
327 {
327 Q_D(QXYSeries);
328 Q_D(QXYSeries);
328 // disconnect signals from old model
329 // disconnect signals from old model
329 if (d->m_model) {
330 if (d->m_model) {
330 QObject::disconnect(d->m_model, 0, this, 0);
331 QObject::disconnect(d->m_model, 0, this, 0);
331 d->m_mapX = -1;
332 d->m_mapX = -1;
332 d->m_mapY = -1;
333 d->m_mapY = -1;
333 d->m_mapOrientation = Qt::Vertical;
334 d->m_mapOrientation = Qt::Vertical;
334 }
335 }
335
336
336 // set new model
337 // set new model
337 if (model) {
338 if (model) {
338 d->m_model = model;
339 d->m_model = model;
339 return true;
340 return true;
340 } else {
341 } else {
341 d->m_model = 0;
342 d->m_model = 0;
342 return false;
343 return false;
343 }
344 }
344 }
345 }
345
346
346 /*!
347 /*!
347 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
348 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
348 as a data source for y coordinate. The \a orientation parameter specifies whether the data
349 as a data source for y coordinate. The \a orientation parameter specifies whether the data
349 is in columns or in rows.
350 is in columns or in rows.
350 \sa setModel()
351 \sa setModel()
351 */
352 */
352 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
353 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
353 {
354 {
354 Q_D(QXYSeries);
355 Q_D(QXYSeries);
355 if (d->m_model == 0)
356 if (d->m_model == 0)
356 return;
357 return;
357 d->m_mapX = modelX;
358 d->m_mapX = modelX;
358 d->m_mapY = modelY;
359 d->m_mapY = modelY;
359 d->m_mapOrientation = orientation;
360 d->m_mapOrientation = orientation;
360
361
361 // connect the signals from the model
362 // connect the signals from the model
362 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
363 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
363 if (d->m_mapOrientation == Qt::Vertical) {
364 if (d->m_mapOrientation == Qt::Vertical) {
364 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
365 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
365 connect(d->m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
366 connect(d->m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
366 } else {
367 } else {
367 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
368 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
368 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
369 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
369 }
370 }
370 }
371 }
371
372
372 void QXYSeries::setModelMappingRange(int first, int count)
373 void QXYSeries::setModelMappingRange(int first, int count)
373 {
374 {
374 Q_D(QXYSeries);
375 Q_D(QXYSeries);
375 d->m_mapFirst = first;
376 d->m_mapFirst = first;
376 d->m_mapCount = count;
377 d->m_mapCount = count;
377 }
378 }
378
379
379 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
380 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
380
381
381
382
382 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) : QAbstractSeriesPrivate(q),
383 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) : QAbstractSeriesPrivate(q),
383 m_mapX(-1),
384 m_mapX(-1),
384 m_mapY(-1),
385 m_mapY(-1),
385 m_pointsVisible(false)
386 m_pointsVisible(false)
386 {
387 {
387 }
388 }
388
389
389 void QXYSeriesPrivate::scaleDomain(Domain& domain)
390 void QXYSeriesPrivate::scaleDomain(Domain& domain)
390 {
391 {
391 qreal minX(domain.minX());
392 qreal minX(domain.minX());
392 qreal minY(domain.minY());
393 qreal minY(domain.minY());
393 qreal maxX(domain.maxX());
394 qreal maxX(domain.maxX());
394 qreal maxY(domain.maxY());
395 qreal maxY(domain.maxY());
395 int tickXCount(domain.tickXCount());
396 int tickXCount(domain.tickXCount());
396 int tickYCount(domain.tickYCount());
397 int tickYCount(domain.tickYCount());
397
398
398 Q_Q(QXYSeries);
399 Q_Q(QXYSeries);
399
400
400 const QList<QPointF>& points = q->points();
401 const QList<QPointF>& points = q->points();
401 for (int i = 0; i < points.count(); i++)
402 for (int i = 0; i < points.count(); i++)
402 {
403 {
403 qreal x = points[i].x();
404 qreal x = points[i].x();
404 qreal y = points[i].y();
405 qreal y = points[i].y();
405 minX = qMin(minX, x);
406 minX = qMin(minX, x);
406 minY = qMin(minY, y);
407 minY = qMin(minY, y);
407 maxX = qMax(maxX, x);
408 maxX = qMax(maxX, x);
408 maxY = qMax(maxY, y);
409 maxY = qMax(maxY, y);
409 }
410 }
410
411
411 domain.setRangeX(minX,maxX,tickXCount);
412 domain.setRangeX(minX,maxX,tickXCount);
412 domain.setRangeY(minY,maxY,tickYCount);
413 domain.setRangeY(minY,maxY,tickYCount);
413 }
414 }
414
415
415 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
416 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
416 {
417 {
417 Q_Q(QXYSeries);
418 Q_Q(QXYSeries);
418 QList<LegendMarker*> list;
419 QList<LegendMarker*> list;
419 return list << new XYLegendMarker(q,legend);
420 return list << new XYLegendMarker(q,legend);
420 }
421 }
421
422
422 void QXYSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
423 void QXYSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
423 {
424 {
424 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
425 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
425 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
426 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
426 if (m_mapOrientation == Qt::Vertical) {
427 if (m_mapOrientation == Qt::Vertical) {
427 if ((column == m_mapX || column == m_mapY) // modified item is in a mapped column
428 if ((column == m_mapX || column == m_mapY) // modified item is in a mapped column
428 && row >= m_mapFirst // modfied item in not before first item
429 && row >= m_mapFirst // modfied item in not before first item
429 && (m_mapCount == -1 || row < m_mapFirst + m_mapCount)) // map is not limited or item lays before the end of map
430 && (m_mapCount == -1 || row < m_mapFirst + m_mapCount)) // map is not limited or item lays before the end of map
430 emit pointReplaced(row - m_mapFirst);
431 emit pointReplaced(row - m_mapFirst);
431 } else {
432 } else {
432 if ((row == m_mapX || row == m_mapY) // modified item is in a mapped row
433 if ((row == m_mapX || row == m_mapY) // modified item is in a mapped row
433 && column >= m_mapFirst // modfied item in not before first item
434 && column >= m_mapFirst // modfied item in not before first item
434 && (m_mapCount == -1 || column < m_mapFirst + m_mapCount)) // map is not limited or item lays before the end of map
435 && (m_mapCount == -1 || column < m_mapFirst + m_mapCount)) // map is not limited or item lays before the end of map
435 emit pointReplaced(column - m_mapFirst);
436 emit pointReplaced(column - m_mapFirst);
436 }
437 }
437 }
438 }
438 }
439 }
439 }
440 }
440
441
441
442
442 void QXYSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
443 void QXYSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
443 {
444 {
444 Q_UNUSED(parent);
445 Q_UNUSED(parent);
445 emit pointsAdded(start, end);
446 emit pointsAdded(start, end);
446 }
447 }
447
448
448 void QXYSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
449 void QXYSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
449 {
450 {
450 Q_UNUSED(parent);
451 Q_UNUSED(parent);
451 emit pointsRemoved(start, end);
452 emit pointsRemoved(start, end);
452 }
453 }
453
454
454 #include "moc_qxyseries.cpp"
455 #include "moc_qxyseries.cpp"
455 #include "moc_qxyseries_p.cpp"
456 #include "moc_qxyseries_p.cpp"
456
457
457 QTCOMMERCIALCHART_END_NAMESPACE
458 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,308 +1,325
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 "xychartitem_p.h"
21 #include "xychartitem_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "chartanimator_p.h"
25 #include "chartanimator_p.h"
26 #include <QPainter>
26 #include <QPainter>
27 #include <QGraphicsSceneMouseEvent>
27 #include <QGraphicsSceneMouseEvent>
28 #include <QAbstractItemModel>
28 #include <QAbstractItemModel>
29
29
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 //TODO: optimize : remove points which are not visible
33 //TODO: optimize : remove points which are not visible
34
34
35 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
35 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
36 m_minX(0),
36 m_minX(0),
37 m_maxX(0),
37 m_maxX(0),
38 m_minY(0),
38 m_minY(0),
39 m_maxY(0),
39 m_maxY(0),
40 m_series(series)
40 m_series(series)
41 {
41 {
42 connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
42 connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
43 connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
43 connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
44 connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
44 connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
45 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
45 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
46 connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
46 connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
47 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
47 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
48 }
48 }
49
49
50 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
50 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
51 {
51 {
52 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
52 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
53 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
53 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
54 qreal x = (point.x() - m_minX)* deltaX;
54 qreal x = (point.x() - m_minX)* deltaX;
55 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
55 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
56 return QPointF(x,y);
56 return QPointF(x,y);
57 }
57 }
58
58
59
59
60 QPointF XYChartItem::calculateGeometryPoint(int index) const
60 QPointF XYChartItem::calculateGeometryPoint(int index) const
61 {
61 {
62 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
62 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
63 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
63 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
64 const QList<QPointF>& vector = m_series->points();
64 const QList<QPointF>& vector = m_series->points();
65 qreal x = (vector[index].x() - m_minX)* deltaX;
65 qreal x = (vector[index].x() - m_minX)* deltaX;
66 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
66 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
67 return QPointF(x,y);
67 return QPointF(x,y);
68 }
68 }
69
69
70 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
70 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
71 {
71 {
72 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
72 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
73 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
73 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
74
74
75 QVector<QPointF> result;
75 QVector<QPointF> result;
76 result.resize(m_series->count());
76 result.resize(m_series->count());
77 const QList<QPointF>& vector = m_series->points();
77 const QList<QPointF>& vector = m_series->points();
78 for (int i = 0; i < m_series->count(); ++i) {
78 for (int i = 0; i < m_series->count(); ++i) {
79 qreal x = (vector[i].x() - m_minX)* deltaX;
79 qreal x = (vector[i].x() - m_minX)* deltaX;
80 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
80 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
81 result[i].setX(x);
81 result[i].setX(x);
82 result[i].setY(y);
82 result[i].setY(y);
83 }
83 }
84 return result;
84 return result;
85 }
85 }
86
86
87 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
87 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
88 {
88 {
89 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
89 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
90 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
90 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
91 qreal x = point.x()/deltaX +m_minX;
91 qreal x = point.x()/deltaX +m_minX;
92 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
92 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
93 return QPointF(x,y);
93 return QPointF(x,y);
94 }
94 }
95
95
96 void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
96 void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
97 {
97 {
98 if (animator()) {
98 if (animator()) {
99 animator()->updateLayout(this,oldPoints,newPoints,index);
99 animator()->updateLayout(this,oldPoints,newPoints,index);
100 } else {
100 } else {
101 setLayout(newPoints);
101 setLayout(newPoints);
102 }
102 }
103 }
103 }
104
104
105 void XYChartItem::setLayout(QVector<QPointF> &points)
105 void XYChartItem::setLayout(QVector<QPointF> &points)
106 {
106 {
107 m_points = points;
107 m_points = points;
108 update();
108 update();
109 }
109 }
110
110
111 //handlers
111 //handlers
112
112
113 void XYChartItem::handlePointAdded(int index)
113 void XYChartItem::handlePointAdded(int index)
114 {
114 {
115 if (m_series->model() == 0) {
115 if (m_series->model() == 0) {
116 Q_ASSERT(index<m_series->count());
116 Q_ASSERT(index<m_series->count());
117 Q_ASSERT(index>=0);
117 Q_ASSERT(index>=0);
118 }
118 }
119 QVector<QPointF> points = m_points;
119 QVector<QPointF> points = m_points;
120 QPointF point;
120 QPointF point;
121 point = calculateGeometryPoint(index);
121 point = calculateGeometryPoint(index);
122 points.insert(index, point);
122 points.insert(index, point);
123 updateLayout(m_points, points, index);
123 updateLayout(m_points, points, index);
124 update();
124 update();
125 }
125 }
126
126
127 void XYChartItem::handlePointsAdded(int start, int end)
127 void XYChartItem::handlePointsAdded(int start, int end)
128 {
128 {
129 if (m_series->model() == 0) {
129 if (m_series->model() == 0) {
130 for (int i = start; i <= end; i++)
130 for (int i = start; i <= end; i++)
131 handlePointAdded(i);
131 handlePointAdded(i);
132 } else if (m_series->mapCount() != -1 && start >= m_series->mapFirst() + m_series->mapCount()) {
133 return;
132 } else {
134 } else {
133 // series uses model as a data source
134 int first = m_series->mapFirst();
135 int count = m_series->mapCount();
136 int addedCount = end - start + 1;
135 int addedCount = end - start + 1;
137 if (count != -1 && start >= first + count) {
136 if (m_series->mapCount() != -1 && addedCount > m_series->mapCount())
138 return;
137 addedCount = m_series->mapCount();
138 int first = qMax(start, m_series->mapFirst());
139 int last = qMin(first + addedCount - 1, m_series->mapOrientation() == Qt::Vertical ? m_series->model()->rowCount() - 1 : m_series->model()->columnCount() - 1);
140 for (int i = first; i <= last; i++) {
141 handlePointAdded(i - m_series->mapFirst());
139 }
142 }
140
143 if (m_series->mapCount() != -1 && m_points.size() > m_series->mapCount())
141 // adding items to unlimited map
144 for (int i = m_points.size() - 1; i >= m_series->mapCount(); i--)
142 else if (count == -1 && start >= first) {
145 handlePointRemoved(i);
143 for (int i = start; i <= end; i++)
144 handlePointAdded(i - first);
145 } else if (count == - 1 && start < first) {
146 // not all newly added items
147 for (int i = first; i < first + addedCount; i++)
148 handlePointAdded(i - first);
149 }
146 }
150 // commented out code below does the same thing, but its more confusing.
147
151 // } else if (count == -1) {
148 // else {
152 // int begin = qMax(start, first);
149 // // series uses model as a data source
153 // for (int i = begin; i < begin + (end - start + 1); i++)
150 // int first = m_series->mapFirst();
154 // handlePointAdded(i - first);
151 // int count = m_series->mapCount();
152 // int addedCount = end - start + 1;
153 // if (count != -1 && start >= first + count) {
154 // return;
155 // }
155 // }
156
156
157 // adding items to limited map
157 // // adding items to unlimited map
158 else if (start >= first) {
158 // else if (count == -1 && start >= first) {
159 // remove the items that will no longer fit into the map
159 // for (int i = start; i <= end; i++)
160 // int toRemove = addedCount - (count - points().size());
160 // handlePointAdded(i - first);
161 for (int i = start; i <= end; i++) {
161 // } else if (count == - 1 && start < first) {
162 handlePointAdded(i - first);
162 // // not all newly added items
163 }
163 // for (int i = first; i < first + addedCount; i++)
164 if (m_points.size() > count)
164 // handlePointAdded(i - first);
165 for (int i = m_points.size() - 1; i >= count; i--)
165 // }
166 handlePointRemoved(i);
166 // // commented out code below does the same thing, but its more confusing.
167 // update();
167 // // } else if (count == -1) {
168 } else {
168 // // int begin = qMax(start, first);
169 //
169 // // for (int i = begin; i < begin + (end - start + 1); i++)
170 for (int i = first; i < first + addedCount; i++) {
170 // // handlePointAdded(i - first);
171 handlePointAdded(i - first);
171 // // }
172 }
172
173 if (m_points.size() > count)
173 // // adding items to limited map
174 for (int i = m_points.size() - 1; i >= count; i--)
174 // else if (start >= first) {
175 handlePointRemoved(i);
175 // // remove the items that will no longer fit into the map
176 }
176 // // int toRemove = addedCount - (count - points().size());
177 }
177 // for (int i = start; i <= end; i++) {
178 // handlePointAdded(i - first);
179 // }
180 // if (m_points.size() > count)
181 // for (int i = m_points.size() - 1; i >= count; i--)
182 // handlePointRemoved(i);
183 // // update();
184 // } else {
185 // //
186 // for (int i = first; i < first + addedCount; i++) {
187 // handlePointAdded(i - first);
188 // }
189 // if (m_points.size() > count)
190 // for (int i = m_points.size() - 1; i >= count; i--)
191 // handlePointRemoved(i);
192 // }
193 // }
178 }
194 }
179
195
180 void XYChartItem::handlePointRemoved(int index)
196 void XYChartItem::handlePointRemoved(int index)
181 {
197 {
182 if (m_series->model() == 0) {
198 if (m_series->model() == 0) {
183 Q_ASSERT(index<m_series->count() + 1);
199 Q_ASSERT(index<m_series->count() + 1);
184 Q_ASSERT(index>=0);
200 Q_ASSERT(index>=0);
185 }
201 }
186 QVector<QPointF> points = m_points;
202 QVector<QPointF> points = m_points;
187 points.remove(index);
203 points.remove(index);
188 updateLayout(m_points, points, index);
204 updateLayout(m_points, points, index);
189 update();
205 update();
190 }
206 }
191
207
192 void XYChartItem::handlePointsRemoved(int start, int end)
208 void XYChartItem::handlePointsRemoved(int start, int end)
193 {
209 {
194 Q_UNUSED(start)
210 Q_UNUSED(start)
195 Q_UNUSED(end)
211 Q_UNUSED(end)
196 if (m_series->model() == 0) {
212 if (m_series->model() == 0) {
197 for (int i = end; i >= start; i--)
213 for (int i = end; i >= start; i--)
198 handlePointRemoved(i);
214 handlePointRemoved(i);
199 } else {
215 } else {
200 // series uses model as a data source
216 // series uses model as a data source
201 int first = m_series->mapFirst();
217 int first = m_series->mapFirst();
202 int count = m_series->mapCount();
218 int count = m_series->mapCount();
203 int removedCount = end - start + 1;
219 int removedCount = end - start + 1;
204 if (count != -1 && start >= first + count) {
220 if (count != -1 && start >= first + count) {
205 return;
221 return;
206 }
222 }
207
223
208 // removing items from unlimited map
224 // removing items from unlimited map
209 else if (count == -1 && start >= first) {
225 else if (count == -1 && start >= first) {
210 for (int i = end; i >= start; i--)
226 for (int i = end; i >= start; i--)
211 handlePointRemoved(i - first);
227 handlePointRemoved(i - first);
212 } else if (count == - 1 && start < first) {
228 } else if (count == - 1 && start < first) {
213 // not all removed items
229 // not all removed items
214 for (int i = first + removedCount - 1; i >= first; i--)
230 int lastExisting = qMin(first + m_points.size() - 1, first + removedCount - 1);
231 for (int i = lastExisting; i >= first; i--)
215 handlePointRemoved(i - first);
232 handlePointRemoved(i - first);
216 }
233 }
217
234
218 // removing items from limited map
235 // removing items from limited map
219 else if (start >= first) {
236 else if (start >= first) {
220 //
237 //
221 int lastExisting = qMin(first + m_points.size() - 1, end);
238 int lastExisting = qMin(first + m_points.size() - 1, end);
222 for (int i = lastExisting; i >= start; i--) {
239 for (int i = lastExisting; i >= start; i--) {
223 handlePointRemoved(i - first);
240 handlePointRemoved(i - first);
224 }
241 }
225
242
226 // the map is limited, so after removing the items some new items may have fall within the mapped area
243 // the map is limited, so after removing the items some new items may have fall within the mapped area
227 int itemsAvailable;
244 int itemsAvailable;
228 if (m_series->mapOrientation() == Qt::Vertical)
245 if (m_series->mapOrientation() == Qt::Vertical)
229 itemsAvailable = m_series->model()->rowCount() - first - m_points.size();
246 itemsAvailable = m_series->model()->rowCount() - first - m_points.size();
230 else
247 else
231 itemsAvailable = m_series->model()->columnCount() - first - m_points.size();
248 itemsAvailable = m_series->model()->columnCount() - first - m_points.size();
232 int toBeAdded = qMin(itemsAvailable, count - m_points.size());
249 int toBeAdded = qMin(itemsAvailable, count - m_points.size());
233 int currentSize = m_points.size();
250 int currentSize = m_points.size();
234 if (itemsAvailable > 0)
251 if (itemsAvailable > 0)
235 for (int i = m_points.size(); i < currentSize + toBeAdded; i++)
252 for (int i = m_points.size(); i < currentSize + toBeAdded; i++)
236 handlePointAdded(i);
253 handlePointAdded(i);
237 } else {
254 } else {
238 // first removed item lays before the mapped area
255 // first removed item lays before the mapped area
239 int toRemove = qMin(m_points.size() - 1, removedCount);
256 int toRemove = qMin(m_points.size() - 1, removedCount);
240 for (int i = first; i < first + toRemove; i++)
257 for (int i = first; i < first + toRemove; i++)
241 handlePointRemoved(0);
258 handlePointRemoved(0);
242
259
243 // the map is limited, so after removing the items some new items may have fall into the map
260 // the map is limited, so after removing the items some new items may have fall into the map
244 int itemsAvailable;
261 int itemsAvailable;
245 if (m_series->mapOrientation() == Qt::Vertical)
262 if (m_series->mapOrientation() == Qt::Vertical)
246 itemsAvailable = m_series->model()->rowCount() - first - m_points.size();
263 itemsAvailable = m_series->model()->rowCount() - first - m_points.size();
247 else
264 else
248 itemsAvailable = m_series->model()->columnCount() - first - m_points.size();
265 itemsAvailable = m_series->model()->columnCount() - first - m_points.size();
249 int toBeAdded = qMin(itemsAvailable, count - m_points.size());
266 int toBeAdded = qMin(itemsAvailable, count - m_points.size());
250 int currentSize = m_points.size();
267 int currentSize = m_points.size();
251 if (itemsAvailable > 0)
268 if (itemsAvailable > 0)
252 for (int i = m_points.size(); i < currentSize + toBeAdded; i++)
269 for (int i = m_points.size(); i < currentSize + toBeAdded; i++)
253 handlePointAdded(i);
270 handlePointAdded(i);
254 }
271 }
255 }
272 }
256
273
257 }
274 }
258
275
259 void XYChartItem::handlePointReplaced(int index)
276 void XYChartItem::handlePointReplaced(int index)
260 {
277 {
261 Q_ASSERT(index<m_series->count());
278 Q_ASSERT(index<m_series->count());
262 Q_ASSERT(index>=0);
279 Q_ASSERT(index>=0);
263 QPointF point = calculateGeometryPoint(index);
280 QPointF point = calculateGeometryPoint(index);
264 QVector<QPointF> points = m_points;
281 QVector<QPointF> points = m_points;
265 points.replace(index,point);
282 points.replace(index,point);
266 updateLayout(m_points,points,index);
283 updateLayout(m_points,points,index);
267 update();
284 update();
268 }
285 }
269
286
270 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
287 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
271 {
288 {
272 m_minX=minX;
289 m_minX=minX;
273 m_maxX=maxX;
290 m_maxX=maxX;
274 m_minY=minY;
291 m_minY=minY;
275 m_maxY=maxY;
292 m_maxY=maxY;
276 if (isEmpty()) return;
293 if (isEmpty()) return;
277 QVector<QPointF> points = calculateGeometryPoints();
294 QVector<QPointF> points = calculateGeometryPoints();
278 updateLayout(m_points,points);
295 updateLayout(m_points,points);
279 update();
296 update();
280 }
297 }
281
298
282 void XYChartItem::handleGeometryChanged(const QRectF &rect)
299 void XYChartItem::handleGeometryChanged(const QRectF &rect)
283 {
300 {
284 Q_ASSERT(rect.isValid());
301 Q_ASSERT(rect.isValid());
285 m_size=rect.size();
302 m_size=rect.size();
286 m_clipRect=rect.translated(-rect.topLeft());
303 m_clipRect=rect.translated(-rect.topLeft());
287 setPos(rect.topLeft());
304 setPos(rect.topLeft());
288
305
289 if (isEmpty()) return;
306 if (isEmpty()) return;
290 QVector<QPointF> points = calculateGeometryPoints();
307 QVector<QPointF> points = calculateGeometryPoints();
291 updateLayout(m_points,points);
308 updateLayout(m_points,points);
292 update();
309 update();
293 }
310 }
294
311
295
312
296 bool XYChartItem::isEmpty()
313 bool XYChartItem::isEmpty()
297 {
314 {
298 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY);
315 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY);
299 }
316 }
300
317
301 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
318 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
302 {
319 {
303 emit clicked(calculateDomainPoint(event->pos()));
320 emit clicked(calculateDomainPoint(event->pos()));
304 }
321 }
305
322
306 #include "moc_xychartitem_p.cpp"
323 #include "moc_xychartitem_p.cpp"
307
324
308 QTCOMMERCIALCHART_END_NAMESPACE
325 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,375 +1,375
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 "tablewidget.h"
21 #include "tablewidget.h"
22 #include <QGridLayout>
22 #include <QGridLayout>
23 #include <QTableView>
23 #include <QTableView>
24 #include <QChart>
24 #include <QChart>
25 #include <QStyledItemDelegate>
25 #include <QStyledItemDelegate>
26 #include <QLineSeries>
26 #include <QLineSeries>
27 #include <QSplineSeries>
27 #include <QSplineSeries>
28 #include <QScatterSeries>
28 #include <QScatterSeries>
29 #include "customtablemodel.h"
29 #include "customtablemodel.h"
30 #include <QPieSeries>
30 #include <QPieSeries>
31 #include <QPieSlice>
31 #include <QPieSlice>
32 #include <QAreaSeries>
32 #include <QAreaSeries>
33 #include <QBarSeries>
33 #include <QBarSeries>
34 #include <QBarSet>
34 #include <QBarSet>
35 #include <QPushButton>
35 #include <QPushButton>
36 #include <QRadioButton>
36 #include <QRadioButton>
37 #include <QLabel>
37 #include <QLabel>
38 #include <QSpinBox>
38 #include <QSpinBox>
39 #include <QTime>
39 #include <QTime>
40
40
41 TableWidget::TableWidget(QWidget *parent)
41 TableWidget::TableWidget(QWidget *parent)
42 : QWidget(parent),specialPie(0)
42 : QWidget(parent),specialPie(0)
43 {
43 {
44 setGeometry(1900, 100, 1000, 600);
44 setGeometry(1900, 100, 1000, 600);
45 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
45 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
46 // create simple model for storing data
46 // create simple model for storing data
47 // user's table data model
47 // user's table data model
48 m_model = new CustomTableModel;
48 m_model = new CustomTableModel;
49 m_tableView = new QTableView;
49 m_tableView = new QTableView;
50 m_tableView->setModel(m_model);
50 m_tableView->setModel(m_model);
51 m_tableView->setMinimumHeight(300);
51 m_tableView->setMinimumHeight(300);
52 // tableView->setMinimumSize(340, 480);
52 // tableView->setMinimumSize(340, 480);
53 // tableView->setItemDelegate(new QStyledItemDelegate);
53 // tableView->setItemDelegate(new QStyledItemDelegate);
54 m_chart = new QChart;
54 m_chart = new QChart;
55 m_chart->legend()->setVisible(true);
55 m_chart->legend()->setVisible(true);
56 m_chart->setAnimationOptions(QChart::SeriesAnimations);
56 // m_chart->setAnimationOptions(QChart::SeriesAnimations);
57 m_chartView = new QChartView(m_chart);
57 m_chartView = new QChartView(m_chart);
58 m_chartView->setRenderHint(QPainter::Antialiasing);
58 m_chartView->setRenderHint(QPainter::Antialiasing);
59 m_chartView->setMinimumSize(640, 480);
59 m_chartView->setMinimumSize(640, 480);
60
60
61 // add, remove data buttons
61 // add, remove data buttons
62 QPushButton* addRowAboveButton = new QPushButton("Add row above");
62 QPushButton* addRowAboveButton = new QPushButton("Add row above");
63 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
63 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
64
64
65 QPushButton* addRowBelowButton = new QPushButton("Add row below");
65 QPushButton* addRowBelowButton = new QPushButton("Add row below");
66 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
66 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
67
67
68 QPushButton* removeRowButton = new QPushButton("Remove row");
68 QPushButton* removeRowButton = new QPushButton("Remove row");
69 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
69 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
70
70
71 QPushButton* specialPieButton = new QPushButton("Test pie");
71 QPushButton* specialPieButton = new QPushButton("Test pie");
72 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
72 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
73
73
74
74
75 QLabel *spinBoxLabel = new QLabel("Rows affected:");
75 QLabel *spinBoxLabel = new QLabel("Rows affected:");
76
76
77 // spin box for setting number of affected items (add, remove)
77 // spin box for setting number of affected items (add, remove)
78 m_linesCountSpinBox = new QSpinBox;
78 m_linesCountSpinBox = new QSpinBox;
79 m_linesCountSpinBox->setRange(1, 10);
79 m_linesCountSpinBox->setRange(1, 10);
80 m_linesCountSpinBox->setValue(1);
80 m_linesCountSpinBox->setValue(1);
81
81
82 // buttons layout
82 // buttons layout
83 QVBoxLayout* buttonsLayout = new QVBoxLayout;
83 QVBoxLayout* buttonsLayout = new QVBoxLayout;
84 buttonsLayout->addWidget(spinBoxLabel);
84 buttonsLayout->addWidget(spinBoxLabel);
85 buttonsLayout->addWidget(m_linesCountSpinBox);
85 buttonsLayout->addWidget(m_linesCountSpinBox);
86 buttonsLayout->addWidget(addRowAboveButton);
86 buttonsLayout->addWidget(addRowAboveButton);
87 buttonsLayout->addWidget(addRowBelowButton);
87 buttonsLayout->addWidget(addRowBelowButton);
88 buttonsLayout->addWidget(removeRowButton);
88 buttonsLayout->addWidget(removeRowButton);
89 buttonsLayout->addWidget(specialPieButton);
89 buttonsLayout->addWidget(specialPieButton);
90 buttonsLayout->addStretch();
90 buttonsLayout->addStretch();
91
91
92 // chart type radio buttons
92 // chart type radio buttons
93 m_lineRadioButton = new QRadioButton("Line");
93 m_lineRadioButton = new QRadioButton("Line");
94 m_splineRadioButton = new QRadioButton("Spline");
94 m_splineRadioButton = new QRadioButton("Spline");
95 m_scatterRadioButton = new QRadioButton("Scatter");
95 m_scatterRadioButton = new QRadioButton("Scatter");
96 m_pieRadioButton = new QRadioButton("Pie");
96 m_pieRadioButton = new QRadioButton("Pie");
97 m_areaRadioButton = new QRadioButton("Area");
97 m_areaRadioButton = new QRadioButton("Area");
98 m_barRadioButton = new QRadioButton("Bar");
98 m_barRadioButton = new QRadioButton("Bar");
99
99
100 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
100 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
101 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
101 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
102 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
102 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
103 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
103 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
104 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
104 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
105 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
105 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
106 m_lineRadioButton->setChecked(true);
106 m_lineRadioButton->setChecked(true);
107
107
108 // radio buttons layout
108 // radio buttons layout
109 QVBoxLayout* radioLayout = new QVBoxLayout;
109 QVBoxLayout* radioLayout = new QVBoxLayout;
110 radioLayout->addWidget(m_lineRadioButton);
110 radioLayout->addWidget(m_lineRadioButton);
111 radioLayout->addWidget(m_splineRadioButton);
111 radioLayout->addWidget(m_splineRadioButton);
112 radioLayout->addWidget(m_scatterRadioButton);
112 radioLayout->addWidget(m_scatterRadioButton);
113 radioLayout->addWidget(m_pieRadioButton);
113 radioLayout->addWidget(m_pieRadioButton);
114 radioLayout->addWidget(m_areaRadioButton);
114 radioLayout->addWidget(m_areaRadioButton);
115 radioLayout->addWidget(m_barRadioButton);
115 radioLayout->addWidget(m_barRadioButton);
116 radioLayout->addStretch();
116 radioLayout->addStretch();
117
117
118 // create main layout
118 // create main layout
119 QGridLayout* mainLayout = new QGridLayout;
119 QGridLayout* mainLayout = new QGridLayout;
120 mainLayout->addLayout(buttonsLayout, 1, 1);
120 mainLayout->addLayout(buttonsLayout, 1, 1);
121 mainLayout->addLayout(radioLayout, 2, 1);
121 mainLayout->addLayout(radioLayout, 2, 1);
122 mainLayout->addWidget(m_tableView, 1, 0);
122 mainLayout->addWidget(m_tableView, 1, 0);
123 mainLayout->addWidget(m_chartView, 2, 0);
123 mainLayout->addWidget(m_chartView, 2, 0);
124 setLayout(mainLayout);
124 setLayout(mainLayout);
125 m_lineRadioButton->setFocus();
125 m_lineRadioButton->setFocus();
126 }
126 }
127
127
128 void TableWidget::addRowAbove()
128 void TableWidget::addRowAbove()
129 {
129 {
130 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
130 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
131
131
132 }
132 }
133
133
134 void TableWidget::addRowBelow()
134 void TableWidget::addRowBelow()
135 {
135 {
136 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
136 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
137
137
138 }
138 }
139
139
140 void TableWidget::removeRow()
140 void TableWidget::removeRow()
141 {
141 {
142 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
142 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
143 }
143 }
144
144
145 void TableWidget::updateChartType(bool toggle)
145 void TableWidget::updateChartType(bool toggle)
146 {
146 {
147 // this if is needed, so that the function is only called once.
147 // this if is needed, so that the function is only called once.
148 // For the radioButton that was enabled.
148 // For the radioButton that was enabled.
149 if (toggle) {
149 if (toggle) {
150 specialPie = 0;
150 specialPie = 0;
151 m_chart->removeAllSeries();
151 m_chart->removeAllSeries();
152 m_chart->axisX()->setNiceNumbersEnabled(false);
152 m_chart->axisX()->setNiceNumbersEnabled(false);
153 m_chart->axisY()->setNiceNumbersEnabled(false);
153 m_chart->axisY()->setNiceNumbersEnabled(false);
154
154
155 // renable axes of the chart (pie hides them)
155 // renable axes of the chart (pie hides them)
156 // x axis
156 // x axis
157 QAxis *axis = m_chart->axisX();
157 QAxis *axis = m_chart->axisX();
158 axis->setAxisVisible(true);
158 axis->setAxisVisible(true);
159 axis->setGridLineVisible(true);
159 axis->setGridLineVisible(true);
160 axis->setLabelsVisible(true);
160 axis->setLabelsVisible(true);
161
161
162 // y axis
162 // y axis
163 axis = m_chart->axisY();
163 axis = m_chart->axisY();
164 axis->setAxisVisible(true);
164 axis->setAxisVisible(true);
165 axis->setGridLineVisible(true);
165 axis->setGridLineVisible(true);
166 axis->setLabelsVisible(true);
166 axis->setLabelsVisible(true);
167
167
168 m_model->clearMapping();
168 m_model->clearMapping();
169
169
170 QString seriesColorHex = "#000000";
170 QString seriesColorHex = "#000000";
171 QPen pen;
171 QPen pen;
172 pen.setWidth(2);
172 pen.setWidth(2);
173
173
174 if (m_lineRadioButton->isChecked())
174 if (m_lineRadioButton->isChecked())
175 {
175 {
176 // series 1
176 // series 1
177 m_series = new QLineSeries;
177 m_series = new QLineSeries;
178 m_series->setModel(m_model);
178 m_series->setModel(m_model);
179 m_series->setModelMapping(0,1, Qt::Vertical);
179 m_series->setModelMapping(0,1, Qt::Vertical);
180 m_series->setModelMappingRange(4, 4);
180 m_series->setModelMappingRange(4, 4);
181 m_chart->addSeries(m_series);
181 m_chart->addSeries(m_series);
182 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
182 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
183 m_model->addMapping(seriesColorHex, QRect(0, 4, 2, 4));
183 m_model->addMapping(seriesColorHex, QRect(0, 4, 2, 4));
184
184
185 // series 2
185 // series 2
186 m_series = new QLineSeries;
186 m_series = new QLineSeries;
187 m_series->setModel(m_model);
187 m_series->setModel(m_model);
188 m_series->setModelMapping(2,3, Qt::Vertical);
188 m_series->setModelMapping(2,3, Qt::Vertical);
189 m_chart->addSeries(m_series);
189 m_chart->addSeries(m_series);
190 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
190 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
191 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
191 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
192
192
193 // series 3
193 // series 3
194 m_series = new QLineSeries;
194 m_series = new QLineSeries;
195 m_series->setModel(m_model);
195 m_series->setModel(m_model);
196 m_series->setModelMapping(4,5, Qt::Vertical);
196 m_series->setModelMapping(4,5, Qt::Vertical);
197 m_series->setModelMappingRange(2, -1);
197 m_series->setModelMappingRange(2, -1);
198 m_chart->addSeries(m_series);
198 m_chart->addSeries(m_series);
199 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
199 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
200 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
200 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
201 }
201 }
202 else if (m_splineRadioButton->isChecked())
202 else if (m_splineRadioButton->isChecked())
203 {
203 {
204 // series 1
204 // series 1
205 m_series = new QSplineSeries;
205 m_series = new QSplineSeries;
206 m_series->setModel(m_model);
206 m_series->setModel(m_model);
207 m_series->setModelMapping(0,1, Qt::Vertical);
207 m_series->setModelMapping(0,1, Qt::Vertical);
208 // m_series->setModelMappingRange(1, 4);
208 // m_series->setModelMappingRange(1, 4);
209 // series->setModelMapping(0,1, Qt::Horizontal);
209 // series->setModelMapping(0,1, Qt::Horizontal);
210 m_chart->addSeries(m_series);
210 m_chart->addSeries(m_series);
211 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
211 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
212 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
212 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
213
213
214 // series 2
214 // series 2
215 m_series = new QSplineSeries;
215 m_series = new QSplineSeries;
216 m_series->setModel(m_model);
216 m_series->setModel(m_model);
217 m_series->setModelMapping(2,3, Qt::Vertical);
217 m_series->setModelMapping(2,3, Qt::Vertical);
218 // m_series->setModelMappingRange(0, 0);
218 // m_series->setModelMappingRange(0, 0);
219 // series->setModelMapping(2,3, Qt::Horizontal);
219 // series->setModelMapping(2,3, Qt::Horizontal);
220 m_chart->addSeries(m_series);
220 m_chart->addSeries(m_series);
221 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
221 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
222 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
222 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
223
223
224 // series 3
224 // series 3
225 m_series = new QSplineSeries;
225 m_series = new QSplineSeries;
226 m_series->setModel(m_model);
226 m_series->setModel(m_model);
227 m_series->setModelMapping(4,5, Qt::Vertical);
227 m_series->setModelMapping(4,5, Qt::Vertical);
228 // m_series->setModelMappingRange(2, 0);
228 // m_series->setModelMappingRange(2, 0);
229 // series->setModelMapping(4,5, Qt::Horizontal);
229 // series->setModelMapping(4,5, Qt::Horizontal);
230 m_chart->addSeries(m_series);
230 m_chart->addSeries(m_series);
231 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
231 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
232 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
232 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
233 }
233 }
234 else if (m_scatterRadioButton->isChecked())
234 else if (m_scatterRadioButton->isChecked())
235 {
235 {
236 // series 1
236 // series 1
237 m_series = new QScatterSeries;
237 m_series = new QScatterSeries;
238 m_series->setModel(m_model);
238 m_series->setModel(m_model);
239 m_series->setModelMapping(0,1, Qt::Vertical);
239 m_series->setModelMapping(0,1, Qt::Vertical);
240 // m_series->setModelMappingRange(2, 0);
240 // m_series->setModelMappingRange(2, 0);
241 // series->setModelMapping(0,1, Qt::Horizontal);
241 // series->setModelMapping(0,1, Qt::Horizontal);
242 m_chart->addSeries(m_series);
242 m_chart->addSeries(m_series);
243
243
244 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
244 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
245 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
245 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
246
246
247 // series 2
247 // series 2
248 m_series = new QScatterSeries;
248 m_series = new QScatterSeries;
249 m_series->setModel(m_model);
249 m_series->setModel(m_model);
250 m_series->setModelMapping(2,3, Qt::Vertical);
250 m_series->setModelMapping(2,3, Qt::Vertical);
251 // m_series->setModelMappingRange(1, 6);
251 // m_series->setModelMappingRange(1, 6);
252 // series->setModelMapping(2,3, Qt::Horizontal);
252 // series->setModelMapping(2,3, Qt::Horizontal);
253 m_chart->addSeries(m_series);
253 m_chart->addSeries(m_series);
254
254
255 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
255 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
256 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
256 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
257
257
258 // series 3
258 // series 3
259 m_series = new QScatterSeries;
259 m_series = new QScatterSeries;
260 m_series->setModel(m_model);
260 m_series->setModel(m_model);
261 m_series->setModelMapping(4,5, Qt::Vertical);
261 m_series->setModelMapping(4,5, Qt::Vertical);
262 // series->setModelMapping(4,5, Qt::Horizontal);
262 // series->setModelMapping(4,5, Qt::Horizontal);
263 m_chart->addSeries(m_series);
263 m_chart->addSeries(m_series);
264 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
264 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
265 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
265 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
266 }
266 }
267 else if (m_pieRadioButton->isChecked())
267 else if (m_pieRadioButton->isChecked())
268 {
268 {
269 // pie 1
269 // pie 1
270 QPieSeries* pieSeries = new QPieSeries;
270 QPieSeries* pieSeries = new QPieSeries;
271 pieSeries->setModel(m_model);
271 pieSeries->setModel(m_model);
272 pieSeries->setModelMappingRange(3, 3);
272 pieSeries->setModelMappingRange(3, 3);
273 pieSeries->setModelMapping(0,0, Qt::Vertical);
273 pieSeries->setModelMapping(0,0, Qt::Vertical);
274 pieSeries->setLabelsVisible(true);
274 pieSeries->setLabelsVisible(true);
275 pieSeries->setPieSize(0.35);
275 pieSeries->setPieSize(0.35);
276 pieSeries->setHorizontalPosition(0.2);
276 pieSeries->setHorizontalPosition(0.2);
277 pieSeries->setVerticalPosition(0.3);
277 pieSeries->setVerticalPosition(0.3);
278
278
279 m_chart->addSeries(pieSeries);
279 m_chart->addSeries(pieSeries);
280 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
280 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
281 m_model->addMapping(seriesColorHex, QRect(0, 3, 1, 3));
281 m_model->addMapping(seriesColorHex, QRect(0, 3, 1, 3));
282
282
283 // pie 2
283 // pie 2
284 pieSeries = new QPieSeries;
284 pieSeries = new QPieSeries;
285 pieSeries->setModel(m_model);
285 pieSeries->setModel(m_model);
286 pieSeries->setModelMappingRange(2, -1);
286 pieSeries->setModelMappingRange(2, -1);
287 pieSeries->setModelMapping(1,1, Qt::Vertical);
287 pieSeries->setModelMapping(1,1, Qt::Vertical);
288 pieSeries->setLabelsVisible(true);
288 pieSeries->setLabelsVisible(true);
289 pieSeries->setPieSize(0.35);
289 pieSeries->setPieSize(0.35);
290 pieSeries->setHorizontalPosition(0.8);
290 pieSeries->setHorizontalPosition(0.8);
291 pieSeries->setVerticalPosition(0.3);
291 pieSeries->setVerticalPosition(0.3);
292 m_chart->addSeries(pieSeries);
292 m_chart->addSeries(pieSeries);
293 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
293 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
294 m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
294 m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
295
295
296 // pie 3
296 // pie 3
297 pieSeries = new QPieSeries;
297 pieSeries = new QPieSeries;
298 pieSeries->setModel(m_model);
298 pieSeries->setModel(m_model);
299 pieSeries->setModelMapping(2,2, Qt::Vertical);
299 pieSeries->setModelMapping(2,2, Qt::Vertical);
300 pieSeries->setLabelsVisible(true);
300 pieSeries->setLabelsVisible(true);
301 pieSeries->setPieSize(0.35);
301 pieSeries->setPieSize(0.35);
302 pieSeries->setHorizontalPosition(0.2);
302 pieSeries->setHorizontalPosition(0.2);
303 pieSeries->setVerticalPosition(0.75);
303 pieSeries->setVerticalPosition(0.75);
304 m_chart->addSeries(pieSeries);
304 m_chart->addSeries(pieSeries);
305 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
305 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
306 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
306 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
307
307
308 // special pie
308 // special pie
309 specialPie = new QPieSeries;
309 specialPie = new QPieSeries;
310 specialPie->append(17, "1");
310 specialPie->append(17, "1");
311 specialPie->append(45, "2");
311 specialPie->append(45, "2");
312 specialPie->append(77, "3");
312 specialPie->append(77, "3");
313 specialPie->append(37, "4");
313 specialPie->append(37, "4");
314 specialPie->append(27, "5");
314 specialPie->append(27, "5");
315 specialPie->append(47, "6");
315 specialPie->append(47, "6");
316 specialPie->setPieSize(0.35);
316 specialPie->setPieSize(0.35);
317 specialPie->setHorizontalPosition(0.8);
317 specialPie->setHorizontalPosition(0.8);
318 specialPie->setVerticalPosition(0.75);
318 specialPie->setVerticalPosition(0.75);
319 specialPie->setLabelsVisible(true);
319 specialPie->setLabelsVisible(true);
320 m_chart->addSeries(specialPie);
320 m_chart->addSeries(specialPie);
321 }
321 }
322 else if (m_areaRadioButton->isChecked())
322 else if (m_areaRadioButton->isChecked())
323 {
323 {
324 QLineSeries* upperLineSeries = new QLineSeries;
324 QLineSeries* upperLineSeries = new QLineSeries;
325 upperLineSeries->setModel(m_model);
325 upperLineSeries->setModel(m_model);
326 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
326 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
327 // upperLineSeries->setModelMappingRange(1, 5);
327 // upperLineSeries->setModelMappingRange(1, 5);
328 QLineSeries* lowerLineSeries = new QLineSeries;
328 QLineSeries* lowerLineSeries = new QLineSeries;
329 lowerLineSeries->setModel(m_model);
329 lowerLineSeries->setModel(m_model);
330 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
330 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
331 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
331 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
332 m_chart->addSeries(areaSeries);
332 m_chart->addSeries(areaSeries);
333 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
333 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
334 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
334 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
335 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
335 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
336 }
336 }
337 else if (m_barRadioButton->isChecked())
337 else if (m_barRadioButton->isChecked())
338 {
338 {
339 QBarSeries* barSeries = new QBarSeries(QStringList());
339 QBarSeries* barSeries = new QBarSeries(QStringList());
340 barSeries->setModel(m_model);
340 barSeries->setModel(m_model);
341 // barSeries->setModelMappingRange(2, 5);
341 // barSeries->setModelMappingRange(2, 5);
342 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
342 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
343 m_chart->addSeries(barSeries);
343 m_chart->addSeries(barSeries);
344 QList<QBarSet*> barsets = barSeries->barSets();
344 QList<QBarSet*> barsets = barSeries->barSets();
345 for (int i = 0; i < barsets.count(); i++) {
345 for (int i = 0; i < barsets.count(); i++) {
346 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
346 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
347 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
347 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
348 }
348 }
349 }
349 }
350
350
351
351
352 if (!m_barRadioButton->isChecked())
352 if (!m_barRadioButton->isChecked())
353 m_chart->axisX()->setRange(0, 500);
353 m_chart->axisX()->setRange(0, 500);
354 // m_chart->axisY()->setRange(0, 120);
354 // m_chart->axisY()->setRange(0, 120);
355 m_chart->legend()->setVisible(true);
355 m_chart->legend()->setVisible(true);
356
356
357 // repaint table view colors
357 // repaint table view colors
358 m_tableView->repaint();
358 m_tableView->repaint();
359 m_tableView->setFocus();
359 m_tableView->setFocus();
360 }
360 }
361 }
361 }
362
362
363 void TableWidget::testPie()
363 void TableWidget::testPie()
364 {
364 {
365 if (specialPie) {
365 if (specialPie) {
366 specialPie->remove(specialPie->slices().at(2));
366 specialPie->remove(specialPie->slices().at(2));
367 // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
367 // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
368 specialPie->append(4, "heloo");
368 specialPie->append(4, "heloo");
369 }
369 }
370 }
370 }
371
371
372 TableWidget::~TableWidget()
372 TableWidget::~TableWidget()
373 {
373 {
374
374
375 }
375 }
General Comments 0
You need to be logged in to leave comments. Login now