##// END OF EJS Templates
fix QPieSeries property docs
Jani Honkonen -
r928:93cb42d22fe1
parent child
Show More
@@ -1,721 +1,751
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 qreal QPieSeries::horizontalPosition
228 \property QPieSeries::horizontalPosition
229 \brief Pie horizontal postion property
229 \brief Pie horizontal position property
230
230
231 By default horizontal postion is 0
231 By default horizontal position is 0.5 (center).
232 */
232 */
233
233
234 /*!
235 \property QPieSeries::verticalPosition
236 \brief Pie vertical position property
237
238 By default vertical position is 0.5 (center)
239 */
240
241 /*!
242 \property QPieSeries::size
243 \brief Pie size property
244
245 By default size is 0.7.
246 */
247
248 /*!
249 \property QPieSeries::startAngle
250 \brief Pie start angle.
251
252 Starting angle of the pie. Default is 0.
253 */
254
255
256 /*!
257 \property QPieSeries::endAngle
258 \brief Pie end angle.
259
260 Ending angle of the pie. Default is 360.
261 */
262
263
234
264
235 /*!
265 /*!
236 Constructs a series object which is a child of \a parent.
266 Constructs a series object which is a child of \a parent.
237 */
267 */
238 QPieSeries::QPieSeries(QObject *parent) :
268 QPieSeries::QPieSeries(QObject *parent) :
239 QSeries(parent),
269 QSeries(parent),
240 d_ptr(new QPieSeriesPrivate(this))
270 d_ptr(new QPieSeriesPrivate(this))
241 {
271 {
242
272
243 }
273 }
244
274
245 /*!
275 /*!
246 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
276 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
247 */
277 */
248 QPieSeries::~QPieSeries()
278 QPieSeries::~QPieSeries()
249 {
279 {
250 // NOTE: d_prt destroyed by QObject
280 // NOTE: d_prt destroyed by QObject
251 }
281 }
252
282
253 /*!
283 /*!
254 Returns QChartSeries::SeriesTypePie.
284 Returns QChartSeries::SeriesTypePie.
255 */
285 */
256 QSeries::QSeriesType QPieSeries::type() const
286 QSeries::QSeriesType QPieSeries::type() const
257 {
287 {
258 return QSeries::SeriesTypePie;
288 return QSeries::SeriesTypePie;
259 }
289 }
260
290
261 /*!
291 /*!
262 Sets an array of \a slices to the series replacing the existing slices.
292 Sets an array of \a slices to the series replacing the existing slices.
263 Slice ownership is passed to the series.
293 Slice ownership is passed to the series.
264 */
294 */
265 void QPieSeries::replace(QList<QPieSlice*> slices)
295 void QPieSeries::replace(QList<QPieSlice*> slices)
266 {
296 {
267 clear();
297 clear();
268 append(slices);
298 append(slices);
269 }
299 }
270
300
271 /*!
301 /*!
272 Adds an array of \a slices to the series.
302 Adds an array of \a slices to the series.
273 Slice ownership is passed to the series.
303 Slice ownership is passed to the series.
274 */
304 */
275 void QPieSeries::append(QList<QPieSlice*> slices)
305 void QPieSeries::append(QList<QPieSlice*> slices)
276 {
306 {
277 Q_D(QPieSeries);
307 Q_D(QPieSeries);
278
308
279 foreach (QPieSlice* s, slices) {
309 foreach (QPieSlice* s, slices) {
280 s->setParent(this);
310 s->setParent(this);
281 d->m_slices << s;
311 d->m_slices << s;
282 }
312 }
283
313
284 d->updateDerivativeData();
314 d->updateDerivativeData();
285
315
286 foreach (QPieSlice* s, slices) {
316 foreach (QPieSlice* s, slices) {
287 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
317 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
288 connect(s, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
318 connect(s, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
289 connect(s, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
319 connect(s, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
290 connect(s, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
320 connect(s, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
291 }
321 }
292
322
293 emit added(slices);
323 emit added(slices);
294 }
324 }
295
325
296 /*!
326 /*!
297 Adds a single \a slice to the series.
327 Adds a single \a slice to the series.
298 Slice ownership is passed to the series.
328 Slice ownership is passed to the series.
299 */
329 */
300 void QPieSeries::append(QPieSlice* slice)
330 void QPieSeries::append(QPieSlice* slice)
301 {
331 {
302 append(QList<QPieSlice*>() << slice);
332 append(QList<QPieSlice*>() << slice);
303 }
333 }
304
334
305 /*!
335 /*!
306 Adds a single \a slice to the series and returns a reference to the series.
336 Adds a single \a slice to the series and returns a reference to the series.
307 Slice ownership is passed to the series.
337 Slice ownership is passed to the series.
308 */
338 */
309 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
339 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
310 {
340 {
311 append(slice);
341 append(slice);
312 return *this;
342 return *this;
313 }
343 }
314
344
315
345
316 /*!
346 /*!
317 Appends a single slice to the series with give \a value and \a name.
347 Appends a single slice to the series with give \a value and \a name.
318 Slice ownership is passed to the series.
348 Slice ownership is passed to the series.
319 */
349 */
320 QPieSlice* QPieSeries::append(qreal value, QString name)
350 QPieSlice* QPieSeries::append(qreal value, QString name)
321 {
351 {
322 QPieSlice* slice = new QPieSlice(value, name);
352 QPieSlice* slice = new QPieSlice(value, name);
323 append(slice);
353 append(slice);
324 return slice;
354 return slice;
325 }
355 }
326
356
327 /*!
357 /*!
328 Inserts a single \a slice to the series before the slice at \a index position.
358 Inserts a single \a slice to the series before the slice at \a index position.
329 Slice ownership is passed to the series.
359 Slice ownership is passed to the series.
330 */
360 */
331 void QPieSeries::insert(int index, QPieSlice* slice)
361 void QPieSeries::insert(int index, QPieSlice* slice)
332 {
362 {
333 Q_D(QPieSeries);
363 Q_D(QPieSeries);
334 Q_ASSERT(index <= d->m_slices.count());
364 Q_ASSERT(index <= d->m_slices.count());
335 slice->setParent(this);
365 slice->setParent(this);
336 d->m_slices.insert(index, slice);
366 d->m_slices.insert(index, slice);
337
367
338 d->updateDerivativeData();
368 d->updateDerivativeData();
339
369
340 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
370 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
341 connect(slice, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
371 connect(slice, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
342 connect(slice, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
372 connect(slice, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
343 connect(slice, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
373 connect(slice, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
344
374
345 emit added(QList<QPieSlice*>() << slice);
375 emit added(QList<QPieSlice*>() << slice);
346 }
376 }
347
377
348 /*!
378 /*!
349 Removes a single \a slice from the series and deletes the slice.
379 Removes a single \a slice from the series and deletes the slice.
350
380
351 Do not reference this pointer after this call.
381 Do not reference this pointer after this call.
352 */
382 */
353 void QPieSeries::remove(QPieSlice* slice)
383 void QPieSeries::remove(QPieSlice* slice)
354 {
384 {
355 Q_D(QPieSeries);
385 Q_D(QPieSeries);
356 if (!d->m_slices.removeOne(slice)) {
386 if (!d->m_slices.removeOne(slice)) {
357 Q_ASSERT(0); // TODO: how should this be reported?
387 Q_ASSERT(0); // TODO: how should this be reported?
358 return;
388 return;
359 }
389 }
360
390
361 d->updateDerivativeData();
391 d->updateDerivativeData();
362
392
363 emit removed(QList<QPieSlice*>() << slice);
393 emit removed(QList<QPieSlice*>() << slice);
364
394
365 delete slice;
395 delete slice;
366 slice = 0;
396 slice = 0;
367 }
397 }
368
398
369 /*!
399 /*!
370 Clears all slices from the series.
400 Clears all slices from the series.
371 */
401 */
372 void QPieSeries::clear()
402 void QPieSeries::clear()
373 {
403 {
374 Q_D(QPieSeries);
404 Q_D(QPieSeries);
375 if (d->m_slices.count() == 0)
405 if (d->m_slices.count() == 0)
376 return;
406 return;
377
407
378 QList<QPieSlice*> slices = d->m_slices;
408 QList<QPieSlice*> slices = d->m_slices;
379 foreach (QPieSlice* s, d->m_slices) {
409 foreach (QPieSlice* s, d->m_slices) {
380 d->m_slices.removeOne(s);
410 d->m_slices.removeOne(s);
381 delete s;
411 delete s;
382 }
412 }
383
413
384 d->updateDerivativeData();
414 d->updateDerivativeData();
385
415
386 emit removed(slices);
416 emit removed(slices);
387 }
417 }
388
418
389 /*!
419 /*!
390 Counts the number of the slices in this series.
420 Counts the number of the slices in this series.
391 */
421 */
392 int QPieSeries::count() const
422 int QPieSeries::count() const
393 {
423 {
394 Q_D(const QPieSeries);
424 Q_D(const QPieSeries);
395 return d->m_slices.count();
425 return d->m_slices.count();
396 }
426 }
397
427
398 /*!
428 /*!
399 Returns true is the series is empty.
429 Returns true is the series is empty.
400 */
430 */
401 bool QPieSeries::isEmpty() const
431 bool QPieSeries::isEmpty() const
402 {
432 {
403 Q_D(const QPieSeries);
433 Q_D(const QPieSeries);
404 return d->m_slices.isEmpty();
434 return d->m_slices.isEmpty();
405 }
435 }
406
436
407 /*!
437 /*!
408 Returns a list of slices that belong to this series.
438 Returns a list of slices that belong to this series.
409 */
439 */
410 QList<QPieSlice*> QPieSeries::slices() const
440 QList<QPieSlice*> QPieSeries::slices() const
411 {
441 {
412 Q_D(const QPieSeries);
442 Q_D(const QPieSeries);
413 return d->m_slices;
443 return d->m_slices;
414 }
444 }
415
445
416 /*!
446 /*!
417 Sets the horizontal center position of the pie to \a relativePosition. If \a relativePosition is
447 Sets the horizontal center position of the pie to \a relativePosition. If \a relativePosition is
418 set to 0.0 the pie is drawn on the left side of the chart and if it's set to 1.0 the pie is
448 set to 0.0 the pie is drawn on the left side of the chart and if it's set to 1.0 the pie is
419 drawn on right side of the chart. The default value 0.5 puts the pie in the middle.
449 drawn on right side of the chart. The default value 0.5 puts the pie in the middle.
420
450
421 \sa setHorizontalPosition(), setPieSize()
451 \sa setHorizontalPosition(), setPieSize()
422 */
452 */
423 void QPieSeries::setHorizontalPosition(qreal relativePosition)
453 void QPieSeries::setHorizontalPosition(qreal relativePosition)
424 {
454 {
425 Q_D(QPieSeries);
455 Q_D(QPieSeries);
426 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
456 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
427 emit piePositionChanged();
457 emit piePositionChanged();
428 }
458 }
429
459
430 /*!
460 /*!
431 Sets the vertical center position of the pie to \a relativePosition. If \a relativePosition is
461 Sets the vertical center position of the pie to \a relativePosition. If \a relativePosition is
432 set to 0.0 the pie is drawn on the top of the chart and if it's set to 1.0 the pie is drawn
462 set to 0.0 the pie is drawn on the top of the chart and if it's set to 1.0 the pie is drawn
433 on bottom of the chart. The default value 0.5 puts the pie in the middle.
463 on bottom of the chart. The default value 0.5 puts the pie in the middle.
434
464
435 \sa setVerticalPosition(), setPieSize()
465 \sa verticalPosition(), setPieSize()
436 */
466 */
437 void QPieSeries::setVerticalPosition(qreal relativePosition)
467 void QPieSeries::setVerticalPosition(qreal relativePosition)
438 {
468 {
439 Q_D(QPieSeries);
469 Q_D(QPieSeries);
440 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
470 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
441 emit piePositionChanged();
471 emit piePositionChanged();
442 }
472 }
443
473
444 /*!
474 /*!
445 Gets the horizontal position of the pie.
475 Gets the horizontal position of the pie.
446
476
447 The returned value is relative to the chart rectangle where:
477 The returned value is relative to the chart rectangle where:
448
478
449 0.0 means the absolute left.
479 0.0 means the absolute left.
450 1.0 means the absolute right.
480 1.0 means the absolute right.
451
481
452 By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle.
482 By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle.
453
483
454 \sa verticalPosition(), setPieSize()
484 \sa verticalPosition(), setPieSize()
455 */
485 */
456 qreal QPieSeries::horizontalPosition() const
486 qreal QPieSeries::horizontalPosition() const
457 {
487 {
458 Q_D(const QPieSeries);
488 Q_D(const QPieSeries);
459 return d->m_pieRelativeHorPos;
489 return d->m_pieRelativeHorPos;
460 }
490 }
461
491
462 /*!
492 /*!
463 Gets the vertical position position of the pie.
493 Gets the vertical position position of the pie.
464
494
465 The returned value is relative to the chart rectangle where:
495 The returned value is relative to the chart rectangle where:
466
496
467 0.0 means the absolute top.
497 0.0 means the absolute top.
468 1.0 means the absolute bottom.
498 1.0 means the absolute bottom.
469
499
470 By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle.
500 By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle.
471
501
472 \sa horizontalPosition(), setPieSize()
502 \sa horizontalPosition(), setPieSize()
473 */
503 */
474 qreal QPieSeries::verticalPosition() const
504 qreal QPieSeries::verticalPosition() const
475 {
505 {
476 Q_D(const QPieSeries);
506 Q_D(const QPieSeries);
477 return d->m_pieRelativeVerPos;
507 return d->m_pieRelativeVerPos;
478 }
508 }
479
509
480 /*!
510 /*!
481 Sets the relative size of the pie.
511 Sets the relative size of the pie.
482
512
483 The \a relativeSize is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
513 The \a relativeSize is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
484
514
485 Default value is 0.7.
515 Default value is 0.7.
486
516
487 \sa pieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
517 \sa pieSize(), verticalPosition(), horizontalPosition()
488 */
518 */
489 void QPieSeries::setPieSize(qreal relativeSize)
519 void QPieSeries::setPieSize(qreal relativeSize)
490 {
520 {
491 Q_D(QPieSeries);
521 Q_D(QPieSeries);
492 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
522 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
493 emit pieSizeChanged();
523 emit pieSizeChanged();
494 }
524 }
495
525
496 /*!
526 /*!
497 Gets the relative size of the pie.
527 Gets the relative size of the pie.
498
528
499 The size is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
529 The size is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
500
530
501 Default value is 0.7.
531 Default value is 0.7.
502
532
503 \sa setPieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
533 \sa setPieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
504 */
534 */
505 qreal QPieSeries::pieSize() const
535 qreal QPieSeries::pieSize() const
506 {
536 {
507 Q_D(const QPieSeries);
537 Q_D(const QPieSeries);
508 return d->m_pieRelativeSize;
538 return d->m_pieRelativeSize;
509 }
539 }
510
540
511
541
512 /*!
542 /*!
513 Sets the end angle of the pie.
543 Sets the end angle of the pie.
514
544
515 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
545 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
516
546
517 \a angle must be less than pie end angle. Default value is 0.
547 \a angle must be less than pie end angle. Default value is 0.
518
548
519 \sa pieStartAngle(), pieEndAngle(), setPieEndAngle()
549 \sa pieStartAngle(), pieEndAngle(), setPieEndAngle()
520 */
550 */
521 void QPieSeries::setPieStartAngle(qreal angle)
551 void QPieSeries::setPieStartAngle(qreal angle)
522 {
552 {
523 Q_D(QPieSeries);
553 Q_D(QPieSeries);
524 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
554 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
525 d->updateDerivativeData();
555 d->updateDerivativeData();
526 }
556 }
527
557
528 /*!
558 /*!
529 Gets the start angle of the pie.
559 Gets the start angle of the pie.
530
560
531 Full pie is 360 degrees where 0 degrees is at 12 a'clock. Default value is 360.
561 Full pie is 360 degrees where 0 degrees is at 12 a'clock. Default value is 360.
532
562
533 \sa setPieStartAngle(), pieEndAngle(), setPieEndAngle()
563 \sa setPieStartAngle(), pieEndAngle(), setPieEndAngle()
534 */
564 */
535 qreal QPieSeries::pieStartAngle() const
565 qreal QPieSeries::pieStartAngle() const
536 {
566 {
537 Q_D(const QPieSeries);
567 Q_D(const QPieSeries);
538 return d->m_pieStartAngle;
568 return d->m_pieStartAngle;
539 }
569 }
540
570
541 /*!
571 /*!
542 Sets the end angle of the pie.
572 Sets the end angle of the pie.
543
573
544 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
574 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
545
575
546 \a angle must be greater than start angle.
576 \a angle must be greater than start angle.
547
577
548 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
578 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
549 */
579 */
550 void QPieSeries::setPieEndAngle(qreal angle)
580 void QPieSeries::setPieEndAngle(qreal angle)
551 {
581 {
552 Q_D(QPieSeries);
582 Q_D(QPieSeries);
553
583
554 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
584 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
555 d->updateDerivativeData();
585 d->updateDerivativeData();
556 }
586 }
557
587
558 /*!
588 /*!
559 Returns the end angle of the pie.
589 Returns the end angle of the pie.
560
590
561 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
591 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
562
592
563 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
593 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
564 */
594 */
565 qreal QPieSeries::pieEndAngle() const
595 qreal QPieSeries::pieEndAngle() const
566 {
596 {
567 Q_D(const QPieSeries);
597 Q_D(const QPieSeries);
568 return d->m_pieEndAngle;
598 return d->m_pieEndAngle;
569 }
599 }
570
600
571 /*!
601 /*!
572 Sets the all the slice labels \a visible or invisible.
602 Sets the all the slice labels \a visible or invisible.
573
603
574 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
604 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
575 */
605 */
576 void QPieSeries::setLabelsVisible(bool visible)
606 void QPieSeries::setLabelsVisible(bool visible)
577 {
607 {
578 Q_D(QPieSeries);
608 Q_D(QPieSeries);
579 foreach (QPieSlice* s, d->m_slices)
609 foreach (QPieSlice* s, d->m_slices)
580 s->setLabelVisible(visible);
610 s->setLabelVisible(visible);
581 }
611 }
582
612
583 /*!
613 /*!
584 Returns the sum of all slice values in this series.
614 Returns the sum of all slice values in this series.
585
615
586 \sa QPieSlice::value(), QPieSlice::setValue()
616 \sa QPieSlice::value(), QPieSlice::setValue()
587 */
617 */
588 qreal QPieSeries::total() const
618 qreal QPieSeries::total() const
589 {
619 {
590 Q_D(const QPieSeries);
620 Q_D(const QPieSeries);
591 return d->m_total;
621 return d->m_total;
592 }
622 }
593
623
594 /*!
624 /*!
595 \fn void QPieSeries::clicked(QPieSlice* slice, Qt::MouseButtons buttons)
625 \fn void QPieSeries::clicked(QPieSlice* slice, Qt::MouseButtons buttons)
596
626
597 This signal is emitted when a \a slice has been clicked with mouse \a buttons.
627 This signal is emitted when a \a slice has been clicked with mouse \a buttons.
598
628
599 \sa QPieSlice::clicked()
629 \sa QPieSlice::clicked()
600 */
630 */
601
631
602 /*!
632 /*!
603 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
633 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
604
634
605 This signal is emitted when user has hovered over a \a slice.
635 This signal is emitted when user has hovered over a \a slice.
606
636
607 \sa QPieSlice::hoverEnter()
637 \sa QPieSlice::hoverEnter()
608 */
638 */
609
639
610 /*!
640 /*!
611 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
641 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
612
642
613 This signal is emitted when user has hovered away from a \a slice.
643 This signal is emitted when user has hovered away from a \a slice.
614
644
615 \sa QPieSlice::hoverLeave()
645 \sa QPieSlice::hoverLeave()
616 */
646 */
617
647
618 /*!
648 /*!
619 \fn void QPieSeries::added(QList<QPieSlice*> slices)
649 \fn void QPieSeries::added(QList<QPieSlice*> slices)
620
650
621 This signal is emitted when \a slices has been added to the series.
651 This signal is emitted when \a slices has been added to the series.
622
652
623 \sa append(), insert()
653 \sa append(), insert()
624 */
654 */
625
655
626 /*!
656 /*!
627 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
657 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
628
658
629 This signal is emitted when \a slices has been removed from the series.
659 This signal is emitted when \a slices has been removed from the series.
630
660
631 \sa remove(), clear()
661 \sa remove(), clear()
632 */
662 */
633
663
634 /*!
664 /*!
635 \fn void QPieSeries::piePositionChanged()
665 \fn void QPieSeries::piePositionChanged()
636
666
637 This signal is emitted when pie position has changed.
667 This signal is emitted when pie position has changed.
638
668
639 \sa verticalPosition(), setVerticalPosition(), horizontalPosition(), setHorizontalPosition()
669 \sa verticalPosition(), setVerticalPosition(), horizontalPosition(), setHorizontalPosition()
640 */
670 */
641
671
642 /*!
672 /*!
643 \fn void QPieSeries::pieSizeChanged()
673 \fn void QPieSeries::pieSizeChanged()
644
674
645 This signal is emitted when pie size has changed.
675 This signal is emitted when pie size has changed.
646
676
647 \sa pieSize(), setPieSize()
677 \sa pieSize(), setPieSize()
648 */
678 */
649
679
650 /*!
680 /*!
651 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
681 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
652 Sets the \a model to be used as a data source
682 Sets the \a model to be used as a data source
653 */
683 */
654 bool QPieSeries::setModel(QAbstractItemModel* model)
684 bool QPieSeries::setModel(QAbstractItemModel* model)
655 {
685 {
656 Q_D(QPieSeries);
686 Q_D(QPieSeries);
657 // disconnect signals from old model
687 // disconnect signals from old model
658 if(m_model)
688 if(m_model)
659 {
689 {
660 disconnect(m_model, 0, this, 0);
690 disconnect(m_model, 0, this, 0);
661 d->m_mapValues = -1;
691 d->m_mapValues = -1;
662 d->m_mapLabels = -1;
692 d->m_mapLabels = -1;
663 d->m_mapOrientation = Qt::Vertical;
693 d->m_mapOrientation = Qt::Vertical;
664 }
694 }
665
695
666 // set new model
696 // set new model
667 if(model)
697 if(model)
668 {
698 {
669 m_model = model;
699 m_model = model;
670 return true;
700 return true;
671 }
701 }
672 else
702 else
673 {
703 {
674 m_model = 0;
704 m_model = 0;
675 return false;
705 return false;
676 }
706 }
677 }
707 }
678
708
679 /*!
709 /*!
680 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
710 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
681 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
711 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
682 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
712 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
683 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
713 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
684 The \a orientation paramater specifies whether the data is in columns or in rows.
714 The \a orientation paramater specifies whether the data is in columns or in rows.
685 */
715 */
686 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
716 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
687 {
717 {
688 Q_D(QPieSeries);
718 Q_D(QPieSeries);
689
719
690 if (m_model == 0)
720 if (m_model == 0)
691 return;
721 return;
692
722
693 d->m_mapValues = modelValuesLine;
723 d->m_mapValues = modelValuesLine;
694 d->m_mapLabels = modelLabelsLine;
724 d->m_mapLabels = modelLabelsLine;
695 d->m_mapOrientation = orientation;
725 d->m_mapOrientation = orientation;
696
726
697 // connect the signals
727 // connect the signals
698 if (d->m_mapOrientation == Qt::Vertical) {
728 if (d->m_mapOrientation == Qt::Vertical) {
699 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
729 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
700 connect(m_model, SIGNAL(rowsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
730 connect(m_model, SIGNAL(rowsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
701 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
731 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
702 } else {
732 } else {
703 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
733 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
704 connect(m_model, SIGNAL(columnsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
734 connect(m_model, SIGNAL(columnsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
705 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
735 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
706 }
736 }
707
737
708 // create the initial slices set
738 // create the initial slices set
709 if (d->m_mapOrientation == Qt::Vertical) {
739 if (d->m_mapOrientation == Qt::Vertical) {
710 for (int i = 0; i < m_model->rowCount(); i++)
740 for (int i = 0; i < m_model->rowCount(); i++)
711 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());
741 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());
712 } else {
742 } else {
713 for (int i = 0; i < m_model->columnCount(); i++)
743 for (int i = 0; i < m_model->columnCount(); i++)
714 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());
744 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());
715 }
745 }
716 }
746 }
717
747
718 #include "moc_qpieseries.cpp"
748 #include "moc_qpieseries.cpp"
719 #include "moc_qpieseriesprivate_p.cpp"
749 #include "moc_qpieseriesprivate_p.cpp"
720
750
721 QTCOMMERCIALCHART_END_NAMESPACE
751 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now