##// END OF EJS Templates
Model example for the RnD offsite
Marek Rosa -
r1097:431f68143802
parent child
Show More
@@ -1,791 +1,792
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 Q_ASSERT(!d->m_slices.contains(s)); // cannot add same slice twice
147 Q_ASSERT(!d->m_slices.contains(s)); // cannot add same slice twice
148 s->setParent(this);
148 s->setParent(this);
149 d->m_slices << s;
149 d->m_slices << s;
150 }
150 }
151
151
152 d->updateDerivativeData();
152 d->updateDerivativeData();
153
153
154 foreach (QPieSlice* s, slices) {
154 foreach (QPieSlice* s, slices) {
155 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
155 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
156 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
156 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
157 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
157 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
158 }
158 }
159
159
160 emit d->added(slices);
160 emit d->added(slices);
161 }
161 }
162
162
163 /*!
163 /*!
164 Appends a single \a slice to the series.
164 Appends a single \a slice to the series.
165 Slice ownership is passed to the series.
165 Slice ownership is passed to the series.
166 */
166 */
167 void QPieSeries::append(QPieSlice* slice)
167 void QPieSeries::append(QPieSlice* slice)
168 {
168 {
169 append(QList<QPieSlice*>() << slice);
169 append(QList<QPieSlice*>() << slice);
170 }
170 }
171
171
172 /*!
172 /*!
173 Appends a single \a slice to the series and returns a reference to the series.
173 Appends a single \a slice to the series and returns a reference to the series.
174 Slice ownership is passed to the series.
174 Slice ownership is passed to the series.
175 */
175 */
176 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
176 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
177 {
177 {
178 append(slice);
178 append(slice);
179 return *this;
179 return *this;
180 }
180 }
181
181
182
182
183 /*!
183 /*!
184 Appends a single slice to the series with give \a value and \a name.
184 Appends a single slice to the series with give \a value and \a name.
185 Slice ownership is passed to the series.
185 Slice ownership is passed to the series.
186 */
186 */
187 QPieSlice* QPieSeries::append(qreal value, QString name)
187 QPieSlice* QPieSeries::append(qreal value, QString name)
188 {
188 {
189 QPieSlice* slice = new QPieSlice(value, name);
189 QPieSlice* slice = new QPieSlice(value, name);
190 append(slice);
190 append(slice);
191 return slice;
191 return slice;
192 }
192 }
193
193
194 /*!
194 /*!
195 Inserts a single \a slice to the series before the slice at \a index position.
195 Inserts a single \a slice to the series before the slice at \a index position.
196 Slice ownership is passed to the series.
196 Slice ownership is passed to the series.
197 */
197 */
198 void QPieSeries::insert(int index, QPieSlice* slice)
198 void QPieSeries::insert(int index, QPieSlice* slice)
199 {
199 {
200 Q_D(QPieSeries);
200 Q_D(QPieSeries);
201 Q_ASSERT(index <= d->m_slices.count());
201 Q_ASSERT(index <= d->m_slices.count());
202 Q_ASSERT(!d->m_slices.contains(slice)); // cannot add same slice twice
202 Q_ASSERT(!d->m_slices.contains(slice)); // cannot add same slice twice
203 slice->setParent(this);
203 slice->setParent(this);
204 d->m_slices.insert(index, slice);
204 d->m_slices.insert(index, slice);
205
205
206 d->updateDerivativeData();
206 d->updateDerivativeData();
207
207
208 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
208 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
209 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
209 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
210 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
210 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
211
211
212 emit d->added(QList<QPieSlice*>() << slice);
212 emit d->added(QList<QPieSlice*>() << slice);
213 }
213 }
214
214
215 /*!
215 /*!
216 Removes a single \a slice from the series and deletes the slice.
216 Removes a single \a slice from the series and deletes the slice.
217
217
218 Do not reference the pointer after this call.
218 Do not reference the pointer after this call.
219 */
219 */
220 void QPieSeries::remove(QPieSlice* slice)
220 void QPieSeries::remove(QPieSlice* slice)
221 {
221 {
222 Q_D(QPieSeries);
222 Q_D(QPieSeries);
223 if (!d->m_slices.removeOne(slice)) {
223 if (!d->m_slices.removeOne(slice)) {
224 Q_ASSERT(0); // TODO: how should this be reported?
224 Q_ASSERT(0); // TODO: how should this be reported?
225 return;
225 return;
226 }
226 }
227
227
228 d->updateDerivativeData();
228 d->updateDerivativeData();
229
229
230 emit d->removed(QList<QPieSlice*>() << slice);
230 emit d->removed(QList<QPieSlice*>() << slice);
231
231
232 delete slice;
232 delete slice;
233 slice = 0;
233 slice = 0;
234 }
234 }
235
235
236 /*!
236 /*!
237 Clears all slices from the series.
237 Clears all slices from the series.
238 */
238 */
239 void QPieSeries::clear()
239 void QPieSeries::clear()
240 {
240 {
241 Q_D(QPieSeries);
241 Q_D(QPieSeries);
242 if (d->m_slices.count() == 0)
242 if (d->m_slices.count() == 0)
243 return;
243 return;
244
244
245 QList<QPieSlice*> slices = d->m_slices;
245 QList<QPieSlice*> slices = d->m_slices;
246 foreach (QPieSlice* s, d->m_slices) {
246 foreach (QPieSlice* s, d->m_slices) {
247 d->m_slices.removeOne(s);
247 d->m_slices.removeOne(s);
248 delete s;
248 delete s;
249 }
249 }
250
250
251 d->updateDerivativeData();
251 d->updateDerivativeData();
252
252
253 emit d->removed(slices);
253 emit d->removed(slices);
254 }
254 }
255
255
256 /*!
256 /*!
257 returns the number of the slices in this series.
257 returns the number of the slices in this series.
258 */
258 */
259 int QPieSeries::count() const
259 int QPieSeries::count() const
260 {
260 {
261 Q_D(const QPieSeries);
261 Q_D(const QPieSeries);
262 return d->m_slices.count();
262 return d->m_slices.count();
263 }
263 }
264
264
265 /*!
265 /*!
266 Returns true is the series is empty.
266 Returns true is the series is empty.
267 */
267 */
268 bool QPieSeries::isEmpty() const
268 bool QPieSeries::isEmpty() const
269 {
269 {
270 Q_D(const QPieSeries);
270 Q_D(const QPieSeries);
271 return d->m_slices.isEmpty();
271 return d->m_slices.isEmpty();
272 }
272 }
273
273
274 /*!
274 /*!
275 Returns a list of slices that belong to this series.
275 Returns a list of slices that belong to this series.
276 */
276 */
277 QList<QPieSlice*> QPieSeries::slices() const
277 QList<QPieSlice*> QPieSeries::slices() const
278 {
278 {
279 Q_D(const QPieSeries);
279 Q_D(const QPieSeries);
280 return d->m_slices;
280 return d->m_slices;
281 }
281 }
282
282
283 void QPieSeries::setHorizontalPosition(qreal relativePosition)
283 void QPieSeries::setHorizontalPosition(qreal relativePosition)
284 {
284 {
285 Q_D(QPieSeries);
285 Q_D(QPieSeries);
286 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
286 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
287 emit d->piePositionChanged();
287 emit d->piePositionChanged();
288 }
288 }
289
289
290 void QPieSeries::setVerticalPosition(qreal relativePosition)
290 void QPieSeries::setVerticalPosition(qreal relativePosition)
291 {
291 {
292 Q_D(QPieSeries);
292 Q_D(QPieSeries);
293 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
293 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
294 emit d->piePositionChanged();
294 emit d->piePositionChanged();
295 }
295 }
296
296
297 qreal QPieSeries::horizontalPosition() const
297 qreal QPieSeries::horizontalPosition() const
298 {
298 {
299 Q_D(const QPieSeries);
299 Q_D(const QPieSeries);
300 return d->m_pieRelativeHorPos;
300 return d->m_pieRelativeHorPos;
301 }
301 }
302
302
303 qreal QPieSeries::verticalPosition() const
303 qreal QPieSeries::verticalPosition() const
304 {
304 {
305 Q_D(const QPieSeries);
305 Q_D(const QPieSeries);
306 return d->m_pieRelativeVerPos;
306 return d->m_pieRelativeVerPos;
307 }
307 }
308
308
309 void QPieSeries::setPieSize(qreal relativeSize)
309 void QPieSeries::setPieSize(qreal relativeSize)
310 {
310 {
311 Q_D(QPieSeries);
311 Q_D(QPieSeries);
312 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
312 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
313 emit d->pieSizeChanged();
313 emit d->pieSizeChanged();
314 }
314 }
315
315
316 qreal QPieSeries::pieSize() const
316 qreal QPieSeries::pieSize() const
317 {
317 {
318 Q_D(const QPieSeries);
318 Q_D(const QPieSeries);
319 return d->m_pieRelativeSize;
319 return d->m_pieRelativeSize;
320 }
320 }
321
321
322
322
323 void QPieSeries::setPieStartAngle(qreal angle)
323 void QPieSeries::setPieStartAngle(qreal angle)
324 {
324 {
325 Q_D(QPieSeries);
325 Q_D(QPieSeries);
326 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
326 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
327 d->updateDerivativeData();
327 d->updateDerivativeData();
328 }
328 }
329
329
330 qreal QPieSeries::pieStartAngle() const
330 qreal QPieSeries::pieStartAngle() const
331 {
331 {
332 Q_D(const QPieSeries);
332 Q_D(const QPieSeries);
333 return d->m_pieStartAngle;
333 return d->m_pieStartAngle;
334 }
334 }
335
335
336 /*!
336 /*!
337 Sets the end angle of the pie.
337 Sets the end angle of the pie.
338
338
339 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
339 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
340
340
341 \a angle must be greater than start angle.
341 \a angle must be greater than start angle.
342
342
343 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
343 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
344 */
344 */
345 void QPieSeries::setPieEndAngle(qreal angle)
345 void QPieSeries::setPieEndAngle(qreal angle)
346 {
346 {
347 Q_D(QPieSeries);
347 Q_D(QPieSeries);
348
348
349 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
349 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
350 d->updateDerivativeData();
350 d->updateDerivativeData();
351 }
351 }
352
352
353 /*!
353 /*!
354 Returns the end angle of the pie.
354 Returns the end angle of the pie.
355
355
356 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
356 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
357
357
358 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
358 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
359 */
359 */
360 qreal QPieSeries::pieEndAngle() const
360 qreal QPieSeries::pieEndAngle() const
361 {
361 {
362 Q_D(const QPieSeries);
362 Q_D(const QPieSeries);
363 return d->m_pieEndAngle;
363 return d->m_pieEndAngle;
364 }
364 }
365
365
366 /*!
366 /*!
367 Sets the all the slice labels \a visible or invisible.
367 Sets the all the slice labels \a visible or invisible.
368
368
369 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
369 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
370 */
370 */
371 void QPieSeries::setLabelsVisible(bool visible)
371 void QPieSeries::setLabelsVisible(bool visible)
372 {
372 {
373 Q_D(QPieSeries);
373 Q_D(QPieSeries);
374 foreach (QPieSlice* s, d->m_slices)
374 foreach (QPieSlice* s, d->m_slices)
375 s->setLabelVisible(visible);
375 s->setLabelVisible(visible);
376 }
376 }
377
377
378 /*!
378 /*!
379 Returns the sum of all slice values in this series.
379 Returns the sum of all slice values in this series.
380
380
381 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
381 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
382 */
382 */
383 qreal QPieSeries::sum() const
383 qreal QPieSeries::sum() const
384 {
384 {
385 Q_D(const QPieSeries);
385 Q_D(const QPieSeries);
386 return d->m_sum;
386 return d->m_sum;
387 }
387 }
388
388
389 /*!
389 /*!
390 \fn void QPieSeries::clicked(QPieSlice* slice)
390 \fn void QPieSeries::clicked(QPieSlice* slice)
391
391
392 This signal is emitted when a \a slice has been clicked.
392 This signal is emitted when a \a slice has been clicked.
393
393
394 \sa QPieSlice::clicked()
394 \sa QPieSlice::clicked()
395 */
395 */
396
396
397 /*!
397 /*!
398 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
398 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
399
399
400 This signal is emitted when user has hovered over or away from the \a slice.
400 This signal is emitted when user has hovered over or away from the \a slice.
401
401
402 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
402 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
403
403
404 \sa QPieSlice::hovered()
404 \sa QPieSlice::hovered()
405 */
405 */
406
406
407 /*!
407 /*!
408 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
408 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
409 Sets the \a model to be used as a data source
409 Sets the \a model to be used as a data source
410 */
410 */
411 bool QPieSeries::setModel(QAbstractItemModel* model)
411 bool QPieSeries::setModel(QAbstractItemModel* model)
412 {
412 {
413 Q_D(QPieSeries);
413 Q_D(QPieSeries);
414 // disconnect signals from old model
414 // disconnect signals from old model
415 if(d->m_model)
415 if(d->m_model)
416 {
416 {
417 disconnect(d->m_model, 0, this, 0);
417 disconnect(d->m_model, 0, this, 0);
418 d->m_mapValues = -1;
418 d->m_mapValues = -1;
419 d->m_mapLabels = -1;
419 d->m_mapLabels = -1;
420 d->m_mapOrientation = Qt::Vertical;
420 d->m_mapOrientation = Qt::Vertical;
421 }
421 }
422
422
423 // set new model
423 // set new model
424 if(model)
424 if(model)
425 {
425 {
426 d->m_model = model;
426 d->m_model = model;
427 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
427 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
428 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
428 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
429 connect(d->m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
429 connect(d->m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
430 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
430 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
431 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
431 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
432 return true;
432 return true;
433 }
433 }
434 else
434 else
435 {
435 {
436 d->m_model = 0;
436 d->m_model = 0;
437 return false;
437 return false;
438 }
438 }
439 }
439 }
440
440
441 /*!
441 /*!
442 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
442 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
443 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
443 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
444 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
444 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
445 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
445 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
446 The \a orientation parameter specifies whether the data is in columns or in rows.
446 The \a orientation parameter specifies whether the data is in columns or in rows.
447 */
447 */
448 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
448 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
449 {
449 {
450 Q_D(QPieSeries);
450 Q_D(QPieSeries);
451
451
452 if (d->m_model == 0)
452 if (d->m_model == 0)
453 return;
453 return;
454
454
455 d->m_mapValues = modelValuesLine;
455 d->m_mapValues = modelValuesLine;
456 d->m_mapLabels = modelLabelsLine;
456 d->m_mapLabels = modelLabelsLine;
457 d->m_mapOrientation = orientation;
457 d->m_mapOrientation = orientation;
458
458
459 d->initializePieFromModel();
459 d->initializePieFromModel();
460 // // connect the signals
460 // // connect the signals
461 // connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
461 // connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
462
462
463
463
464 // // create the initial slices set
464 // // create the initial slices set
465 // if (d->m_mapOrientation == Qt::Vertical) {
465 // if (d->m_mapOrientation == Qt::Vertical) {
466 // for (int i = 0; i < d->m_model->rowCount(); i++)
466 // for (int i = 0; i < d->m_model->rowCount(); i++)
467 // 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());
467 // 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());
468 // } else {
468 // } else {
469 // for (int i = 0; i < d->m_model->columnCount(); i++)
469 // for (int i = 0; i < d->m_model->columnCount(); i++)
470 // 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());
470 // 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());
471 // }
471 // }
472 }
472 }
473
473
474 void QPieSeries::setModelMappingRange(int first, int count)
474 void QPieSeries::setModelMappingRange(int first, int count)
475 {
475 {
476 Q_D(QPieSeries);
476 Q_D(QPieSeries);
477 d->m_mapFirst = first;
477 d->m_mapFirst = first;
478 d->m_mapCount = count;
478 d->m_mapCount = count;
479 d->initializePieFromModel();
479 }
480 }
480
481
481 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
482 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
482
483
483
484
484 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
485 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
485 QAbstractSeriesPrivate(parent),
486 QAbstractSeriesPrivate(parent),
486 m_pieRelativeHorPos(0.5),
487 m_pieRelativeHorPos(0.5),
487 m_pieRelativeVerPos(0.5),
488 m_pieRelativeVerPos(0.5),
488 m_pieRelativeSize(0.7),
489 m_pieRelativeSize(0.7),
489 m_pieStartAngle(0),
490 m_pieStartAngle(0),
490 m_pieEndAngle(360),
491 m_pieEndAngle(360),
491 m_sum(0),
492 m_sum(0),
492 m_mapValues(0),
493 m_mapValues(0),
493 m_mapLabels(0)
494 m_mapLabels(0)
494 {
495 {
495
496
496 }
497 }
497
498
498 QPieSeriesPrivate::~QPieSeriesPrivate()
499 QPieSeriesPrivate::~QPieSeriesPrivate()
499 {
500 {
500
501
501 }
502 }
502
503
503 void QPieSeriesPrivate::updateDerivativeData()
504 void QPieSeriesPrivate::updateDerivativeData()
504 {
505 {
505 m_sum = 0;
506 m_sum = 0;
506
507
507 // nothing to do?
508 // nothing to do?
508 if (m_slices.count() == 0)
509 if (m_slices.count() == 0)
509 return;
510 return;
510
511
511 // calculate sum of all slices
512 // calculate sum of all slices
512 foreach (QPieSlice* s, m_slices)
513 foreach (QPieSlice* s, m_slices)
513 m_sum += s->value();
514 m_sum += s->value();
514
515
515 // nothing to show..
516 // nothing to show..
516 if (qFuzzyIsNull(m_sum))
517 if (qFuzzyIsNull(m_sum))
517 return;
518 return;
518
519
519 // update slice attributes
520 // update slice attributes
520 qreal sliceAngle = m_pieStartAngle;
521 qreal sliceAngle = m_pieStartAngle;
521 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
522 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
522 QVector<QPieSlice*> changed;
523 QVector<QPieSlice*> changed;
523 foreach (QPieSlice* s, m_slices) {
524 foreach (QPieSlice* s, m_slices) {
524
525
525 PieSliceData data = PieSliceData::data(s);
526 PieSliceData data = PieSliceData::data(s);
526 data.m_percentage = s->value() / m_sum;
527 data.m_percentage = s->value() / m_sum;
527 data.m_angleSpan = pieSpan * data.m_percentage;
528 data.m_angleSpan = pieSpan * data.m_percentage;
528 data.m_startAngle = sliceAngle;
529 data.m_startAngle = sliceAngle;
529 sliceAngle += data.m_angleSpan;
530 sliceAngle += data.m_angleSpan;
530
531
531 if (PieSliceData::data(s) != data) {
532 if (PieSliceData::data(s) != data) {
532 PieSliceData::data(s) = data;
533 PieSliceData::data(s) = data;
533 changed << s;
534 changed << s;
534 }
535 }
535 }
536 }
536
537
537 // emit signals
538 // emit signals
538 foreach (QPieSlice* s, changed)
539 foreach (QPieSlice* s, changed)
539 PieSliceData::data(s).emitChangedSignal(s);
540 PieSliceData::data(s).emitChangedSignal(s);
540 }
541 }
541
542
542 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
543 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
543 {
544 {
544 return series.d_func();
545 return series.d_func();
545 }
546 }
546
547
547 void QPieSeriesPrivate::sliceChanged()
548 void QPieSeriesPrivate::sliceChanged()
548 {
549 {
549 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
550 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
550 updateDerivativeData();
551 updateDerivativeData();
551 }
552 }
552
553
553 void QPieSeriesPrivate::sliceClicked()
554 void QPieSeriesPrivate::sliceClicked()
554 {
555 {
555 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
556 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
556 Q_ASSERT(m_slices.contains(slice));
557 Q_ASSERT(m_slices.contains(slice));
557 Q_Q(QPieSeries);
558 Q_Q(QPieSeries);
558 emit q->clicked(slice);
559 emit q->clicked(slice);
559 }
560 }
560
561
561 void QPieSeriesPrivate::sliceHovered(bool state)
562 void QPieSeriesPrivate::sliceHovered(bool state)
562 {
563 {
563 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
564 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
564 Q_ASSERT(m_slices.contains(slice));
565 Q_ASSERT(m_slices.contains(slice));
565 Q_Q(QPieSeries);
566 Q_Q(QPieSeries);
566 emit q->hovered(slice, state);
567 emit q->hovered(slice, state);
567 }
568 }
568
569
569 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
570 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
570 {
571 {
571 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
572 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
572 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
573 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
573 if (m_mapOrientation == Qt::Vertical)
574 if (m_mapOrientation == Qt::Vertical)
574 {
575 {
575 if ( topLeft.row() >= m_mapFirst && (m_mapCount == - 1 || topLeft.row() < m_mapFirst + m_mapCount)) {
576 if ( topLeft.row() >= m_mapFirst && (m_mapCount == - 1 || topLeft.row() < m_mapFirst + m_mapCount)) {
576 if (topLeft.column() == m_mapValues)
577 if (topLeft.column() == m_mapValues)
577 m_slices.at(topLeft.row() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
578 m_slices.at(topLeft.row() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
578 if (topLeft.column() == m_mapLabels)
579 if (topLeft.column() == m_mapLabels)
579 m_slices.at(topLeft.row() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
580 m_slices.at(topLeft.row() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
580 }
581 }
581 }
582 }
582 else
583 else
583 {
584 {
584 if (topLeft.column() >= m_mapFirst && (m_mapCount == - 1 || topLeft.column() < m_mapFirst + m_mapCount)) {
585 if (topLeft.column() >= m_mapFirst && (m_mapCount == - 1 || topLeft.column() < m_mapFirst + m_mapCount)) {
585 if (topLeft.row() == m_mapValues)
586 if (topLeft.row() == m_mapValues)
586 m_slices.at(topLeft.column() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
587 m_slices.at(topLeft.column() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
587 if (topLeft.row() == m_mapLabels)
588 if (topLeft.row() == m_mapLabels)
588 m_slices.at(topLeft.column() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
589 m_slices.at(topLeft.column() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
589 }
590 }
590 }
591 }
591 }
592 }
592 }
593 }
593 }
594 }
594
595
595
596
596 void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
597 void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
597 {
598 {
598 Q_UNUSED(parent);
599 Q_UNUSED(parent);
599 if (m_mapOrientation == Qt::Vertical)
600 if (m_mapOrientation == Qt::Vertical)
600 insertData(start, end);
601 insertData(start, end);
601 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
602 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
602 initializePieFromModel();
603 initializePieFromModel();
603 }
604 }
604
605
605 void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
606 void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
606 {
607 {
607 Q_UNUSED(parent);
608 Q_UNUSED(parent);
608 if (m_mapOrientation == Qt::Vertical)
609 if (m_mapOrientation == Qt::Vertical)
609 removeData(start, end);
610 removeData(start, end);
610 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
611 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
611 initializePieFromModel();
612 initializePieFromModel();
612 }
613 }
613
614
614 void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
615 void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
615 {
616 {
616 Q_UNUSED(parent);
617 Q_UNUSED(parent);
617 if (m_mapOrientation == Qt::Horizontal)
618 if (m_mapOrientation == Qt::Horizontal)
618 insertData(start, end);
619 insertData(start, end);
619 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
620 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
620 initializePieFromModel();
621 initializePieFromModel();
621 }
622 }
622
623
623 void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
624 void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
624 {
625 {
625 Q_UNUSED(parent);
626 Q_UNUSED(parent);
626 if (m_mapOrientation == Qt::Horizontal)
627 if (m_mapOrientation == Qt::Horizontal)
627 removeData(start, end);
628 removeData(start, end);
628 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
629 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
629 initializePieFromModel();
630 initializePieFromModel();
630 }
631 }
631
632
632 void QPieSeriesPrivate::insertData(int start, int end)
633 void QPieSeriesPrivate::insertData(int start, int end)
633 {
634 {
634 Q_Q(QPieSeries);
635 Q_Q(QPieSeries);
635 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
636 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
636 return;
637 return;
637 } else {
638 } else {
638 int addedCount = end - start + 1;
639 int addedCount = end - start + 1;
639 if (m_mapCount != -1 && addedCount > m_mapCount)
640 if (m_mapCount != -1 && addedCount > m_mapCount)
640 addedCount = m_mapCount;
641 addedCount = m_mapCount;
641 int first = qMax(start, m_mapFirst);
642 int first = qMax(start, m_mapFirst);
642 int last = qMin(first + addedCount - 1, m_mapOrientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
643 int last = qMin(first + addedCount - 1, m_mapOrientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
643 for (int i = first; i <= last; i++) {
644 for (int i = first; i <= last; i++) {
644 QPieSlice *slice = new QPieSlice;
645 QPieSlice *slice = new QPieSlice;
645 if (m_mapOrientation == Qt::Vertical) {
646 if (m_mapOrientation == Qt::Vertical) {
646 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());
647 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());
648 } else {
649 } else {
649 slice->setValue(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble());
650 slice->setValue(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble());
650 slice->setLabel(m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
651 slice->setLabel(m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
651 }
652 }
652 slice->setLabelVisible();
653 slice->setLabelVisible();
653 q->insert(i - m_mapFirst, slice);
654 q->insert(i - m_mapFirst, slice);
654 }
655 }
655 if (m_mapCount != -1 && m_slices.size() > m_mapCount)
656 if (m_mapCount != -1 && m_slices.size() > m_mapCount)
656 for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
657 for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
657 q->remove(q->slices().at(i));
658 q->remove(q->slices().at(i));
658 }
659 }
659 }
660 }
660
661
661 void QPieSeriesPrivate::removeData(int start, int end)
662 void QPieSeriesPrivate::removeData(int start, int end)
662 {
663 {
663 Q_Q(QPieSeries);
664 Q_Q(QPieSeries);
664 int removedCount = end - start + 1;
665 int removedCount = end - start + 1;
665 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
666 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
666 return;
667 return;
667 } else {
668 } else {
668 int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed
669 int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed
669 int first = qMax(start, m_mapFirst); // get the index of the first item that will be removed.
670 int first = qMax(start, m_mapFirst); // get the index of the first item that will be removed.
670 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapFirst - 1); // get the index of the last item that will be removed.
671 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapFirst - 1); // get the index of the last item that will be removed.
671 for (int i = last; i >= first; i--)
672 for (int i = last; i >= first; i--)
672 q->remove(q->slices().at(i - m_mapFirst));
673 q->remove(q->slices().at(i - m_mapFirst));
673
674
674 if (m_mapCount != -1) {
675 if (m_mapCount != -1) {
675 int itemsAvailable; // check how many are available to be added
676 int itemsAvailable; // check how many are available to be added
676 if (m_mapOrientation == Qt::Vertical)
677 if (m_mapOrientation == Qt::Vertical)
677 itemsAvailable = m_model->rowCount() - m_mapFirst - m_slices.size();
678 itemsAvailable = m_model->rowCount() - m_mapFirst - m_slices.size();
678 else
679 else
679 itemsAvailable = m_model->columnCount() - m_mapFirst - m_slices.size();
680 itemsAvailable = m_model->columnCount() - m_mapFirst - m_slices.size();
680 int toBeAdded = qMin(itemsAvailable, m_mapCount - m_slices.size()); // add not more items than there is space left to be filled.
681 int toBeAdded = qMin(itemsAvailable, m_mapCount - m_slices.size()); // add not more items than there is space left to be filled.
681 int currentSize = m_slices.size();
682 int currentSize = m_slices.size();
682 if (toBeAdded > 0)
683 if (toBeAdded > 0)
683 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
684 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
684 QPieSlice *slice = new QPieSlice;
685 QPieSlice *slice = new QPieSlice;
685 if (m_mapOrientation == Qt::Vertical) {
686 if (m_mapOrientation == Qt::Vertical) {
686 slice->setValue(m_model->data(m_model->index(i + m_mapFirst, m_mapValues), Qt::DisplayRole).toDouble());
687 slice->setValue(m_model->data(m_model->index(i + m_mapFirst, m_mapValues), Qt::DisplayRole).toDouble());
687 slice->setLabel(m_model->data(m_model->index(i + m_mapFirst, m_mapLabels), Qt::DisplayRole).toString());
688 slice->setLabel(m_model->data(m_model->index(i + m_mapFirst, m_mapLabels), Qt::DisplayRole).toString());
688 } else {
689 } else {
689 slice->setValue(m_model->data(m_model->index(m_mapValues, i + m_mapFirst), Qt::DisplayRole).toDouble());
690 slice->setValue(m_model->data(m_model->index(m_mapValues, i + m_mapFirst), Qt::DisplayRole).toDouble());
690 slice->setLabel(m_model->data(m_model->index(m_mapLabels, i + m_mapFirst), Qt::DisplayRole).toString());
691 slice->setLabel(m_model->data(m_model->index(m_mapLabels, i + m_mapFirst), Qt::DisplayRole).toString());
691 }
692 }
692 slice->setLabelVisible();
693 slice->setLabelVisible();
693 q->insert(i, slice);
694 q->insert(i, slice);
694 }
695 }
695 }
696 }
696 }
697 }
697 }
698 }
698
699
699 void QPieSeriesPrivate::initializePieFromModel()
700 void QPieSeriesPrivate::initializePieFromModel()
700 {
701 {
701 Q_Q(QPieSeries);
702 Q_Q(QPieSeries);
702 // clear current content
703 // clear current content
703 q->clear();
704 q->clear();
704
705
705 // create the initial slices set
706 // create the initial slices set
706 if (m_mapOrientation == Qt::Vertical) {
707 if (m_mapOrientation == Qt::Vertical) {
707 if (m_mapValues >= m_model->columnCount() || m_mapLabels >= m_model->columnCount())
708 if (m_mapValues >= m_model->columnCount() || m_mapLabels >= m_model->columnCount())
708 return; // mapped columns are not existing
709 return; // mapped columns are not existing
709
710
710 int sliceCount = 0;
711 int sliceCount = 0;
711 if(m_mapCount == -1)
712 if(m_mapCount == -1)
712 sliceCount = m_model->rowCount() - m_mapFirst;
713 sliceCount = m_model->rowCount() - m_mapFirst;
713 else
714 else
714 sliceCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
715 sliceCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
715 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
716 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
716 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());
717 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());
717 } else {
718 } else {
718 if (m_mapValues >= m_model->rowCount() || m_mapLabels >= m_model->rowCount())
719 if (m_mapValues >= m_model->rowCount() || m_mapLabels >= m_model->rowCount())
719 return; // mapped columns are not existing
720 return; // mapped columns are not existing
720
721
721 int sliceCount = 0;
722 int sliceCount = 0;
722 if(m_mapCount == -1)
723 if(m_mapCount == -1)
723 sliceCount = m_model->columnCount() - m_mapFirst;
724 sliceCount = m_model->columnCount() - m_mapFirst;
724 else
725 else
725 sliceCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
726 sliceCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
726 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
727 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
727 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());
728 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());
728 }
729 }
729 q->setLabelsVisible(true);
730 q->setLabelsVisible(true);
730 }
731 }
731
732
732 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
733 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
733 {
734 {
734 // Remove rounding errors
735 // Remove rounding errors
735 qreal roundedValue = newValue;
736 qreal roundedValue = newValue;
736 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
737 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
737 roundedValue = 0.0;
738 roundedValue = 0.0;
738 else if (qFuzzyCompare(newValue, max))
739 else if (qFuzzyCompare(newValue, max))
739 roundedValue = max;
740 roundedValue = max;
740 else if (qFuzzyCompare(newValue, min))
741 else if (qFuzzyCompare(newValue, min))
741 roundedValue = min;
742 roundedValue = min;
742
743
743 // Check if the position is valid after removing the rounding errors
744 // Check if the position is valid after removing the rounding errors
744 if (roundedValue < min || roundedValue > max) {
745 if (roundedValue < min || roundedValue > max) {
745 qWarning("QPieSeries: Illegal value");
746 qWarning("QPieSeries: Illegal value");
746 return false;
747 return false;
747 }
748 }
748
749
749 if (!qFuzzyIsNull(value - roundedValue)) {
750 if (!qFuzzyIsNull(value - roundedValue)) {
750 value = roundedValue;
751 value = roundedValue;
751 return true;
752 return true;
752 }
753 }
753
754
754 // The change was so small it is considered a rounding error
755 // The change was so small it is considered a rounding error
755 return false;
756 return false;
756 }
757 }
757
758
758 void QPieSeriesPrivate::scaleDomain(Domain& domain)
759 void QPieSeriesPrivate::scaleDomain(Domain& domain)
759 {
760 {
760 Q_UNUSED(domain);
761 Q_UNUSED(domain);
761 #ifndef QT_NO_DEBUG
762 #ifndef QT_NO_DEBUG
762 qWarning() << __FILE__<<__FUNCTION__<<"not implemented";
763 qWarning() << __FILE__<<__FUNCTION__<<"not implemented";
763 #endif
764 #endif
764 }
765 }
765
766
766 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
767 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
767 {
768 {
768 Q_Q(QPieSeries);
769 Q_Q(QPieSeries);
769 PieChartItem* pie = new PieChartItem(q,presenter);
770 PieChartItem* pie = new PieChartItem(q,presenter);
770 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
771 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
771 presenter->animator()->addAnimation(pie);
772 presenter->animator()->addAnimation(pie);
772 }
773 }
773 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
774 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
774 return pie;
775 return pie;
775 }
776 }
776
777
777 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
778 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
778 {
779 {
779 Q_Q(QPieSeries);
780 Q_Q(QPieSeries);
780 QList<LegendMarker*> markers;
781 QList<LegendMarker*> markers;
781 foreach(QPieSlice* slice, q->slices()) {
782 foreach(QPieSlice* slice, q->slices()) {
782 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
783 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
783 markers << marker;
784 markers << marker;
784 }
785 }
785 return markers;
786 return markers;
786 }
787 }
787
788
788 #include "moc_qpieseries.cpp"
789 #include "moc_qpieseries.cpp"
789 #include "moc_qpieseries_p.cpp"
790 #include "moc_qpieseries_p.cpp"
790
791
791 QTCOMMERCIALCHART_END_NAMESPACE
792 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,262 +1,276
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 "customtablemodel.h"
21 #include "customtablemodel.h"
22 #include <QVector>
22 #include <QVector>
23 #include <QTime>
23 #include <QTime>
24 #include <QRect>
24 #include <QRect>
25 #include <QColor>
25 #include <QColor>
26
26
27 CustomTableModel::CustomTableModel(QObject *parent) :
27 CustomTableModel::CustomTableModel(QObject *parent) :
28 QAbstractTableModel(parent)
28 QAbstractTableModel(parent)
29 {
29 {
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
31
31
32 m_columnCount = 6;
32 m_columnCount = 3;
33 m_rowCount = 9;
33 m_rowCount = 9;
34
34
35 m_labels.append("Apples");
36 m_labels.append("Oranges");
37 m_labels.append("Pears");
38 m_labels.append("Peaches");
39 m_labels.append("Coconuts");
40 m_labels.append("Bananas");
41 m_labels.append("Kiwis");
42 m_labels.append("Grapes");
43 m_labels.append("Plums");
44
35 // m_data
45 // m_data
36 for (int i = 0; i < m_rowCount; i++)
46 for (int i = 0; i < m_rowCount; i++)
37 {
47 {
38 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
48 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
39 for (int k = 0; k < dataVec->size(); k++)
49 for (int k = 0; k < dataVec->size(); k++)
40 {
50 {
41 if (k%2 == 0)
51 if (k%2 == 0)
42 dataVec->replace(k, i * 50 + qrand()%20);
52 dataVec->replace(k, i * 50 + qrand()%20);
43 else
53 else
44 dataVec->replace(k, qrand()%100);
54 dataVec->replace(k, qrand()%100);
45 }
55 }
46 m_data.append(dataVec);
56 m_data.append(dataVec);
47 m_labels.append(QString("Row: %1").arg((i + 1)));
57 // m_labels.append(QString("Row: %1").arg((i + 1)));
48 }
58 }
49 }
59 }
50
60
51 int CustomTableModel::rowCount(const QModelIndex & parent) const
61 int CustomTableModel::rowCount(const QModelIndex & parent) const
52 {
62 {
53 Q_UNUSED(parent)
63 Q_UNUSED(parent)
54 return m_data.count();
64 return m_data.count();
55 }
65 }
56
66
57 int CustomTableModel::columnCount(const QModelIndex & parent) const
67 int CustomTableModel::columnCount(const QModelIndex & parent) const
58 {
68 {
59 Q_UNUSED(parent)
69 Q_UNUSED(parent)
60 return m_columnCount;
70 return m_columnCount;
61 }
71 }
62
72
63 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
73 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
64 {
74 {
65 if (role != Qt::DisplayRole)
75 if (role != Qt::DisplayRole)
66 return QVariant();
76 return QVariant();
67
77
68 if (orientation == Qt::Horizontal)
78 if (orientation == Qt::Horizontal)
69 {
79 {
70 switch(section)
80 switch(section)
71 {
81 {
72 // case 6:
82 case 0:
73 // return "Fruit";
83 return "Fruit";
84 case 1:
85 return "Count";
86 case 2:
87 return "Ordered";
74 default:
88 default:
75 if (section%2 == 0)
89 if (section%2 == 0)
76 return "x";
90 return "x";
77 else
91 else
78 return "y";
92 return "y";
79 }
93 }
80 }
94 }
81 else
95 else
82 return QString("%1").arg(section /*+ 1*/);
96 return QString("%1").arg(section /*+ 1*/);
83 }
97 }
84
98
85 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
99 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
86 {
100 {
87 if (role == Qt::DisplayRole)
101 if (role == Qt::DisplayRole)
88 {
102 {
89 switch(index.column())
103 switch(index.column())
90 {
104 {
91 // case 6:
105 case 0:
92 // return m_labels[index.row()];
106 return m_labels[index.row()];
93 default:
107 default:
94 return m_data[index.row()]->at(index.column());
108 return m_data[index.row()]->at(index.column());
95 break;
109 break;
96 }
110 }
97 }
111 }
98 else if (role == Qt::EditRole)
112 else if (role == Qt::EditRole)
99 {
113 {
100 switch(index.column())
114 switch(index.column())
101 {
115 {
102 // case 6:
116 case 0:
103 // return m_labels[index.row()];
117 return m_labels[index.row()];
104 default:
118 default:
105 return m_data[index.row()]->at(index.column());
119 return m_data[index.row()]->at(index.column());
106 break;
120 break;
107 }
121 }
108 }
122 }
109 else if (role == Qt::BackgroundRole)
123 else if (role == Qt::BackgroundRole)
110 {
124 {
111 QRect rect;
125 QRect rect;
112 foreach(rect, m_mapping)
126 foreach(rect, m_mapping)
113 if(rect.contains(index.column(), index.row()))
127 if(rect.contains(index.column(), index.row()))
114 return QColor(m_mapping.key(rect));
128 return QColor(m_mapping.key(rect));
115
129
116 // cell not mapped return white color
130 // cell not mapped return white color
117 return QColor(Qt::white);
131 return QColor(Qt::white);
118 }
132 }
119 return QVariant();
133 return QVariant();
120 }
134 }
121
135
122 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
136 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
123 {
137 {
124 if (index.isValid() && role == Qt::EditRole)
138 if (index.isValid() && role == Qt::EditRole)
125 {
139 {
126 switch(index.column())
140 switch(index.column())
127 {
141 {
128 // case 6:
142 case 0:
129 // m_labels.replace(index.row(), value.toString());
143 m_labels.replace(index.row(), value.toString());
130 // break;
144 break;
131 default:
145 default:
132 m_data[index.row()]->replace(index.column(), value.toDouble());
146 m_data[index.row()]->replace(index.column(), value.toDouble());
133 break;
147 break;
134 }
148 }
135 emit dataChanged(index, index);
149 emit dataChanged(index, index);
136 return true;
150 return true;
137 }
151 }
138 return false;
152 return false;
139 }
153 }
140
154
141 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
155 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
142 {
156 {
143 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
157 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
144 }
158 }
145
159
146 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
160 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
147 {
161 {
148 Q_UNUSED(parent)
162 Q_UNUSED(parent)
149
163
150 if (row < 0)
164 if (row < 0)
151 row = 0;
165 row = 0;
152 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
166 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
153 for (int i = row; i < row + count; i++)
167 for (int i = row; i < row + count; i++)
154 {
168 {
155 // m_points.insert(row, QPointF(10,20));
169 // m_points.insert(row, QPointF(10,20));
156 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
170 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
157 QVector<QColor>* colorVec = new QVector<QColor>(m_columnCount);
171 QVector<QColor>* colorVec = new QVector<QColor>(m_columnCount);
158 for (int k = 0; k < dataVec->size(); k++)
172 for (int k = 0; k < dataVec->size(); k++)
159 {
173 {
160 if (k%2 == 0)
174 if (k%2 == 0)
161 // dataVec->replace(k, i * 50 + qrand()%20);
175 // dataVec->replace(k, i * 50 + qrand()%20);
162 {
176 {
163 int difference = 0;
177 int difference = 0;
164 if (i < m_data.size())
178 if (i < m_data.size())
165 {
179 {
166 if (i - 1 >= 0)
180 if (i - 1 >= 0)
167 {
181 {
168 if (row > 0)
182 if (row > 0)
169 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
183 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
170 else
184 else
171 difference = (int)((qAbs(m_data[i]->at(k)/count)));
185 difference = (int)((qAbs(m_data[i]->at(k)/count)));
172 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
186 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
173 }
187 }
174 else
188 else
175 dataVec->replace(k, qrand()%40 + 10);
189 dataVec->replace(k, qrand()%40 + 10);
176 }
190 }
177 else
191 else
178 {
192 {
179 if (i - 1 >= 0)
193 if (i - 1 >= 0)
180 {
194 {
181 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
195 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
182 }
196 }
183 else
197 else
184 {
198 {
185 dataVec->replace(k, qrand()%40 + 10);
199 dataVec->replace(k, qrand()%40 + 10);
186 }
200 }
187 }
201 }
188 }
202 }
189 else
203 else
190 dataVec->replace(k, qrand()%100);
204 dataVec->replace(k, qrand()%100);
191 colorVec->replace(k, QColor(Qt::white));
205 colorVec->replace(k, QColor(Qt::white));
192 }
206 }
193 m_data.insert(i, dataVec);
207 m_data.insert(i, dataVec);
194 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
208 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
195 }
209 }
196 endInsertRows();
210 endInsertRows();
197 return true;
211 return true;
198 }
212 }
199
213
200 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
214 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
201 {
215 {
202 if (row > this->rowCount() - 1)
216 if (row > this->rowCount() - 1)
203 return false;
217 return false;
204 if (row < 0)
218 if (row < 0)
205 row = 0;
219 row = 0;
206 if (row + count > rowCount())
220 if (row + count > rowCount())
207 return false;
221 return false;
208 beginRemoveRows(parent, row, row + count - 1);
222 beginRemoveRows(parent, row, row + count - 1);
209 for (int i = row; i < row + count; i++)
223 for (int i = row; i < row + count; i++)
210 {
224 {
211 QVector<qreal>* item = m_data.at(row);
225 QVector<qreal>* item = m_data.at(row);
212 m_data.removeAt(row);
226 m_data.removeAt(row);
213 delete item;
227 delete item;
214 m_labels.removeAt(row);
228 m_labels.removeAt(row);
215 }
229 }
216 endRemoveRows();
230 endRemoveRows();
217 return true;
231 return true;
218 }
232 }
219
233
220 bool CustomTableModel::insertColumns ( int column, int count, const QModelIndex & parent)
234 bool CustomTableModel::insertColumns ( int column, int count, const QModelIndex & parent)
221 {
235 {
222 if (column < 0)
236 if (column < 0)
223 column = 0;
237 column = 0;
224 beginInsertColumns(parent, column, column + count - 1);
238 beginInsertColumns(parent, column, column + count - 1);
225 m_columnCount += count;
239 m_columnCount += count;
226 for (int i = column; i < column + count; i++)
240 for (int i = column; i < column + count; i++)
227 for (int k = 0; k < rowCount(); k++)
241 for (int k = 0; k < rowCount(); k++)
228 if (k - 1 >= 0) {
242 if (k - 1 >= 0) {
229 m_data[k]->insert(i, m_data[k - 1]->at(i) + qrand()%40 + 10);
243 m_data[k]->insert(i, m_data[k - 1]->at(i) + qrand()%40 + 10);
230 } else {
244 } else {
231 m_data[k]->insert(i, qrand()%40);
245 m_data[k]->insert(i, qrand()%40);
232 }
246 }
233 endInsertColumns();
247 endInsertColumns();
234 return true;
248 return true;
235 }
249 }
236
250
237 bool CustomTableModel::removeColumns ( int column, int count, const QModelIndex & parent)
251 bool CustomTableModel::removeColumns ( int column, int count, const QModelIndex & parent)
238 {
252 {
239 if (column > columnCount() - 1)
253 if (column > columnCount() - 1)
240 return false;
254 return false;
241 if (column < 0)
255 if (column < 0)
242 column = 0;
256 column = 0;
243 if (column + count > columnCount())
257 if (column + count > columnCount())
244 return false;
258 return false;
245 beginRemoveColumns(parent, column, column + count -1);
259 beginRemoveColumns(parent, column, column + count -1);
246 m_columnCount -= count;
260 m_columnCount -= count;
247 for (int i = column; i < column + count; i++)
261 for (int i = column; i < column + count; i++)
248 for (int k = 0; k < rowCount(); k++)
262 for (int k = 0; k < rowCount(); k++)
249 m_data[k]->remove(column);
263 m_data[k]->remove(column);
250 endRemoveColumns();
264 endRemoveColumns();
251 return true;
265 return true;
252 }
266 }
253
267
254 void CustomTableModel::addMapping(QString color, QRect area)
268 void CustomTableModel::addMapping(QString color, QRect area)
255 {
269 {
256 m_mapping.insertMulti(color, area);
270 m_mapping.insertMulti(color, area);
257 }
271 }
258
272
259 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
273 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
260 {
274 {
261 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
275 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
262 }
276 }
@@ -1,406 +1,411
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 #include <QHeaderView>
40
41
41 TableWidget::TableWidget(QWidget *parent)
42 TableWidget::TableWidget(QWidget *parent)
42 : QWidget(parent)
43 : QWidget(parent)
43 // specialPie(0)
44 // specialPie(0)
44 {
45 {
45 setGeometry(1900, 100, 1000, 600);
46 setGeometry(1900, 100, 1000, 600);
46 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
47 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
47 // create simple model for storing data
48 // create simple model for storing data
48 // user's table data model
49 // user's table data model
49 m_model = new CustomTableModel;
50 m_model = new CustomTableModel;
50 m_tableView = new QTableView;
51 m_tableView = new QTableView;
51 m_tableView->setModel(m_model);
52 m_tableView->setModel(m_model);
52 m_tableView->setMinimumHeight(300);
53 // m_tableView->setMinimumHeight(300);
54 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
55 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
56
53 m_chart = new QChart;
57 m_chart = new QChart;
54 m_chart->legend()->setVisible(true);
58 m_chart->legend()->setVisible(true);
55 m_chart->setAnimationOptions(QChart::SeriesAnimations);
59 m_chart->setAnimationOptions(QChart::SeriesAnimations);
56 m_chartView = new QChartView(m_chart);
60 m_chartView = new QChartView(m_chart);
57 m_chartView->setRenderHint(QPainter::Antialiasing);
61 m_chartView->setRenderHint(QPainter::Antialiasing);
58 m_chartView->setMinimumSize(640, 480);
62 m_chartView->setMinimumSize(640, 480);
59
63
60 // add, remove data buttons
64 // add, remove data buttons
61 QPushButton* addRowAboveButton = new QPushButton("Add row above");
65 QPushButton* addRowAboveButton = new QPushButton("Add row above");
62 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
66 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
63
67
64 QPushButton* addRowBelowButton = new QPushButton("Add row below");
68 QPushButton* addRowBelowButton = new QPushButton("Add row below");
65 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
69 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
66
70
67 QPushButton* removeRowButton = new QPushButton("Remove row");
71 QPushButton* removeRowButton = new QPushButton("Remove row");
68 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
72 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
69
73
70 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
74 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
71 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
75 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
72
76
73 QPushButton* removeColumnButton = new QPushButton("Remove column");
77 QPushButton* removeColumnButton = new QPushButton("Remove column");
74 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
78 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
75
79
76 QPushButton* specialPieButton = new QPushButton("Test pie");
80 QPushButton* specialPieButton = new QPushButton("Test pie");
77 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
81 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
78
82
79
83
80 QLabel *spinBoxLabel = new QLabel("Rows affected:");
84 QLabel *spinBoxLabel = new QLabel("Rows affected:");
81
85
82 // spin box for setting number of affected items (add, remove)
86 // spin box for setting number of affected items (add, remove)
83 m_linesCountSpinBox = new QSpinBox;
87 m_linesCountSpinBox = new QSpinBox;
84 m_linesCountSpinBox->setRange(1, 10);
88 m_linesCountSpinBox->setRange(1, 10);
85 m_linesCountSpinBox->setValue(1);
89 m_linesCountSpinBox->setValue(1);
86
90
87 // buttons layout
91 // buttons layout
88 QVBoxLayout* buttonsLayout = new QVBoxLayout;
92 QVBoxLayout* buttonsLayout = new QVBoxLayout;
89 buttonsLayout->addWidget(spinBoxLabel);
93 // buttonsLayout->addWidget(spinBoxLabel);
90 buttonsLayout->addWidget(m_linesCountSpinBox);
94 // buttonsLayout->addWidget(m_linesCountSpinBox);
91 buttonsLayout->addWidget(addRowAboveButton);
95 // buttonsLayout->addWidget(addRowAboveButton);
92 buttonsLayout->addWidget(addRowBelowButton);
96 buttonsLayout->addWidget(addRowBelowButton);
93 buttonsLayout->addWidget(removeRowButton);
97 buttonsLayout->addWidget(removeRowButton);
94 buttonsLayout->addWidget(addColumnRightButton);
98 // buttonsLayout->addWidget(addColumnRightButton);
95 buttonsLayout->addWidget(removeColumnButton);
99 // buttonsLayout->addWidget(removeColumnButton);
96 // buttonsLayout->addWidget(specialPieButton);
100 // buttonsLayout->addWidget(specialPieButton);
97 buttonsLayout->addStretch();
101 buttonsLayout->addStretch();
98
102
99 // chart type radio buttons
103 // chart type radio buttons
100 m_lineRadioButton = new QRadioButton("Line");
104 m_lineRadioButton = new QRadioButton("Line");
101 m_splineRadioButton = new QRadioButton("Spline");
105 m_splineRadioButton = new QRadioButton("Spline");
102 m_scatterRadioButton = new QRadioButton("Scatter");
106 m_scatterRadioButton = new QRadioButton("Scatter");
103 m_pieRadioButton = new QRadioButton("Pie");
107 m_pieRadioButton = new QRadioButton("Pie");
104 m_areaRadioButton = new QRadioButton("Area");
108 m_areaRadioButton = new QRadioButton("Area");
105 m_barRadioButton = new QRadioButton("Bar");
109 m_barRadioButton = new QRadioButton("Bar");
106
110
107 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
111 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
108 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
112 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
109 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
113 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
110 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
114 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
111 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
115 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
112 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
116 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
113 m_lineRadioButton->setChecked(true);
117 m_pieRadioButton->setChecked(true);
114
118
115 // radio buttons layout
119 // radio buttons layout
116 QVBoxLayout* radioLayout = new QVBoxLayout;
120 QVBoxLayout* radioLayout = new QVBoxLayout;
117 radioLayout->addWidget(m_lineRadioButton);
121 radioLayout->addWidget(m_lineRadioButton);
118 // radioLayout->addWidget(m_splineRadioButton);
122 // radioLayout->addWidget(m_splineRadioButton);
119 // radioLayout->addWidget(m_scatterRadioButton);
123 // radioLayout->addWidget(m_scatterRadioButton);
120 radioLayout->addWidget(m_pieRadioButton);
124 radioLayout->addWidget(m_pieRadioButton);
121 // radioLayout->addWidget(m_areaRadioButton);
125 // radioLayout->addWidget(m_areaRadioButton);
122 radioLayout->addWidget(m_barRadioButton);
126 radioLayout->addWidget(m_barRadioButton);
123 radioLayout->addStretch();
127 radioLayout->addStretch();
124
128
125 // create main layout
129 // create main layout
126 QGridLayout* mainLayout = new QGridLayout;
130 QGridLayout* mainLayout = new QGridLayout;
127 mainLayout->addLayout(buttonsLayout, 1, 1);
131 mainLayout->addLayout(buttonsLayout, 2, 0);
128 mainLayout->addLayout(radioLayout, 2, 1);
132 // mainLayout->addLayout(radioLayout, 2, 1);
129 mainLayout->addWidget(m_tableView, 1, 0);
133 mainLayout->addWidget(m_tableView, 1, 0);
130 mainLayout->addWidget(m_chartView, 2, 0);
134 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
131 setLayout(mainLayout);
135 setLayout(mainLayout);
132 m_lineRadioButton->setFocus();
136 m_lineRadioButton->setFocus();
133 }
137 }
134
138
135 void TableWidget::addRowAbove()
139 void TableWidget::addRowAbove()
136 {
140 {
137 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
141 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
138
142
139 }
143 }
140
144
141 void TableWidget::addRowBelow()
145 void TableWidget::addRowBelow()
142 {
146 {
143 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
147 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
144
148
145 }
149 }
146
150
147 void TableWidget::removeRow()
151 void TableWidget::removeRow()
148 {
152 {
149 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
153 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
150 }
154 }
151
155
152 void TableWidget::addColumnRight()
156 void TableWidget::addColumnRight()
153 {
157 {
154 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
158 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
155 }
159 }
156
160
157 void TableWidget::removeColumn()
161 void TableWidget::removeColumn()
158 {
162 {
159 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
163 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
160 }
164 }
161
165
162 void TableWidget::updateChartType(bool toggle)
166 void TableWidget::updateChartType(bool toggle)
163 {
167 {
164 // this if is needed, so that the function is only called once.
168 // this if is needed, so that the function is only called once.
165 // For the radioButton that was enabled.
169 // For the radioButton that was enabled.
166 if (toggle) {
170 if (toggle) {
167 // specialPie = 0;
171 // specialPie = 0;
168 m_chart->removeAllSeries();
172 m_chart->removeAllSeries();
169 m_chart->axisX()->setNiceNumbersEnabled(false);
173 m_chart->axisX()->setNiceNumbersEnabled(false);
170 m_chart->axisY()->setNiceNumbersEnabled(false);
174 m_chart->axisY()->setNiceNumbersEnabled(false);
171
175
172 // renable axes of the chart (pie hides them)
176 // renable axes of the chart (pie hides them)
173 // x axis
177 // x axis
174 QAxis *axis = m_chart->axisX();
178 QAxis *axis = m_chart->axisX();
175 axis->setAxisVisible(true);
179 axis->setAxisVisible(true);
176 axis->setGridLineVisible(true);
180 axis->setGridLineVisible(true);
177 axis->setLabelsVisible(true);
181 axis->setLabelsVisible(true);
178
182
179 // y axis
183 // y axis
180 axis = m_chart->axisY();
184 axis = m_chart->axisY();
181 axis->setAxisVisible(true);
185 axis->setAxisVisible(true);
182 axis->setGridLineVisible(true);
186 axis->setGridLineVisible(true);
183 axis->setLabelsVisible(true);
187 axis->setLabelsVisible(true);
184
188
185 m_model->clearMapping();
189 m_model->clearMapping();
186
190
187 QString seriesColorHex = "#000000";
191 QString seriesColorHex = "#000000";
188 QPen pen;
192 QPen pen;
189 pen.setWidth(2);
193 pen.setWidth(2);
190
194
191 if (m_lineRadioButton->isChecked())
195 if (m_lineRadioButton->isChecked())
192 {
196 {
193 m_chart->setAnimationOptions(QChart::NoAnimation);
197 m_chart->setAnimationOptions(QChart::NoAnimation);
194
198
195 // series 1
199 // series 1
196 m_series = new QLineSeries;
200 m_series = new QLineSeries;
197 m_series->setModel(m_model);
201 m_series->setModel(m_model);
198 m_series->setModelMapping(0,1, Qt::Vertical);
202 m_series->setModelMapping(0,1, Qt::Vertical);
199 m_series->setModelMappingRange(3, 4);
203 m_series->setModelMappingRange(3, 4);
200 m_chart->addSeries(m_series);
204 m_chart->addSeries(m_series);
201 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
205 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
202 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
206 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
203
207
204 // series 2
208 // series 2
205 m_series = new QLineSeries;
209 m_series = new QLineSeries;
206 m_series->setModel(m_model);
210 m_series->setModel(m_model);
207 m_series->setModelMapping(2,3, Qt::Vertical);
211 m_series->setModelMapping(2,3, Qt::Vertical);
208 m_chart->addSeries(m_series);
212 m_chart->addSeries(m_series);
209 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
213 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
210 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
214 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
211
215
212 // series 3
216 // series 3
213 m_series = new QLineSeries;
217 m_series = new QLineSeries;
214 m_series->setModel(m_model);
218 m_series->setModel(m_model);
215 m_series->setModelMapping(4,5, Qt::Vertical);
219 m_series->setModelMapping(4,5, Qt::Vertical);
216 m_series->setModelMappingRange(2, -1);
220 m_series->setModelMappingRange(2, -1);
217 m_chart->addSeries(m_series);
221 m_chart->addSeries(m_series);
218 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
222 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
219 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
223 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
220 }
224 }
221 else if (m_splineRadioButton->isChecked())
225 else if (m_splineRadioButton->isChecked())
222 {
226 {
223 m_chart->setAnimationOptions(QChart::NoAnimation);
227 m_chart->setAnimationOptions(QChart::NoAnimation);
224
228
225 // series 1
229 // series 1
226 m_series = new QSplineSeries;
230 m_series = new QSplineSeries;
227 m_series->setModel(m_model);
231 m_series->setModel(m_model);
228 m_series->setModelMapping(0,1, Qt::Vertical);
232 m_series->setModelMapping(0,1, Qt::Vertical);
229 // m_series->setModelMappingRange(1, 4);
233 // m_series->setModelMappingRange(1, 4);
230 // series->setModelMapping(0,1, Qt::Horizontal);
234 // series->setModelMapping(0,1, Qt::Horizontal);
231 m_chart->addSeries(m_series);
235 m_chart->addSeries(m_series);
232 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
236 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
233 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
237 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
234
238
235 // series 2
239 // series 2
236 m_series = new QSplineSeries;
240 m_series = new QSplineSeries;
237 m_series->setModel(m_model);
241 m_series->setModel(m_model);
238 m_series->setModelMapping(2,3, Qt::Vertical);
242 m_series->setModelMapping(2,3, Qt::Vertical);
239 m_series->setModelMappingRange(2, 4);
243 m_series->setModelMappingRange(2, 4);
240 // series->setModelMapping(2,3, Qt::Horizontal);
244 // series->setModelMapping(2,3, Qt::Horizontal);
241 m_chart->addSeries(m_series);
245 m_chart->addSeries(m_series);
242 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
246 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
243 m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
247 m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
244
248
245 // series 3
249 // series 3
246 m_series = new QSplineSeries;
250 m_series = new QSplineSeries;
247 m_series->setModel(m_model);
251 m_series->setModel(m_model);
248 m_series->setModelMapping(4,5, Qt::Vertical);
252 m_series->setModelMapping(4,5, Qt::Vertical);
249 m_series->setModelMappingRange(2, -1);
253 m_series->setModelMappingRange(2, -1);
250 // series->setModelMapping(4,5, Qt::Horizontal);
254 // series->setModelMapping(4,5, Qt::Horizontal);
251 m_chart->addSeries(m_series);
255 m_chart->addSeries(m_series);
252 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
256 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
253 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
257 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
254 }
258 }
255 else if (m_scatterRadioButton->isChecked())
259 else if (m_scatterRadioButton->isChecked())
256 {
260 {
257 m_chart->setAnimationOptions(QChart::NoAnimation);
261 m_chart->setAnimationOptions(QChart::NoAnimation);
258
262
259 // series 1
263 // series 1
260 m_series = new QScatterSeries;
264 m_series = new QScatterSeries;
261 m_series->setModel(m_model);
265 m_series->setModel(m_model);
262 m_series->setModelMapping(0,1, Qt::Vertical);
266 m_series->setModelMapping(0,1, Qt::Vertical);
263 // m_series->setModelMappingRange(2, 0);
267 // m_series->setModelMappingRange(2, 0);
264 // series->setModelMapping(0,1, Qt::Horizontal);
268 // series->setModelMapping(0,1, Qt::Horizontal);
265 m_chart->addSeries(m_series);
269 m_chart->addSeries(m_series);
266
270
267 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
271 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
268 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
272 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
269
273
270 // series 2
274 // series 2
271 m_series = new QScatterSeries;
275 m_series = new QScatterSeries;
272 m_series->setModel(m_model);
276 m_series->setModel(m_model);
273 m_series->setModelMapping(2,3, Qt::Vertical);
277 m_series->setModelMapping(2,3, Qt::Vertical);
274 // m_series->setModelMappingRange(1, 6);
278 // m_series->setModelMappingRange(1, 6);
275 // series->setModelMapping(2,3, Qt::Horizontal);
279 // series->setModelMapping(2,3, Qt::Horizontal);
276 m_chart->addSeries(m_series);
280 m_chart->addSeries(m_series);
277
281
278 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
282 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
279 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
283 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
280
284
281 // series 3
285 // series 3
282 m_series = new QScatterSeries;
286 m_series = new QScatterSeries;
283 m_series->setModel(m_model);
287 m_series->setModel(m_model);
284 m_series->setModelMapping(4,5, Qt::Vertical);
288 m_series->setModelMapping(4,5, Qt::Vertical);
285 // series->setModelMapping(4,5, Qt::Horizontal);
289 // series->setModelMapping(4,5, Qt::Horizontal);
286 m_chart->addSeries(m_series);
290 m_chart->addSeries(m_series);
287 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
291 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
288 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
292 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
289 }
293 }
290 else if (m_pieRadioButton->isChecked())
294 else if (m_pieRadioButton->isChecked())
291 {
295 {
292 m_chart->setAnimationOptions(QChart::SeriesAnimations);
296 m_chart->setAnimationOptions(QChart::SeriesAnimations);
293
297
294 // pie 1
298 // pie 1
295 QPieSeries* pieSeries = new QPieSeries;
299 QPieSeries* pieSeries = new QPieSeries;
296 pieSeries->setModel(m_model);
300 pieSeries->setModel(m_model);
297 pieSeries->setModelMappingRange(3, 3);
301 pieSeries->setModelMappingRange(2, 5);
298 pieSeries->setModelMapping(0,0, Qt::Vertical);
302 pieSeries->setModelMapping(1, 0, Qt::Vertical);
299 pieSeries->setLabelsVisible(true);
303 pieSeries->setLabelsVisible(true);
300 pieSeries->setPieSize(0.35);
304 pieSeries->setPieSize(0.75);
301 pieSeries->setHorizontalPosition(0.2);
305 // pieSeries->setHorizontalPosition(0.2);
302 pieSeries->setVerticalPosition(0.3);
306 // pieSeries->setVerticalPosition(0.3);
303
304 m_chart->addSeries(pieSeries);
305 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
306 m_model->addMapping(seriesColorHex, QRect(0, 3, 1, 3));
307
307
308 // pie 2
309 pieSeries = new QPieSeries;
310 pieSeries->setModel(m_model);
311 pieSeries->setModelMappingRange(2, -1);
312 pieSeries->setModelMapping(1,1, Qt::Vertical);
313 pieSeries->setLabelsVisible(true);
314 pieSeries->setPieSize(0.35);
315 pieSeries->setHorizontalPosition(0.8);
316 pieSeries->setVerticalPosition(0.3);
317 m_chart->addSeries(pieSeries);
318 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
319 m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
320
321 // pie 3
322 pieSeries = new QPieSeries;
323 pieSeries->setModel(m_model);
324 pieSeries->setModelMapping(2,2, Qt::Vertical);
325 pieSeries->setLabelsVisible(true);
326 pieSeries->setPieSize(0.35);
327 pieSeries->setHorizontalPosition(0.5);
328 pieSeries->setVerticalPosition(0.75);
329 m_chart->addSeries(pieSeries);
308 m_chart->addSeries(pieSeries);
330 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
309 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
331 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
310 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
311
312 // // pie 2
313 // pieSeries = new QPieSeries;
314 // pieSeries->setModel(m_model);
315
316 // pieSeries->setModelMapping(1,1, Qt::Vertical);
317 // pieSeries->setModelMappingRange(2, -1);
318 // pieSeries->setLabelsVisible(true);
319 // pieSeries->setPieSize(0.35);
320 // pieSeries->setHorizontalPosition(0.8);
321 // pieSeries->setVerticalPosition(0.3);
322 // m_chart->addSeries(pieSeries);
323 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
324 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
325
326 // // pie 3
327 // pieSeries = new QPieSeries;
328 // pieSeries->setModel(m_model);
329 // pieSeries->setModelMapping(2,2, Qt::Vertical);
330 // pieSeries->setLabelsVisible(true);
331 // pieSeries->setPieSize(0.35);
332 // pieSeries->setHorizontalPosition(0.5);
333 // pieSeries->setVerticalPosition(0.75);
334 // m_chart->addSeries(pieSeries);
335 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
336 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
332
337
333 // // special pie
338 // // special pie
334 // specialPie = new QPieSeries;
339 // specialPie = new QPieSeries;
335 // specialPie->append(17, "1");
340 // specialPie->append(17, "1");
336 // specialPie->append(45, "2");
341 // specialPie->append(45, "2");
337 // specialPie->append(77, "3");
342 // specialPie->append(77, "3");
338 // specialPie->append(37, "4");
343 // specialPie->append(37, "4");
339 // specialPie->append(27, "5");
344 // specialPie->append(27, "5");
340 // specialPie->append(47, "6");
345 // specialPie->append(47, "6");
341 // specialPie->setPieSize(0.35);
346 // specialPie->setPieSize(0.35);
342 // specialPie->setHorizontalPosition(0.8);
347 // specialPie->setHorizontalPosition(0.8);
343 // specialPie->setVerticalPosition(0.75);
348 // specialPie->setVerticalPosition(0.75);
344 // specialPie->setLabelsVisible(true);
349 // specialPie->setLabelsVisible(true);
345 // m_chart->addSeries(specialPie);
350 // m_chart->addSeries(specialPie);
346 }
351 }
347 else if (m_areaRadioButton->isChecked())
352 else if (m_areaRadioButton->isChecked())
348 {
353 {
349 m_chart->setAnimationOptions(QChart::NoAnimation);
354 m_chart->setAnimationOptions(QChart::NoAnimation);
350
355
351 QLineSeries* upperLineSeries = new QLineSeries;
356 QLineSeries* upperLineSeries = new QLineSeries;
352 upperLineSeries->setModel(m_model);
357 upperLineSeries->setModel(m_model);
353 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
358 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
354 // upperLineSeries->setModelMappingRange(1, 5);
359 // upperLineSeries->setModelMappingRange(1, 5);
355 QLineSeries* lowerLineSeries = new QLineSeries;
360 QLineSeries* lowerLineSeries = new QLineSeries;
356 lowerLineSeries->setModel(m_model);
361 lowerLineSeries->setModel(m_model);
357 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
362 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
358 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
363 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
359 m_chart->addSeries(areaSeries);
364 m_chart->addSeries(areaSeries);
360 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
365 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
361 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
366 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
362 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
367 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
363 }
368 }
364 else if (m_barRadioButton->isChecked())
369 else if (m_barRadioButton->isChecked())
365 {
370 {
366 m_chart->setAnimationOptions(QChart::SeriesAnimations);
371 m_chart->setAnimationOptions(QChart::SeriesAnimations);
367
372
368 QBarSeries* barSeries = new QBarSeries(QStringList());
373 QBarSeries* barSeries = new QBarSeries(QStringList());
369 barSeries->setModel(m_model);
374 barSeries->setModel(m_model);
370 // barSeries->setModelMappingRange(2, 5);
375 // barSeries->setModelMappingRange(2, 5);
371 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
376 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
372 m_chart->addSeries(barSeries);
377 m_chart->addSeries(barSeries);
373 QList<QBarSet*> barsets = barSeries->barSets();
378 QList<QBarSet*> barsets = barSeries->barSets();
374 for (int i = 0; i < barsets.count(); i++) {
379 for (int i = 0; i < barsets.count(); i++) {
375 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
380 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
376 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
381 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
377 }
382 }
378 }
383 }
379
384
380
385
381 if (!m_barRadioButton->isChecked()) {
386 if (!m_barRadioButton->isChecked()) {
382 m_chart->axisX()->setRange(0, 500);
387 m_chart->axisX()->setRange(0, 500);
383 m_chart->axisY()->setRange(0, 220);
388 m_chart->axisY()->setRange(0, 220);
384 }
389 }
385 m_chart->legend()->setVisible(true);
390 m_chart->legend()->setVisible(true);
386
391
387 // repaint table view colors
392 // repaint table view colors
388 m_tableView->repaint();
393 m_tableView->repaint();
389 m_tableView->setFocus();
394 m_tableView->setFocus();
390 }
395 }
391 }
396 }
392
397
393 void TableWidget::testPie()
398 void TableWidget::testPie()
394 {
399 {
395 m_tableView->setColumnWidth(10, 250);
400 m_tableView->setColumnWidth(10, 250);
396 // if (specialPie) {
401 // if (specialPie) {
397 // specialPie->remove(specialPie->slices().at(2));
402 // specialPie->remove(specialPie->slices().at(2));
398 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
403 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
399 // specialPie->append(4, "heloo");
404 // specialPie->append(4, "heloo");
400 // }
405 // }
401 }
406 }
402
407
403 TableWidget::~TableWidget()
408 TableWidget::~TableWidget()
404 {
409 {
405
410
406 }
411 }
General Comments 0
You need to be logged in to leave comments. Login now