From 3138389fa706ba95d1cdfbdad8674701ce85d3bb 2012-05-17 11:48:47 From: Marek Rosa Date: 2012-05-17 11:48:47 Subject: [PATCH] Fixes to barseries and pieseries model support. Started adding barseries model tests --- diff --git a/src/barchart/qbarmodelmapper.cpp b/src/barchart/qbarmodelmapper.cpp index 1341061..d861b9a 100644 --- a/src/barchart/qbarmodelmapper.cpp +++ b/src/barchart/qbarmodelmapper.cpp @@ -87,6 +87,7 @@ void QBarModelMapper::reset() m_mapBarBottom = -1; m_mapBarTop = -1; m_mapCategories = -1; + emit updated(); } #include "moc_qbarmodelmapper.cpp" diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index f94a1cb..ef42173 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -255,7 +255,7 @@ void QBarSeries::setModelMapper(QBarModelMapper *mapper) if (mapper) { d->m_mapper = mapper; // connect the signal from the mapper - connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel())); + connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializeDataFromModel())); if (d->m_model) d->initializeDataFromModel(); @@ -436,6 +436,9 @@ qreal QBarSeriesPrivate::maxCategorySum() void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) { + if (m_model == 0 || m_mapper == 0) + return; + for (int row = topLeft.row(); row <= bottomRight.row(); row++) { for (int column = topLeft.column(); column <= bottomRight.column(); column++) { if (m_mapper->orientation() == Qt::Vertical) @@ -480,14 +483,21 @@ void QBarSeriesPrivate::initializeDataFromModel() { Q_Q(QBarSeries); - if (m_model == 0 || m_mapper == 0) - return; - // create the initial bars m_categories.clear(); m_barSets.clear(); + + if (m_model == 0 || m_mapper == 0) + return; + + // check if mappings are set + if (m_mapper->mapBarBottom() == -1 || m_mapper->mapBarTop() == -1 || m_mapper->mapCategories() == -1) + return; + // emit restructuredBars(); if (m_mapper->orientation() == Qt::Vertical) { + if (m_mapCategories >= m_model->columnCount()) + return; int rowCount = 0; if(m_mapper->count() == -1) rowCount = m_model->rowCount() - m_mapper->first(); @@ -497,13 +507,17 @@ void QBarSeriesPrivate::initializeDataFromModel() m_categories << m_model->data(m_model->index(k, m_mapper->mapCategories()), Qt::DisplayRole).toString(); } - for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) { + int lastAvailableBarSet = qMin(m_model->columnCount() - 1, m_mapper->mapBarTop()); + for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) { +// for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) { QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); for(int m = m_mapper->first(); m < m_mapper->first() + rowCount; m++) *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble(); q->appendBarSet(barSet); } } else { + if (m_mapCategories >= m_model->rowCount()) + return; int columnCount = 0; if(m_mapper->count() == -1) columnCount = m_model->columnCount() - m_mapper->first(); @@ -513,7 +527,9 @@ void QBarSeriesPrivate::initializeDataFromModel() m_categories << m_model->data(m_model->index(m_mapper->mapCategories(), k), Qt::DisplayRole).toString(); } - for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) { + int lastAvailableBarSet = qMin(m_model->rowCount() - 1, m_mapper->mapBarTop()); + for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) { +// for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) { QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString()); for(int m = m_mapper->first(); m < m_mapper->first() + columnCount; m++) *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble(); diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index ebda719..076a2ae 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -716,12 +716,12 @@ void QPieSeriesPrivate::initializePieFromModel() { Q_Q(QPieSeries); - if (m_model == 0 || m_mapper == 0) - return; - // clear current content q->clear(); + if (m_model == 0 || m_mapper == 0) + return; + // check if mappings are set if (m_mapper->mapValues() == -1 || m_mapper->mapLabels() == -1) return; diff --git a/test/auto/qgroupedbarseries/tst_qgroupedbarseries.cpp b/test/auto/qgroupedbarseries/tst_qgroupedbarseries.cpp index f84e85e..9757f9b 100644 --- a/test/auto/qgroupedbarseries/tst_qgroupedbarseries.cpp +++ b/test/auto/qgroupedbarseries/tst_qgroupedbarseries.cpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include QTCOMMERCIALCHART_USE_NAMESPACE @@ -47,6 +49,7 @@ private slots: void mouseclicked(); void mousehovered_data(); void mousehovered(); + void model(); private: QGroupedBarSeries* m_barseries; @@ -335,6 +338,59 @@ void tst_QGroupedBarSeries::mousehovered() QVERIFY(seriesSpyArg.at(1).toBool() == false); } +void tst_QGroupedBarSeries::model() +{ + QGroupedBarSeries *series = new QGroupedBarSeries; + QChart *chart = new QChart; + chart->addSeries(series); + QChartView *chartView = new QChartView(chart); + chartView->show(); + + int rowCount = 12; + int columnCount = 5; + QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount); + series->setModel(stdModel); + QVERIFY2((series->model()) == stdModel, "Model should be stdModel"); + + + for (int row = 0; row < rowCount; ++row) { + for (int column = 0; column < columnCount; column++) { + QStandardItem *item = new QStandardItem(row * column); + stdModel->setItem(row, column, item); + } + } + + // data has been added to the model, but mapper is not set the number of slices should still be 0 + QVERIFY2(series->barsetCount() == 0, "Mapper has not been set, so the number of slices should be 0"); + + // set the mapper + QBarModelMapper *mapper = new QBarModelMapper; + mapper->setMapCategories(0); + mapper->setMapBarBottom(1); + mapper->setMapBarTop(3); + 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 + QCOMPARE(series->barsetCount(), 3); + + // set the mappings to be outside of the model + mapper->setMapBarBottom(6); + mapper->setMapBarTop(7); + QCOMPARE(series->barsetCount(), 0); // Mappings are invalid, so the number of slices should be 0 + + // set back to correct ones + mapper->setMapBarBottom(1); + mapper->setMapBarTop(3); + QCOMPARE(series->barsetCount(), 3); + + // reset the mappings + mapper->reset(); + QCOMPARE(series->barsetCount(), 0); // Mappings have been reset and are invalid, so the number of slices should be 0 + + // unset the model and the mapper + series->setModel(0); + series->setModelMapper(0); + QVERIFY(series->model() == 0); // Model should be unset + QVERIFY(series->modelMapper() == 0); // Model mapper should be unset +} /* bool setModel(QAbstractItemModel *model); diff --git a/test/auto/qpieseries/tst_qpieseries.cpp b/test/auto/qpieseries/tst_qpieseries.cpp index 1aa5e0e..6a03511 100644 --- a/test/auto/qpieseries/tst_qpieseries.cpp +++ b/test/auto/qpieseries/tst_qpieseries.cpp @@ -425,9 +425,9 @@ void tst_qpieseries::modelUpdate() { int rowCount = 12; int columnCount = 7; - QStandardItemModel *stdModel = new QStandardItemModel(0, 3); + QStandardItemModel *stdModel = new QStandardItemModel(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { - for (int column = 0; column < 2; column++) { + for (int column = 0; column < columnCount; column++) { QStandardItem *item = new QStandardItem(row * column); stdModel->setItem(row, column, item); }