##// END OF EJS Templates
Fixes to barseries and pieseries model support. Started adding barseries model tests
Marek Rosa -
r1184:3138389fa706
parent child
Show More
@@ -87,6 +87,7 void QBarModelMapper::reset()
87 m_mapBarBottom = -1;
87 m_mapBarBottom = -1;
88 m_mapBarTop = -1;
88 m_mapBarTop = -1;
89 m_mapCategories = -1;
89 m_mapCategories = -1;
90 emit updated();
90 }
91 }
91
92
92 #include "moc_qbarmodelmapper.cpp"
93 #include "moc_qbarmodelmapper.cpp"
@@ -255,7 +255,7 void QBarSeries::setModelMapper(QBarModelMapper *mapper)
255 if (mapper) {
255 if (mapper) {
256 d->m_mapper = mapper;
256 d->m_mapper = mapper;
257 // connect the signal from the mapper
257 // connect the signal from the mapper
258 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
258 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializeDataFromModel()));
259
259
260 if (d->m_model)
260 if (d->m_model)
261 d->initializeDataFromModel();
261 d->initializeDataFromModel();
@@ -436,6 +436,9 qreal QBarSeriesPrivate::maxCategorySum()
436
436
437 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
437 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
438 {
438 {
439 if (m_model == 0 || m_mapper == 0)
440 return;
441
439 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
442 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
440 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
443 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
441 if (m_mapper->orientation() == Qt::Vertical)
444 if (m_mapper->orientation() == Qt::Vertical)
@@ -480,14 +483,21 void QBarSeriesPrivate::initializeDataFromModel()
480 {
483 {
481 Q_Q(QBarSeries);
484 Q_Q(QBarSeries);
482
485
483 if (m_model == 0 || m_mapper == 0)
484 return;
485
486 // create the initial bars
486 // create the initial bars
487 m_categories.clear();
487 m_categories.clear();
488 m_barSets.clear();
488 m_barSets.clear();
489
490 if (m_model == 0 || m_mapper == 0)
491 return;
492
493 // check if mappings are set
494 if (m_mapper->mapBarBottom() == -1 || m_mapper->mapBarTop() == -1 || m_mapper->mapCategories() == -1)
495 return;
496
489 // emit restructuredBars();
497 // emit restructuredBars();
490 if (m_mapper->orientation() == Qt::Vertical) {
498 if (m_mapper->orientation() == Qt::Vertical) {
499 if (m_mapCategories >= m_model->columnCount())
500 return;
491 int rowCount = 0;
501 int rowCount = 0;
492 if(m_mapper->count() == -1)
502 if(m_mapper->count() == -1)
493 rowCount = m_model->rowCount() - m_mapper->first();
503 rowCount = m_model->rowCount() - m_mapper->first();
@@ -497,13 +507,17 void QBarSeriesPrivate::initializeDataFromModel()
497 m_categories << m_model->data(m_model->index(k, m_mapper->mapCategories()), Qt::DisplayRole).toString();
507 m_categories << m_model->data(m_model->index(k, m_mapper->mapCategories()), Qt::DisplayRole).toString();
498 }
508 }
499
509
500 for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
510 int lastAvailableBarSet = qMin(m_model->columnCount() - 1, m_mapper->mapBarTop());
511 for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) {
512 // for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
501 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
513 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
502 for(int m = m_mapper->first(); m < m_mapper->first() + rowCount; m++)
514 for(int m = m_mapper->first(); m < m_mapper->first() + rowCount; m++)
503 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
515 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
504 q->appendBarSet(barSet);
516 q->appendBarSet(barSet);
505 }
517 }
506 } else {
518 } else {
519 if (m_mapCategories >= m_model->rowCount())
520 return;
507 int columnCount = 0;
521 int columnCount = 0;
508 if(m_mapper->count() == -1)
522 if(m_mapper->count() == -1)
509 columnCount = m_model->columnCount() - m_mapper->first();
523 columnCount = m_model->columnCount() - m_mapper->first();
@@ -513,7 +527,9 void QBarSeriesPrivate::initializeDataFromModel()
513 m_categories << m_model->data(m_model->index(m_mapper->mapCategories(), k), Qt::DisplayRole).toString();
527 m_categories << m_model->data(m_model->index(m_mapper->mapCategories(), k), Qt::DisplayRole).toString();
514 }
528 }
515
529
516 for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
530 int lastAvailableBarSet = qMin(m_model->rowCount() - 1, m_mapper->mapBarTop());
531 for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) {
532 // for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
517 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
533 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
518 for(int m = m_mapper->first(); m < m_mapper->first() + columnCount; m++)
534 for(int m = m_mapper->first(); m < m_mapper->first() + columnCount; m++)
519 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
535 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
@@ -716,12 +716,12 void QPieSeriesPrivate::initializePieFromModel()
716 {
716 {
717 Q_Q(QPieSeries);
717 Q_Q(QPieSeries);
718
718
719 if (m_model == 0 || m_mapper == 0)
720 return;
721
722 // clear current content
719 // clear current content
723 q->clear();
720 q->clear();
724
721
722 if (m_model == 0 || m_mapper == 0)
723 return;
724
725 // check if mappings are set
725 // check if mappings are set
726 if (m_mapper->mapValues() == -1 || m_mapper->mapLabels() == -1)
726 if (m_mapper->mapValues() == -1 || m_mapper->mapLabels() == -1)
727 return;
727 return;
@@ -23,6 +23,8
23 #include <qbarset.h>
23 #include <qbarset.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25 #include <qchart.h>
25 #include <qchart.h>
26 #include <QBarModelMapper>
27 #include <QStandardItemModel>
26
28
27 QTCOMMERCIALCHART_USE_NAMESPACE
29 QTCOMMERCIALCHART_USE_NAMESPACE
28
30
@@ -47,6 +49,7 private slots:
47 void mouseclicked();
49 void mouseclicked();
48 void mousehovered_data();
50 void mousehovered_data();
49 void mousehovered();
51 void mousehovered();
52 void model();
50
53
51 private:
54 private:
52 QGroupedBarSeries* m_barseries;
55 QGroupedBarSeries* m_barseries;
@@ -335,6 +338,59 void tst_QGroupedBarSeries::mousehovered()
335 QVERIFY(seriesSpyArg.at(1).toBool() == false);
338 QVERIFY(seriesSpyArg.at(1).toBool() == false);
336 }
339 }
337
340
341 void tst_QGroupedBarSeries::model()
342 {
343 QGroupedBarSeries *series = new QGroupedBarSeries;
344 QChart *chart = new QChart;
345 chart->addSeries(series);
346 QChartView *chartView = new QChartView(chart);
347 chartView->show();
348
349 int rowCount = 12;
350 int columnCount = 5;
351 QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount);
352 series->setModel(stdModel);
353 QVERIFY2((series->model()) == stdModel, "Model should be stdModel");
354
355
356 for (int row = 0; row < rowCount; ++row) {
357 for (int column = 0; column < columnCount; column++) {
358 QStandardItem *item = new QStandardItem(row * column);
359 stdModel->setItem(row, column, item);
360 }
361 }
362
363 // data has been added to the model, but mapper is not set the number of slices should still be 0
364 QVERIFY2(series->barsetCount() == 0, "Mapper has not been set, so the number of slices should be 0");
365
366 // set the mapper
367 QBarModelMapper *mapper = new QBarModelMapper;
368 mapper->setMapCategories(0);
369 mapper->setMapBarBottom(1);
370 mapper->setMapBarTop(3);
371 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
372 QCOMPARE(series->barsetCount(), 3);
373
374 // set the mappings to be outside of the model
375 mapper->setMapBarBottom(6);
376 mapper->setMapBarTop(7);
377 QCOMPARE(series->barsetCount(), 0); // Mappings are invalid, so the number of slices should be 0
378
379 // set back to correct ones
380 mapper->setMapBarBottom(1);
381 mapper->setMapBarTop(3);
382 QCOMPARE(series->barsetCount(), 3);
383
384 // reset the mappings
385 mapper->reset();
386 QCOMPARE(series->barsetCount(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0
387
388 // unset the model and the mapper
389 series->setModel(0);
390 series->setModelMapper(0);
391 QVERIFY(series->model() == 0); // Model should be unset
392 QVERIFY(series->modelMapper() == 0); // Model mapper should be unset
393 }
338
394
339 /*
395 /*
340 bool setModel(QAbstractItemModel *model);
396 bool setModel(QAbstractItemModel *model);
@@ -425,9 +425,9 void tst_qpieseries::modelUpdate()
425 {
425 {
426 int rowCount = 12;
426 int rowCount = 12;
427 int columnCount = 7;
427 int columnCount = 7;
428 QStandardItemModel *stdModel = new QStandardItemModel(0, 3);
428 QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount);
429 for (int row = 0; row < rowCount; ++row) {
429 for (int row = 0; row < rowCount; ++row) {
430 for (int column = 0; column < 2; column++) {
430 for (int column = 0; column < columnCount; column++) {
431 QStandardItem *item = new QStandardItem(row * column);
431 QStandardItem *item = new QStandardItem(row * column);
432 stdModel->setItem(row, column, item);
432 stdModel->setItem(row, column, item);
433 }
433 }
General Comments 0
You need to be logged in to leave comments. Login now