##// END OF EJS Templates
Minor qpieseries doc updates.
Jani Honkonen -
r933:7957ef91e645
parent child
Show More
@@ -1,702 +1,702
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 "qpieseriesprivate_p.h"
22 #include "qpieseriesprivate_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "pieslicedata_p.h"
24 #include "pieslicedata_p.h"
25 #include <QAbstractItemModel>
25 #include <QAbstractItemModel>
26 #include <QDebug>
26 #include <QDebug>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent)
30 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent)
31 :QObject(parent),
31 :QObject(parent),
32 q_ptr(parent),
32 q_ptr(parent),
33 m_pieRelativeHorPos(0.5),
33 m_pieRelativeHorPos(0.5),
34 m_pieRelativeVerPos(0.5),
34 m_pieRelativeVerPos(0.5),
35 m_pieRelativeSize(0.7),
35 m_pieRelativeSize(0.7),
36 m_pieStartAngle(0),
36 m_pieStartAngle(0),
37 m_pieEndAngle(360),
37 m_pieEndAngle(360),
38 m_total(0),
38 m_total(0),
39 m_mapValues(0),
39 m_mapValues(0),
40 m_mapLabels(0),
40 m_mapLabels(0),
41 m_mapOrientation(Qt::Horizontal)
41 m_mapOrientation(Qt::Horizontal)
42 {
42 {
43
43
44 }
44 }
45
45
46 QPieSeriesPrivate::~QPieSeriesPrivate()
46 QPieSeriesPrivate::~QPieSeriesPrivate()
47 {
47 {
48
48
49 }
49 }
50
50
51 void QPieSeriesPrivate::updateDerivativeData()
51 void QPieSeriesPrivate::updateDerivativeData()
52 {
52 {
53 m_total = 0;
53 m_total = 0;
54
54
55 // nothing to do?
55 // nothing to do?
56 if (m_slices.count() == 0)
56 if (m_slices.count() == 0)
57 return;
57 return;
58
58
59 // calculate total
59 // calculate total
60 foreach (QPieSlice* s, m_slices)
60 foreach (QPieSlice* s, m_slices)
61 m_total += s->value();
61 m_total += s->value();
62
62
63 // nothing to show..
63 // nothing to show..
64 if (qFuzzyIsNull(m_total))
64 if (qFuzzyIsNull(m_total))
65 return;
65 return;
66
66
67 // update slice attributes
67 // update slice attributes
68 qreal sliceAngle = m_pieStartAngle;
68 qreal sliceAngle = m_pieStartAngle;
69 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
69 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
70 QVector<QPieSlice*> changed;
70 QVector<QPieSlice*> changed;
71 foreach (QPieSlice* s, m_slices) {
71 foreach (QPieSlice* s, m_slices) {
72
72
73 PieSliceData data = PieSliceData::data(s);
73 PieSliceData data = PieSliceData::data(s);
74 data.m_percentage = s->value() / m_total;
74 data.m_percentage = s->value() / m_total;
75 data.m_angleSpan = pieSpan * data.m_percentage;
75 data.m_angleSpan = pieSpan * data.m_percentage;
76 data.m_startAngle = sliceAngle;
76 data.m_startAngle = sliceAngle;
77 sliceAngle += data.m_angleSpan;
77 sliceAngle += data.m_angleSpan;
78
78
79 if (PieSliceData::data(s) != data) {
79 if (PieSliceData::data(s) != data) {
80 PieSliceData::data(s) = data;
80 PieSliceData::data(s) = data;
81 changed << s;
81 changed << s;
82 }
82 }
83 }
83 }
84
84
85 // emit signals
85 // emit signals
86 foreach (QPieSlice* s, changed)
86 foreach (QPieSlice* s, changed)
87 PieSliceData::data(s).emitChangedSignal(s);
87 PieSliceData::data(s).emitChangedSignal(s);
88 }
88 }
89
89
90 void QPieSeriesPrivate::sliceChanged()
90 void QPieSeriesPrivate::sliceChanged()
91 {
91 {
92 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
92 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
93 updateDerivativeData();
93 updateDerivativeData();
94 }
94 }
95
95
96 void QPieSeriesPrivate::sliceClicked(Qt::MouseButtons buttons)
96 void QPieSeriesPrivate::sliceClicked(Qt::MouseButtons buttons)
97 {
97 {
98 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
98 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
99 Q_ASSERT(m_slices.contains(slice));
99 Q_ASSERT(m_slices.contains(slice));
100 Q_Q(QPieSeries);
100 Q_Q(QPieSeries);
101 emit q->clicked(slice, buttons);
101 emit q->clicked(slice, buttons);
102 }
102 }
103
103
104 void QPieSeriesPrivate::sliceHoverEnter()
104 void QPieSeriesPrivate::sliceHoverEnter()
105 {
105 {
106 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
106 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
107 Q_ASSERT(m_slices.contains(slice));
107 Q_ASSERT(m_slices.contains(slice));
108 Q_Q(QPieSeries);
108 Q_Q(QPieSeries);
109 emit q->hoverEnter(slice);
109 emit q->hoverEnter(slice);
110 }
110 }
111
111
112 void QPieSeriesPrivate::sliceHoverLeave()
112 void QPieSeriesPrivate::sliceHoverLeave()
113 {
113 {
114 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
114 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
115 Q_ASSERT(m_slices.contains(slice));
115 Q_ASSERT(m_slices.contains(slice));
116 Q_Q(QPieSeries);
116 Q_Q(QPieSeries);
117 emit q->hoverLeave(slice);
117 emit q->hoverLeave(slice);
118 }
118 }
119
119
120 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
120 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
121 {
121 {
122 Q_UNUSED(bottomRight)
122 Q_UNUSED(bottomRight)
123 Q_Q(QPieSeries);
123 Q_Q(QPieSeries);
124
124
125 if (m_mapOrientation == Qt::Vertical)
125 if (m_mapOrientation == Qt::Vertical)
126 {
126 {
127 if (topLeft.column() == m_mapValues)
127 if (topLeft.column() == m_mapValues)
128 if (m_mapValues == m_mapLabels)
128 if (m_mapValues == m_mapLabels)
129 {
129 {
130 m_slices.at(topLeft.row())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
130 m_slices.at(topLeft.row())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
131 m_slices.at(topLeft.row())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
131 m_slices.at(topLeft.row())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
132 }
132 }
133 else
133 else
134 {
134 {
135 m_slices.at(topLeft.row())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
135 m_slices.at(topLeft.row())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
136 }
136 }
137 else if (topLeft.column() == m_mapLabels)
137 else if (topLeft.column() == m_mapLabels)
138 m_slices.at(topLeft.row())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
138 m_slices.at(topLeft.row())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
139 }
139 }
140 else
140 else
141 {
141 {
142 if (topLeft.row() == m_mapValues)
142 if (topLeft.row() == m_mapValues)
143 if (m_mapValues == m_mapLabels)
143 if (m_mapValues == m_mapLabels)
144 {
144 {
145 m_slices.at(topLeft.column())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
145 m_slices.at(topLeft.column())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
146 m_slices.at(topLeft.column())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
146 m_slices.at(topLeft.column())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
147 }
147 }
148 else
148 else
149 {
149 {
150 m_slices.at(topLeft.column())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
150 m_slices.at(topLeft.column())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
151 }
151 }
152 else if (topLeft.row() == m_mapLabels)
152 else if (topLeft.row() == m_mapLabels)
153 m_slices.at(topLeft.column())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
153 m_slices.at(topLeft.column())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
154 }
154 }
155 }
155 }
156
156
157 void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
157 void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
158 {
158 {
159 Q_UNUSED(parent)
159 Q_UNUSED(parent)
160 Q_UNUSED(end)
160 Q_UNUSED(end)
161 Q_Q(QPieSeries);
161 Q_Q(QPieSeries);
162
162
163 QPieSlice* newSlice = new QPieSlice;
163 QPieSlice* newSlice = new QPieSlice;
164 newSlice->setLabelVisible(true);
164 newSlice->setLabelVisible(true);
165 if (m_mapOrientation == Qt::Vertical)
165 if (m_mapOrientation == Qt::Vertical)
166 {
166 {
167 newSlice->setValue(q->m_model->data(q->m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble());
167 newSlice->setValue(q->m_model->data(q->m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble());
168 newSlice->setLabel(q->m_model->data(q->m_model->index(start, m_mapLabels), Qt::DisplayRole).toString());
168 newSlice->setLabel(q->m_model->data(q->m_model->index(start, m_mapLabels), Qt::DisplayRole).toString());
169 }
169 }
170 else
170 else
171 {
171 {
172 newSlice->setValue(q->m_model->data(q->m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble());
172 newSlice->setValue(q->m_model->data(q->m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble());
173 newSlice->setLabel(q->m_model->data(q->m_model->index(m_mapLabels, start), Qt::DisplayRole).toString());
173 newSlice->setLabel(q->m_model->data(q->m_model->index(m_mapLabels, start), Qt::DisplayRole).toString());
174 }
174 }
175
175
176 q->insert(start, newSlice);
176 q->insert(start, newSlice);
177 }
177 }
178
178
179 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
179 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
180 {
180 {
181 Q_UNUSED(parent)
181 Q_UNUSED(parent)
182 Q_UNUSED(end)
182 Q_UNUSED(end)
183 Q_Q(QPieSeries);
183 Q_Q(QPieSeries);
184 q->remove(m_slices.at(start));
184 q->remove(m_slices.at(start));
185 }
185 }
186
186
187 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
187 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
188 {
188 {
189 // Remove rounding errors
189 // Remove rounding errors
190 qreal roundedValue = newValue;
190 qreal roundedValue = newValue;
191 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
191 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
192 roundedValue = 0.0;
192 roundedValue = 0.0;
193 else if (qFuzzyCompare(newValue, max))
193 else if (qFuzzyCompare(newValue, max))
194 roundedValue = max;
194 roundedValue = max;
195 else if (qFuzzyCompare(newValue, min))
195 else if (qFuzzyCompare(newValue, min))
196 roundedValue = min;
196 roundedValue = min;
197
197
198 // Check if the position is valid after removing the rounding errors
198 // Check if the position is valid after removing the rounding errors
199 if (roundedValue < min || roundedValue > max) {
199 if (roundedValue < min || roundedValue > max) {
200 qWarning("QPieSeries: Illegal value");
200 qWarning("QPieSeries: Illegal value");
201 return false;
201 return false;
202 }
202 }
203
203
204 if (!qFuzzyIsNull(value - roundedValue)) {
204 if (!qFuzzyIsNull(value - roundedValue)) {
205 value = roundedValue;
205 value = roundedValue;
206 return true;
206 return true;
207 }
207 }
208
208
209 // The change was so small it is considered a rounding error
209 // The change was so small it is considered a rounding error
210 return false;
210 return false;
211 }
211 }
212
212
213
213
214
214
215 /*!
215 /*!
216 \class QPieSeries
216 \class QPieSeries
217 \brief Pie series API for QtCommercial Charts
217 \brief Pie series API for QtCommercial Charts
218
218
219 The pie series defines a pie chart which consists of pie slices which are QPieSlice objects.
219 The pie series defines a pie chart which consists of pie slices which are QPieSlice objects.
220 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
220 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
221 The actual slice size is determined by that relative value.
221 The actual slice size is determined by that relative value.
222
222
223 By default the pie is defined as a full pie but it can be a partial pie.
223 By default the pie is defined as a full pie but it can be a partial pie.
224 This can be done by setting a starting angle and angle span to the series.
224 This can be done by setting a starting angle and angle span to the series.
225 */
225 */
226
226
227 /*!
227 /*!
228 \property QPieSeries::horizontalPosition
228 \property QPieSeries::horizontalPosition
229 \brief Defines the horizontal position of the pie.
229 \brief Defines the horizontal position of the pie.
230
230
231 The value is a relative value to the chart rectangle where:
231 The value is a relative value to the chart rectangle where:
232
232
233 \list
233 \list
234 \o 0.0 is the absolute left.
234 \o 0.0 is the absolute left.
235 \o 1.0 is the absolute right.
235 \o 1.0 is the absolute right.
236 \endlist
236 \endlist
237
237
238 Default value is 0.5 (center).
238 Default value is 0.5 (center).
239 */
239 */
240
240
241 /*!
241 /*!
242 \property QPieSeries::verticalPosition
242 \property QPieSeries::verticalPosition
243 \brief Defines the vertical position of the pie.
243 \brief Defines the vertical position of the pie.
244
244
245 The value is a relative value to the chart rectangle where:
245 The value is a relative value to the chart rectangle where:
246
246
247 \list
247 \list
248 \o 0.0 is the absolute top.
248 \o 0.0 is the absolute top.
249 \o 1.0 is the absolute bottom.
249 \o 1.0 is the absolute bottom.
250 \endlist
250 \endlist
251
251
252 Default value is 0.5 (center).
252 Default value is 0.5 (center).
253 */
253 */
254
254
255 /*!
255 /*!
256 \property QPieSeries::size
256 \property QPieSeries::size
257 \brief Defines the pie size.
257 \brief Defines the pie size.
258
258
259 The value is a relative value to the chart rectangle where:
259 The value is a relative value to the chart rectangle where:
260
260
261 \list
261 \list
262 \o 0.0 is the minumum size (pie not drawn).
262 \o 0.0 is the minumum size (pie not drawn).
263 \o 1.0 is the maximum size that can fit the chart.
263 \o 1.0 is the maximum size that can fit the chart.
264 \endlist
264 \endlist
265
265
266 Default value is 0.7.
266 Default value is 0.7.
267 */
267 */
268
268
269 /*!
269 /*!
270 \property QPieSeries::startAngle
270 \property QPieSeries::startAngle
271 \brief Defines the starting angle of the pie.
271 \brief Defines the starting angle of the pie.
272
272
273 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
273 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
274
274
275 Default is value is 0.
275 Default is value is 0.
276 */
276 */
277
277
278 /*!
278 /*!
279 \property QPieSeries::endAngle
279 \property QPieSeries::endAngle
280 \brief Defines the ending angle of the pie.
280 \brief Defines the ending angle of the pie.
281
281
282 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
282 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
283
283
284 Default is value is 360.
284 Default is value is 360.
285 */
285 */
286
286
287
287
288 /*!
288 /*!
289 Constructs a series object which is a child of \a parent.
289 Constructs a series object which is a child of \a parent.
290 */
290 */
291 QPieSeries::QPieSeries(QObject *parent) :
291 QPieSeries::QPieSeries(QObject *parent) :
292 QSeries(parent),
292 QSeries(parent),
293 d_ptr(new QPieSeriesPrivate(this))
293 d_ptr(new QPieSeriesPrivate(this))
294 {
294 {
295
295
296 }
296 }
297
297
298 /*!
298 /*!
299 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
299 Destroys the series and its slices.
300 */
300 */
301 QPieSeries::~QPieSeries()
301 QPieSeries::~QPieSeries()
302 {
302 {
303 // NOTE: d_prt destroyed by QObject
303 // NOTE: d_prt destroyed by QObject
304 }
304 }
305
305
306 /*!
306 /*!
307 Returns QChartSeries::SeriesTypePie.
307 Returns QChartSeries::SeriesTypePie.
308 */
308 */
309 QSeries::QSeriesType QPieSeries::type() const
309 QSeries::QSeriesType QPieSeries::type() const
310 {
310 {
311 return QSeries::SeriesTypePie;
311 return QSeries::SeriesTypePie;
312 }
312 }
313
313
314 /*!
314 /*!
315 Sets an array of \a slices to the series replacing the existing slices.
315 Sets an array of \a slices to the series replacing the existing slices.
316 Slice ownership is passed to the series.
316 Slice ownership is passed to the series.
317 */
317 */
318 void QPieSeries::replace(QList<QPieSlice*> slices)
318 void QPieSeries::replace(QList<QPieSlice*> slices)
319 {
319 {
320 clear();
320 clear();
321 append(slices);
321 append(slices);
322 }
322 }
323
323
324 /*!
324 /*!
325 Adds an array of \a slices to the series.
325 Appends an array of \a slices to the series.
326 Slice ownership is passed to the series.
326 Slice ownership is passed to the series.
327 */
327 */
328 void QPieSeries::append(QList<QPieSlice*> slices)
328 void QPieSeries::append(QList<QPieSlice*> slices)
329 {
329 {
330 Q_D(QPieSeries);
330 Q_D(QPieSeries);
331
331
332 foreach (QPieSlice* s, slices) {
332 foreach (QPieSlice* s, slices) {
333 s->setParent(this);
333 s->setParent(this);
334 d->m_slices << s;
334 d->m_slices << s;
335 }
335 }
336
336
337 d->updateDerivativeData();
337 d->updateDerivativeData();
338
338
339 foreach (QPieSlice* s, slices) {
339 foreach (QPieSlice* s, slices) {
340 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
340 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
341 connect(s, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
341 connect(s, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
342 connect(s, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
342 connect(s, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
343 connect(s, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
343 connect(s, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
344 }
344 }
345
345
346 emit added(slices);
346 emit added(slices);
347 }
347 }
348
348
349 /*!
349 /*!
350 Adds a single \a slice to the series.
350 Appends a single \a slice to the series.
351 Slice ownership is passed to the series.
351 Slice ownership is passed to the series.
352 */
352 */
353 void QPieSeries::append(QPieSlice* slice)
353 void QPieSeries::append(QPieSlice* slice)
354 {
354 {
355 append(QList<QPieSlice*>() << slice);
355 append(QList<QPieSlice*>() << slice);
356 }
356 }
357
357
358 /*!
358 /*!
359 Adds a single \a slice to the series and returns a reference to the series.
359 Appends a single \a slice to the series and returns a reference to the series.
360 Slice ownership is passed to the series.
360 Slice ownership is passed to the series.
361 */
361 */
362 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
362 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
363 {
363 {
364 append(slice);
364 append(slice);
365 return *this;
365 return *this;
366 }
366 }
367
367
368
368
369 /*!
369 /*!
370 Appends a single slice to the series with give \a value and \a name.
370 Appends a single slice to the series with give \a value and \a name.
371 Slice ownership is passed to the series.
371 Slice ownership is passed to the series.
372 */
372 */
373 QPieSlice* QPieSeries::append(qreal value, QString name)
373 QPieSlice* QPieSeries::append(qreal value, QString name)
374 {
374 {
375 QPieSlice* slice = new QPieSlice(value, name);
375 QPieSlice* slice = new QPieSlice(value, name);
376 append(slice);
376 append(slice);
377 return slice;
377 return slice;
378 }
378 }
379
379
380 /*!
380 /*!
381 Inserts a single \a slice to the series before the slice at \a index position.
381 Inserts a single \a slice to the series before the slice at \a index position.
382 Slice ownership is passed to the series.
382 Slice ownership is passed to the series.
383 */
383 */
384 void QPieSeries::insert(int index, QPieSlice* slice)
384 void QPieSeries::insert(int index, QPieSlice* slice)
385 {
385 {
386 Q_D(QPieSeries);
386 Q_D(QPieSeries);
387 Q_ASSERT(index <= d->m_slices.count());
387 Q_ASSERT(index <= d->m_slices.count());
388 slice->setParent(this);
388 slice->setParent(this);
389 d->m_slices.insert(index, slice);
389 d->m_slices.insert(index, slice);
390
390
391 d->updateDerivativeData();
391 d->updateDerivativeData();
392
392
393 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
393 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
394 connect(slice, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
394 connect(slice, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
395 connect(slice, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
395 connect(slice, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
396 connect(slice, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
396 connect(slice, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
397
397
398 emit added(QList<QPieSlice*>() << slice);
398 emit added(QList<QPieSlice*>() << slice);
399 }
399 }
400
400
401 /*!
401 /*!
402 Removes a single \a slice from the series and deletes the slice.
402 Removes a single \a slice from the series and deletes the slice.
403
403
404 Do not reference this pointer after this call.
404 Do not reference this pointer after this call.
405 */
405 */
406 void QPieSeries::remove(QPieSlice* slice)
406 void QPieSeries::remove(QPieSlice* slice)
407 {
407 {
408 Q_D(QPieSeries);
408 Q_D(QPieSeries);
409 if (!d->m_slices.removeOne(slice)) {
409 if (!d->m_slices.removeOne(slice)) {
410 Q_ASSERT(0); // TODO: how should this be reported?
410 Q_ASSERT(0); // TODO: how should this be reported?
411 return;
411 return;
412 }
412 }
413
413
414 d->updateDerivativeData();
414 d->updateDerivativeData();
415
415
416 emit removed(QList<QPieSlice*>() << slice);
416 emit removed(QList<QPieSlice*>() << slice);
417
417
418 delete slice;
418 delete slice;
419 slice = 0;
419 slice = 0;
420 }
420 }
421
421
422 /*!
422 /*!
423 Clears all slices from the series.
423 Clears all slices from the series.
424 */
424 */
425 void QPieSeries::clear()
425 void QPieSeries::clear()
426 {
426 {
427 Q_D(QPieSeries);
427 Q_D(QPieSeries);
428 if (d->m_slices.count() == 0)
428 if (d->m_slices.count() == 0)
429 return;
429 return;
430
430
431 QList<QPieSlice*> slices = d->m_slices;
431 QList<QPieSlice*> slices = d->m_slices;
432 foreach (QPieSlice* s, d->m_slices) {
432 foreach (QPieSlice* s, d->m_slices) {
433 d->m_slices.removeOne(s);
433 d->m_slices.removeOne(s);
434 delete s;
434 delete s;
435 }
435 }
436
436
437 d->updateDerivativeData();
437 d->updateDerivativeData();
438
438
439 emit removed(slices);
439 emit removed(slices);
440 }
440 }
441
441
442 /*!
442 /*!
443 Counts the number of the slices in this series.
443 returns the number of the slices in this series.
444 */
444 */
445 int QPieSeries::count() const
445 int QPieSeries::count() const
446 {
446 {
447 Q_D(const QPieSeries);
447 Q_D(const QPieSeries);
448 return d->m_slices.count();
448 return d->m_slices.count();
449 }
449 }
450
450
451 /*!
451 /*!
452 Returns true is the series is empty.
452 Returns true is the series is empty.
453 */
453 */
454 bool QPieSeries::isEmpty() const
454 bool QPieSeries::isEmpty() const
455 {
455 {
456 Q_D(const QPieSeries);
456 Q_D(const QPieSeries);
457 return d->m_slices.isEmpty();
457 return d->m_slices.isEmpty();
458 }
458 }
459
459
460 /*!
460 /*!
461 Returns a list of slices that belong to this series.
461 Returns a list of slices that belong to this series.
462 */
462 */
463 QList<QPieSlice*> QPieSeries::slices() const
463 QList<QPieSlice*> QPieSeries::slices() const
464 {
464 {
465 Q_D(const QPieSeries);
465 Q_D(const QPieSeries);
466 return d->m_slices;
466 return d->m_slices;
467 }
467 }
468
468
469 void QPieSeries::setHorizontalPosition(qreal relativePosition)
469 void QPieSeries::setHorizontalPosition(qreal relativePosition)
470 {
470 {
471 Q_D(QPieSeries);
471 Q_D(QPieSeries);
472 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
472 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
473 emit piePositionChanged();
473 emit piePositionChanged();
474 }
474 }
475
475
476 void QPieSeries::setVerticalPosition(qreal relativePosition)
476 void QPieSeries::setVerticalPosition(qreal relativePosition)
477 {
477 {
478 Q_D(QPieSeries);
478 Q_D(QPieSeries);
479 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
479 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
480 emit piePositionChanged();
480 emit piePositionChanged();
481 }
481 }
482
482
483 qreal QPieSeries::horizontalPosition() const
483 qreal QPieSeries::horizontalPosition() const
484 {
484 {
485 Q_D(const QPieSeries);
485 Q_D(const QPieSeries);
486 return d->m_pieRelativeHorPos;
486 return d->m_pieRelativeHorPos;
487 }
487 }
488
488
489 qreal QPieSeries::verticalPosition() const
489 qreal QPieSeries::verticalPosition() const
490 {
490 {
491 Q_D(const QPieSeries);
491 Q_D(const QPieSeries);
492 return d->m_pieRelativeVerPos;
492 return d->m_pieRelativeVerPos;
493 }
493 }
494
494
495 void QPieSeries::setPieSize(qreal relativeSize)
495 void QPieSeries::setPieSize(qreal relativeSize)
496 {
496 {
497 Q_D(QPieSeries);
497 Q_D(QPieSeries);
498 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
498 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
499 emit pieSizeChanged();
499 emit pieSizeChanged();
500 }
500 }
501
501
502 qreal QPieSeries::pieSize() const
502 qreal QPieSeries::pieSize() const
503 {
503 {
504 Q_D(const QPieSeries);
504 Q_D(const QPieSeries);
505 return d->m_pieRelativeSize;
505 return d->m_pieRelativeSize;
506 }
506 }
507
507
508
508
509 void QPieSeries::setPieStartAngle(qreal angle)
509 void QPieSeries::setPieStartAngle(qreal angle)
510 {
510 {
511 Q_D(QPieSeries);
511 Q_D(QPieSeries);
512 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
512 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
513 d->updateDerivativeData();
513 d->updateDerivativeData();
514 }
514 }
515
515
516 qreal QPieSeries::pieStartAngle() const
516 qreal QPieSeries::pieStartAngle() const
517 {
517 {
518 Q_D(const QPieSeries);
518 Q_D(const QPieSeries);
519 return d->m_pieStartAngle;
519 return d->m_pieStartAngle;
520 }
520 }
521
521
522 /*!
522 /*!
523 Sets the end angle of the pie.
523 Sets the end angle of the pie.
524
524
525 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
525 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
526
526
527 \a angle must be greater than start angle.
527 \a angle must be greater than start angle.
528
528
529 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
529 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
530 */
530 */
531 void QPieSeries::setPieEndAngle(qreal angle)
531 void QPieSeries::setPieEndAngle(qreal angle)
532 {
532 {
533 Q_D(QPieSeries);
533 Q_D(QPieSeries);
534
534
535 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
535 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
536 d->updateDerivativeData();
536 d->updateDerivativeData();
537 }
537 }
538
538
539 /*!
539 /*!
540 Returns the end angle of the pie.
540 Returns the end angle of the pie.
541
541
542 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
542 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
543
543
544 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
544 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
545 */
545 */
546 qreal QPieSeries::pieEndAngle() const
546 qreal QPieSeries::pieEndAngle() const
547 {
547 {
548 Q_D(const QPieSeries);
548 Q_D(const QPieSeries);
549 return d->m_pieEndAngle;
549 return d->m_pieEndAngle;
550 }
550 }
551
551
552 /*!
552 /*!
553 Sets the all the slice labels \a visible or invisible.
553 Sets the all the slice labels \a visible or invisible.
554
554
555 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
555 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
556 */
556 */
557 void QPieSeries::setLabelsVisible(bool visible)
557 void QPieSeries::setLabelsVisible(bool visible)
558 {
558 {
559 Q_D(QPieSeries);
559 Q_D(QPieSeries);
560 foreach (QPieSlice* s, d->m_slices)
560 foreach (QPieSlice* s, d->m_slices)
561 s->setLabelVisible(visible);
561 s->setLabelVisible(visible);
562 }
562 }
563
563
564 /*!
564 /*!
565 Returns the sum of all slice values in this series.
565 Returns the sum of all slice values in this series.
566
566
567 \sa QPieSlice::value(), QPieSlice::setValue()
567 \sa QPieSlice::value(), QPieSlice::setValue()
568 */
568 */
569 qreal QPieSeries::total() const
569 qreal QPieSeries::total() const
570 {
570 {
571 Q_D(const QPieSeries);
571 Q_D(const QPieSeries);
572 return d->m_total;
572 return d->m_total;
573 }
573 }
574
574
575 /*!
575 /*!
576 \fn void QPieSeries::clicked(QPieSlice* slice, Qt::MouseButtons buttons)
576 \fn void QPieSeries::clicked(QPieSlice* slice, Qt::MouseButtons buttons)
577
577
578 This signal is emitted when a \a slice has been clicked with mouse \a buttons.
578 This signal is emitted when a \a slice has been clicked with mouse \a buttons.
579
579
580 \sa QPieSlice::clicked()
580 \sa QPieSlice::clicked()
581 */
581 */
582
582
583 /*!
583 /*!
584 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
584 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
585
585
586 This signal is emitted when user has hovered over a \a slice.
586 This signal is emitted when user has hovered over a \a slice.
587
587
588 \sa QPieSlice::hoverEnter()
588 \sa QPieSlice::hoverEnter()
589 */
589 */
590
590
591 /*!
591 /*!
592 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
592 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
593
593
594 This signal is emitted when user has hovered away from a \a slice.
594 This signal is emitted when user has hovered away from a \a slice.
595
595
596 \sa QPieSlice::hoverLeave()
596 \sa QPieSlice::hoverLeave()
597 */
597 */
598
598
599 /*!
599 /*!
600 \fn void QPieSeries::added(QList<QPieSlice*> slices)
600 \fn void QPieSeries::added(QList<QPieSlice*> slices)
601
601
602 This signal is emitted when \a slices has been added to the series.
602 This signal is emitted when \a slices has been added to the series.
603
603
604 \sa append(), insert()
604 \sa append(), insert()
605 */
605 */
606
606
607 /*!
607 /*!
608 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
608 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
609
609
610 This signal is emitted when \a slices has been removed from the series.
610 This signal is emitted when \a slices has been removed from the series.
611
611
612 \sa remove(), clear()
612 \sa remove(), clear()
613 */
613 */
614
614
615 /*!
615 /*!
616 \fn void QPieSeries::piePositionChanged()
616 \fn void QPieSeries::piePositionChanged()
617
617
618 This signal is emitted when pie position has changed.
618 This signal is emitted when pie position has changed.
619
619
620 \sa verticalPosition(), setVerticalPosition(), horizontalPosition(), setHorizontalPosition()
620 \sa verticalPosition(), setVerticalPosition(), horizontalPosition(), setHorizontalPosition()
621 */
621 */
622
622
623 /*!
623 /*!
624 \fn void QPieSeries::pieSizeChanged()
624 \fn void QPieSeries::pieSizeChanged()
625
625
626 This signal is emitted when pie size has changed.
626 This signal is emitted when pie size has changed.
627
627
628 \sa pieSize(), setPieSize()
628 \sa pieSize(), setPieSize()
629 */
629 */
630
630
631 /*!
631 /*!
632 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
632 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
633 Sets the \a model to be used as a data source
633 Sets the \a model to be used as a data source
634 */
634 */
635 bool QPieSeries::setModel(QAbstractItemModel* model)
635 bool QPieSeries::setModel(QAbstractItemModel* model)
636 {
636 {
637 Q_D(QPieSeries);
637 Q_D(QPieSeries);
638 // disconnect signals from old model
638 // disconnect signals from old model
639 if(m_model)
639 if(m_model)
640 {
640 {
641 disconnect(m_model, 0, this, 0);
641 disconnect(m_model, 0, this, 0);
642 d->m_mapValues = -1;
642 d->m_mapValues = -1;
643 d->m_mapLabels = -1;
643 d->m_mapLabels = -1;
644 d->m_mapOrientation = Qt::Vertical;
644 d->m_mapOrientation = Qt::Vertical;
645 }
645 }
646
646
647 // set new model
647 // set new model
648 if(model)
648 if(model)
649 {
649 {
650 m_model = model;
650 m_model = model;
651 return true;
651 return true;
652 }
652 }
653 else
653 else
654 {
654 {
655 m_model = 0;
655 m_model = 0;
656 return false;
656 return false;
657 }
657 }
658 }
658 }
659
659
660 /*!
660 /*!
661 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
661 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
662 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
662 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
663 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
663 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
664 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
664 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
665 The \a orientation paramater specifies whether the data is in columns or in rows.
665 The \a orientation paramater specifies whether the data is in columns or in rows.
666 */
666 */
667 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
667 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
668 {
668 {
669 Q_D(QPieSeries);
669 Q_D(QPieSeries);
670
670
671 if (m_model == 0)
671 if (m_model == 0)
672 return;
672 return;
673
673
674 d->m_mapValues = modelValuesLine;
674 d->m_mapValues = modelValuesLine;
675 d->m_mapLabels = modelLabelsLine;
675 d->m_mapLabels = modelLabelsLine;
676 d->m_mapOrientation = orientation;
676 d->m_mapOrientation = orientation;
677
677
678 // connect the signals
678 // connect the signals
679 if (d->m_mapOrientation == Qt::Vertical) {
679 if (d->m_mapOrientation == Qt::Vertical) {
680 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
680 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
681 connect(m_model, SIGNAL(rowsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
681 connect(m_model, SIGNAL(rowsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
682 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
682 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
683 } else {
683 } else {
684 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
684 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
685 connect(m_model, SIGNAL(columnsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
685 connect(m_model, SIGNAL(columnsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
686 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
686 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
687 }
687 }
688
688
689 // create the initial slices set
689 // create the initial slices set
690 if (d->m_mapOrientation == Qt::Vertical) {
690 if (d->m_mapOrientation == Qt::Vertical) {
691 for (int i = 0; i < m_model->rowCount(); i++)
691 for (int i = 0; i < m_model->rowCount(); i++)
692 append(m_model->data(m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
692 append(m_model->data(m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
693 } else {
693 } else {
694 for (int i = 0; i < m_model->columnCount(); i++)
694 for (int i = 0; i < m_model->columnCount(); i++)
695 append(m_model->data(m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
695 append(m_model->data(m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
696 }
696 }
697 }
697 }
698
698
699 #include "moc_qpieseries.cpp"
699 #include "moc_qpieseries.cpp"
700 #include "moc_qpieseriesprivate_p.cpp"
700 #include "moc_qpieseriesprivate_p.cpp"
701
701
702 QTCOMMERCIALCHART_END_NAMESPACE
702 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now