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