From c31e07d12ed24842f66979c3082e5c7c24fe0f8b 2012-05-24 08:05:58 From: Marek Rosa Date: 2012-05-24 08:05:58 Subject: [PATCH] Refactored PieSeriesModelMapper --- diff --git a/src/piechart/qpiemodelmapper.cpp b/src/piechart/qpiemodelmapper.cpp index cc1b50a..272d0e4 100644 --- a/src/piechart/qpiemodelmapper.cpp +++ b/src/piechart/qpiemodelmapper.cpp @@ -57,8 +57,8 @@ void QPieModelMapper::setSeries(QPieSeries *series) d->m_series = series; d->initializePieFromModel(); // connect the signals from the series - connect(d->m_series, SIGNAL(added(QList)), d, SLOT(slicesAdded())); - connect(d->m_series, SIGNAL(removed(QList)), d, SLOT(slicesRemoved())); + connect(d->m_series, SIGNAL(added(QList)), d, SLOT(slicesAdded(QList))); + connect(d->m_series, SIGNAL(removed(QList)), d, SLOT(slicesRemoved(QList))); // connect(d->m_model, SIGNAL(), d, SLOT()); } @@ -155,6 +155,18 @@ QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) : m_orientation = Qt::Vertical; m_valuesIndex = -1; m_labelsIndex = -1; + m_seriesSignalsBlock = false; + m_modelSignalsBlock = false; +} + +void QPieModelMapperPrivate::blockModelSignals(bool block) +{ + m_modelSignalsBlock = block; +} + +void QPieModelMapperPrivate::blockSeriesSignals(bool block) +{ + m_seriesSignalsBlock = block; } @@ -192,23 +204,63 @@ QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos) return m_model->index(m_labelsIndex, slicePos + m_first); } -void QPieModelMapperPrivate::slicesAdded() +void QPieModelMapperPrivate::slicesAdded(QList slices) { - // + if (m_seriesSignalsBlock) + return; + + if (slices.count() == 0) + return; + + int firstIndex = m_series->slices().indexOf(slices.at(0)); + if (firstIndex == -1) + return; + + blockModelSignals(); + if (m_orientation == Qt::Vertical) + m_model->insertRows(firstIndex + m_first, slices.count()); + else + m_model->insertColumns(firstIndex + m_first, slices.count()); + + for(int i = firstIndex; i < firstIndex + slices.count(); i++) { + m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value()); + m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label()); + } + blockModelSignals(false); } -void QPieModelMapperPrivate::slicesRemoved() +void QPieModelMapperPrivate::slicesRemoved(QList slices) { - // + if (m_seriesSignalsBlock) + return; + + if (slices.count() == 0) + return; + + int firstIndex = m_series->slices().indexOf(slices.at(0)); + if (firstIndex == -1) + return; + + blockModelSignals(); + if (m_orientation == Qt::Vertical) + m_model->removeRows(firstIndex + m_first, slices.count()); + else + m_model->removeColumns(firstIndex + m_first, slices.count()); + blockModelSignals(false); } void QPieModelMapperPrivate::sliceChanged() { - // + blockModelSignals(); + blockModelSignals(false); } void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) { + if (m_modelSignalsBlock) + return; + + blockSeriesSignals(); QModelIndex index; QPieSlice *slice; for (int row = topLeft.row(); row <= bottomRight.row(); row++) { @@ -219,64 +271,66 @@ void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex botto slice->setValue(m_model->data(index, Qt::DisplayRole).toReal()); slice->setLabel(m_model->data(index, Qt::DisplayRole).toString()); } - - // if (m_orientation == Qt::Vertical) - // { - // if ( topLeft.row() >= m_first && (m_count == - 1 || topLeft.row() < m_first + m_count)) { - // if (topLeft.column() == m_valuesIndex) - // m_series->slices().at(topLeft.row() - m_first)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); - // if (topLeft.column() == m_labelsIndex) - // m_series->slices().at(topLeft.row() - m_first)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); - // } - // } - // else - // { - // if (topLeft.column() >= m_first && (m_count == - 1 || topLeft.column() < m_first + m_count)) { - // if (topLeft.row() == m_valuesIndex) - // m_series->slices().at(topLeft.column() - m_first)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); - // if (topLeft.row() == m_labelsIndex) - // m_series->slices().at(topLeft.column() - m_first)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); - // } - // } } } + blockSeriesSignals(false); } void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end) { Q_UNUSED(parent); + if (m_modelSignalsBlock) + return; + + blockSeriesSignals(); if (m_orientation == Qt::Vertical) insertData(start, end); else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie initializePieFromModel(); + blockSeriesSignals(false); } void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) { Q_UNUSED(parent); + if (m_modelSignalsBlock) + return; + + blockSeriesSignals(); if (m_orientation == Qt::Vertical) removeData(start, end); else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie initializePieFromModel(); + blockSeriesSignals(false); } void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) { Q_UNUSED(parent); + if (m_modelSignalsBlock) + return; + + blockSeriesSignals(); if (m_orientation == Qt::Horizontal) insertData(start, end); else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie initializePieFromModel(); + blockSeriesSignals(false); } void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) { Q_UNUSED(parent); + if (m_modelSignalsBlock) + return; + + blockSeriesSignals(); if (m_orientation == Qt::Horizontal) removeData(start, end); else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie initializePieFromModel(); + blockSeriesSignals(false); } void QPieModelMapperPrivate::insertData(int start, int end) @@ -293,16 +347,11 @@ void QPieModelMapperPrivate::insertData(int start, int end) QPieSlice *slice = new QPieSlice; slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble()); slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString()); - // if (m_orientation == Qt::Vertical) { - // slice->setValue(m_model->data(m_model->index(i, m_valuesIndex), Qt::DisplayRole).toDouble()); - // slice->setLabel(m_model->data(m_model->index(i, m_labelsIndex), Qt::DisplayRole).toString()); - // } else { - // slice->setValue(m_model->data(m_model->index(m_valuesIndex, i), Qt::DisplayRole).toDouble()); - // slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i), Qt::DisplayRole).toString()); - // } slice->setLabelVisible(); m_series->insert(i - m_first, slice); } + + // remove excess of slices (abouve m_count) if (m_count != -1 && m_series->slices().size() > m_count) for (int i = m_series->slices().size() - 1; i >= m_count; i--) m_series->remove(m_series->slices().at(i)); @@ -358,6 +407,7 @@ void QPieModelMapperPrivate::initializePieFromModel() if (m_valuesIndex == -1 || m_labelsIndex == -1) return; + blockSeriesSignals(); // create the initial slices set int slicePos = 0; QModelIndex valueIndex = valueModelIndex(slicePos); @@ -368,53 +418,8 @@ void QPieModelMapperPrivate::initializePieFromModel() valueIndex = valueModelIndex(slicePos); labelIndex = labelModelIndex(slicePos); } - -// if (m_orientation == Qt::Vertical) { -// if (m_valuesIndex >= m_model->columnCount() || m_labelsIndex >= m_model->columnCount()) -// return; // mapped columns are not existing - -// int sliceCount = 0; -// if(m_count == -1) -// sliceCount = m_model->rowCount() - m_first; -// else -// sliceCount = qMin(m_count, m_model->rowCount() - m_first); -// } else { -// if (m_valuesIndex >= m_model->rowCount() || m_labelsIndex >= m_model->rowCount()) -// return; // mapped columns are not existing - -// int sliceCount = 0; -// if(m_count == -1) -// sliceCount = m_model->columnCount() - m_first; -// else -// sliceCount = qMin(m_count, m_model->columnCount() - m_first); -// } -// for (int i = m_first; i < m_first + sliceCount; i++) -// m_series->append(m_model->data(labelModelIndex(i), Qt::DisplayRole).toString(), m_model->data(valueModelIndex(i), Qt::DisplayRole).toDouble()); m_series->setLabelsVisible(true); - // // create the initial slices set - // if (m_orientation == Qt::Vertical) { - // if (m_valuesIndex >= m_model->columnCount() || m_labelsIndex >= m_model->columnCount()) - // return; // mapped columns are not existing - - // int sliceCount = 0; - // if(m_count == -1) - // sliceCount = m_model->rowCount() - m_first; - // else - // sliceCount = qMin(m_count, m_model->rowCount() - m_first); - // for (int i = m_first; i < m_first + sliceCount; i++) - // m_series->append(m_model->data(m_model->index(i, m_labelsIndex), Qt::DisplayRole).toString(), m_model->data(m_model->index(i, m_valuesIndex), Qt::DisplayRole).toDouble()); - // } else { - // if (m_valuesIndex >= m_model->rowCount() || m_labelsIndex >= m_model->rowCount()) - // return; // mapped columns are not existing - - // int sliceCount = 0; - // if(m_count == -1) - // sliceCount = m_model->columnCount() - m_first; - // else - // sliceCount = qMin(m_count, m_model->columnCount() - m_first); - // for (int i = m_first; i < m_first + sliceCount; i++) - // m_series->append(m_model->data(m_model->index(m_labelsIndex, i), Qt::DisplayRole).toString(), m_model->data(m_model->index(m_valuesIndex, i), Qt::DisplayRole).toDouble()); - // m_series->setLabelsVisible(true); + blockSeriesSignals(false); } #include "moc_qpiemodelmapper_p.cpp" diff --git a/src/piechart/qpiemodelmapper_p.h b/src/piechart/qpiemodelmapper_p.h index 15615e5..6d4b23f 100644 --- a/src/piechart/qpiemodelmapper_p.h +++ b/src/piechart/qpiemodelmapper_p.h @@ -29,8 +29,8 @@ public Q_SLOTS: void modelColumnsRemoved(QModelIndex parent, int start, int end); // for the series - void slicesAdded(); - void slicesRemoved(); + void slicesAdded(QList slices); + void slicesRemoved(QList slices); void sliceChanged(); void initializePieFromModel(); @@ -42,7 +42,12 @@ private: void insertData(int start, int end); void removeData(int start, int end); + void blockModelSignals(bool block = true); + void blockSeriesSignals(bool block = true); + private: + bool m_seriesSignalsBlock; + bool m_modelSignalsBlock; QPieSeries *m_series; QAbstractItemModel *m_model; int m_first; diff --git a/tests/tablemodelchart/tablewidget.cpp b/tests/tablemodelchart/tablewidget.cpp index 972db8b..b7d0115 100644 --- a/tests/tablemodelchart/tablewidget.cpp +++ b/tests/tablemodelchart/tablewidget.cpp @@ -81,9 +81,12 @@ TableWidget::TableWidget(QWidget *parent) QPushButton* removeColumnButton = new QPushButton("Remove column"); connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn())); - QPushButton* specialPieButton = new QPushButton("Test pie"); + QPushButton* specialPieButton = new QPushButton("Add slices from series"); connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie())); + QPushButton* specialPieButton2 = new QPushButton("Remove slices from series"); + connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2())); + // QLabel *spinBoxLabel = new QLabel("Rows affected:"); @@ -102,6 +105,7 @@ TableWidget::TableWidget(QWidget *parent) // buttonsLayout->addWidget(addColumnRightButton); // buttonsLayout->addWidget(removeColumnButton); buttonsLayout->addWidget(specialPieButton); + buttonsLayout->addWidget(specialPieButton2); buttonsLayout->addStretch(); // chart type radio buttons @@ -339,25 +343,25 @@ void TableWidget::updateChartType(bool toggle) m_chart->setAnimationOptions(QChart::SeriesAnimations); // pie 1 - QPieSeries* pieSeries = new QPieSeries; + m_pieSeries = new QPieSeries; m_pieMapper = new QPieModelMapper; m_pieMapper->setValuesIndex(1); m_pieMapper->setLabelsIndex(1); - m_pieMapper->setSeries(pieSeries); + m_pieMapper->setSeries(m_pieSeries); m_pieMapper->setModel(m_model); m_pieMapper->setFirst(2); - m_pieMapper->setCount(5); + // m_pieMapper->setCount(5); // pieSeries->setModelMapper(mapper); - pieSeries->setLabelsVisible(true); - pieSeries->setPieSize(0.75); + m_pieSeries->setLabelsVisible(true); + m_pieSeries->setPieSize(0.75); // pieSeries->setHorizontalPosition(0.2); // pieSeries->setVerticalPosition(0.3); - m_chart->addSeries(pieSeries); - seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); - m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5)); + m_chart->addSeries(m_pieSeries); + seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 50)); // pieSeries->slices().at(0)->setValue(400); @@ -458,7 +462,14 @@ void TableWidget::updateChartType(bool toggle) void TableWidget::testPie() { - m_pieMapper->setCount(-1); + // m_pieMapper->setCount(-1); + QPieSlice *slice = new QPieSlice("Hehe", 145); + slice->setLabelVisible(); + m_pieSeries->append(slice); + + slice = new QPieSlice("Hoho", 34); + slice->setLabelVisible(); + m_pieSeries->append(slice); // m_series->modelMapper()->setMapX(4); // m_tableView->setColumnWidth(10, 250); // if (specialPie) { @@ -468,6 +479,20 @@ void TableWidget::testPie() // } } +void TableWidget::testPie2() +{ + QPieSlice *slice; + if (m_pieSeries->count() > 0) { + slice = m_pieSeries->slices().last(); + m_pieSeries->remove(slice); + } + + if (m_pieSeries->count() > 0) { + slice = m_pieSeries->slices().first(); + m_pieSeries->remove(slice); + } +} + TableWidget::~TableWidget() { diff --git a/tests/tablemodelchart/tablewidget.h b/tests/tablemodelchart/tablewidget.h index 2dcdb6b..99c29b9 100644 --- a/tests/tablemodelchart/tablewidget.h +++ b/tests/tablemodelchart/tablewidget.h @@ -25,7 +25,7 @@ //#include #include "qchartview.h" //#include "qxyseries.h" -//#include +#include #include class CustomTableModel; @@ -52,6 +52,7 @@ public: void removeColumn(); void updateChartType(bool toggle); void testPie(); + void testPie2(); private: QChartView* m_chartView; @@ -67,6 +68,7 @@ public: QRadioButton* m_barRadioButton; QSpinBox* m_linesCountSpinBox; QPieModelMapper *m_pieMapper; + QPieSeries* m_pieSeries; // QPieSeries* specialPie; };