From a37d91e875427c28aab398d689c15f4263b443c1 2012-05-24 12:55:41 From: Marek Rosa Date: 2012-05-24 12:55:41 Subject: [PATCH] QPieModelMapper: added support for labelChanged and valueChanged signals from the slice --- diff --git a/src/piechart/qpiemodelmapper.cpp b/src/piechart/qpiemodelmapper.cpp index 272d0e4..775d402 100644 --- a/src/piechart/qpiemodelmapper.cpp +++ b/src/piechart/qpiemodelmapper.cpp @@ -216,6 +216,15 @@ void QPieModelMapperPrivate::slicesAdded(QList slices) if (firstIndex == -1) return; + if (m_count != -1) + m_count += slices.count(); + + for (int i = firstIndex; i < firstIndex + slices.count(); i++) { + m_slices.insert(i, slices.at(i - firstIndex)); + connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged())); + connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged())); + } + blockModelSignals(); if (m_orientation == Qt::Vertical) m_model->insertRows(firstIndex + m_first, slices.count()); @@ -237,10 +246,16 @@ void QPieModelMapperPrivate::slicesRemoved(QList slices) if (slices.count() == 0) return; - int firstIndex = m_series->slices().indexOf(slices.at(0)); + int firstIndex = m_slices.indexOf(slices.at(0)); if (firstIndex == -1) return; + if (m_count != -1) + m_count -= slices.count(); + + for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--) + m_slices.removeAt(i); + blockModelSignals(); if (m_orientation == Qt::Vertical) m_model->removeRows(firstIndex + m_first, slices.count()); @@ -249,9 +264,25 @@ void QPieModelMapperPrivate::slicesRemoved(QList slices) blockModelSignals(false); } -void QPieModelMapperPrivate::sliceChanged() +void QPieModelMapperPrivate::sliceLabelChanged() { + if (m_seriesSignalsBlock) + return; + blockModelSignals(); + QPieSlice *slice = qobject_cast(QObject::sender()); + m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label()); + blockModelSignals(false); +} + +void QPieModelMapperPrivate::sliceValueChanged() +{ + if (m_seriesSignalsBlock) + return; + + blockModelSignals(); + QPieSlice *slice = qobject_cast(QObject::sender()); + m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value()); blockModelSignals(false); } @@ -348,13 +379,18 @@ void QPieModelMapperPrivate::insertData(int start, int end) slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble()); slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString()); slice->setLabelVisible(); + connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged())); + connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged())); m_series->insert(i - m_first, slice); + m_slices.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--) + for (int i = m_series->slices().size() - 1; i >= m_count; i--) { m_series->remove(m_series->slices().at(i)); + m_slices.removeAt(i); + } } } @@ -367,8 +403,10 @@ void QPieModelMapperPrivate::removeData(int start, int end) int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed int first = qMax(start, m_first); // get the index of the first item that will be removed. int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed. - for (int i = last; i >= first; i--) + for (int i = last; i >= first; i--) { m_series->remove(m_series->slices().at(i - m_first)); + m_slices.removeAt(i - m_first); + } if (m_count != -1) { int itemsAvailable; // check how many are available to be added @@ -390,6 +428,7 @@ void QPieModelMapperPrivate::removeData(int start, int end) } slice->setLabelVisible(); m_series->insert(i, slice); + m_slices.insert(i, slice); } } } @@ -400,20 +439,28 @@ void QPieModelMapperPrivate::initializePieFromModel() if (m_model == 0 || m_series == 0) return; - // clear current content - m_series->clear(); - // check if mappings are set if (m_valuesIndex == -1 || m_labelsIndex == -1) return; blockSeriesSignals(); + // clear current content + m_series->clear(); + m_slices.clear(); + // create the initial slices set int slicePos = 0; QModelIndex valueIndex = valueModelIndex(slicePos); QModelIndex labelIndex = labelModelIndex(slicePos); while (valueIndex.isValid() && labelIndex.isValid()) { - m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble()); + QPieSlice *slice = new QPieSlice; + slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString()); + slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble()); + connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged())); + connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged())); + m_series->append(slice); + m_slices.append(slice); +// m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble()); slicePos++; valueIndex = valueModelIndex(slicePos); labelIndex = labelModelIndex(slicePos); diff --git a/src/piechart/qpiemodelmapper.h b/src/piechart/qpiemodelmapper.h index 76fccc5..38ec0a2 100644 --- a/src/piechart/qpiemodelmapper.h +++ b/src/piechart/qpiemodelmapper.h @@ -47,7 +47,7 @@ public: void reset(); -private: +protected: QPieModelMapperPrivate * const d_ptr; Q_DECLARE_PRIVATE(QPieModelMapper) }; diff --git a/src/piechart/qpiemodelmapper_p.h b/src/piechart/qpiemodelmapper_p.h index 6d4b23f..c059df5 100644 --- a/src/piechart/qpiemodelmapper_p.h +++ b/src/piechart/qpiemodelmapper_p.h @@ -31,7 +31,8 @@ public Q_SLOTS: // for the series void slicesAdded(QList slices); void slicesRemoved(QList slices); - void sliceChanged(); + void sliceLabelChanged(); + void sliceValueChanged(); void initializePieFromModel(); @@ -49,6 +50,7 @@ private: bool m_seriesSignalsBlock; bool m_modelSignalsBlock; QPieSeries *m_series; + QList m_slices; QAbstractItemModel *m_model; int m_first; int m_count; diff --git a/tests/tablemodelchart/tablewidget.cpp b/tests/tablemodelchart/tablewidget.cpp index b7d0115..e2319ea 100644 --- a/tests/tablemodelchart/tablewidget.cpp +++ b/tests/tablemodelchart/tablewidget.cpp @@ -87,6 +87,9 @@ TableWidget::TableWidget(QWidget *parent) QPushButton* specialPieButton2 = new QPushButton("Remove slices from series"); connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2())); + QPushButton* specialPieButton3 = new QPushButton("Remove slices from series"); + connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3())); + // QLabel *spinBoxLabel = new QLabel("Rows affected:"); @@ -106,6 +109,7 @@ TableWidget::TableWidget(QWidget *parent) // buttonsLayout->addWidget(removeColumnButton); buttonsLayout->addWidget(specialPieButton); buttonsLayout->addWidget(specialPieButton2); + buttonsLayout->addWidget(specialPieButton3); buttonsLayout->addStretch(); // chart type radio buttons @@ -347,11 +351,11 @@ void TableWidget::updateChartType(bool toggle) m_pieMapper = new QPieModelMapper; m_pieMapper->setValuesIndex(1); - m_pieMapper->setLabelsIndex(1); + m_pieMapper->setLabelsIndex(7); 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); m_pieSeries->setLabelsVisible(true); @@ -361,7 +365,7 @@ void TableWidget::updateChartType(bool toggle) 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)); + m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5)); // pieSeries->slices().at(0)->setValue(400); @@ -493,6 +497,22 @@ void TableWidget::testPie2() } } +void TableWidget::testPie3() +{ + QPieSlice *slice; + if (m_pieSeries->count() > 0) { + slice = m_pieSeries->slices().last(); + slice->setLabel("Dalej"); + slice->setValue(222); + } + + if (m_pieSeries->count() > 0) { + slice = m_pieSeries->slices().first(); + slice->setLabel("Prawie"); + slice->setValue(111); + } +} + TableWidget::~TableWidget() { diff --git a/tests/tablemodelchart/tablewidget.h b/tests/tablemodelchart/tablewidget.h index 99c29b9..537fa9b 100644 --- a/tests/tablemodelchart/tablewidget.h +++ b/tests/tablemodelchart/tablewidget.h @@ -53,6 +53,7 @@ public: void updateChartType(bool toggle); void testPie(); void testPie2(); + void testPie3(); private: QChartView* m_chartView;