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