##// END OF EJS Templates
Pie now has defualt Model to which slice data is copied.
Marek Rosa -
r1195:505ff5d19e3d
parent child
Show More
@@ -1,95 +1,95
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 "declarativepieseries.h"
21 #include "declarativepieseries.h"
22 #include "declarativechart.h"
22 #include "declarativechart.h"
23 #include "qchart.h"
23 #include "qchart.h"
24 #include <qdeclarativelist.h>
24 #include <qdeclarativelist.h>
25 #include "qpiemodelmapper.h"
25 #include "qpiemodelmapper.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
29 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
30 QPieSeries(parent)
30 QPieSeries(parent)
31 {
31 {
32 // TODO: set default model on init?
32 // TODO: set default model on init?
33 // setModel(new DeclarativeTableModel());
33 // setModel(new DeclarativeTableModel());
34
34
35 // TODO: Set default mapper parameters to allow easy to use PieSeries api?
35 // TODO: Set default mapper parameters to allow easy to use PieSeries api?
36 QPieModelMapper *mapper = new QPieModelMapper();
36 QPieModelMapper *mapper = modelMapper();//new QPieModelMapper();
37 mapper->setMapLabels(0);
37 mapper->setMapLabels(0);
38 mapper->setMapValues(1);
38 mapper->setMapValues(1);
39 mapper->setOrientation(Qt::Vertical);
39 mapper->setOrientation(Qt::Vertical);
40 mapper->setFirst(0);
40 mapper->setFirst(0);
41 mapper->setCount(-1);
41 mapper->setCount(-1);
42 setModelMapper(mapper);
42 // setModelMapper(mapper);
43 }
43 }
44
44
45 void DeclarativePieSeries::classBegin()
45 void DeclarativePieSeries::classBegin()
46 {
46 {
47 }
47 }
48
48
49 void DeclarativePieSeries::componentComplete()
49 void DeclarativePieSeries::componentComplete()
50 {
50 {
51 foreach(QObject *child, children()) {
51 foreach(QObject *child, children()) {
52 if (qobject_cast<QPieSlice *>(child)) {
52 if (qobject_cast<QPieSlice *>(child)) {
53 QPieSeries::append(qobject_cast<QPieSlice *>(child));
53 QPieSeries::append(qobject_cast<QPieSlice *>(child));
54 }
54 }
55 }
55 }
56 }
56 }
57
57
58 QDeclarativeListProperty<QPieSlice> DeclarativePieSeries::initialSlices()
58 QDeclarativeListProperty<QPieSlice> DeclarativePieSeries::initialSlices()
59 {
59 {
60 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieSeries::appendInitialSlices);
60 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieSeries::appendInitialSlices);
61 }
61 }
62
62
63 QPieSlice *DeclarativePieSeries::at(int index)
63 QPieSlice *DeclarativePieSeries::at(int index)
64 {
64 {
65 QList<QPieSlice*> sliceList = slices();
65 QList<QPieSlice*> sliceList = slices();
66 if (index < sliceList.count())
66 if (index < sliceList.count())
67 return sliceList[index];
67 return sliceList[index];
68
68
69 return 0;
69 return 0;
70 }
70 }
71
71
72 QPieSlice* DeclarativePieSeries::append(QString name, qreal value)
72 QPieSlice* DeclarativePieSeries::append(QString name, qreal value)
73 {
73 {
74 // TODO: parameter order is wrong, switch it:
74 // TODO: parameter order is wrong, switch it:
75 return QPieSeries::append(value, name);
75 return QPieSeries::append(value, name);
76 }
76 }
77
77
78 void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model)
78 void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model)
79 {
79 {
80 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
80 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
81 if (m) {
81 if (m) {
82 QPieSeries::setModel(m);
82 QPieSeries::setModel(m);
83 } else {
83 } else {
84 qWarning("DeclarativePieSeries: Illegal model");
84 qWarning("DeclarativePieSeries: Illegal model");
85 }
85 }
86 }
86 }
87
87
88 DeclarativeTableModel *DeclarativePieSeries::pieModel()
88 DeclarativeTableModel *DeclarativePieSeries::pieModel()
89 {
89 {
90 return qobject_cast<DeclarativeTableModel *>(model());
90 return qobject_cast<DeclarativeTableModel *>(model());
91 }
91 }
92
92
93 #include "moc_declarativepieseries.cpp"
93 #include "moc_declarativepieseries.cpp"
94
94
95 QTCOMMERCIALCHART_END_NAMESPACE
95 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,813 +1,949
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 #include <QStandardItemModel>
30 #include "qpiemodelmapper.h"
31 #include "qpiemodelmapper.h"
31
32
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
34
34 /*!
35 /*!
35 \class QPieSeries
36 \class QPieSeries
36 \brief Pie series API for QtCommercial Charts
37 \brief Pie series API for QtCommercial Charts
37
38
38 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
39 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
39 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
40 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
40 The actual slice size is determined by that relative value.
41 The actual slice size is determined by that relative value.
41
42
42 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
43 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
43 These relate to the actual chart rectangle.
44 These relate to the actual chart rectangle.
44
45
45 By default the pie is defined as a full pie but it can also be a partial pie.
46 By default the pie is defined as a full pie but it can also be a partial pie.
46 This can be done by setting a starting angle and angle span to the series.
47 This can be done by setting a starting angle and angle span to the series.
47 Full pie is 360 degrees where 0 is at 12 a'clock.
48 Full pie is 360 degrees where 0 is at 12 a'clock.
48
49
49 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
50 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
50 \image examples_piechart.png
51 \image examples_piechart.png
51 */
52 */
52
53
53 /*!
54 /*!
54 \property QPieSeries::horizontalPosition
55 \property QPieSeries::horizontalPosition
55 \brief Defines the horizontal position of the pie.
56 \brief Defines the horizontal position of the pie.
56
57
57 The value is a relative value to the chart rectangle where:
58 The value is a relative value to the chart rectangle where:
58
59
59 \list
60 \list
60 \o 0.0 is the absolute left.
61 \o 0.0 is the absolute left.
61 \o 1.0 is the absolute right.
62 \o 1.0 is the absolute right.
62 \endlist
63 \endlist
63
64
64 Default value is 0.5 (center).
65 Default value is 0.5 (center).
65 */
66 */
66
67
67 /*!
68 /*!
68 \property QPieSeries::verticalPosition
69 \property QPieSeries::verticalPosition
69 \brief Defines the vertical position of the pie.
70 \brief Defines the vertical position of the pie.
70
71
71 The value is a relative value to the chart rectangle where:
72 The value is a relative value to the chart rectangle where:
72
73
73 \list
74 \list
74 \o 0.0 is the absolute top.
75 \o 0.0 is the absolute top.
75 \o 1.0 is the absolute bottom.
76 \o 1.0 is the absolute bottom.
76 \endlist
77 \endlist
77
78
78 Default value is 0.5 (center).
79 Default value is 0.5 (center).
79 */
80 */
80
81
81 /*!
82 /*!
82 \property QPieSeries::size
83 \property QPieSeries::size
83 \brief Defines the pie size.
84 \brief Defines the pie size.
84
85
85 The value is a relative value to the chart rectangle where:
86 The value is a relative value to the chart rectangle where:
86
87
87 \list
88 \list
88 \o 0.0 is the minimum size (pie not drawn).
89 \o 0.0 is the minimum size (pie not drawn).
89 \o 1.0 is the maximum size that can fit the chart.
90 \o 1.0 is the maximum size that can fit the chart.
90 \endlist
91 \endlist
91
92
92 Default value is 0.7.
93 Default value is 0.7.
93 */
94 */
94
95
95 /*!
96 /*!
96 \property QPieSeries::startAngle
97 \property QPieSeries::startAngle
97 \brief Defines the starting angle of the pie.
98 \brief Defines the starting angle of the pie.
98
99
99 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
100 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
100
101
101 Default is value is 0.
102 Default is value is 0.
102 */
103 */
103
104
104 /*!
105 /*!
105 \property QPieSeries::endAngle
106 \property QPieSeries::endAngle
106 \brief Defines the ending angle of the pie.
107 \brief Defines the ending angle of the pie.
107
108
108 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
109 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
109
110
110 Default is value is 360.
111 Default is value is 360.
111 */
112 */
112
113
113
114
114 /*!
115 /*!
115 Constructs a series object which is a child of \a parent.
116 Constructs a series object which is a child of \a parent.
116 */
117 */
117 QPieSeries::QPieSeries(QObject *parent) :
118 QPieSeries::QPieSeries(QObject *parent) :
118 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
119 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
119 {
120 {
120
121
121 }
122 }
122
123
123 /*!
124 /*!
124 Destroys the series and its slices.
125 Destroys the series and its slices.
125 */
126 */
126 QPieSeries::~QPieSeries()
127 QPieSeries::~QPieSeries()
127 {
128 {
128 // NOTE: d_prt destroyed by QObject
129 // NOTE: d_prt destroyed by QObject
129 }
130 }
130
131
131 /*!
132 /*!
132 Returns QChartSeries::SeriesTypePie.
133 Returns QChartSeries::SeriesTypePie.
133 */
134 */
134 QAbstractSeries::SeriesType QPieSeries::type() const
135 QAbstractSeries::SeriesType QPieSeries::type() const
135 {
136 {
136 return QAbstractSeries::SeriesTypePie;
137 return QAbstractSeries::SeriesTypePie;
137 }
138 }
138
139
139 /*!
140 /*!
140 Appends an array of \a slices to the series.
141 Appends an array of \a slices to the series.
141 Slice ownership is passed to the series.
142 Slice ownership is passed to the series.
142 */
143 */
143 bool QPieSeries::append(QList<QPieSlice*> slices)
144 bool QPieSeries::append(QList<QPieSlice*> slices)
144 {
145 {
145 Q_D(QPieSeries);
146 Q_D(QPieSeries);
146
147
147 if (slices.count() == 0)
148 if (slices.count() == 0)
148 return false;
149 return false;
149
150
150 foreach (QPieSlice* s, slices) {
151 foreach (QPieSlice* s, slices) {
151 if (!s || d->m_slices.contains(s))
152 if (!s || d->m_slices.contains(s))
152 return false;
153 return false;
153 }
154 }
154
155
155 foreach (QPieSlice* s, slices) {
156 foreach (QPieSlice* s, slices) {
156 s->setParent(this);
157 s->setParent(this);
157 d->m_slices << s;
158 d->m_slices << s;
158 }
159 }
159
160
160 d->updateDerivativeData();
161 d->updateDerivativeData();
161
162
162 foreach (QPieSlice* s, slices) {
163 foreach (QPieSlice* s, slices) {
163 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
164 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
164 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
165 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
165 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
166 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
166 }
167 }
167
168
168 emit d->added(slices);
169 emit d->added(slices);
169
170
170 return true;
171 return true;
171 }
172 }
172
173
173 /*!
174 /*!
174 Appends a single \a slice to the series.
175 Appends a single \a slice to the series.
175 Slice ownership is passed to the series.
176 Slice ownership is passed to the series.
176 */
177 */
177 bool QPieSeries::append(QPieSlice* slice)
178 bool QPieSeries::append(QPieSlice* slice)
178 {
179 {
179 return append(QList<QPieSlice*>() << slice);
180 return append(QList<QPieSlice*>() << slice);
180 }
181 }
181
182
182 /*!
183 /*!
183 Appends a single \a slice to the series and returns a reference to the series.
184 Appends a single \a slice to the series and returns a reference to the series.
184 Slice ownership is passed to the series.
185 Slice ownership is passed to the series.
185 */
186 */
186 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
187 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
187 {
188 {
188 append(slice);
189 append(slice);
189 return *this;
190 return *this;
190 }
191 }
191
192
192
193
193 /*!
194 /*!
194 Appends a single slice to the series with give \a value and \a name.
195 Appends a single slice to the series with give \a value and \a name.
195 Slice ownership is passed to the series.
196 Slice ownership is passed to the series.
196 */
197 */
197 QPieSlice* QPieSeries::append(qreal value, QString name)
198 QPieSlice* QPieSeries::append(qreal value, QString name)
198 {
199 {
199 QPieSlice* slice = new QPieSlice(value, name);
200 QPieSlice* slice = new QPieSlice(value, name);
200 append(slice);
201 append(slice);
201 return slice;
202 return slice;
202 }
203 }
203
204
204 /*!
205 /*!
205 Inserts a single \a slice to the series before the slice at \a index position.
206 Inserts a single \a slice to the series before the slice at \a index position.
206 Slice ownership is passed to the series.
207 Slice ownership is passed to the series.
207 */
208 */
208 bool QPieSeries::insert(int index, QPieSlice* slice)
209 bool QPieSeries::insert(int index, QPieSlice* slice)
209 {
210 {
210 Q_D(QPieSeries);
211 Q_D(QPieSeries);
211
212
212 if (index < 0 || index > d->m_slices.count())
213 if (index < 0 || index > d->m_slices.count())
213 return false;
214 return false;
214
215
215 if (!slice || d->m_slices.contains(slice))
216 if (!slice || d->m_slices.contains(slice))
216 return false;
217 return false;
217
218
218 slice->setParent(this);
219 d->m_model->insertRow(index);
219 d->m_slices.insert(index, slice);
220 if (d->m_mapper->orientation() == Qt::Vertical) {
221 d->m_model->setData(d->m_model->index(index, d->m_mapper->mapValues()), slice->value());
222 d->m_model->setData(d->m_model->index(index, d->m_mapper->mapLabels()), slice->label());
223 } else {
224 d->m_model->setData(d->m_model->index(d->m_mapper->mapValues(), index), slice->value());
225 d->m_model->setData(d->m_model->index(d->m_mapper->mapLabels(), index), slice->label());
226 }
220
227
221 d->updateDerivativeData();
228 // if (index < 0 || index > d->m_slices.count())
229 // return false;
230
231 // if (!slice || d->m_slices.contains(slice))
232 // return false;
233
234 // slice->setParent(this);
235 // d->m_slices.insert(index, slice);
236
237 // d->updateDerivativeData();
238
239 // connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
240 // connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
241 // connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
242
243 // emit d->added(QList<QPieSlice*>() << slice);
222
244
223 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
224 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
225 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
226
245
227 emit d->added(QList<QPieSlice*>() << slice);
228
246
229 return true;
247 return true;
230 }
248 }
231
249
232 /*!
250 /*!
233 Removes a single \a slice from the series and deletes the slice.
251 Removes a single \a slice from the series and deletes the slice.
234
252
235 Do not reference the pointer after this call.
253 Do not reference the pointer after this call.
236 */
254 */
237 bool QPieSeries::remove(QPieSlice* slice)
255 bool QPieSeries::remove(QPieSlice* slice)
238 {
256 {
239 Q_D(QPieSeries);
257 Q_D(QPieSeries);
240
258
241 if (!d->m_slices.removeOne(slice))
259 if (!d->m_slices.removeOne(slice))
242 return false;
260 return false;
243
261
244 d->updateDerivativeData();
262 d->updateDerivativeData();
245
263
246 emit d->removed(QList<QPieSlice*>() << slice);
264 emit d->removed(QList<QPieSlice*>() << slice);
247
265
248 delete slice;
266 delete slice;
249 slice = 0;
267 slice = 0;
250
268
251 return true;
269 return true;
252 }
270 }
253
271
254 /*!
272 /*!
255 Clears all slices from the series.
273 Clears all slices from the series.
256 */
274 */
257 void QPieSeries::clear()
275 void QPieSeries::clear()
258 {
276 {
259 Q_D(QPieSeries);
277 Q_D(QPieSeries);
260 if (d->m_slices.count() == 0)
278 if (count() == 0)
261 return;
279 return;
280 if (d->m_mapper->orientation() == Qt::Vertical)
281 d->m_model->removeRows(d->m_mapper->first(), count());
282 else
283 d->m_model->removeColumns(d->m_mapper->first(), count());
284 // if (d->m_slices.count() == 0)
285 // return;
262
286
263 QList<QPieSlice*> slices = d->m_slices;
287 // QList<QPieSlice*> slices = d->m_slices;
264 foreach (QPieSlice* s, d->m_slices) {
288 // foreach (QPieSlice* s, d->m_slices) {
265 d->m_slices.removeOne(s);
289 // d->m_slices.removeOne(s);
266 delete s;
290 // delete s;
267 }
291 // }
268
292
269 d->updateDerivativeData();
293 // d->updateDerivativeData();
270
294
271 emit d->removed(slices);
295 // emit d->removed(slices);
272 }
296 }
273
297
274 /*!
298 /*!
275 returns the number of the slices in this series.
299 returns the number of the slices in this series.
276 */
300 */
277 int QPieSeries::count() const
301 int QPieSeries::count() const
278 {
302 {
279 Q_D(const QPieSeries);
303 Q_D(const QPieSeries);
280 return d->m_slices.count();
304 // return d->m_slices.count();
305
306 // if (d->m_model && d->m_mapper) {
307
308 if (d->m_mapper->orientation() == Qt::Vertical) {
309 // data is in a column. Return the number of mapped items if the model's column have enough items
310 // or the number of items that can be mapped
311 if (d->m_mapper->mapValues() >= d->m_model->columnCount() || d->m_mapper->mapLabels() >= d->m_model->columnCount())
312 return 0; // mapped columns are not existing
313 else if (d->m_mapper->count() != -1)
314 // void setModelMapper(QPieModelMapper *mapper);
315 return qMin(d->m_mapper->count(), qMax(d->m_model->rowCount() - d->m_mapper->first(), 0));
316 else
317 return qMax(d->m_model->rowCount() - d->m_mapper->first(), 0);
318 } else {
319 // data is in a row. Return the number of mapped items if the model's row have enough items
320 // or the number of items that can be mapped
321 if (d->m_mapper->mapValues() >= d->m_model->rowCount() || d->m_mapper->mapLabels() >= d->m_model->rowCount())
322 return 0; // mapped rows are not existing
323 else if (d->m_mapper->count() != -1)
324 return qMin(d->m_mapper->count(), qMax(d->m_model->columnCount() - d->m_mapper->first(), 0));
325 else
326 return qMax(d->m_model->columnCount() - d->m_mapper->first(), 0);
327 }
328 // }
281 }
329 }
282
330
283 /*!
331 /*!
284 Returns true is the series is empty.
332 Returns true is the series is empty.
285 */
333 */
286 bool QPieSeries::isEmpty() const
334 bool QPieSeries::isEmpty() const
287 {
335 {
288 Q_D(const QPieSeries);
336 Q_D(const QPieSeries);
289 return d->m_slices.isEmpty();
337 return d->m_slices.isEmpty();
290 }
338 }
291
339
292 /*!
340 /*!
293 Returns a list of slices that belong to this series.
341 Returns a list of slices that belong to this series.
294 */
342 */
295 QList<QPieSlice*> QPieSeries::slices() const
343 QList<QPieSlice*> QPieSeries::slices() const
296 {
344 {
297 Q_D(const QPieSeries);
345 Q_D(const QPieSeries);
298 return d->m_slices;
346 return d->m_slices;
299 }
347 }
300
348
301 void QPieSeries::setHorizontalPosition(qreal relativePosition)
349 void QPieSeries::setHorizontalPosition(qreal relativePosition)
302 {
350 {
303 Q_D(QPieSeries);
351 Q_D(QPieSeries);
304 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
352 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
305 emit d->piePositionChanged();
353 emit d->piePositionChanged();
306 }
354 }
307
355
308 void QPieSeries::setVerticalPosition(qreal relativePosition)
356 void QPieSeries::setVerticalPosition(qreal relativePosition)
309 {
357 {
310 Q_D(QPieSeries);
358 Q_D(QPieSeries);
311 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
359 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
312 emit d->piePositionChanged();
360 emit d->piePositionChanged();
313 }
361 }
314
362
315 qreal QPieSeries::horizontalPosition() const
363 qreal QPieSeries::horizontalPosition() const
316 {
364 {
317 Q_D(const QPieSeries);
365 Q_D(const QPieSeries);
318 return d->m_pieRelativeHorPos;
366 return d->m_pieRelativeHorPos;
319 }
367 }
320
368
321 qreal QPieSeries::verticalPosition() const
369 qreal QPieSeries::verticalPosition() const
322 {
370 {
323 Q_D(const QPieSeries);
371 Q_D(const QPieSeries);
324 return d->m_pieRelativeVerPos;
372 return d->m_pieRelativeVerPos;
325 }
373 }
326
374
327 void QPieSeries::setPieSize(qreal relativeSize)
375 void QPieSeries::setPieSize(qreal relativeSize)
328 {
376 {
329 Q_D(QPieSeries);
377 Q_D(QPieSeries);
330 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
378 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
331 emit d->pieSizeChanged();
379 emit d->pieSizeChanged();
332 }
380 }
333
381
334 qreal QPieSeries::pieSize() const
382 qreal QPieSeries::pieSize() const
335 {
383 {
336 Q_D(const QPieSeries);
384 Q_D(const QPieSeries);
337 return d->m_pieRelativeSize;
385 return d->m_pieRelativeSize;
338 }
386 }
339
387
340
388
341 void QPieSeries::setPieStartAngle(qreal angle)
389 void QPieSeries::setPieStartAngle(qreal angle)
342 {
390 {
343 Q_D(QPieSeries);
391 Q_D(QPieSeries);
344 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
392 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
345 d->updateDerivativeData();
393 d->updateDerivativeData();
346 }
394 }
347
395
348 qreal QPieSeries::pieStartAngle() const
396 qreal QPieSeries::pieStartAngle() const
349 {
397 {
350 Q_D(const QPieSeries);
398 Q_D(const QPieSeries);
351 return d->m_pieStartAngle;
399 return d->m_pieStartAngle;
352 }
400 }
353
401
354 /*!
402 /*!
355 Sets the end angle of the pie.
403 Sets the end angle of the pie.
356
404
357 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
405 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
358
406
359 \a angle must be greater than start angle.
407 \a angle must be greater than start angle.
360
408
361 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
409 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
362 */
410 */
363 void QPieSeries::setPieEndAngle(qreal angle)
411 void QPieSeries::setPieEndAngle(qreal angle)
364 {
412 {
365 Q_D(QPieSeries);
413 Q_D(QPieSeries);
366
414
367 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
415 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
368 d->updateDerivativeData();
416 d->updateDerivativeData();
369 }
417 }
370
418
371 /*!
419 /*!
372 Returns the end angle of the pie.
420 Returns the end angle of the pie.
373
421
374 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
422 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
375
423
376 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
424 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
377 */
425 */
378 qreal QPieSeries::pieEndAngle() const
426 qreal QPieSeries::pieEndAngle() const
379 {
427 {
380 Q_D(const QPieSeries);
428 Q_D(const QPieSeries);
381 return d->m_pieEndAngle;
429 return d->m_pieEndAngle;
382 }
430 }
383
431
384 /*!
432 /*!
385 Sets the all the slice labels \a visible or invisible.
433 Sets the all the slice labels \a visible or invisible.
386
434
387 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
435 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
388 */
436 */
389 void QPieSeries::setLabelsVisible(bool visible)
437 void QPieSeries::setLabelsVisible(bool visible)
390 {
438 {
391 Q_D(QPieSeries);
439 Q_D(QPieSeries);
392 foreach (QPieSlice* s, d->m_slices)
440 foreach (QPieSlice* s, d->m_slices)
393 s->setLabelVisible(visible);
441 s->setLabelVisible(visible);
394 }
442 }
395
443
396 /*!
444 /*!
397 Returns the sum of all slice values in this series.
445 Returns the sum of all slice values in this series.
398
446
399 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
447 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
400 */
448 */
401 qreal QPieSeries::sum() const
449 qreal QPieSeries::sum() const
402 {
450 {
403 Q_D(const QPieSeries);
451 Q_D(const QPieSeries);
404 return d->m_sum;
452 return d->m_sum;
405 }
453 }
406
454
407 /*!
455 /*!
408 \fn void QPieSeries::clicked(QPieSlice* slice)
456 \fn void QPieSeries::clicked(QPieSlice* slice)
409
457
410 This signal is emitted when a \a slice has been clicked.
458 This signal is emitted when a \a slice has been clicked.
411
459
412 \sa QPieSlice::clicked()
460 \sa QPieSlice::clicked()
413 */
461 */
414
462
415 /*!
463 /*!
416 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
464 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
417
465
418 This signal is emitted when user has hovered over or away from the \a slice.
466 This signal is emitted when user has hovered over or away from the \a slice.
419
467
420 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
468 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
421
469
422 \sa QPieSlice::hovered()
470 \sa QPieSlice::hovered()
423 */
471 */
424
472
425 /*!
473 /*!
426 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
474 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
427 Sets the \a model to be used as a data source
475 Sets the \a model to be used as a data source
428 */
476 */
429 void QPieSeries::setModel(QAbstractItemModel* model)
477 void QPieSeries::setModel(QAbstractItemModel* model)
430 {
478 {
431 Q_D(QPieSeries);
479 Q_D(QPieSeries);
432 // disconnect signals from old model
433 if(d->m_model)
434 {
435 disconnect(d->m_model, 0, this, 0);
436 }
437
480
438 // set new model
481 // set new model
439 if(model)
482 if(model)
440 {
483 {
484 // disconnect signals from old model and delete it
485 disconnect(d->m_model, 0, this, 0);
486 delete d->m_model;
487
441 d->m_model = model;
488 d->m_model = model;
442 // connect signals from the model
489 // connect signals from the model
443 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
490 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
444 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
491 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
445 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
492 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
446 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
493 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
447 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
494 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
448
495
449 if (d->m_mapper)
496 if (d->m_mapper)
450 d->initializePieFromModel();
497 d->initializePieFromModel();
451 }
498 }
452 else
499 // else
453 {
500 // {
454 d->m_model = 0;
501 // d->m_model = new QStandardItemModel;
455 }
502 // d->m_mapper->reset();
456 }
503 // d->m_mapper->setMapLabels(0);
457
504 // d->m_mapper->setMapValues(1);
458 void QPieSeries::setModelMapper(QPieModelMapper *mapper)
505 // }
459 {
506 }
460 Q_D(QPieSeries);
507
461 // disconnect signals from old mapper
508 //void QPieSeries::setModelMapper(QPieModelMapper *mapper)
462 if (d->m_mapper) {
509 //{
463 QObject::disconnect(d->m_mapper, 0, this, 0);
510 // Q_D(QPieSeries);
464 }
511 // // disconnect signals from old mapper
465
512 // if (d->m_mapper) {
466 if (mapper) {
513 // QObject::disconnect(d->m_mapper, 0, this, 0);
467 d->m_mapper = mapper;
514 // }
468 // connect the signal from the mapper
515
469 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
516 // if (mapper) {
470
517 // d->m_mapper = mapper;
471 if (d->m_model)
518
472 d->initializePieFromModel();
519 // if (d->m_model)
473 } else {
520 // d->initializePieFromModel();
474 d->m_mapper = 0;
521 // } else {
475 }
522 // d->m_mapper = new QPieModelMapper;
476 }
523 // d->m_mapper->setMapLabels(0);
524 // d->m_mapper->setMapValues(1);
525 // }
526
527 // // connect the signal from the mapper
528 // connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
529 //}
477
530
478 QPieModelMapper* QPieSeries::modelMapper() const
531 QPieModelMapper* QPieSeries::modelMapper() const
479 {
532 {
480 Q_D(const QPieSeries);
533 Q_D(const QPieSeries);
481 return d->m_mapper;
534 return d->m_mapper;
482 }
535 }
483
536
484 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
537 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
485
538
486
539
487 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
540 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
488 QAbstractSeriesPrivate(parent),
541 QAbstractSeriesPrivate(parent),
489 m_pieRelativeHorPos(0.5),
542 m_pieRelativeHorPos(0.5),
490 m_pieRelativeVerPos(0.5),
543 m_pieRelativeVerPos(0.5),
491 m_pieRelativeSize(0.7),
544 m_pieRelativeSize(0.7),
492 m_pieStartAngle(0),
545 m_pieStartAngle(0),
493 m_pieEndAngle(360),
546 m_pieEndAngle(360),
494 m_sum(0),
547 m_sum(0),
495 m_mapper(0)
548 m_model(new QStandardItemModel),
549 m_mapper(new QPieModelMapper)
496 {
550 {
551 // connect signals from the model
552 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
553 connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelRowsAdded(QModelIndex,int,int)));
554 connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelRowsRemoved(QModelIndex,int,int)));
555 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelColumnsAdded(QModelIndex,int,int)));
556 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
497
557
558 // connect the signal from the mapper
559 connect(m_mapper, SIGNAL(updated()), this, SLOT(initializePieFromModel()));
498 }
560 }
499
561
500 QPieSeriesPrivate::~QPieSeriesPrivate()
562 QPieSeriesPrivate::~QPieSeriesPrivate()
501 {
563 {
502
564
503 }
565 }
504
566
505 void QPieSeriesPrivate::updateDerivativeData()
567 void QPieSeriesPrivate::updateDerivativeData()
506 {
568 {
507 m_sum = 0;
569 m_sum = 0;
508
570
509 // nothing to do?
571 // nothing to do?
510 if (m_slices.count() == 0)
572 if (m_slices.count() == 0)
511 return;
573 return;
512
574
513 // calculate sum of all slices
575 // calculate sum of all slices
514 foreach (QPieSlice* s, m_slices)
576 foreach (QPieSlice* s, m_slices)
515 m_sum += s->value();
577 m_sum += s->value();
516
578
517 // nothing to show..
579 // nothing to show..
518 if (qFuzzyIsNull(m_sum))
580 if (qFuzzyIsNull(m_sum))
519 return;
581 return;
520
582
521 // update slice attributes
583 // update slice attributes
522 qreal sliceAngle = m_pieStartAngle;
584 qreal sliceAngle = m_pieStartAngle;
523 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
585 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
524 QVector<QPieSlice*> changed;
586 QVector<QPieSlice*> changed;
525 foreach (QPieSlice* s, m_slices) {
587 foreach (QPieSlice* s, m_slices) {
526
588
527 PieSliceData data = PieSliceData::data(s);
589 PieSliceData data = PieSliceData::data(s);
528 data.m_percentage = s->value() / m_sum;
590 data.m_percentage = s->value() / m_sum;
529 data.m_angleSpan = pieSpan * data.m_percentage;
591 data.m_angleSpan = pieSpan * data.m_percentage;
530 data.m_startAngle = sliceAngle;
592 data.m_startAngle = sliceAngle;
531 sliceAngle += data.m_angleSpan;
593 sliceAngle += data.m_angleSpan;
532
594
533 if (PieSliceData::data(s) != data) {
595 if (PieSliceData::data(s) != data) {
534 PieSliceData::data(s) = data;
596 PieSliceData::data(s) = data;
535 changed << s;
597 changed << s;
536 }
598 }
537 }
599 }
538
600
539 // emit signals
601 // emit signals
540 foreach (QPieSlice* s, changed)
602 foreach (QPieSlice* s, changed)
541 PieSliceData::data(s).emitChangedSignal(s);
603 PieSliceData::data(s).emitChangedSignal(s);
542 }
604 }
543
605
544 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
606 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
545 {
607 {
546 return series.d_func();
608 return series.d_func();
547 }
609 }
548
610
549 void QPieSeriesPrivate::sliceChanged()
611 void QPieSeriesPrivate::sliceChanged()
550 {
612 {
551 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
613 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
614 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
615 int sliceIndex = m_slices.indexOf(slice);
616 if (m_mapper->orientation() == Qt::Vertical) {
617 QModelIndex modelIndex = m_model->index(sliceIndex + m_mapper->first(), m_mapper->mapValues());
618 if (m_model->data(modelIndex).toReal() != slice->value())
619 m_model->setData(modelIndex, slice->value());
620 modelIndex = m_model->index(sliceIndex + m_mapper->first(), m_mapper->mapLabels());
621 if (m_model->data(modelIndex).toString() != slice->label())
622 m_model->setData(modelIndex, slice->label());
623 } else {
624 QModelIndex modelIndex = m_model->index(m_mapper->mapValues(), sliceIndex + m_mapper->first());
625 if (m_model->data(modelIndex).toReal() != slice->value())
626 m_model->setData(modelIndex, slice->value());
627 modelIndex = m_model->index(m_mapper->mapLabels(), sliceIndex + m_mapper->first());
628 if (m_model->data(modelIndex).toString() != slice->label())
629 m_model->setData(modelIndex, slice->label());
630 }
552 updateDerivativeData();
631 updateDerivativeData();
553 }
632 }
554
633
555 void QPieSeriesPrivate::sliceClicked()
634 void QPieSeriesPrivate::sliceClicked()
556 {
635 {
557 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
636 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
558 Q_ASSERT(m_slices.contains(slice));
637 Q_ASSERT(m_slices.contains(slice));
559 Q_Q(QPieSeries);
638 Q_Q(QPieSeries);
560 emit q->clicked(slice);
639 emit q->clicked(slice);
561 }
640 }
562
641
563 void QPieSeriesPrivate::sliceHovered(bool state)
642 void QPieSeriesPrivate::sliceHovered(bool state)
564 {
643 {
565 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
644 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
566 Q_ASSERT(m_slices.contains(slice));
645 Q_ASSERT(m_slices.contains(slice));
567 Q_Q(QPieSeries);
646 Q_Q(QPieSeries);
568 emit q->hovered(slice, state);
647 emit q->hovered(slice, state);
569 }
648 }
570
649
650 void QPieSeriesPrivate::doClear()
651 {
652 if (m_slices.count() == 0)
653 return;
654
655 QList<QPieSlice*> slices = m_slices;
656 foreach (QPieSlice* s, m_slices) {
657 m_slices.removeOne(s);
658 delete s;
659 }
660
661 updateDerivativeData();
662
663 emit removed(slices);
664 }
665
666 void QPieSeriesPrivate::doRemove(QPieSlice* slice)
667 {
668 if (!m_slices.removeOne(slice))
669 return;// false;
670
671 updateDerivativeData();
672
673 emit removed(QList<QPieSlice*>() << slice);
674
675 delete slice;
676 slice = 0;
677
678 return;// true;
679 }
680
681 void QPieSeriesPrivate::doInsert(int index, QPieSlice* slice)
682 {
683 if (index < 0 || index > m_slices.count())
684 return;// false;
685
686 if (!slice || m_slices.contains(slice))
687 return;// false;
688
689 slice->setParent(this);
690 m_slices.insert(index, slice);
691
692 updateDerivativeData();
693
694 connect(slice, SIGNAL(changed()), this, SLOT(sliceChanged()));
695 connect(slice, SIGNAL(clicked()), this, SLOT(sliceClicked()));
696 connect(slice, SIGNAL(hovered(bool)), this, SLOT(sliceHovered(bool)));
697
698 emit added(QList<QPieSlice*>() << slice);
699
700 return;// true;
701 }
702
571 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
703 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
572 {
704 {
573 if (m_mapper) {
705 if (m_mapper) {
574 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
706 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
575 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
707 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
576 if (m_mapper->orientation() == Qt::Vertical)
708 if (m_mapper->orientation() == Qt::Vertical)
577 {
709 {
578 if ( topLeft.row() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.row() < m_mapper->first() + m_mapper->count())) {
710 if ( topLeft.row() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.row() < m_mapper->first() + m_mapper->count())) {
579 if (topLeft.column() == m_mapper->mapValues())
711 if (topLeft.column() == m_mapper->mapValues())
580 m_slices.at(topLeft.row() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
712 m_slices.at(topLeft.row() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
581 if (topLeft.column() == m_mapper->mapLabels())
713 if (topLeft.column() == m_mapper->mapLabels())
582 m_slices.at(topLeft.row() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
714 m_slices.at(topLeft.row() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
583 }
715 }
584 }
716 }
585 else
717 else
586 {
718 {
587 if (topLeft.column() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.column() < m_mapper->first() + m_mapper->count())) {
719 if (topLeft.column() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.column() < m_mapper->first() + m_mapper->count())) {
588 if (topLeft.row() == m_mapper->mapValues())
720 if (topLeft.row() == m_mapper->mapValues())
589 m_slices.at(topLeft.column() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
721 m_slices.at(topLeft.column() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
590 if (topLeft.row() == m_mapper->mapLabels())
722 if (topLeft.row() == m_mapper->mapLabels())
591 m_slices.at(topLeft.column() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
723 m_slices.at(topLeft.column() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
592 }
724 }
593 }
725 }
594 }
726 }
595 }
727 }
596 }
728 }
597 }
729 }
598
730
599
731
600 void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
732 void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
601 {
733 {
602 Q_UNUSED(parent);
734 Q_UNUSED(parent);
603 if (m_mapper) {
735 if (m_mapper) {
604 if (m_mapper->orientation() == Qt::Vertical)
736 if (m_mapper->orientation() == Qt::Vertical)
605 insertData(start, end);
737 insertData(start, end);
606 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
738 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
607 initializePieFromModel();
739 initializePieFromModel();
608 }
740 }
609 }
741 }
610
742
611 void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
743 void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
612 {
744 {
613 Q_UNUSED(parent);
745 Q_UNUSED(parent);
614 if (m_mapper) {
746 if (m_mapper) {
615 if (m_mapper->orientation() == Qt::Vertical)
747 if (m_mapper->orientation() == Qt::Vertical)
616 removeData(start, end);
748 removeData(start, end);
617 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
749 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
618 initializePieFromModel();
750 initializePieFromModel();
619 }
751 }
620 }
752 }
621
753
622 void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
754 void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
623 {
755 {
624 Q_UNUSED(parent);
756 Q_UNUSED(parent);
625 if (m_mapper) {
757 if (m_mapper) {
626 if (m_mapper->orientation() == Qt::Horizontal)
758 if (m_mapper->orientation() == Qt::Horizontal)
627 insertData(start, end);
759 insertData(start, end);
628 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
760 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
629 initializePieFromModel();
761 initializePieFromModel();
630 }
762 }
631 }
763 }
632
764
633 void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
765 void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
634 {
766 {
635 Q_UNUSED(parent);
767 Q_UNUSED(parent);
636 if (m_mapper) {
768 if (m_mapper) {
637 if (m_mapper->orientation() == Qt::Horizontal)
769 if (m_mapper->orientation() == Qt::Horizontal)
638 removeData(start, end);
770 removeData(start, end);
639 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
771 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
640 initializePieFromModel();
772 initializePieFromModel();
641 }
773 }
642 }
774 }
643
775
644 void QPieSeriesPrivate::insertData(int start, int end)
776 void QPieSeriesPrivate::insertData(int start, int end)
645 {
777 {
646 Q_Q(QPieSeries);
778 Q_Q(QPieSeries);
647 if (m_mapper) {
779 if (m_mapper) {
648 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
780 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
649 return;
781 return;
650 } else {
782 } else {
651 int addedCount = end - start + 1;
783 int addedCount = end - start + 1;
652 if (m_mapper->count() != -1 && addedCount > m_mapper->count())
784 if (m_mapper->count() != -1 && addedCount > m_mapper->count())
653 addedCount = m_mapper->count();
785 addedCount = m_mapper->count();
654 int first = qMax(start, m_mapper->first());
786 int first = qMax(start, m_mapper->first());
655 int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
787 int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
656 for (int i = first; i <= last; i++) {
788 for (int i = first; i <= last; i++) {
657 QPieSlice *slice = new QPieSlice;
789 QPieSlice *slice = new QPieSlice;
658 if (m_mapper->orientation() == Qt::Vertical) {
790 if (m_mapper->orientation() == Qt::Vertical) {
659 slice->setValue(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble());
791 slice->setValue(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble());
660 slice->setLabel(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
792 slice->setLabel(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
661 } else {
793 } else {
662 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble());
794 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble());
663 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
795 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
664 }
796 }
665 slice->setLabelVisible();
797 slice->setLabelVisible();
666 q->insert(i - m_mapper->first(), slice);
798 // q->insert(i - m_mapper->first(), slice);
799 doInsert(i - m_mapper->first(), slice);
667 }
800 }
668 if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count())
801 if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count())
669 for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--)
802 for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--)
670 q->remove(q->slices().at(i));
803 // q->remove(q->slices().at(i));
804 doRemove(q->slices().at(i));
671 }
805 }
672 }
806 }
673 }
807 }
674
808
675 void QPieSeriesPrivate::removeData(int start, int end)
809 void QPieSeriesPrivate::removeData(int start, int end)
676 {
810 {
677 Q_Q(QPieSeries);
811 Q_Q(QPieSeries);
678 if (m_mapper) {
812 if (m_mapper) {
679 int removedCount = end - start + 1;
813 int removedCount = end - start + 1;
680 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
814 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
681 return;
815 return;
682 } else {
816 } else {
683 int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed
817 int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed
684 int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed.
818 int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed.
685 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed.
819 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed.
686 for (int i = last; i >= first; i--)
820 for (int i = last; i >= first; i--)
687 q->remove(q->slices().at(i - m_mapper->first()));
821 // q->remove(q->slices().at(i - m_mapper->first()));
822 doRemove(q->slices().at(i - m_mapper->first()));
688
823
689 if (m_mapper->count() != -1) {
824 if (m_mapper->count() != -1) {
690 int itemsAvailable; // check how many are available to be added
825 int itemsAvailable; // check how many are available to be added
691 if (m_mapper->orientation() == Qt::Vertical)
826 if (m_mapper->orientation() == Qt::Vertical)
692 itemsAvailable = m_model->rowCount() - m_mapper->first() - m_slices.size();
827 itemsAvailable = m_model->rowCount() - m_mapper->first() - m_slices.size();
693 else
828 else
694 itemsAvailable = m_model->columnCount() - m_mapper->first() - m_slices.size();
829 itemsAvailable = m_model->columnCount() - m_mapper->first() - m_slices.size();
695 int toBeAdded = qMin(itemsAvailable, m_mapper->count() - m_slices.size()); // add not more items than there is space left to be filled.
830 int toBeAdded = qMin(itemsAvailable, m_mapper->count() - m_slices.size()); // add not more items than there is space left to be filled.
696 int currentSize = m_slices.size();
831 int currentSize = m_slices.size();
697 if (toBeAdded > 0)
832 if (toBeAdded > 0)
698 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
833 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
699 QPieSlice *slice = new QPieSlice;
834 QPieSlice *slice = new QPieSlice;
700 if (m_mapper->orientation() == Qt::Vertical) {
835 if (m_mapper->orientation() == Qt::Vertical) {
701 slice->setValue(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapValues()), Qt::DisplayRole).toDouble());
836 slice->setValue(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapValues()), Qt::DisplayRole).toDouble());
702 slice->setLabel(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapLabels()), Qt::DisplayRole).toString());
837 slice->setLabel(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapLabels()), Qt::DisplayRole).toString());
703 } else {
838 } else {
704 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i + m_mapper->first()), Qt::DisplayRole).toDouble());
839 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i + m_mapper->first()), Qt::DisplayRole).toDouble());
705 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString());
840 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString());
706 }
841 }
707 slice->setLabelVisible();
842 slice->setLabelVisible();
708 q->insert(i, slice);
843 // q->insert(i, slice);
844 doInsert(i, slice);
709 }
845 }
710 }
846 }
711 }
847 }
712 }
848 }
713 }
849 }
714
850
715 void QPieSeriesPrivate::initializePieFromModel()
851 void QPieSeriesPrivate::initializePieFromModel()
716 {
852 {
717 Q_Q(QPieSeries);
853 Q_Q(QPieSeries);
718
854
719 // clear current content
855 // clear current content
720 q->clear();
856 doClear();
721
857
722 if (m_model == 0 || m_mapper == 0)
858 if (m_model == 0 || m_mapper == 0)
723 return;
859 return;
724
860
725 // check if mappings are set
861 // check if mappings are set
726 if (m_mapper->mapValues() == -1 || m_mapper->mapLabels() == -1)
862 if (m_mapper->mapValues() == -1 || m_mapper->mapLabels() == -1)
727 return;
863 return;
728
864
729 // create the initial slices set
865 // create the initial slices set
730 if (m_mapper->orientation() == Qt::Vertical) {
866 if (m_mapper->orientation() == Qt::Vertical) {
731 if (m_mapper->mapValues() >= m_model->columnCount() || m_mapper->mapLabels() >= m_model->columnCount())
867 if (m_mapper->mapValues() >= m_model->columnCount() || m_mapper->mapLabels() >= m_model->columnCount())
732 return; // mapped columns are not existing
868 return; // mapped columns are not existing
733
869
734 int sliceCount = 0;
870 int sliceCount = 0;
735 if(m_mapper->count() == -1)
871 if(m_mapper->count() == -1)
736 sliceCount = m_model->rowCount() - m_mapper->first();
872 sliceCount = m_model->rowCount() - m_mapper->first();
737 else
873 else
738 sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
874 sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
739 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
875 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
740 q->append(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
876 q->append(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
741 } else {
877 } else {
742 if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount())
878 if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount())
743 return; // mapped columns are not existing
879 return; // mapped columns are not existing
744
880
745 int sliceCount = 0;
881 int sliceCount = 0;
746 if(m_mapper->count() == -1)
882 if(m_mapper->count() == -1)
747 sliceCount = m_model->columnCount() - m_mapper->first();
883 sliceCount = m_model->columnCount() - m_mapper->first();
748 else
884 else
749 sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
885 sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
750 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
886 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
751 q->append(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
887 q->append(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
752 }
888 }
753 q->setLabelsVisible(true);
889 q->setLabelsVisible(true);
754 }
890 }
755
891
756 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
892 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
757 {
893 {
758 // Remove rounding errors
894 // Remove rounding errors
759 qreal roundedValue = newValue;
895 qreal roundedValue = newValue;
760 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
896 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
761 roundedValue = 0.0;
897 roundedValue = 0.0;
762 else if (qFuzzyCompare(newValue, max))
898 else if (qFuzzyCompare(newValue, max))
763 roundedValue = max;
899 roundedValue = max;
764 else if (qFuzzyCompare(newValue, min))
900 else if (qFuzzyCompare(newValue, min))
765 roundedValue = min;
901 roundedValue = min;
766
902
767 // Check if the position is valid after removing the rounding errors
903 // Check if the position is valid after removing the rounding errors
768 if (roundedValue < min || roundedValue > max) {
904 if (roundedValue < min || roundedValue > max) {
769 qWarning("QPieSeries: Illegal value");
905 qWarning("QPieSeries: Illegal value");
770 return false;
906 return false;
771 }
907 }
772
908
773 if (!qFuzzyIsNull(value - roundedValue)) {
909 if (!qFuzzyIsNull(value - roundedValue)) {
774 value = roundedValue;
910 value = roundedValue;
775 return true;
911 return true;
776 }
912 }
777
913
778 // The change was so small it is considered a rounding error
914 // The change was so small it is considered a rounding error
779 return false;
915 return false;
780 }
916 }
781
917
782 void QPieSeriesPrivate::scaleDomain(Domain& domain)
918 void QPieSeriesPrivate::scaleDomain(Domain& domain)
783 {
919 {
784 Q_UNUSED(domain);
920 Q_UNUSED(domain);
785 // does not apply to pie
921 // does not apply to pie
786 }
922 }
787
923
788 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
924 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
789 {
925 {
790 Q_Q(QPieSeries);
926 Q_Q(QPieSeries);
791 PieChartItem* pie = new PieChartItem(q,presenter);
927 PieChartItem* pie = new PieChartItem(q,presenter);
792 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
928 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
793 presenter->animator()->addAnimation(pie);
929 presenter->animator()->addAnimation(pie);
794 }
930 }
795 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
931 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
796 return pie;
932 return pie;
797 }
933 }
798
934
799 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
935 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
800 {
936 {
801 Q_Q(QPieSeries);
937 Q_Q(QPieSeries);
802 QList<LegendMarker*> markers;
938 QList<LegendMarker*> markers;
803 foreach(QPieSlice* slice, q->slices()) {
939 foreach(QPieSlice* slice, q->slices()) {
804 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
940 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
805 markers << marker;
941 markers << marker;
806 }
942 }
807 return markers;
943 return markers;
808 }
944 }
809
945
810 #include "moc_qpieseries.cpp"
946 #include "moc_qpieseries.cpp"
811 #include "moc_qpieseries_p.cpp"
947 #include "moc_qpieseries_p.cpp"
812
948
813 QTCOMMERCIALCHART_END_NAMESPACE
949 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,92 +1,92
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef PIESERIES_H
21 #ifndef PIESERIES_H
22 #define PIESERIES_H
22 #define PIESERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 class QPieSeriesPrivate;
27 class QPieSeriesPrivate;
28 class QPieSlice;
28 class QPieSlice;
29 class QPieModelMapper;
29 class QPieModelMapper;
30
30
31 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
34 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
35 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
35 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
36 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
36 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
37 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
37 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
38 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
38 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
39 Q_PROPERTY(int count READ count)
39 Q_PROPERTY(int count READ count)
40 Q_PROPERTY(QPieModelMapper *modelMapper READ modelMapper)
40 Q_PROPERTY(QPieModelMapper *modelMapper READ modelMapper)
41
41
42 public:
42 public:
43 explicit QPieSeries(QObject *parent = 0);
43 explicit QPieSeries(QObject *parent = 0);
44 virtual ~QPieSeries();
44 virtual ~QPieSeries();
45
45
46 QAbstractSeries::SeriesType type() const;
46 QAbstractSeries::SeriesType type() const;
47
47
48 bool append(QPieSlice* slice);
48 bool append(QPieSlice* slice);
49 bool append(QList<QPieSlice*> slices);
49 bool append(QList<QPieSlice*> slices);
50 QPieSeries& operator << (QPieSlice* slice);
50 QPieSeries& operator << (QPieSlice* slice);
51 QPieSlice* append(qreal value, QString name);
51 QPieSlice* append(qreal value, QString name);
52 bool insert(int index, QPieSlice* slice);
52 bool insert(int index, QPieSlice* slice);
53 bool remove(QPieSlice* slice);
53 bool remove(QPieSlice* slice);
54 void clear();
54 void clear();
55
55
56 QList<QPieSlice*> slices() const;
56 QList<QPieSlice*> slices() const;
57 int count() const;
57 int count() const;
58 bool isEmpty() const;
58 bool isEmpty() const;
59
59
60 qreal sum() const;
60 qreal sum() const;
61
61
62 void setHorizontalPosition(qreal relativePosition);
62 void setHorizontalPosition(qreal relativePosition);
63 qreal horizontalPosition() const;
63 qreal horizontalPosition() const;
64 void setVerticalPosition(qreal relativePosition);
64 void setVerticalPosition(qreal relativePosition);
65 qreal verticalPosition() const;
65 qreal verticalPosition() const;
66
66
67 void setPieSize(qreal relativeSize);
67 void setPieSize(qreal relativeSize);
68 qreal pieSize() const;
68 qreal pieSize() const;
69
69
70 void setPieStartAngle(qreal startAngle);
70 void setPieStartAngle(qreal startAngle);
71 qreal pieStartAngle() const;
71 qreal pieStartAngle() const;
72 void setPieEndAngle(qreal endAngle);
72 void setPieEndAngle(qreal endAngle);
73 qreal pieEndAngle() const;
73 qreal pieEndAngle() const;
74
74
75 void setLabelsVisible(bool visible = true);
75 void setLabelsVisible(bool visible = true);
76
76
77 void setModel(QAbstractItemModel* model);
77 void setModel(QAbstractItemModel* model);
78 void setModelMapper(QPieModelMapper *mapper);
78 // void setModelMapper(QPieModelMapper *mapper);
79 QPieModelMapper* modelMapper() const;
79 QPieModelMapper* modelMapper() const;
80
80
81 Q_SIGNALS:
81 Q_SIGNALS:
82 void clicked(QPieSlice* slice);
82 void clicked(QPieSlice* slice);
83 void hovered(QPieSlice* slice, bool state);
83 void hovered(QPieSlice* slice, bool state);
84
84
85 private:
85 private:
86 Q_DECLARE_PRIVATE(QPieSeries)
86 Q_DECLARE_PRIVATE(QPieSeries)
87 Q_DISABLE_COPY(QPieSeries)
87 Q_DISABLE_COPY(QPieSeries)
88 };
88 };
89
89
90 QTCOMMERCIALCHART_END_NAMESPACE
90 QTCOMMERCIALCHART_END_NAMESPACE
91
91
92 #endif // PIESERIES_H
92 #endif // PIESERIES_H
@@ -1,90 +1,94
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QPIESERIES_P_H
21 #ifndef QPIESERIES_P_H
22 #define QPIESERIES_P_H
22 #define QPIESERIES_P_H
23
23
24 #include "qpieseries.h"
24 #include "qpieseries.h"
25 #include "qabstractseries_p.h"
25 #include "qabstractseries_p.h"
26
26
27 class QModelIndex;
27 class QModelIndex;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QLegendPrivate;
30 class QLegendPrivate;
31 class QPieModelMapper;
31 class QPieModelMapper;
32
32
33 class QPieSeriesPrivate : public QAbstractSeriesPrivate
33 class QPieSeriesPrivate : public QAbstractSeriesPrivate
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36
36
37 public:
37 public:
38 QPieSeriesPrivate(QPieSeries *parent);
38 QPieSeriesPrivate(QPieSeries *parent);
39 ~QPieSeriesPrivate();
39 ~QPieSeriesPrivate();
40
40
41 void scaleDomain(Domain& domain);
41 void scaleDomain(Domain& domain);
42 Chart* createGraphics(ChartPresenter *presenter);
42 Chart* createGraphics(ChartPresenter *presenter);
43 QList<LegendMarker*> createLegendMarker(QLegend *legend);
43 QList<LegendMarker*> createLegendMarker(QLegend *legend);
44
44
45 void updateDerivativeData();
45 void updateDerivativeData();
46
46
47 static QPieSeriesPrivate* seriesData(QPieSeries &series);
47 static QPieSeriesPrivate* seriesData(QPieSeries &series);
48
48
49 Q_SIGNALS:
49 Q_SIGNALS:
50 void added(QList<QPieSlice*> slices);
50 void added(QList<QPieSlice*> slices);
51 void removed(QList<QPieSlice*> slices);
51 void removed(QList<QPieSlice*> slices);
52 void piePositionChanged();
52 void piePositionChanged();
53 void pieSizeChanged();
53 void pieSizeChanged();
54
54
55 public Q_SLOTS:
55 public Q_SLOTS:
56 void sliceChanged();
56 void sliceChanged();
57 void sliceClicked();
57 void sliceClicked();
58 void sliceHovered(bool state);
58 void sliceHovered(bool state);
59 void initializePieFromModel();
59 void initializePieFromModel();
60 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
60 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
61 void modelRowsAdded(QModelIndex parent, int start, int end);
61 void modelRowsAdded(QModelIndex parent, int start, int end);
62 void modelRowsRemoved(QModelIndex parent, int start, int end);
62 void modelRowsRemoved(QModelIndex parent, int start, int end);
63 void modelColumnsAdded(QModelIndex parent, int start, int end);
63 void modelColumnsAdded(QModelIndex parent, int start, int end);
64 void modelColumnsRemoved(QModelIndex parent, int start, int end);
64 void modelColumnsRemoved(QModelIndex parent, int start, int end);
65 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
65 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
66
66
67 private:
67 private:
68 void doClear();
69 void doRemove(QPieSlice* slice);
70 void doInsert(int index, QPieSlice* slice);
68 void insertData(int start, int end);
71 void insertData(int start, int end);
69 void removeData(int start, int end);
72 void removeData(int start, int end);
70
73
71 public:
74 public:
72 QList<QPieSlice*> m_slices;
75 QList<QPieSlice*> m_slices;
73 qreal m_pieRelativeHorPos;
76 qreal m_pieRelativeHorPos;
74 qreal m_pieRelativeVerPos;
77 qreal m_pieRelativeVerPos;
75 qreal m_pieRelativeSize;
78 qreal m_pieRelativeSize;
76 qreal m_pieStartAngle;
79 qreal m_pieStartAngle;
77 qreal m_pieEndAngle;
80 qreal m_pieEndAngle;
78 qreal m_sum;
81 qreal m_sum;
79
82
80 // model map
83 // model
84 QAbstractItemModel *m_model;
81 QPieModelMapper *m_mapper;
85 QPieModelMapper *m_mapper;
82
86
83 private:
87 private:
84 friend class QLegendPrivate;
88 friend class QLegendPrivate;
85 Q_DECLARE_PUBLIC(QPieSeries)
89 Q_DECLARE_PUBLIC(QPieSeries)
86 };
90 };
87
91
88 QTCOMMERCIALCHART_END_NAMESPACE
92 QTCOMMERCIALCHART_END_NAMESPACE
89
93
90 #endif // QPIESERIES_P_H
94 #endif // QPIESERIES_P_H
@@ -1,410 +1,412
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 "qpieslice.h"
21 #include "qpieslice.h"
22 #include "pieslicedata_p.h"
22 #include "pieslicedata_p.h"
23 #include <QAbstractItemModel>
23
24
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
26
26 /*!
27 /*!
27 \class QPieSlice
28 \class QPieSlice
28 \brief Defines a slice in pie series.
29 \brief Defines a slice in pie series.
29
30
30 This object defines the properties of a single slice in a QPieSeries.
31 This object defines the properties of a single slice in a QPieSeries.
31
32
32 In addition to the obvious value and label properties the user can also control
33 In addition to the obvious value and label properties the user can also control
33 the visual appearance of a slice. By modifying the visual appearance also means that
34 the visual appearance of a slice. By modifying the visual appearance also means that
34 the user is overriding the default appearance set by the theme. Even if the theme is
35 the user is overriding the default appearance set by the theme. Even if the theme is
35 changed users settings will persist.
36 changed users settings will persist.
36
37
37 To enable user interaction customization with the slices some basic signals
38 To enable user interaction customization with the slices some basic signals
38 are provided about clicking and hovering.
39 are provided about clicking and hovering.
39 */
40 */
40
41
41 /*!
42 /*!
42 \property QPieSlice::label
43 \property QPieSlice::label
43
44
44 Label of the slice.
45 Label of the slice.
45 */
46 */
46
47
47 /*!
48 /*!
48 \property QPieSlice::value
49 \property QPieSlice::value
49
50
50 Value of the slice.
51 Value of the slice.
51
52
52 \sa percentage(), QPieSeries::sum()
53 \sa percentage(), QPieSeries::sum()
53 */
54 */
54
55
55 /*!
56 /*!
56 Constructs an empty slice with a \a parent.
57 Constructs an empty slice with a \a parent.
57
58
58 \sa QPieSeries::append(), QPieSeries::insert()
59 \sa QPieSeries::append(), QPieSeries::insert()
59 */
60 */
60 QPieSlice::QPieSlice(QObject *parent)
61 QPieSlice::QPieSlice(QObject *parent)
61 :QObject(parent),
62 :QObject(parent),
62 d(new PieSliceData())
63 d(new PieSliceData())
63 {
64 {
64
65
65 }
66 }
66
67
67 /*!
68 /*!
68 Constructs an empty slice with given \a value, \a label and a \a parent.
69 Constructs an empty slice with given \a value, \a label and a \a parent.
69 \sa QPieSeries::append(), QPieSeries::insert()
70 \sa QPieSeries::append(), QPieSeries::insert()
70 */
71 */
71 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
72 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
72 :QObject(parent),
73 :QObject(parent),
73 d(new PieSliceData())
74 d(new PieSliceData())
74 {
75 {
75 d->m_value = value;
76 d->m_value = value;
76 d->m_labelText = label;
77 d->m_labelText = label;
77 }
78 }
78
79
79 /*!
80 /*!
80 Destroys the slice.
81 Destroys the slice.
81 User should not delete the slice if it has been added to the series.
82 User should not delete the slice if it has been added to the series.
82 */
83 */
83 QPieSlice::~QPieSlice()
84 QPieSlice::~QPieSlice()
84 {
85 {
85 delete d;
86 delete d;
86 }
87 }
87
88
88 /*!
89 /*!
89 Gets the value of the slice.
90 Gets the value of the slice.
90 Note that all values in the series
91 Note that all values in the series
91 \sa setValue()
92 \sa setValue()
92 */
93 */
93 qreal QPieSlice::value() const
94 qreal QPieSlice::value() const
94 {
95 {
95 return d->m_value;
96 return d->m_value;
96 }
97 }
97
98
98 /*!
99 /*!
99 Gets the label of the slice.
100 Gets the label of the slice.
100 \sa setLabel()
101 \sa setLabel()
101 */
102 */
102 QString QPieSlice::label() const
103 QString QPieSlice::label() const
103 {
104 {
104 return d->m_labelText;
105 return d->m_labelText;
105 }
106 }
106
107
107 /*!
108 /*!
108 Returns true if label is set as visible.
109 Returns true if label is set as visible.
109 \sa setLabelVisible()
110 \sa setLabelVisible()
110 */
111 */
111 bool QPieSlice::isLabelVisible() const
112 bool QPieSlice::isLabelVisible() const
112 {
113 {
113 return d->m_isLabelVisible;
114 return d->m_isLabelVisible;
114 }
115 }
115
116
116 /*!
117 /*!
117 Returns true if slice is exloded from the pie.
118 Returns true if slice is exloded from the pie.
118 \sa setExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
119 \sa setExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
119 */
120 */
120 bool QPieSlice::isExploded() const
121 bool QPieSlice::isExploded() const
121 {
122 {
122 return d->m_isExploded;
123 return d->m_isExploded;
123 }
124 }
124
125
125 /*!
126 /*!
126 Returns the explode distance factor.
127 Returns the explode distance factor.
127
128
128 The factor is relative to pie radius. For example:
129 The factor is relative to pie radius. For example:
129 1.0 means the distance is the same as the radius.
130 1.0 means the distance is the same as the radius.
130 0.5 means the distance is half of the radius.
131 0.5 means the distance is half of the radius.
131
132
132 Default value is 0.15.
133 Default value is 0.15.
133
134
134 \sa setExplodeDistanceFactor(), isExploded(), setExploded()
135 \sa setExplodeDistanceFactor(), isExploded(), setExploded()
135 */
136 */
136 qreal QPieSlice::explodeDistanceFactor() const
137 qreal QPieSlice::explodeDistanceFactor() const
137 {
138 {
138 return d->m_explodeDistanceFactor;
139 return d->m_explodeDistanceFactor;
139 }
140 }
140
141
141 /*!
142 /*!
142 Returns the percentage of this slice compared to the sum of all slices in the same series.
143 Returns the percentage of this slice compared to the sum of all slices in the same series.
143 The returned value ranges from 0 to 1.0.
144 The returned value ranges from 0 to 1.0.
144
145
145 Updated internally after the slice is added to the series.
146 Updated internally after the slice is added to the series.
146
147
147 \sa QPieSeries::sum()
148 \sa QPieSeries::sum()
148 */
149 */
149 qreal QPieSlice::percentage() const
150 qreal QPieSlice::percentage() const
150 {
151 {
151 return d->m_percentage;
152 return d->m_percentage;
152 }
153 }
153
154
154 /*!
155 /*!
155 Returns the starting angle of this slice in the series it belongs to.
156 Returns the starting angle of this slice in the series it belongs to.
156
157
157 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
158 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
158
159
159 Updated internally after the slice is added to the series.
160 Updated internally after the slice is added to the series.
160 */
161 */
161 qreal QPieSlice::startAngle() const
162 qreal QPieSlice::startAngle() const
162 {
163 {
163 return d->m_startAngle;
164 return d->m_startAngle;
164 }
165 }
165
166
166 /*!
167 /*!
167 Returns the end angle of this slice in the series it belongs to.
168 Returns the end angle of this slice in the series it belongs to.
168
169
169 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
170 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
170
171
171 Updated internally after the slice is added to the series.
172 Updated internally after the slice is added to the series.
172 */
173 */
173 qreal QPieSlice::endAngle() const
174 qreal QPieSlice::endAngle() const
174 {
175 {
175 return d->m_startAngle + d->m_angleSpan;
176 return d->m_startAngle + d->m_angleSpan;
176 }
177 }
177
178
178 /*!
179 /*!
179 Returns the pen used to draw this slice.
180 Returns the pen used to draw this slice.
180 \sa setPen()
181 \sa setPen()
181 */
182 */
182 QPen QPieSlice::pen() const
183 QPen QPieSlice::pen() const
183 {
184 {
184 return d->m_slicePen;
185 return d->m_slicePen;
185 }
186 }
186
187
187 /*!
188 /*!
188 Returns the brush used to draw this slice.
189 Returns the brush used to draw this slice.
189 \sa setBrush()
190 \sa setBrush()
190 */
191 */
191 QBrush QPieSlice::brush() const
192 QBrush QPieSlice::brush() const
192 {
193 {
193 return d->m_sliceBrush;
194 return d->m_sliceBrush;
194 }
195 }
195
196
196 /*!
197 /*!
197 Returns the pen used to draw the label in this slice.
198 Returns the pen used to draw the label in this slice.
198 \sa setLabelPen()
199 \sa setLabelPen()
199 */
200 */
200 QPen QPieSlice::labelPen() const
201 QPen QPieSlice::labelPen() const
201 {
202 {
202 return d->m_labelPen;
203 return d->m_labelPen;
203 }
204 }
204
205
205 /*!
206 /*!
206 Returns the font used to draw label in this slice.
207 Returns the font used to draw label in this slice.
207 \sa setLabelFont()
208 \sa setLabelFont()
208 */
209 */
209 QFont QPieSlice::labelFont() const
210 QFont QPieSlice::labelFont() const
210 {
211 {
211 return d->m_labelFont;
212 return d->m_labelFont;
212 }
213 }
213
214
214 /*!
215 /*!
215 Gets the label arm length factor.
216 Gets the label arm length factor.
216
217
217 The factor is relative to pie radius. For example:
218 The factor is relative to pie radius. For example:
218 1.0 means the length is the same as the radius.
219 1.0 means the length is the same as the radius.
219 0.5 means the length is half of the radius.
220 0.5 means the length is half of the radius.
220
221
221 Default value is 0.15
222 Default value is 0.15
222
223
223 \sa setLabelArmLengthFactor()
224 \sa setLabelArmLengthFactor()
224 */
225 */
225 qreal QPieSlice::labelArmLengthFactor() const
226 qreal QPieSlice::labelArmLengthFactor() const
226 {
227 {
227 return d->m_labelArmLengthFactor;
228 return d->m_labelArmLengthFactor;
228 }
229 }
229
230
230 /*!
231 /*!
231 \fn void QPieSlice::clicked()
232 \fn void QPieSlice::clicked()
232
233
233 This signal is emitted when user has clicked the slice.
234 This signal is emitted when user has clicked the slice.
234
235
235 \sa QPieSeries::clicked()
236 \sa QPieSeries::clicked()
236 */
237 */
237
238
238 /*!
239 /*!
239 \fn void QPieSlice::hovered(bool state)
240 \fn void QPieSlice::hovered(bool state)
240
241
241 This signal is emitted when user has hovered over or away from the slice.
242 This signal is emitted when user has hovered over or away from the slice.
242
243
243 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
244 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
244
245
245 \sa QPieSeries::hovered()
246 \sa QPieSeries::hovered()
246 */
247 */
247
248
248 /*!
249 /*!
249 \fn void QPieSlice::changed()
250 \fn void QPieSlice::changed()
250
251
251 This signal emitted when something has changed in the slice.
252 This signal emitted when something has changed in the slice.
252 */
253 */
253
254
254 /*!
255 /*!
255 Sets the \a value of this slice.
256 Sets the \a value of this slice.
256 \sa value()
257 \sa value()
257 */
258 */
258 void QPieSlice::setValue(qreal value)
259 void QPieSlice::setValue(qreal value)
259 {
260 {
260 if (!qFuzzyIsNull(d->m_value - value)) {
261 if (!qFuzzyIsNull(d->m_value - value)) {
261 d->m_value = value;
262 d->m_value = value;
262 emit changed();
263 emit changed();
263 }
264 }
264 }
265 }
265
266
266 /*!
267 /*!
267 Sets the \a label of the slice.
268 Sets the \a label of the slice.
268 \sa label()
269 \sa label()
269 */
270 */
270 void QPieSlice::setLabel(QString label)
271 void QPieSlice::setLabel(QString label)
271 {
272 {
272 if (d->m_labelText != label) {
273 if (d->m_labelText != label) {
273 d->m_labelText = label;
274 d->m_labelText = label;
275
274 emit changed();
276 emit changed();
275 }
277 }
276 }
278 }
277
279
278 /*!
280 /*!
279 Sets the label \a visible in this slice.
281 Sets the label \a visible in this slice.
280 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
282 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
281 */
283 */
282 void QPieSlice::setLabelVisible(bool visible)
284 void QPieSlice::setLabelVisible(bool visible)
283 {
285 {
284 if (d->m_isLabelVisible != visible) {
286 if (d->m_isLabelVisible != visible) {
285 d->m_isLabelVisible = visible;
287 d->m_isLabelVisible = visible;
286 emit changed();
288 emit changed();
287 }
289 }
288 }
290 }
289
291
290 /*!
292 /*!
291 Sets this slices \a exploded state.
293 Sets this slices \a exploded state.
292
294
293 If the slice is exploded it is moved away from the pie center. The distance is defined by the explode distance factor.
295 If the slice is exploded it is moved away from the pie center. The distance is defined by the explode distance factor.
294
296
295 \sa isExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
297 \sa isExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
296 */
298 */
297 void QPieSlice::setExploded(bool exploded)
299 void QPieSlice::setExploded(bool exploded)
298 {
300 {
299 if (d->m_isExploded != exploded) {
301 if (d->m_isExploded != exploded) {
300 d->m_isExploded = exploded;
302 d->m_isExploded = exploded;
301 emit changed();
303 emit changed();
302 }
304 }
303 }
305 }
304
306
305 /*!
307 /*!
306 Sets the explode distance \a factor.
308 Sets the explode distance \a factor.
307
309
308 The factor is relative to pie radius. For example:
310 The factor is relative to pie radius. For example:
309 1.0 means the distance is the same as the radius.
311 1.0 means the distance is the same as the radius.
310 0.5 means the distance is half of the radius.
312 0.5 means the distance is half of the radius.
311
313
312 Default value is 0.15
314 Default value is 0.15
313
315
314 \sa explodeDistanceFactor(), isExploded(), setExploded()
316 \sa explodeDistanceFactor(), isExploded(), setExploded()
315 */
317 */
316 void QPieSlice::setExplodeDistanceFactor(qreal factor)
318 void QPieSlice::setExplodeDistanceFactor(qreal factor)
317 {
319 {
318 if (!qFuzzyIsNull(d->m_explodeDistanceFactor - factor)) {
320 if (!qFuzzyIsNull(d->m_explodeDistanceFactor - factor)) {
319 d->m_explodeDistanceFactor = factor;
321 d->m_explodeDistanceFactor = factor;
320 emit changed();
322 emit changed();
321 }
323 }
322 }
324 }
323
325
324 /*!
326 /*!
325 Sets the \a pen used to draw this slice.
327 Sets the \a pen used to draw this slice.
326
328
327 Overrides the pen set by the theme.
329 Overrides the pen set by the theme.
328
330
329 \sa pen()
331 \sa pen()
330 */
332 */
331 void QPieSlice::setPen(const QPen &pen)
333 void QPieSlice::setPen(const QPen &pen)
332 {
334 {
333 if (d->m_slicePen != pen) {
335 if (d->m_slicePen != pen) {
334 d->m_slicePen = pen;
336 d->m_slicePen = pen;
335 d->m_slicePen.setThemed(false);
337 d->m_slicePen.setThemed(false);
336 emit changed();
338 emit changed();
337 }
339 }
338 }
340 }
339
341
340 /*!
342 /*!
341 Sets the \a brush used to draw this slice.
343 Sets the \a brush used to draw this slice.
342
344
343 Overrides the brush set by the theme.
345 Overrides the brush set by the theme.
344
346
345 \sa brush()
347 \sa brush()
346 */
348 */
347 void QPieSlice::setBrush(const QBrush &brush)
349 void QPieSlice::setBrush(const QBrush &brush)
348 {
350 {
349 if (d->m_sliceBrush != brush) {
351 if (d->m_sliceBrush != brush) {
350 d->m_sliceBrush = brush;
352 d->m_sliceBrush = brush;
351 d->m_sliceBrush.setThemed(false);
353 d->m_sliceBrush.setThemed(false);
352 emit changed();
354 emit changed();
353 }
355 }
354 }
356 }
355
357
356 /*!
358 /*!
357 Sets the \a pen used to draw the label in this slice.
359 Sets the \a pen used to draw the label in this slice.
358
360
359 Overrides the pen set by the theme.
361 Overrides the pen set by the theme.
360
362
361 \sa labelPen()
363 \sa labelPen()
362 */
364 */
363 void QPieSlice::setLabelPen(const QPen &pen)
365 void QPieSlice::setLabelPen(const QPen &pen)
364 {
366 {
365 if (d->m_labelPen != pen) {
367 if (d->m_labelPen != pen) {
366 d->m_labelPen = pen;
368 d->m_labelPen = pen;
367 d->m_labelPen.setThemed(false);
369 d->m_labelPen.setThemed(false);
368 emit changed();
370 emit changed();
369 }
371 }
370 }
372 }
371
373
372 /*!
374 /*!
373 Sets the \a font used to draw the label in this slice.
375 Sets the \a font used to draw the label in this slice.
374
376
375 Overrides the font set by the theme.
377 Overrides the font set by the theme.
376
378
377 \sa labelFont()
379 \sa labelFont()
378 */
380 */
379 void QPieSlice::setLabelFont(const QFont &font)
381 void QPieSlice::setLabelFont(const QFont &font)
380 {
382 {
381 if (d->m_labelFont != font) {
383 if (d->m_labelFont != font) {
382 d->m_labelFont = font;
384 d->m_labelFont = font;
383 d->m_labelFont.setThemed(false);
385 d->m_labelFont.setThemed(false);
384 emit changed();
386 emit changed();
385 }
387 }
386 }
388 }
387
389
388 /*!
390 /*!
389 Sets the label arm length \a factor.
391 Sets the label arm length \a factor.
390
392
391 The factor is relative to pie radius. For example:
393 The factor is relative to pie radius. For example:
392 1.0 means the length is the same as the radius.
394 1.0 means the length is the same as the radius.
393 0.5 means the length is half of the radius.
395 0.5 means the length is half of the radius.
394
396
395 Default value is 0.15
397 Default value is 0.15
396
398
397 \sa labelArmLengthFactor()
399 \sa labelArmLengthFactor()
398 */
400 */
399 void QPieSlice::setLabelArmLengthFactor(qreal factor)
401 void QPieSlice::setLabelArmLengthFactor(qreal factor)
400 {
402 {
401 if (!qFuzzyIsNull(d->m_labelArmLengthFactor - factor)) {
403 if (!qFuzzyIsNull(d->m_labelArmLengthFactor - factor)) {
402 d->m_labelArmLengthFactor = factor;
404 d->m_labelArmLengthFactor = factor;
403 emit changed();
405 emit changed();
404 }
406 }
405 }
407 }
406
408
407 QTCOMMERCIALCHART_END_NAMESPACE
409 QTCOMMERCIALCHART_END_NAMESPACE
408
410
409 QTCOMMERCIALCHART_USE_NAMESPACE
411 QTCOMMERCIALCHART_USE_NAMESPACE
410 #include "moc_qpieslice.cpp"
412 #include "moc_qpieslice.cpp"
@@ -1,479 +1,479
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 <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qchartview.h>
22 #include <qchartview.h>
23 #include <qchart.h>
23 #include <qchart.h>
24 #include <qpieseries.h>
24 #include <qpieseries.h>
25 #include <qpieslice.h>
25 #include <qpieslice.h>
26 #include <qpiemodelmapper.h>
26 #include <qpiemodelmapper.h>
27 #include <QStandardItemModel>
27 #include <QStandardItemModel>
28 #include <tst_definitions.h>
28 #include <tst_definitions.h>
29
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
31
32 Q_DECLARE_METATYPE(QPieSlice*)
32 Q_DECLARE_METATYPE(QPieSlice*)
33
33
34 class tst_qpieseries : public QObject
34 class tst_qpieseries : public QObject
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37
37
38 public slots:
38 public slots:
39 void initTestCase();
39 void initTestCase();
40 void cleanupTestCase();
40 void cleanupTestCase();
41 void init();
41 void init();
42 void cleanup();
42 void cleanup();
43
43
44 private slots:
44 private slots:
45 void construction();
45 void construction();
46 void append();
46 void append();
47 void insert();
47 void insert();
48 void remove();
48 void remove();
49 void calculatedValues();
49 void calculatedValues();
50 void clickedSignal();
50 void clickedSignal();
51 void hoverSignal();
51 void hoverSignal();
52 void model();
52 void model();
53 void modelCustomMap();
53 void modelCustomMap();
54 void modelUpdate();
54 void modelUpdate();
55
55
56 private:
56 private:
57 void verifyCalculatedData(const QPieSeries &series, bool *ok);
57 void verifyCalculatedData(const QPieSeries &series, bool *ok);
58
58
59 private:
59 private:
60
60
61 };
61 };
62
62
63 void tst_qpieseries::initTestCase()
63 void tst_qpieseries::initTestCase()
64 {
64 {
65 qRegisterMetaType<QPieSlice*>("QPieSlice*");
65 qRegisterMetaType<QPieSlice*>("QPieSlice*");
66 }
66 }
67
67
68 void tst_qpieseries::cleanupTestCase()
68 void tst_qpieseries::cleanupTestCase()
69 {
69 {
70 }
70 }
71
71
72 void tst_qpieseries::init()
72 void tst_qpieseries::init()
73 {
73 {
74
74
75 }
75 }
76
76
77 void tst_qpieseries::cleanup()
77 void tst_qpieseries::cleanup()
78 {
78 {
79
79
80 }
80 }
81
81
82 void tst_qpieseries::construction()
82 void tst_qpieseries::construction()
83 {
83 {
84 // verify default values
84 // verify default values
85 QPieSeries s;
85 QPieSeries s;
86 QVERIFY(s.type() == QAbstractSeries::SeriesTypePie);
86 QVERIFY(s.type() == QAbstractSeries::SeriesTypePie);
87 QVERIFY(s.count() == 0);
87 QVERIFY(s.count() == 0);
88 QVERIFY(s.isEmpty());
88 QVERIFY(s.isEmpty());
89 QCOMPARE(s.sum(), 0.0);
89 QCOMPARE(s.sum(), 0.0);
90 QCOMPARE(s.horizontalPosition(), 0.5);
90 QCOMPARE(s.horizontalPosition(), 0.5);
91 QCOMPARE(s.verticalPosition(), 0.5);
91 QCOMPARE(s.verticalPosition(), 0.5);
92 QCOMPARE(s.pieSize(), 0.7);
92 QCOMPARE(s.pieSize(), 0.7);
93 QCOMPARE(s.pieStartAngle(), 0.0);
93 QCOMPARE(s.pieStartAngle(), 0.0);
94 QCOMPARE(s.pieEndAngle(), 360.0);
94 QCOMPARE(s.pieEndAngle(), 360.0);
95 }
95 }
96
96
97 void tst_qpieseries::append()
97 void tst_qpieseries::append()
98 {
98 {
99 QPieSeries s;
99 QPieSeries s;
100
100
101 // append pointer
101 // append pointer
102 QPieSlice *slice1 = 0;
102 QPieSlice *slice1 = 0;
103 QVERIFY(!s.append(slice1));
103 QVERIFY(!s.append(slice1));
104 slice1 = new QPieSlice(1, "slice 1");
104 slice1 = new QPieSlice(1, "slice 1");
105 QVERIFY(s.append(slice1));
105 QVERIFY(s.append(slice1));
106 QVERIFY(!s.append(slice1));
106 QVERIFY(!s.append(slice1));
107 QCOMPARE(s.count(), 1);
107 QCOMPARE(s.count(), 1);
108
108
109 // append pointer list
109 // append pointer list
110 QList<QPieSlice *> list;
110 QList<QPieSlice *> list;
111 QVERIFY(!s.append(list));
111 QVERIFY(!s.append(list));
112 list << (QPieSlice *) 0;
112 list << (QPieSlice *) 0;
113 QVERIFY(!s.append(list));
113 QVERIFY(!s.append(list));
114 list.clear();
114 list.clear();
115 list << new QPieSlice(2, "slice 2");
115 list << new QPieSlice(2, "slice 2");
116 list << new QPieSlice(3, "slice 3");
116 list << new QPieSlice(3, "slice 3");
117 QVERIFY(s.append(list));
117 QVERIFY(s.append(list));
118 QVERIFY(!s.append(list));
118 QVERIFY(!s.append(list));
119 QCOMPARE(s.count(), 3);
119 QCOMPARE(s.count(), 3);
120
120
121 // append operator
121 // append operator
122 s << new QPieSlice(4, "slice 4");
122 s << new QPieSlice(4, "slice 4");
123 s << slice1; // fails because already added
123 s << slice1; // fails because already added
124 QCOMPARE(s.count(), 4);
124 QCOMPARE(s.count(), 4);
125
125
126 // append with params
126 // append with params
127 QPieSlice *slice5 = s.append(5, "slice 5");
127 QPieSlice *slice5 = s.append(5, "slice 5");
128 QVERIFY(slice5 != 0);
128 QVERIFY(slice5 != 0);
129 QCOMPARE(slice5->value(), 5.0);
129 QCOMPARE(slice5->value(), 5.0);
130 QCOMPARE(slice5->label(), QString("slice 5"));
130 QCOMPARE(slice5->label(), QString("slice 5"));
131 QCOMPARE(s.count(), 5);
131 QCOMPARE(s.count(), 5);
132
132
133 // check slices
133 // check slices
134 QVERIFY(!s.isEmpty());
134 QVERIFY(!s.isEmpty());
135 for (int i=0; i<s.count(); i++) {
135 for (int i=0; i<s.count(); i++) {
136 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
136 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
137 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
137 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
138 }
138 }
139 }
139 }
140
140
141 void tst_qpieseries::insert()
141 void tst_qpieseries::insert()
142 {
142 {
143 QPieSeries s;
143 QPieSeries s;
144
144
145 // insert one slice
145 // insert one slice
146 QPieSlice *slice1 = 0;
146 QPieSlice *slice1 = 0;
147 QVERIFY(!s.insert(0, slice1));
147 QVERIFY(!s.insert(0, slice1));
148 slice1 = new QPieSlice(1, "slice 1");
148 slice1 = new QPieSlice(1, "slice 1");
149 QVERIFY(!s.insert(-1, slice1));
149 QVERIFY(!s.insert(-1, slice1));
150 QVERIFY(!s.insert(5, slice1));
150 QVERIFY(!s.insert(5, slice1));
151 QVERIFY(s.insert(0, slice1));
151 QVERIFY(s.insert(0, slice1));
152 QVERIFY(!s.insert(0, slice1));
152 QVERIFY(!s.insert(0, slice1));
153 QCOMPARE(s.count(), 1);
153 QCOMPARE(s.count(), 1);
154
154
155 // add some more slices
155 // add some more slices
156 s.append(2, "slice 2");
156 s.append(2, "slice 2");
157 s.append(4, "slice 4");
157 s.append(4, "slice 4");
158 QCOMPARE(s.count(), 3);
158 QCOMPARE(s.count(), 3);
159
159
160 // insert between slices
160 // insert between slices
161 s.insert(2, new QPieSlice(3, "slice 3"));
161 s.insert(2, new QPieSlice(3, "slice 3"));
162 QCOMPARE(s.count(), 4);
162 QCOMPARE(s.count(), 4);
163
163
164 // check slices
164 // check slices
165 for (int i=0; i<s.count(); i++) {
165 for (int i=0; i<s.count(); i++) {
166 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
166 QCOMPARE(s.slices().at(i)->value(), (qreal) i+1);
167 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
167 QCOMPARE(s.slices().at(i)->label(), QString("slice ") + QString::number(i+1));
168 }
168 }
169 }
169 }
170
170
171 void tst_qpieseries::remove()
171 void tst_qpieseries::remove()
172 {
172 {
173 QPieSeries s;
173 QPieSeries s;
174
174
175 // add some slices
175 // add some slices
176 QPieSlice *slice1 = s.append(1, "slice 1");
176 QPieSlice *slice1 = s.append(1, "slice 1");
177 QPieSlice *slice2 = s.append(2, "slice 2");
177 QPieSlice *slice2 = s.append(2, "slice 2");
178 QPieSlice *slice3 = s.append(3, "slice 3");
178 QPieSlice *slice3 = s.append(3, "slice 3");
179 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
179 QSignalSpy spy1(slice1, SIGNAL(destroyed()));
180 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
180 QSignalSpy spy2(slice2, SIGNAL(destroyed()));
181 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
181 QSignalSpy spy3(slice3, SIGNAL(destroyed()));
182 QCOMPARE(s.count(), 3);
182 QCOMPARE(s.count(), 3);
183
183
184 // null pointer remove
184 // null pointer remove
185 QVERIFY(!s.remove(0));
185 QVERIFY(!s.remove(0));
186
186
187 // remove first
187 // remove first
188 QVERIFY(s.remove(slice1));
188 QVERIFY(s.remove(slice1));
189 QVERIFY(!s.remove(slice1));
189 QVERIFY(!s.remove(slice1));
190 QCOMPARE(s.count(), 2);
190 QCOMPARE(s.count(), 2);
191 QCOMPARE(s.slices().at(0)->label(), slice2->label());
191 QCOMPARE(s.slices().at(0)->label(), slice2->label());
192
192
193 // remove all
193 // remove all
194 s.clear();
194 s.clear();
195 QVERIFY(s.isEmpty());
195 QVERIFY(s.isEmpty());
196 QVERIFY(s.slices().isEmpty());
196 QVERIFY(s.slices().isEmpty());
197 QCOMPARE(s.count(), 0);
197 QCOMPARE(s.count(), 0);
198
198
199 // check that slices were actually destroyed
199 // check that slices were actually destroyed
200 TRY_COMPARE(spy1.count(), 1);
200 TRY_COMPARE(spy1.count(), 1);
201 TRY_COMPARE(spy2.count(), 1);
201 TRY_COMPARE(spy2.count(), 1);
202 TRY_COMPARE(spy3.count(), 1);
202 TRY_COMPARE(spy3.count(), 1);
203 }
203 }
204
204
205 void tst_qpieseries::calculatedValues()
205 void tst_qpieseries::calculatedValues()
206 {
206 {
207 bool ok;
207 bool ok;
208 QPieSeries s;
208 QPieSeries s;
209
209
210 // add a slice
210 // add a slice
211 QPieSlice *slice1 = s.append(1, "slice 1");
211 QPieSlice *slice1 = s.append(1, "slice 1");
212 verifyCalculatedData(s, &ok);
212 verifyCalculatedData(s, &ok);
213 if (!ok)
213 if (!ok)
214 return;
214 return;
215
215
216 // add some more slices
216 // add some more slices
217 QList<QPieSlice *> list;
217 QList<QPieSlice *> list;
218 list << new QPieSlice(2, "slice 2");
218 list << new QPieSlice(2, "slice 2");
219 list << new QPieSlice(3, "slice 3");
219 list << new QPieSlice(3, "slice 3");
220 s.append(list);
220 s.append(list);
221 verifyCalculatedData(s, &ok);
221 verifyCalculatedData(s, &ok);
222 if (!ok)
222 if (!ok)
223 return;
223 return;
224
224
225 // remove a slice
225 // remove a slice
226 s.remove(slice1);
226 s.remove(slice1);
227 verifyCalculatedData(s, &ok);
227 verifyCalculatedData(s, &ok);
228 if (!ok)
228 if (!ok)
229 return;
229 return;
230
230
231 // insert a slice
231 // insert a slice
232 s.insert(0, new QPieSlice(1, "Slice 4"));
232 s.insert(0, new QPieSlice(1, "Slice 4"));
233 verifyCalculatedData(s, &ok);
233 verifyCalculatedData(s, &ok);
234 if (!ok)
234 if (!ok)
235 return;
235 return;
236
236
237 // clear all
237 // clear all
238 s.clear();
238 s.clear();
239 verifyCalculatedData(s, &ok);
239 verifyCalculatedData(s, &ok);
240 }
240 }
241
241
242 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
242 void tst_qpieseries::verifyCalculatedData(const QPieSeries &series, bool *ok)
243 {
243 {
244 *ok = false;
244 *ok = false;
245
245
246 qreal sum = 0;
246 qreal sum = 0;
247 foreach (const QPieSlice *slice, series.slices())
247 foreach (const QPieSlice *slice, series.slices())
248 sum += slice->value();
248 sum += slice->value();
249 QCOMPARE(series.sum(), sum);
249 QCOMPARE(series.sum(), sum);
250
250
251 qreal startAngle = series.pieStartAngle();
251 qreal startAngle = series.pieStartAngle();
252 qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle();
252 qreal pieAngleSpan = series.pieEndAngle() - series.pieStartAngle();
253 foreach (const QPieSlice *slice, series.slices()) {
253 foreach (const QPieSlice *slice, series.slices()) {
254 qreal ratio = slice->value() / sum;
254 qreal ratio = slice->value() / sum;
255 qreal sliceSpan = pieAngleSpan * ratio;
255 qreal sliceSpan = pieAngleSpan * ratio;
256 QCOMPARE(slice->startAngle(), startAngle);
256 QCOMPARE(slice->startAngle(), startAngle);
257 QCOMPARE(slice->endAngle(), startAngle + sliceSpan);
257 QCOMPARE(slice->endAngle(), startAngle + sliceSpan);
258 QCOMPARE(slice->percentage(), ratio);
258 QCOMPARE(slice->percentage(), ratio);
259 startAngle += sliceSpan;
259 startAngle += sliceSpan;
260 }
260 }
261
261
262 if (!series.isEmpty())
262 if (!series.isEmpty())
263 QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle());
263 QCOMPARE(series.slices().last()->endAngle(), series.pieEndAngle());
264
264
265 *ok = true;
265 *ok = true;
266 }
266 }
267
267
268
268
269 void tst_qpieseries::clickedSignal()
269 void tst_qpieseries::clickedSignal()
270 {
270 {
271 // create a pie series
271 // create a pie series
272 QPieSeries *series = new QPieSeries();
272 QPieSeries *series = new QPieSeries();
273 series->setPieSize(1.0);
273 series->setPieSize(1.0);
274 QPieSlice *s1 = series->append(1, "slice 1");
274 QPieSlice *s1 = series->append(1, "slice 1");
275 series->append(2, "slice 2");
275 series->append(2, "slice 2");
276 series->append(3, "slice 3");
276 series->append(3, "slice 3");
277 QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*)));
277 QSignalSpy clickSpy1(series, SIGNAL(clicked(QPieSlice*)));
278
278
279 // add series to the chart
279 // add series to the chart
280 QChartView view(new QChart());
280 QChartView view(new QChart());
281 view.chart()->legend()->setVisible(false);
281 view.chart()->legend()->setVisible(false);
282 view.resize(200, 200);
282 view.resize(200, 200);
283 view.chart()->addSeries(series);
283 view.chart()->addSeries(series);
284 view.show();
284 view.show();
285 QTest::qWaitForWindowShown(&view);
285 QTest::qWaitForWindowShown(&view);
286
286
287 // simulate clicks
287 // simulate clicks
288 // pie rectangle: QRectF(60,60 121x121)
288 // pie rectangle: QRectF(60,60 121x121)
289 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 90)); // inside slice 1
289 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 90)); // inside slice 1
290 TRY_COMPARE(clickSpy1.count(), 1);
290 TRY_COMPARE(clickSpy1.count(), 1);
291 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1);
291 QCOMPARE(qvariant_cast<QPieSlice*>(clickSpy1.at(0).at(0)), s1);
292 }
292 }
293
293
294 void tst_qpieseries::hoverSignal()
294 void tst_qpieseries::hoverSignal()
295 {
295 {
296 // create a pie series
296 // create a pie series
297 QPieSeries *series = new QPieSeries();
297 QPieSeries *series = new QPieSeries();
298 series->setPieSize(1.0);
298 series->setPieSize(1.0);
299 QPieSlice *s1 = series->append(1, "slice 1");
299 QPieSlice *s1 = series->append(1, "slice 1");
300 series->append(2, "slice 2");
300 series->append(2, "slice 2");
301 series->append(3, "slice 3");
301 series->append(3, "slice 3");
302
302
303 // add series to the chart
303 // add series to the chart
304 QChartView view(new QChart());
304 QChartView view(new QChart());
305 view.chart()->legend()->setVisible(false);
305 view.chart()->legend()->setVisible(false);
306 view.resize(200, 200);
306 view.resize(200, 200);
307 view.chart()->addSeries(series);
307 view.chart()->addSeries(series);
308 view.show();
308 view.show();
309 QTest::qWaitForWindowShown(&view);
309 QTest::qWaitForWindowShown(&view);
310
310
311 // first move to right top corner
311 // first move to right top corner
312 QTest::mouseMove(view.viewport(), QPoint(200, 0));
312 QTest::mouseMove(view.viewport(), QPoint(200, 0));
313 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
313 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
314
314
315 // move inside the slice
315 // move inside the slice
316 // pie rectangle: QRectF(60,60 121x121)
316 // pie rectangle: QRectF(60,60 121x121)
317 QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool)));
317 QSignalSpy hoverSpy(series, SIGNAL(hovered(QPieSlice*,bool)));
318 QTest::mouseMove(view.viewport(), QPoint(139, 85));
318 QTest::mouseMove(view.viewport(), QPoint(139, 85));
319 TRY_COMPARE(hoverSpy.count(), 1);
319 TRY_COMPARE(hoverSpy.count(), 1);
320 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1);
320 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(0).at(0)), s1);
321 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true);
321 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(1)), true);
322
322
323 // move outside the slice
323 // move outside the slice
324 QTest::mouseMove(view.viewport(), QPoint(200, 0));
324 QTest::mouseMove(view.viewport(), QPoint(200, 0));
325 TRY_COMPARE(hoverSpy.count(), 2);
325 TRY_COMPARE(hoverSpy.count(), 2);
326 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1);
326 QCOMPARE(qvariant_cast<QPieSlice*>(hoverSpy.at(1).at(0)), s1);
327 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false);
327 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(1)), false);
328 }
328 }
329
329
330 void tst_qpieseries::model()
330 void tst_qpieseries::model()
331 {
331 {
332 QPieSeries *series = new QPieSeries;
332 QPieSeries *series = new QPieSeries;
333 QChart *chart = new QChart;
333 QChart *chart = new QChart;
334 chart->addSeries(series);
334 chart->addSeries(series);
335 QChartView *chartView = new QChartView(chart);
335 QChartView *chartView = new QChartView(chart);
336 chartView->show();
336 chartView->show();
337
337
338 QStandardItemModel *stdModel = new QStandardItemModel(0, 2);
338 QStandardItemModel *stdModel = new QStandardItemModel(0, 2);
339 series->setModel(stdModel);
339 series->setModel(stdModel);
340 QVERIFY2((series->model()) == stdModel, "Model should be stdModel");
340 QVERIFY2((series->model()) == stdModel, "Model should be stdModel");
341
341
342 int rowCount = 3;
342 int rowCount = 3;
343 for (int row = 0; row < rowCount; ++row) {
343 for (int row = 0; row < rowCount; ++row) {
344 for (int column = 0; column < 2; column++) {
344 for (int column = 0; column < 2; column++) {
345 QStandardItem *item = new QStandardItem(row * column);
345 QStandardItem *item = new QStandardItem(row * column);
346 stdModel->setItem(row, column, item);
346 stdModel->setItem(row, column, item);
347 }
347 }
348 }
348 }
349
349
350 // data has been added to the model, but mapper is not set the number of slices should still be 0
350 // data has been added to the model, but mapper is not set the number of slices should still be 0
351 QVERIFY2(series->slices().count() == 0, "Mapper has not been set, so the number of slices should be 0");
351 QVERIFY2(series->slices().count() == 0, "Mapper has not been set, so the number of slices should be 0");
352
352
353 // set the mapper
353 // set the mapper
354 QPieModelMapper *mapper = new QPieModelMapper;
354 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
355 mapper->setMapValues(0);
355 mapper->setMapValues(0);
356 mapper->setMapLabels(0);
356 mapper->setMapLabels(0);
357 series->setModelMapper(mapper); // this should cause the Pie to get initialized from the model, since there is now both the model and the mapper defined
357 // series->setModelMapper(mapper); // this should cause the Pie to get initialized from the model, since there is now both the model and the mapper defined
358 QCOMPARE(series->slices().count(), rowCount);
358 QCOMPARE(series->slices().count(), rowCount);
359
359
360 // set the mappings to be outside of the model
360 // set the mappings to be outside of the model
361 mapper->setMapLabels(5);
361 mapper->setMapLabels(5);
362 mapper->setMapValues(4);
362 mapper->setMapValues(4);
363 QCOMPARE(series->slices().count(), 0); // Mappings are invalid, so the number of slices should be 0
363 QCOMPARE(series->slices().count(), 0); // Mappings are invalid, so the number of slices should be 0
364
364
365 // set back to correct ones
365 // set back to correct ones
366 mapper->setMapValues(0);
366 mapper->setMapValues(0);
367 mapper->setMapLabels(0);
367 mapper->setMapLabels(0);
368 QCOMPARE(series->slices().count(), rowCount);
368 QCOMPARE(series->slices().count(), rowCount);
369
369
370 // reset the mappings
370 // reset the mappings
371 mapper->reset();
371 mapper->reset();
372 QCOMPARE(series->slices().count(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0
372 QCOMPARE(series->slices().count(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0
373
373
374 // unset the model and the mapper
374 // unset the model and the mapper
375 series->setModel(0);
375 series->setModel(0);
376 series->setModelMapper(0);
376 // series->setModelMapper(0);
377 QVERIFY(series->model() == 0); // Model should be unset
377 QVERIFY(series->model() == 0); // Model should be unset
378 QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
378 // QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
379 }
379 }
380
380
381 void tst_qpieseries::modelCustomMap()
381 void tst_qpieseries::modelCustomMap()
382 {
382 {
383 int rowCount = 12;
383 int rowCount = 12;
384 int columnCount = 3;
384 int columnCount = 3;
385 QStandardItemModel *stdModel = new QStandardItemModel(0, 3);
385 QStandardItemModel *stdModel = new QStandardItemModel(0, 3);
386 for (int row = 0; row < rowCount; ++row) {
386 for (int row = 0; row < rowCount; ++row) {
387 for (int column = 0; column < 2; column++) {
387 for (int column = 0; column < 2; column++) {
388 QStandardItem *item = new QStandardItem(row * column);
388 QStandardItem *item = new QStandardItem(row * column);
389 stdModel->setItem(row, column, item);
389 stdModel->setItem(row, column, item);
390 }
390 }
391 }
391 }
392
392
393 QPieSeries *series = new QPieSeries;
393 QPieSeries *series = new QPieSeries;
394 QChart *chart = new QChart;
394 QChart *chart = new QChart;
395 chart->addSeries(series);
395 chart->addSeries(series);
396 QChartView *chartView = new QChartView(chart);
396 QChartView *chartView = new QChartView(chart);
397 chartView->show();
397 chartView->show();
398 series->setModel(stdModel);
398 series->setModel(stdModel);
399
399
400 QPieModelMapper *mapper = new QPieModelMapper;
400 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
401 mapper->setMapValues(0);
401 mapper->setMapValues(0);
402 mapper->setMapLabels(0);
402 mapper->setMapLabels(0);
403 series->setModelMapper(mapper);
403 // series->setModelMapper(mapper);
404 QCOMPARE(series->slices().count(), rowCount);
404 QCOMPARE(series->slices().count(), rowCount);
405
405
406 // lets change the orientation to horizontal
406 // lets change the orientation to horizontal
407 mapper->setOrientation(Qt::Horizontal);
407 mapper->setOrientation(Qt::Horizontal);
408 QCOMPARE(series->slices().count(), columnCount);
408 QCOMPARE(series->slices().count(), columnCount);
409
409
410 // change it back to vertical
410 // change it back to vertical
411 mapper->setOrientation(Qt::Vertical);
411 mapper->setOrientation(Qt::Vertical);
412 QCOMPARE(series->slices().count(), rowCount);
412 QCOMPARE(series->slices().count(), rowCount);
413
413
414 // lets customize the mapping
414 // lets customize the mapping
415 int first = 3;
415 int first = 3;
416 mapper->setFirst(first);
416 mapper->setFirst(first);
417 QCOMPARE(series->slices().count(), rowCount - first);
417 QCOMPARE(series->slices().count(), rowCount - first);
418 int count = 7;
418 int count = 7;
419 mapper->setCount(count);
419 mapper->setCount(count);
420 QCOMPARE(series->slices().count(), count);
420 QCOMPARE(series->slices().count(), count);
421 first = 9;
421 first = 9;
422 mapper->setFirst(first);
422 mapper->setFirst(first);
423 QCOMPARE(series->slices().count(), qMin(count, rowCount - first));
423 QCOMPARE(series->slices().count(), qMin(count, rowCount - first));
424 }
424 }
425
425
426 void tst_qpieseries::modelUpdate()
426 void tst_qpieseries::modelUpdate()
427 {
427 {
428 int rowCount = 12;
428 int rowCount = 12;
429 int columnCount = 7;
429 int columnCount = 7;
430 QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount);
430 QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount);
431 for (int row = 0; row < rowCount; ++row) {
431 for (int row = 0; row < rowCount; ++row) {
432 for (int column = 0; column < columnCount; column++) {
432 for (int column = 0; column < columnCount; column++) {
433 QStandardItem *item = new QStandardItem(row * column);
433 QStandardItem *item = new QStandardItem(row * column);
434 stdModel->setItem(row, column, item);
434 stdModel->setItem(row, column, item);
435 }
435 }
436 }
436 }
437
437
438 QPieSeries *series = new QPieSeries;
438 QPieSeries *series = new QPieSeries;
439 QChart *chart = new QChart;
439 QChart *chart = new QChart;
440 chart->addSeries(series);
440 chart->addSeries(series);
441 QChartView *chartView = new QChartView(chart);
441 QChartView *chartView = new QChartView(chart);
442 chartView->show();
442 chartView->show();
443 series->setModel(stdModel);
443 series->setModel(stdModel);
444
444
445 QPieModelMapper *mapper = new QPieModelMapper;
445 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
446 mapper->setMapValues(0);
446 mapper->setMapValues(0);
447 mapper->setMapLabels(0);
447 mapper->setMapLabels(0);
448 series->setModelMapper(mapper);
448 // series->setModelMapper(mapper);
449
449
450 stdModel->insertRows(3, 5);
450 stdModel->insertRows(3, 5);
451 QCOMPARE(series->slices().count(), rowCount + 5);
451 QCOMPARE(series->slices().count(), rowCount + 5);
452
452
453 stdModel->removeRows(10, 5);
453 stdModel->removeRows(10, 5);
454 QCOMPARE(series->slices().count(), rowCount);
454 QCOMPARE(series->slices().count(), rowCount);
455
455
456 // limit the number of slices taken from the model to 12
456 // limit the number of slices taken from the model to 12
457 mapper->setCount(rowCount);
457 mapper->setCount(rowCount);
458 stdModel->insertRows(3, 5);
458 stdModel->insertRows(3, 5);
459 QCOMPARE(series->slices().count(), rowCount);
459 QCOMPARE(series->slices().count(), rowCount);
460
460
461 stdModel->removeRows(0, 10);
461 stdModel->removeRows(0, 10);
462 QCOMPARE(series->slices().count(), rowCount - 5);
462 QCOMPARE(series->slices().count(), rowCount - 5);
463
463
464 // change the orientation to horizontal
464 // change the orientation to horizontal
465 mapper->setOrientation(Qt::Horizontal);
465 mapper->setOrientation(Qt::Horizontal);
466 QCOMPARE(series->slices().count(), columnCount);
466 QCOMPARE(series->slices().count(), columnCount);
467
467
468 stdModel->insertColumns(3, 10);
468 stdModel->insertColumns(3, 10);
469 QCOMPARE(series->slices().count(), rowCount); // count is limited to rowCount (12)
469 QCOMPARE(series->slices().count(), rowCount); // count is limited to rowCount (12)
470
470
471 stdModel->removeColumns(5, 10);
471 stdModel->removeColumns(5, 10);
472 QCOMPARE(series->slices().count(), columnCount);
472 QCOMPARE(series->slices().count(), columnCount);
473
473
474 }
474 }
475
475
476 QTEST_MAIN(tst_qpieseries)
476 QTEST_MAIN(tst_qpieseries)
477
477
478 #include "tst_qpieseries.moc"
478 #include "tst_qpieseries.moc"
479
479
@@ -1,468 +1,472
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 <QXYModelMapper>
29 #include <QXYModelMapper>
30 #include "customtablemodel.h"
30 #include "customtablemodel.h"
31 #include <QPieSeries>
31 #include <QPieSeries>
32 #include <QPieModelMapper>
32 #include <QPieModelMapper>
33 #include <QPieSlice>
33 #include <QPieSlice>
34 #include <QAreaSeries>
34 #include <QAreaSeries>
35 #include <QBarSeries>
35 #include <QBarSeries>
36 #include <QGroupedBarSeries>
36 #include <QGroupedBarSeries>
37 #include <QBarSet>
37 #include <QBarSet>
38 #include <QBarModelMapper>
38 #include <QBarModelMapper>
39 #include <QPushButton>
39 #include <QPushButton>
40 #include <QRadioButton>
40 #include <QRadioButton>
41 #include <QLabel>
41 #include <QLabel>
42 #include <QSpinBox>
42 #include <QSpinBox>
43 #include <QTime>
43 #include <QTime>
44 #include <QHeaderView>
44 #include <QHeaderView>
45
45
46 TableWidget::TableWidget(QWidget *parent)
46 TableWidget::TableWidget(QWidget *parent)
47 : QWidget(parent)
47 : QWidget(parent)
48 // specialPie(0)
48 // specialPie(0)
49 {
49 {
50 setGeometry(1900, 100, 1000, 600);
50 setGeometry(1900, 100, 1000, 600);
51 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
51 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
52 // create simple model for storing data
52 // create simple model for storing data
53 // user's table data model
53 // user's table data model
54 m_model = new CustomTableModel;
54 m_model = new CustomTableModel;
55 m_tableView = new QTableView;
55 m_tableView = new QTableView;
56 m_tableView->setModel(m_model);
56 m_tableView->setModel(m_model);
57 // m_tableView->setMinimumHeight(300);
57 // m_tableView->setMinimumHeight(300);
58 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
58 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
59 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
59 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
60
60
61 m_chart = new QChart;
61 m_chart = new QChart;
62 m_chart->legend()->setVisible(true);
62 m_chart->legend()->setVisible(true);
63 m_chart->setAnimationOptions(QChart::SeriesAnimations);
63 m_chart->setAnimationOptions(QChart::SeriesAnimations);
64 m_chartView = new QChartView(m_chart);
64 m_chartView = new QChartView(m_chart);
65 m_chartView->setRenderHint(QPainter::Antialiasing);
65 m_chartView->setRenderHint(QPainter::Antialiasing);
66 m_chartView->setMinimumSize(640, 480);
66 m_chartView->setMinimumSize(640, 480);
67
67
68 // add, remove data buttons
68 // add, remove data buttons
69 QPushButton* addRowAboveButton = new QPushButton("Add row above");
69 QPushButton* addRowAboveButton = new QPushButton("Add row above");
70 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
70 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
71
71
72 QPushButton* addRowBelowButton = new QPushButton("Add row below");
72 QPushButton* addRowBelowButton = new QPushButton("Add row below");
73 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
73 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
74
74
75 QPushButton* removeRowButton = new QPushButton("Remove row");
75 QPushButton* removeRowButton = new QPushButton("Remove row");
76 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
76 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
77
77
78 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
78 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
79 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
79 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
80
80
81 QPushButton* removeColumnButton = new QPushButton("Remove column");
81 QPushButton* removeColumnButton = new QPushButton("Remove column");
82 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
82 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
83
83
84 QPushButton* specialPieButton = new QPushButton("Test pie");
84 QPushButton* specialPieButton = new QPushButton("Test pie");
85 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
85 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
86
86
87
87
88 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
88 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
89
89
90 // spin box for setting number of affected items (add, remove)
90 // spin box for setting number of affected items (add, remove)
91 m_linesCountSpinBox = new QSpinBox;
91 m_linesCountSpinBox = new QSpinBox;
92 m_linesCountSpinBox->setRange(1, 10);
92 m_linesCountSpinBox->setRange(1, 10);
93 m_linesCountSpinBox->setValue(1);
93 m_linesCountSpinBox->setValue(1);
94
94
95 // buttons layout
95 // buttons layout
96 QVBoxLayout* buttonsLayout = new QVBoxLayout;
96 QVBoxLayout* buttonsLayout = new QVBoxLayout;
97 // buttonsLayout->addWidget(spinBoxLabel);
97 // buttonsLayout->addWidget(spinBoxLabel);
98 // buttonsLayout->addWidget(m_linesCountSpinBox);
98 // buttonsLayout->addWidget(m_linesCountSpinBox);
99 // buttonsLayout->addWidget(addRowAboveButton);
99 // buttonsLayout->addWidget(addRowAboveButton);
100 buttonsLayout->addWidget(addRowBelowButton);
100 buttonsLayout->addWidget(addRowBelowButton);
101 buttonsLayout->addWidget(removeRowButton);
101 buttonsLayout->addWidget(removeRowButton);
102 // buttonsLayout->addWidget(addColumnRightButton);
102 // buttonsLayout->addWidget(addColumnRightButton);
103 // buttonsLayout->addWidget(removeColumnButton);
103 // buttonsLayout->addWidget(removeColumnButton);
104 buttonsLayout->addWidget(specialPieButton);
104 buttonsLayout->addWidget(specialPieButton);
105 buttonsLayout->addStretch();
105 buttonsLayout->addStretch();
106
106
107 // chart type radio buttons
107 // chart type radio buttons
108 m_lineRadioButton = new QRadioButton("Line");
108 m_lineRadioButton = new QRadioButton("Line");
109 m_splineRadioButton = new QRadioButton("Spline");
109 m_splineRadioButton = new QRadioButton("Spline");
110 m_scatterRadioButton = new QRadioButton("Scatter");
110 m_scatterRadioButton = new QRadioButton("Scatter");
111 m_pieRadioButton = new QRadioButton("Pie");
111 m_pieRadioButton = new QRadioButton("Pie");
112 m_areaRadioButton = new QRadioButton("Area");
112 m_areaRadioButton = new QRadioButton("Area");
113 m_barRadioButton = new QRadioButton("Bar");
113 m_barRadioButton = new QRadioButton("Bar");
114
114
115 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
115 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
116 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
116 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
117 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
117 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
118 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
118 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
119 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
119 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
120 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
120 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
121 m_lineRadioButton->setChecked(true);
121 m_lineRadioButton->setChecked(true);
122
122
123 // radio buttons layout
123 // radio buttons layout
124 QVBoxLayout* radioLayout = new QVBoxLayout;
124 QVBoxLayout* radioLayout = new QVBoxLayout;
125 radioLayout->addWidget(m_lineRadioButton);
125 radioLayout->addWidget(m_lineRadioButton);
126 radioLayout->addWidget(m_splineRadioButton);
126 radioLayout->addWidget(m_splineRadioButton);
127 // radioLayout->addWidget(m_scatterRadioButton);
127 // radioLayout->addWidget(m_scatterRadioButton);
128 radioLayout->addWidget(m_pieRadioButton);
128 radioLayout->addWidget(m_pieRadioButton);
129 // radioLayout->addWidget(m_areaRadioButton);
129 // radioLayout->addWidget(m_areaRadioButton);
130 radioLayout->addWidget(m_barRadioButton);
130 radioLayout->addWidget(m_barRadioButton);
131 radioLayout->addStretch();
131 radioLayout->addStretch();
132
132
133 // create main layout
133 // create main layout
134 QGridLayout* mainLayout = new QGridLayout;
134 QGridLayout* mainLayout = new QGridLayout;
135 mainLayout->addLayout(buttonsLayout, 2, 0);
135 mainLayout->addLayout(buttonsLayout, 2, 0);
136 mainLayout->addLayout(radioLayout, 3, 0);
136 mainLayout->addLayout(radioLayout, 3, 0);
137 mainLayout->addWidget(m_tableView, 1, 0);
137 mainLayout->addWidget(m_tableView, 1, 0);
138 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
138 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
139 setLayout(mainLayout);
139 setLayout(mainLayout);
140 m_lineRadioButton->setFocus();
140 m_lineRadioButton->setFocus();
141 }
141 }
142
142
143 void TableWidget::addRowAbove()
143 void TableWidget::addRowAbove()
144 {
144 {
145 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
145 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
146
146
147 }
147 }
148
148
149 void TableWidget::addRowBelow()
149 void TableWidget::addRowBelow()
150 {
150 {
151 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
151 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
152
152
153 }
153 }
154
154
155 void TableWidget::removeRow()
155 void TableWidget::removeRow()
156 {
156 {
157 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
157 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
158 }
158 }
159
159
160 void TableWidget::addColumnRight()
160 void TableWidget::addColumnRight()
161 {
161 {
162 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
162 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
163 }
163 }
164
164
165 void TableWidget::removeColumn()
165 void TableWidget::removeColumn()
166 {
166 {
167 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
167 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
168 }
168 }
169
169
170 void TableWidget::updateChartType(bool toggle)
170 void TableWidget::updateChartType(bool toggle)
171 {
171 {
172 // this if is needed, so that the function is only called once.
172 // this if is needed, so that the function is only called once.
173 // For the radioButton that was enabled.
173 // For the radioButton that was enabled.
174 if (toggle) {
174 if (toggle) {
175 // specialPie = 0;
175 // specialPie = 0;
176 m_chart->removeAllSeries();
176 m_chart->removeAllSeries();
177 m_chart->axisX()->setNiceNumbersEnabled(false);
177 m_chart->axisX()->setNiceNumbersEnabled(false);
178 m_chart->axisY()->setNiceNumbersEnabled(false);
178 m_chart->axisY()->setNiceNumbersEnabled(false);
179
179
180 // renable axes of the chart (pie hides them)
180 // renable axes of the chart (pie hides them)
181 // x axis
181 // x axis
182 QAxis *axis = m_chart->axisX();
182 QAxis *axis = m_chart->axisX();
183 axis->setAxisVisible(true);
183 axis->setAxisVisible(true);
184 axis->setGridLineVisible(true);
184 axis->setGridLineVisible(true);
185 axis->setLabelsVisible(true);
185 axis->setLabelsVisible(true);
186
186
187 // y axis
187 // y axis
188 axis = m_chart->axisY();
188 axis = m_chart->axisY();
189 axis->setAxisVisible(true);
189 axis->setAxisVisible(true);
190 axis->setGridLineVisible(true);
190 axis->setGridLineVisible(true);
191 axis->setLabelsVisible(true);
191 axis->setLabelsVisible(true);
192
192
193 m_model->clearMapping();
193 m_model->clearMapping();
194
194
195 QString seriesColorHex = "#000000";
195 QString seriesColorHex = "#000000";
196 QPen pen;
196 QPen pen;
197 pen.setWidth(2);
197 pen.setWidth(2);
198
198
199 if (m_lineRadioButton->isChecked())
199 if (m_lineRadioButton->isChecked())
200 {
200 {
201 m_chart->setAnimationOptions(QChart::NoAnimation);
201 m_chart->setAnimationOptions(QChart::NoAnimation);
202
202
203 // series 1
203 // series 1
204 m_series = new QLineSeries;
204 m_series = new QLineSeries;
205 m_series->setModel(m_model);
205 m_series->setModel(m_model);
206
206
207 QXYModelMapper *mapper = new QXYModelMapper;
207 QXYModelMapper *mapper = new QXYModelMapper;
208 mapper->setMapX(0);
208 mapper->setMapX(0);
209 mapper->setMapY(1);
209 mapper->setMapY(1);
210 mapper->setFirst(3);
210 mapper->setFirst(3);
211 mapper->setCount(4);
211 mapper->setCount(4);
212 m_series->setModelMapper(mapper);
212 m_series->setModelMapper(mapper);
213 // m_series->setModelMapping(0,1, Qt::Vertical);
213 // m_series->setModelMapping(0,1, Qt::Vertical);
214 // m_series->setModelMappingRange(3, 4);
214 // m_series->setModelMappingRange(3, 4);
215 m_chart->addSeries(m_series);
215 m_chart->addSeries(m_series);
216 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
216 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
217 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
217 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
218
218
219 // series 2
219 // series 2
220 m_series = new QLineSeries;
220 m_series = new QLineSeries;
221 m_series->setModel(m_model);
221 m_series->setModel(m_model);
222
222
223 mapper = new QXYModelMapper;
223 mapper = new QXYModelMapper;
224 mapper->setMapX(3);
224 mapper->setMapX(3);
225 mapper->setMapY(4);
225 mapper->setMapY(4);
226 // mapper->setFirst(3);
226 // mapper->setFirst(3);
227 // mapper->setCount(4);
227 // mapper->setCount(4);
228 m_series->setModelMapper(mapper);
228 m_series->setModelMapper(mapper);
229 // m_series->setModelMapping(2,3, Qt::Vertical);
229 // m_series->setModelMapping(2,3, Qt::Vertical);
230 m_chart->addSeries(m_series);
230 m_chart->addSeries(m_series);
231 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
231 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
232 m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
232 m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
233
233
234 // series 3
234 // series 3
235 m_series = new QLineSeries;
235 m_series = new QLineSeries;
236 m_series->setModel(m_model);
236 m_series->setModel(m_model);
237
237
238 mapper = new QXYModelMapper;
238 mapper = new QXYModelMapper;
239 mapper->setMapX(5);
239 mapper->setMapX(5);
240 mapper->setMapY(6);
240 mapper->setMapY(6);
241 mapper->setFirst(2);
241 mapper->setFirst(2);
242 mapper->setCount(-1);
242 mapper->setCount(-1);
243 m_series->setModelMapper(mapper);
243 m_series->setModelMapper(mapper);
244 // m_series->setModelMapping(4,5, Qt::Vertical);
244 // m_series->setModelMapping(4,5, Qt::Vertical);
245 // m_series->setModelMappingRange(2, -1);
245 // m_series->setModelMappingRange(2, -1);
246 m_chart->addSeries(m_series);
246 m_chart->addSeries(m_series);
247 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
247 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
248 m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
248 m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
249 }
249 }
250 else if (m_splineRadioButton->isChecked())
250 else if (m_splineRadioButton->isChecked())
251 {
251 {
252 m_chart->setAnimationOptions(QChart::NoAnimation);
252 m_chart->setAnimationOptions(QChart::NoAnimation);
253
253
254 // series 1
254 // series 1
255 m_series = new QSplineSeries;
255 m_series = new QSplineSeries;
256 m_series->setModel(m_model);
256 m_series->setModel(m_model);
257
257
258 QXYModelMapper *mapper = new QXYModelMapper;
258 QXYModelMapper *mapper = new QXYModelMapper;
259 mapper->setMapX(0);
259 mapper->setMapX(0);
260 mapper->setMapY(1);
260 mapper->setMapY(1);
261 mapper->setFirst(0);
261 mapper->setFirst(0);
262 mapper->setCount(-1);
262 mapper->setCount(-1);
263
263
264 m_series->setModelMapper(mapper);
264 m_series->setModelMapper(mapper);
265
265
266 m_chart->addSeries(m_series);
266 m_chart->addSeries(m_series);
267 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
267 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
268 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
268 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
269
269
270 // series 2
270 // series 2
271 m_series = new QSplineSeries;
271 m_series = new QSplineSeries;
272 m_series->setModel(m_model);
272 m_series->setModel(m_model);
273
273
274 mapper = new QXYModelMapper;
274 mapper = new QXYModelMapper;
275 mapper->setMapX(2);
275 mapper->setMapX(2);
276 mapper->setMapY(3);
276 mapper->setMapY(3);
277 mapper->setFirst(2);
277 mapper->setFirst(2);
278 mapper->setCount(4);
278 mapper->setCount(4);
279
279
280 m_series->setModelMapper(mapper);
280 m_series->setModelMapper(mapper);
281
281
282 m_chart->addSeries(m_series);
282 m_chart->addSeries(m_series);
283 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
283 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
284 m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
284 m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
285
285
286 // series 3
286 // series 3
287 m_series = new QSplineSeries;
287 m_series = new QSplineSeries;
288 m_series->setModel(m_model);
288 m_series->setModel(m_model);
289
289
290 mapper = new QXYModelMapper;
290 mapper = new QXYModelMapper;
291 mapper->setMapX(4);
291 mapper->setMapX(4);
292 mapper->setMapY(5);
292 mapper->setMapY(5);
293 mapper->setFirst(2);
293 mapper->setFirst(2);
294 mapper->setCount(-1);
294 mapper->setCount(-1);
295
295
296 m_series->setModelMapper(mapper);
296 m_series->setModelMapper(mapper);
297
297
298 m_chart->addSeries(m_series);
298 m_chart->addSeries(m_series);
299 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
299 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
300 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
300 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
301 }
301 }
302 // else if (m_scatterRadioButton->isChecked())
302 // else if (m_scatterRadioButton->isChecked())
303 // {
303 // {
304 // m_chart->setAnimationOptions(QChart::NoAnimation);
304 // m_chart->setAnimationOptions(QChart::NoAnimation);
305
305
306 // // series 1
306 // // series 1
307 // m_series = new QScatterSeries;
307 // m_series = new QScatterSeries;
308 // m_series->setModel(m_model);
308 // m_series->setModel(m_model);
309 // m_series->setModelMapping(0,1, Qt::Vertical);
309 // m_series->setModelMapping(0,1, Qt::Vertical);
310 // // m_series->setModelMappingRange(2, 0);
310 // // m_series->setModelMappingRange(2, 0);
311 // // series->setModelMapping(0,1, Qt::Horizontal);
311 // // series->setModelMapping(0,1, Qt::Horizontal);
312 // m_chart->addSeries(m_series);
312 // m_chart->addSeries(m_series);
313
313
314 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
314 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
315 // m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
315 // m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
316
316
317 // // series 2
317 // // series 2
318 // m_series = new QScatterSeries;
318 // m_series = new QScatterSeries;
319 // m_series->setModel(m_model);
319 // m_series->setModel(m_model);
320 // m_series->setModelMapping(2,3, Qt::Vertical);
320 // m_series->setModelMapping(2,3, Qt::Vertical);
321 // // m_series->setModelMappingRange(1, 6);
321 // // m_series->setModelMappingRange(1, 6);
322 // // series->setModelMapping(2,3, Qt::Horizontal);
322 // // series->setModelMapping(2,3, Qt::Horizontal);
323 // m_chart->addSeries(m_series);
323 // m_chart->addSeries(m_series);
324
324
325 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
325 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
326 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
326 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
327
327
328 // // series 3
328 // // series 3
329 // m_series = new QScatterSeries;
329 // m_series = new QScatterSeries;
330 // m_series->setModel(m_model);
330 // m_series->setModel(m_model);
331 // m_series->setModelMapping(4,5, Qt::Vertical);
331 // m_series->setModelMapping(4,5, Qt::Vertical);
332 // // series->setModelMapping(4,5, Qt::Horizontal);
332 // // series->setModelMapping(4,5, Qt::Horizontal);
333 // m_chart->addSeries(m_series);
333 // m_chart->addSeries(m_series);
334 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
334 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
335 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
335 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
336 // }
336 // }
337 else if (m_pieRadioButton->isChecked())
337 else if (m_pieRadioButton->isChecked())
338 {
338 {
339 m_chart->setAnimationOptions(QChart::SeriesAnimations);
339 m_chart->setAnimationOptions(QChart::SeriesAnimations);
340
340
341 // pie 1
341 // pie 1
342 QPieSeries* pieSeries = new QPieSeries;
342 QPieSeries* pieSeries = new QPieSeries;
343 pieSeries->setModel(m_model);
343 pieSeries->setModel(m_model);
344
344
345 QPieModelMapper *mapper = new QPieModelMapper;
345 QPieModelMapper *mapper = pieSeries->modelMapper();//new QPieModelMapper;
346 mapper->setMapValues(-1);
346 mapper->setMapValues(1);
347 mapper->setMapLabels(-1);
347 mapper->setMapLabels(0);
348 mapper->setFirst(2);
348 mapper->setFirst(2);
349 mapper->setCount(5);
349 mapper->setCount(5);
350 pieSeries->setModelMapper(mapper);
350 // pieSeries->setModelMapper(mapper);
351
351
352 pieSeries->setLabelsVisible(true);
352 pieSeries->setLabelsVisible(true);
353 pieSeries->setPieSize(0.75);
353 pieSeries->setPieSize(0.75);
354 // pieSeries->setHorizontalPosition(0.2);
354 // pieSeries->setHorizontalPosition(0.2);
355 // pieSeries->setVerticalPosition(0.3);
355 // pieSeries->setVerticalPosition(0.3);
356
356
357 m_chart->addSeries(pieSeries);
357 m_chart->addSeries(pieSeries);
358 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
358 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
359 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
359 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
360
360
361
362 pieSeries->slices().at(0)->setValue(400);
363 pieSeries->slices().at(0)->setLabel(QString("36"));
364
361 // // pie 2
365 // // pie 2
362 // pieSeries = new QPieSeries;
366 // pieSeries = new QPieSeries;
363 // pieSeries->setModel(m_model);
367 // pieSeries->setModel(m_model);
364
368
365 // pieSeries->setModelMapping(1,1, Qt::Vertical);
369 // pieSeries->setModelMapping(1,1, Qt::Vertical);
366 // pieSeries->setModelMappingRange(2, -1);
370 // pieSeries->setModelMappingRange(2, -1);
367 // pieSeries->setLabelsVisible(true);
371 // pieSeries->setLabelsVisible(true);
368 // pieSeries->setPieSize(0.35);
372 // pieSeries->setPieSize(0.35);
369 // pieSeries->setHorizontalPosition(0.8);
373 // pieSeries->setHorizontalPosition(0.8);
370 // pieSeries->setVerticalPosition(0.3);
374 // pieSeries->setVerticalPosition(0.3);
371 // m_chart->addSeries(pieSeries);
375 // m_chart->addSeries(pieSeries);
372 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
376 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
373 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
377 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
374
378
375 // // pie 3
379 // // pie 3
376 // pieSeries = new QPieSeries;
380 // pieSeries = new QPieSeries;
377 // pieSeries->setModel(m_model);
381 // pieSeries->setModel(m_model);
378 // pieSeries->setModelMapping(2,2, Qt::Vertical);
382 // pieSeries->setModelMapping(2,2, Qt::Vertical);
379 // pieSeries->setLabelsVisible(true);
383 // pieSeries->setLabelsVisible(true);
380 // pieSeries->setPieSize(0.35);
384 // pieSeries->setPieSize(0.35);
381 // pieSeries->setHorizontalPosition(0.5);
385 // pieSeries->setHorizontalPosition(0.5);
382 // pieSeries->setVerticalPosition(0.75);
386 // pieSeries->setVerticalPosition(0.75);
383 // m_chart->addSeries(pieSeries);
387 // m_chart->addSeries(pieSeries);
384 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
388 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
385 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
389 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
386
390
387 // // special pie
391 // // special pie
388 // specialPie = new QPieSeries;
392 // specialPie = new QPieSeries;
389 // specialPie->append(17, "1");
393 // specialPie->append(17, "1");
390 // specialPie->append(45, "2");
394 // specialPie->append(45, "2");
391 // specialPie->append(77, "3");
395 // specialPie->append(77, "3");
392 // specialPie->append(37, "4");
396 // specialPie->append(37, "4");
393 // specialPie->append(27, "5");
397 // specialPie->append(27, "5");
394 // specialPie->append(47, "6");
398 // specialPie->append(47, "6");
395 // specialPie->setPieSize(0.35);
399 // specialPie->setPieSize(0.35);
396 // specialPie->setHorizontalPosition(0.8);
400 // specialPie->setHorizontalPosition(0.8);
397 // specialPie->setVerticalPosition(0.75);
401 // specialPie->setVerticalPosition(0.75);
398 // specialPie->setLabelsVisible(true);
402 // specialPie->setLabelsVisible(true);
399 // m_chart->addSeries(specialPie);
403 // m_chart->addSeries(specialPie);
400 }
404 }
401 // else if (m_areaRadioButton->isChecked())
405 // else if (m_areaRadioButton->isChecked())
402 // {
406 // {
403 // m_chart->setAnimationOptions(QChart::NoAnimation);
407 // m_chart->setAnimationOptions(QChart::NoAnimation);
404
408
405 // QLineSeries* upperLineSeries = new QLineSeries;
409 // QLineSeries* upperLineSeries = new QLineSeries;
406 // upperLineSeries->setModel(m_model);
410 // upperLineSeries->setModel(m_model);
407 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
411 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
408 // // upperLineSeries->setModelMappingRange(1, 5);
412 // // upperLineSeries->setModelMappingRange(1, 5);
409 // QLineSeries* lowerLineSeries = new QLineSeries;
413 // QLineSeries* lowerLineSeries = new QLineSeries;
410 // lowerLineSeries->setModel(m_model);
414 // lowerLineSeries->setModel(m_model);
411 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
415 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
412 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
416 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
413 // m_chart->addSeries(areaSeries);
417 // m_chart->addSeries(areaSeries);
414 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
418 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
415 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
419 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
416 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
420 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
417 // }
421 // }
418 else if (m_barRadioButton->isChecked())
422 else if (m_barRadioButton->isChecked())
419 {
423 {
420 m_chart->setAnimationOptions(QChart::SeriesAnimations);
424 m_chart->setAnimationOptions(QChart::SeriesAnimations);
421
425
422 QGroupedBarSeries* barSeries = new QGroupedBarSeries();
426 QGroupedBarSeries* barSeries = new QGroupedBarSeries();
423 barSeries->setCategories(QStringList());
427 barSeries->setCategories(QStringList());
424 barSeries->setModel(m_model);
428 barSeries->setModel(m_model);
425 // barSeries->setModelMappingRange(2, 5);
429 // barSeries->setModelMappingRange(2, 5);
426 // barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
430 // barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
427
431
428 QBarModelMapper *mapper = new QBarModelMapper;
432 QBarModelMapper *mapper = new QBarModelMapper;
429 mapper->setMapCategories(5);
433 mapper->setMapCategories(5);
430 mapper->setMapBarBottom(2);
434 mapper->setMapBarBottom(2);
431 mapper->setMapBarTop(4);
435 mapper->setMapBarTop(4);
432 barSeries->setModelMapper(mapper);
436 barSeries->setModelMapper(mapper);
433 m_chart->addSeries(barSeries);
437 m_chart->addSeries(barSeries);
434 QList<QBarSet*> barsets = barSeries->barSets();
438 QList<QBarSet*> barsets = barSeries->barSets();
435 for (int i = 0; i < barsets.count(); i++) {
439 for (int i = 0; i < barsets.count(); i++) {
436 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
440 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
437 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
441 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
438 }
442 }
439 }
443 }
440
444
441
445
442 if (!m_barRadioButton->isChecked()) {
446 if (!m_barRadioButton->isChecked()) {
443 m_chart->axisX()->setRange(0, 500);
447 m_chart->axisX()->setRange(0, 500);
444 m_chart->axisY()->setRange(0, 220);
448 m_chart->axisY()->setRange(0, 220);
445 }
449 }
446 m_chart->legend()->setVisible(true);
450 m_chart->legend()->setVisible(true);
447
451
448 // repaint table view colors
452 // repaint table view colors
449 m_tableView->repaint();
453 m_tableView->repaint();
450 m_tableView->setFocus();
454 m_tableView->setFocus();
451 }
455 }
452 }
456 }
453
457
454 void TableWidget::testPie()
458 void TableWidget::testPie()
455 {
459 {
456 m_series->modelMapper()->setMapX(4);
460 // m_series->modelMapper()->setMapX(4);
457 // m_tableView->setColumnWidth(10, 250);
461 // m_tableView->setColumnWidth(10, 250);
458 // if (specialPie) {
462 // if (specialPie) {
459 // specialPie->remove(specialPie->slices().at(2));
463 // specialPie->remove(specialPie->slices().at(2));
460 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
464 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
461 // specialPie->append(4, "heloo");
465 // specialPie->append(4, "heloo");
462 // }
466 // }
463 }
467 }
464
468
465 TableWidget::~TableWidget()
469 TableWidget::~TableWidget()
466 {
470 {
467
471
468 }
472 }
General Comments 0
You need to be logged in to leave comments. Login now