##// END OF EJS Templates
Revert: Pie now has defualt Model to which slice data is copied.
Marek Rosa -
r1204:9aa9e4af8c0e
parent child
Show More
@@ -26,8 +26,7
26 26 #include "charttheme_p.h"
27 27 #include "chartanimator_p.h"
28 28 #include "legendmarker_p.h"
29 //#include <QAbstractItemModel>
30 #include <QStandardItemModel>
29 #include <QAbstractItemModel>
31 30 #include "qpiemodelmapper.h"
32 31
33 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -216,33 +215,16 bool QPieSeries::insert(int index, QPieSlice* slice)
216 215 if (!slice || d->m_slices.contains(slice))
217 216 return false;
218 217
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 }
227
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)));
218 slice->setParent(this);
219 d->m_slices.insert(index, slice);
242 220
243 // emit d->added(QList<QPieSlice*>() << slice);
221 d->updateDerivativeData();
244 222
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)));
245 226
227 emit d->added(QList<QPieSlice*>() << slice);
246 228
247 229 return true;
248 230 }
@@ -275,24 +257,18 bool QPieSeries::remove(QPieSlice* slice)
275 257 void QPieSeries::clear()
276 258 {
277 259 Q_D(QPieSeries);
278 if (count() == 0)
260 if (d->m_slices.count() == 0)
279 261 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;
286 262
287 // QList<QPieSlice*> slices = d->m_slices;
288 // foreach (QPieSlice* s, d->m_slices) {
289 // d->m_slices.removeOne(s);
290 // delete s;
291 // }
263 QList<QPieSlice*> slices = d->m_slices;
264 foreach (QPieSlice* s, d->m_slices) {
265 d->m_slices.removeOne(s);
266 delete s;
267 }
292 268
293 // d->updateDerivativeData();
269 d->updateDerivativeData();
294 270
295 // emit d->removed(slices);
271 emit d->removed(slices);
296 272 }
297 273
298 274 /*!
@@ -301,31 +277,7 void QPieSeries::clear()
301 277 int QPieSeries::count() const
302 278 {
303 279 Q_D(const QPieSeries);
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 // }
280 return d->m_slices.count();
329 281 }
330 282
331 283 /*!
@@ -477,14 +429,15 qreal QPieSeries::sum() const
477 429 void QPieSeries::setModel(QAbstractItemModel* model)
478 430 {
479 431 Q_D(QPieSeries);
432 // disconnect signals from old model
433 if(d->m_model)
434 {
435 disconnect(d->m_model, 0, this, 0);
436 }
480 437
481 438 // set new model
482 439 if(model)
483 440 {
484 // disconnect signals from old model and delete it
485 disconnect(d->m_model, 0, this, 0);
486 delete d->m_model;
487
488 441 d->m_model = model;
489 442 // connect signals from the model
490 443 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
@@ -496,37 +449,31 void QPieSeries::setModel(QAbstractItemModel* model)
496 449 if (d->m_mapper)
497 450 d->initializePieFromModel();
498 451 }
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 //}
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 }
530 477
531 478 QPieModelMapper* QPieSeries::modelMapper() const
532 479 {
@@ -545,18 +492,9 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
545 492 m_pieStartAngle(0),
546 493 m_pieEndAngle(360),
547 494 m_sum(0),
548 m_model(new QStandardItemModel),
549 m_mapper(new QPieModelMapper)
495 m_mapper(0)
550 496 {
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)));
557 497
558 // connect the signal from the mapper
559 connect(m_mapper, SIGNAL(updated()), this, SLOT(initializePieFromModel()));
560 498 }
561 499
562 500 QPieSeriesPrivate::~QPieSeriesPrivate()
@@ -611,23 +549,6 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
611 549 void QPieSeriesPrivate::sliceChanged()
612 550 {
613 551 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 }
631 552 updateDerivativeData();
632 553 }
633 554
@@ -647,59 +568,6 void QPieSeriesPrivate::sliceHovered(bool state)
647 568 emit q->hovered(slice, state);
648 569 }
649 570
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
703 571 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
704 572 {
705 573 if (m_mapper) {
@@ -795,13 +663,11 void QPieSeriesPrivate::insertData(int start, int end)
795 663 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
796 664 }
797 665 slice->setLabelVisible();
798 // q->insert(i - m_mapper->first(), slice);
799 doInsert(i - m_mapper->first(), slice);
666 q->insert(i - m_mapper->first(), slice);
800 667 }
801 668 if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count())
802 669 for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--)
803 // q->remove(q->slices().at(i));
804 doRemove(q->slices().at(i));
670 q->remove(q->slices().at(i));
805 671 }
806 672 }
807 673 }
@@ -818,8 +684,7 void QPieSeriesPrivate::removeData(int start, int end)
818 684 int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed.
819 685 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed.
820 686 for (int i = last; i >= first; i--)
821 // q->remove(q->slices().at(i - m_mapper->first()));
822 doRemove(q->slices().at(i - m_mapper->first()));
687 q->remove(q->slices().at(i - m_mapper->first()));
823 688
824 689 if (m_mapper->count() != -1) {
825 690 int itemsAvailable; // check how many are available to be added
@@ -840,8 +705,7 void QPieSeriesPrivate::removeData(int start, int end)
840 705 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString());
841 706 }
842 707 slice->setLabelVisible();
843 // q->insert(i, slice);
844 doInsert(i, slice);
708 q->insert(i, slice);
845 709 }
846 710 }
847 711 }
@@ -853,7 +717,7 void QPieSeriesPrivate::initializePieFromModel()
853 717 Q_Q(QPieSeries);
854 718
855 719 // clear current content
856 doClear();
720 q->clear();
857 721
858 722 if (m_model == 0 || m_mapper == 0)
859 723 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:
@@ -329,6 +329,8 void tst_qpieseries::hoverSignal()
329 329
330 330 void tst_qpieseries::model()
331 331 {
332 QSKIP("Needs to be checked again. Model implementation changed", SkipAll);
333
332 334 QPieSeries *series = new QPieSeries;
333 335 QChart *chart = new QChart;
334 336 chart->addSeries(series);
@@ -337,7 +339,6 void tst_qpieseries::model()
337 339
338 340 QStandardItemModel *stdModel = new QStandardItemModel(0, 2);
339 341 series->setModel(stdModel);
340 QVERIFY2((series->model()) == stdModel, "Model should be stdModel");
341 342
342 343 int rowCount = 3;
343 344 for (int row = 0; row < rowCount; ++row) {
@@ -380,6 +381,8 void tst_qpieseries::model()
380 381
381 382 void tst_qpieseries::modelCustomMap()
382 383 {
384 QSKIP("Needs to be checked again. Model implementation changed", SkipAll);
385
383 386 int rowCount = 12;
384 387 int columnCount = 3;
385 388 QStandardItemModel *stdModel = new QStandardItemModel(0, 3);
@@ -425,6 +428,8 void tst_qpieseries::modelCustomMap()
425 428
426 429 void tst_qpieseries::modelUpdate()
427 430 {
431 QSKIP("Needs to be checked again. Model implementation changed", SkipAll);
432
428 433 int rowCount = 12;
429 434 int columnCount = 7;
430 435 QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount);
General Comments 0
You need to be logged in to leave comments. Login now