##// END OF EJS Templates
Pie now has defualt Model to which slice data is copied.
Marek Rosa -
r1195:505ff5d19e3d
parent child
Show More
@@ -33,13 +33,13 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
33 33 // setModel(new DeclarativeTableModel());
34 34
35 35 // TODO: Set default mapper parameters to allow easy to use PieSeries api?
36 QPieModelMapper *mapper = new QPieModelMapper();
36 QPieModelMapper *mapper = modelMapper();//new QPieModelMapper();
37 37 mapper->setMapLabels(0);
38 38 mapper->setMapValues(1);
39 39 mapper->setOrientation(Qt::Vertical);
40 40 mapper->setFirst(0);
41 41 mapper->setCount(-1);
42 setModelMapper(mapper);
42 // setModelMapper(mapper);
43 43 }
44 44
45 45 void DeclarativePieSeries::classBegin()
@@ -26,7 +26,8
26 26 #include "charttheme_p.h"
27 27 #include "chartanimator_p.h"
28 28 #include "legendmarker_p.h"
29 #include <QAbstractItemModel>
29 //#include <QAbstractItemModel>
30 #include <QStandardItemModel>
30 31 #include "qpiemodelmapper.h"
31 32
32 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -215,16 +216,33 bool QPieSeries::insert(int index, QPieSlice* slice)
215 216 if (!slice || d->m_slices.contains(slice))
216 217 return false;
217 218
218 slice->setParent(this);
219 d->m_slices.insert(index, slice);
219 d->m_model->insertRow(index);
220 if (d->m_mapper->orientation() == Qt::Vertical) {
221 d->m_model->setData(d->m_model->index(index, d->m_mapper->mapValues()), slice->value());
222 d->m_model->setData(d->m_model->index(index, d->m_mapper->mapLabels()), slice->label());
223 } else {
224 d->m_model->setData(d->m_model->index(d->m_mapper->mapValues(), index), slice->value());
225 d->m_model->setData(d->m_model->index(d->m_mapper->mapLabels(), index), slice->label());
226 }
220 227
221 d->updateDerivativeData();
228 // if (index < 0 || index > d->m_slices.count())
229 // return false;
230
231 // if (!slice || d->m_slices.contains(slice))
232 // return false;
233
234 // slice->setParent(this);
235 // d->m_slices.insert(index, slice);
236
237 // d->updateDerivativeData();
238
239 // connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
240 // connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
241 // connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
242
243 // emit d->added(QList<QPieSlice*>() << slice);
222 244
223 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
224 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
225 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
226 245
227 emit d->added(QList<QPieSlice*>() << slice);
228 246
229 247 return true;
230 248 }
@@ -257,18 +275,24 bool QPieSeries::remove(QPieSlice* slice)
257 275 void QPieSeries::clear()
258 276 {
259 277 Q_D(QPieSeries);
260 if (d->m_slices.count() == 0)
278 if (count() == 0)
261 279 return;
280 if (d->m_mapper->orientation() == Qt::Vertical)
281 d->m_model->removeRows(d->m_mapper->first(), count());
282 else
283 d->m_model->removeColumns(d->m_mapper->first(), count());
284 // if (d->m_slices.count() == 0)
285 // return;
262 286
263 QList<QPieSlice*> slices = d->m_slices;
264 foreach (QPieSlice* s, d->m_slices) {
265 d->m_slices.removeOne(s);
266 delete s;
267 }
287 // QList<QPieSlice*> slices = d->m_slices;
288 // foreach (QPieSlice* s, d->m_slices) {
289 // d->m_slices.removeOne(s);
290 // delete s;
291 // }
268 292
269 d->updateDerivativeData();
293 // d->updateDerivativeData();
270 294
271 emit d->removed(slices);
295 // emit d->removed(slices);
272 296 }
273 297
274 298 /*!
@@ -277,7 +301,31 void QPieSeries::clear()
277 301 int QPieSeries::count() const
278 302 {
279 303 Q_D(const QPieSeries);
280 return d->m_slices.count();
304 // return d->m_slices.count();
305
306 // if (d->m_model && d->m_mapper) {
307
308 if (d->m_mapper->orientation() == Qt::Vertical) {
309 // data is in a column. Return the number of mapped items if the model's column have enough items
310 // or the number of items that can be mapped
311 if (d->m_mapper->mapValues() >= d->m_model->columnCount() || d->m_mapper->mapLabels() >= d->m_model->columnCount())
312 return 0; // mapped columns are not existing
313 else if (d->m_mapper->count() != -1)
314 // void setModelMapper(QPieModelMapper *mapper);
315 return qMin(d->m_mapper->count(), qMax(d->m_model->rowCount() - d->m_mapper->first(), 0));
316 else
317 return qMax(d->m_model->rowCount() - d->m_mapper->first(), 0);
318 } else {
319 // data is in a row. Return the number of mapped items if the model's row have enough items
320 // or the number of items that can be mapped
321 if (d->m_mapper->mapValues() >= d->m_model->rowCount() || d->m_mapper->mapLabels() >= d->m_model->rowCount())
322 return 0; // mapped rows are not existing
323 else if (d->m_mapper->count() != -1)
324 return qMin(d->m_mapper->count(), qMax(d->m_model->columnCount() - d->m_mapper->first(), 0));
325 else
326 return qMax(d->m_model->columnCount() - d->m_mapper->first(), 0);
327 }
328 // }
281 329 }
282 330
283 331 /*!
@@ -429,15 +477,14 qreal QPieSeries::sum() const
429 477 void QPieSeries::setModel(QAbstractItemModel* model)
430 478 {
431 479 Q_D(QPieSeries);
432 // disconnect signals from old model
433 if(d->m_model)
434 {
435 disconnect(d->m_model, 0, this, 0);
436 }
437 480
438 481 // set new model
439 482 if(model)
440 483 {
484 // disconnect signals from old model and delete it
485 disconnect(d->m_model, 0, this, 0);
486 delete d->m_model;
487
441 488 d->m_model = model;
442 489 // connect signals from the model
443 490 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
@@ -449,31 +496,37 void QPieSeries::setModel(QAbstractItemModel* model)
449 496 if (d->m_mapper)
450 497 d->initializePieFromModel();
451 498 }
452 else
453 {
454 d->m_model = 0;
455 }
456 }
457
458 void QPieSeries::setModelMapper(QPieModelMapper *mapper)
459 {
460 Q_D(QPieSeries);
461 // disconnect signals from old mapper
462 if (d->m_mapper) {
463 QObject::disconnect(d->m_mapper, 0, this, 0);
464 }
465
466 if (mapper) {
467 d->m_mapper = mapper;
468 // connect the signal from the mapper
469 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
470
471 if (d->m_model)
472 d->initializePieFromModel();
473 } else {
474 d->m_mapper = 0;
475 }
476 }
499 // else
500 // {
501 // d->m_model = new QStandardItemModel;
502 // d->m_mapper->reset();
503 // d->m_mapper->setMapLabels(0);
504 // d->m_mapper->setMapValues(1);
505 // }
506 }
507
508 //void QPieSeries::setModelMapper(QPieModelMapper *mapper)
509 //{
510 // Q_D(QPieSeries);
511 // // disconnect signals from old mapper
512 // if (d->m_mapper) {
513 // QObject::disconnect(d->m_mapper, 0, this, 0);
514 // }
515
516 // if (mapper) {
517 // d->m_mapper = mapper;
518
519 // if (d->m_model)
520 // d->initializePieFromModel();
521 // } else {
522 // d->m_mapper = new QPieModelMapper;
523 // d->m_mapper->setMapLabels(0);
524 // d->m_mapper->setMapValues(1);
525 // }
526
527 // // connect the signal from the mapper
528 // connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
529 //}
477 530
478 531 QPieModelMapper* QPieSeries::modelMapper() const
479 532 {
@@ -492,9 +545,18 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
492 545 m_pieStartAngle(0),
493 546 m_pieEndAngle(360),
494 547 m_sum(0),
495 m_mapper(0)
548 m_model(new QStandardItemModel),
549 m_mapper(new QPieModelMapper)
496 550 {
551 // connect signals from the model
552 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
553 connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelRowsAdded(QModelIndex,int,int)));
554 connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelRowsRemoved(QModelIndex,int,int)));
555 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelColumnsAdded(QModelIndex,int,int)));
556 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
497 557
558 // connect the signal from the mapper
559 connect(m_mapper, SIGNAL(updated()), this, SLOT(initializePieFromModel()));
498 560 }
499 561
500 562 QPieSeriesPrivate::~QPieSeriesPrivate()
@@ -549,6 +611,23 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
549 611 void QPieSeriesPrivate::sliceChanged()
550 612 {
551 613 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
614 QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
615 int sliceIndex = m_slices.indexOf(slice);
616 if (m_mapper->orientation() == Qt::Vertical) {
617 QModelIndex modelIndex = m_model->index(sliceIndex + m_mapper->first(), m_mapper->mapValues());
618 if (m_model->data(modelIndex).toReal() != slice->value())
619 m_model->setData(modelIndex, slice->value());
620 modelIndex = m_model->index(sliceIndex + m_mapper->first(), m_mapper->mapLabels());
621 if (m_model->data(modelIndex).toString() != slice->label())
622 m_model->setData(modelIndex, slice->label());
623 } else {
624 QModelIndex modelIndex = m_model->index(m_mapper->mapValues(), sliceIndex + m_mapper->first());
625 if (m_model->data(modelIndex).toReal() != slice->value())
626 m_model->setData(modelIndex, slice->value());
627 modelIndex = m_model->index(m_mapper->mapLabels(), sliceIndex + m_mapper->first());
628 if (m_model->data(modelIndex).toString() != slice->label())
629 m_model->setData(modelIndex, slice->label());
630 }
552 631 updateDerivativeData();
553 632 }
554 633
@@ -568,6 +647,59 void QPieSeriesPrivate::sliceHovered(bool state)
568 647 emit q->hovered(slice, state);
569 648 }
570 649
650 void QPieSeriesPrivate::doClear()
651 {
652 if (m_slices.count() == 0)
653 return;
654
655 QList<QPieSlice*> slices = m_slices;
656 foreach (QPieSlice* s, m_slices) {
657 m_slices.removeOne(s);
658 delete s;
659 }
660
661 updateDerivativeData();
662
663 emit removed(slices);
664 }
665
666 void QPieSeriesPrivate::doRemove(QPieSlice* slice)
667 {
668 if (!m_slices.removeOne(slice))
669 return;// false;
670
671 updateDerivativeData();
672
673 emit removed(QList<QPieSlice*>() << slice);
674
675 delete slice;
676 slice = 0;
677
678 return;// true;
679 }
680
681 void QPieSeriesPrivate::doInsert(int index, QPieSlice* slice)
682 {
683 if (index < 0 || index > m_slices.count())
684 return;// false;
685
686 if (!slice || m_slices.contains(slice))
687 return;// false;
688
689 slice->setParent(this);
690 m_slices.insert(index, slice);
691
692 updateDerivativeData();
693
694 connect(slice, SIGNAL(changed()), this, SLOT(sliceChanged()));
695 connect(slice, SIGNAL(clicked()), this, SLOT(sliceClicked()));
696 connect(slice, SIGNAL(hovered(bool)), this, SLOT(sliceHovered(bool)));
697
698 emit added(QList<QPieSlice*>() << slice);
699
700 return;// true;
701 }
702
571 703 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
572 704 {
573 705 if (m_mapper) {
@@ -663,11 +795,13 void QPieSeriesPrivate::insertData(int start, int end)
663 795 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
664 796 }
665 797 slice->setLabelVisible();
666 q->insert(i - m_mapper->first(), slice);
798 // q->insert(i - m_mapper->first(), slice);
799 doInsert(i - m_mapper->first(), slice);
667 800 }
668 801 if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count())
669 802 for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--)
670 q->remove(q->slices().at(i));
803 // q->remove(q->slices().at(i));
804 doRemove(q->slices().at(i));
671 805 }
672 806 }
673 807 }
@@ -684,7 +818,8 void QPieSeriesPrivate::removeData(int start, int end)
684 818 int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed.
685 819 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed.
686 820 for (int i = last; i >= first; i--)
687 q->remove(q->slices().at(i - m_mapper->first()));
821 // q->remove(q->slices().at(i - m_mapper->first()));
822 doRemove(q->slices().at(i - m_mapper->first()));
688 823
689 824 if (m_mapper->count() != -1) {
690 825 int itemsAvailable; // check how many are available to be added
@@ -705,7 +840,8 void QPieSeriesPrivate::removeData(int start, int end)
705 840 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString());
706 841 }
707 842 slice->setLabelVisible();
708 q->insert(i, slice);
843 // q->insert(i, slice);
844 doInsert(i, slice);
709 845 }
710 846 }
711 847 }
@@ -717,7 +853,7 void QPieSeriesPrivate::initializePieFromModel()
717 853 Q_Q(QPieSeries);
718 854
719 855 // clear current content
720 q->clear();
856 doClear();
721 857
722 858 if (m_model == 0 || m_mapper == 0)
723 859 return;
@@ -75,7 +75,7 public:
75 75 void setLabelsVisible(bool visible = true);
76 76
77 77 void setModel(QAbstractItemModel* model);
78 void setModelMapper(QPieModelMapper *mapper);
78 // void setModelMapper(QPieModelMapper *mapper);
79 79 QPieModelMapper* modelMapper() const;
80 80
81 81 Q_SIGNALS:
@@ -65,6 +65,9 public Q_SLOTS:
65 65 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
66 66
67 67 private:
68 void doClear();
69 void doRemove(QPieSlice* slice);
70 void doInsert(int index, QPieSlice* slice);
68 71 void insertData(int start, int end);
69 72 void removeData(int start, int end);
70 73
@@ -77,7 +80,8 public:
77 80 qreal m_pieEndAngle;
78 81 qreal m_sum;
79 82
80 // model map
83 // model
84 QAbstractItemModel *m_model;
81 85 QPieModelMapper *m_mapper;
82 86
83 87 private:
@@ -20,6 +20,7
20 20
21 21 #include "qpieslice.h"
22 22 #include "pieslicedata_p.h"
23 #include <QAbstractItemModel>
23 24
24 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 26
@@ -271,6 +272,7 void QPieSlice::setLabel(QString label)
271 272 {
272 273 if (d->m_labelText != label) {
273 274 d->m_labelText = label;
275
274 276 emit changed();
275 277 }
276 278 }
@@ -351,10 +351,10 void tst_qpieseries::model()
351 351 QVERIFY2(series->slices().count() == 0, "Mapper has not been set, so the number of slices should be 0");
352 352
353 353 // set the mapper
354 QPieModelMapper *mapper = new QPieModelMapper;
354 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
355 355 mapper->setMapValues(0);
356 356 mapper->setMapLabels(0);
357 series->setModelMapper(mapper); // this should cause the Pie to get initialized from the model, since there is now both the model and the mapper defined
357 // series->setModelMapper(mapper); // this should cause the Pie to get initialized from the model, since there is now both the model and the mapper defined
358 358 QCOMPARE(series->slices().count(), rowCount);
359 359
360 360 // set the mappings to be outside of the model
@@ -373,9 +373,9 void tst_qpieseries::model()
373 373
374 374 // unset the model and the mapper
375 375 series->setModel(0);
376 series->setModelMapper(0);
376 // series->setModelMapper(0);
377 377 QVERIFY(series->model() == 0); // Model should be unset
378 QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
378 // QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
379 379 }
380 380
381 381 void tst_qpieseries::modelCustomMap()
@@ -397,10 +397,10 void tst_qpieseries::modelCustomMap()
397 397 chartView->show();
398 398 series->setModel(stdModel);
399 399
400 QPieModelMapper *mapper = new QPieModelMapper;
400 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
401 401 mapper->setMapValues(0);
402 402 mapper->setMapLabels(0);
403 series->setModelMapper(mapper);
403 // series->setModelMapper(mapper);
404 404 QCOMPARE(series->slices().count(), rowCount);
405 405
406 406 // lets change the orientation to horizontal
@@ -442,10 +442,10 void tst_qpieseries::modelUpdate()
442 442 chartView->show();
443 443 series->setModel(stdModel);
444 444
445 QPieModelMapper *mapper = new QPieModelMapper;
445 QPieModelMapper *mapper = series->modelMapper();//new QPieModelMapper;
446 446 mapper->setMapValues(0);
447 447 mapper->setMapLabels(0);
448 series->setModelMapper(mapper);
448 // series->setModelMapper(mapper);
449 449
450 450 stdModel->insertRows(3, 5);
451 451 QCOMPARE(series->slices().count(), rowCount + 5);
@@ -342,12 +342,12 void TableWidget::updateChartType(bool toggle)
342 342 QPieSeries* pieSeries = new QPieSeries;
343 343 pieSeries->setModel(m_model);
344 344
345 QPieModelMapper *mapper = new QPieModelMapper;
346 mapper->setMapValues(-1);
347 mapper->setMapLabels(-1);
345 QPieModelMapper *mapper = pieSeries->modelMapper();//new QPieModelMapper;
346 mapper->setMapValues(1);
347 mapper->setMapLabels(0);
348 348 mapper->setFirst(2);
349 349 mapper->setCount(5);
350 pieSeries->setModelMapper(mapper);
350 // pieSeries->setModelMapper(mapper);
351 351
352 352 pieSeries->setLabelsVisible(true);
353 353 pieSeries->setPieSize(0.75);
@@ -355,9 +355,13 void TableWidget::updateChartType(bool toggle)
355 355 // pieSeries->setVerticalPosition(0.3);
356 356
357 357 m_chart->addSeries(pieSeries);
358 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
358 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
359 359 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
360 360
361
362 pieSeries->slices().at(0)->setValue(400);
363 pieSeries->slices().at(0)->setLabel(QString("36"));
364
361 365 // // pie 2
362 366 // pieSeries = new QPieSeries;
363 367 // pieSeries->setModel(m_model);
@@ -453,7 +457,7 void TableWidget::updateChartType(bool toggle)
453 457
454 458 void TableWidget::testPie()
455 459 {
456 m_series->modelMapper()->setMapX(4);
460 // m_series->modelMapper()->setMapX(4);
457 461 // m_tableView->setColumnWidth(10, 250);
458 462 // if (specialPie) {
459 463 // specialPie->remove(specialPie->slices().at(2));
General Comments 0
You need to be logged in to leave comments. Login now