From d938c059b23b7c359bbe90bdbcb2285704d65d90 2012-05-24 07:49:07 From: Marek Rosa Date: 2012-05-24 07:49:07 Subject: [PATCH] ModelMapper proposal --- diff --git a/src/piechart/piechart.pri b/src/piechart/piechart.pri index db76bdc..0c39862 100644 --- a/src/piechart/piechart.pri +++ b/src/piechart/piechart.pri @@ -12,7 +12,8 @@ PRIVATE_HEADERS += \ $$PWD/pieslicedata_p.h \ $$PWD/piechartitem_p.h \ $$PWD/piesliceitem_p.h \ - $$PWD/qpieseries_p.h + $$PWD/qpieseries_p.h \ + $$PWD/qpiemodelmapper_p.h PUBLIC_HEADERS += \ $$PWD/qpieseries.h \ diff --git a/src/piechart/qpiemodelmapper.cpp b/src/piechart/qpiemodelmapper.cpp index 54306d9..f65ff19 100644 --- a/src/piechart/qpiemodelmapper.cpp +++ b/src/piechart/qpiemodelmapper.cpp @@ -1,82 +1,319 @@ +#include "qpiemodelmapper_p.h" #include "qpiemodelmapper.h" +#include "qpieseries.h" +#include "qpieslice.h" +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE QPieModelMapper::QPieModelMapper(QObject *parent) : QObject(parent), - m_first(0), - m_count(-1), - m_orientation(Qt::Vertical), - m_mapValues(-1), - m_mapLabels(-1) + d_ptr(new QPieModelMapperPrivate(this)) { } int QPieModelMapper::first() const { - return m_first; + Q_D(const QPieModelMapper); + return d->m_first; } void QPieModelMapper::setFirst(int first) { - m_first = qMax(first, 0); - emit updated(); + Q_D(QPieModelMapper); + d->m_first = qMax(first, 0); + // emit updated(); } int QPieModelMapper::count() const { - return m_count; + Q_D(const QPieModelMapper); + return d->m_count; } void QPieModelMapper::setCount(int count) { - m_count = qMax(count, -1); - emit updated(); + Q_D(QPieModelMapper); + d->m_count = qMax(count, -1); + // emit updated(); } Qt::Orientation QPieModelMapper::orientation() const { - return m_orientation; + Q_D(const QPieModelMapper); + return d->m_orientation; } void QPieModelMapper::setOrientation(Qt::Orientation orientation) { - m_orientation = orientation; - emit updated(); + Q_D(QPieModelMapper); + d->m_orientation = orientation; + // emit updated(); } int QPieModelMapper::mapValues() const { - return m_mapValues; + Q_D(const QPieModelMapper); + return d->m_mapValues; } void QPieModelMapper::setMapValues(int mapValues) { - m_mapValues = mapValues; - emit updated(); + Q_D(QPieModelMapper); + d->m_mapValues = mapValues; + // emit updated(); } int QPieModelMapper::mapLabels() const { - return m_mapLabels; + Q_D(const QPieModelMapper); + return d->m_mapLabels; } void QPieModelMapper::setMapLabels(int mapLabels) { - m_mapLabels = mapLabels; - emit updated(); + Q_D(QPieModelMapper); + d->m_mapLabels = mapLabels; + // emit updated(); } void QPieModelMapper::reset() { + Q_D(QPieModelMapper); + d->m_first = 0; + d->m_count = -1; + d->m_orientation = Qt::Vertical; + d->m_mapValues = -1; + d->m_mapLabels = -1; + // emit updated(); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) : + q_ptr(q) +{ m_first = 0; m_count = -1; m_orientation = Qt::Vertical; m_mapValues = -1; m_mapLabels = -1; - emit updated(); } +int QPieModelMapperPrivate::getSliceIndex() +{ + + // invalid + return -1; +} + +void QPieModelMapperPrivate::setModel(QAbstractItemModel *model) +{ + if (model == 0) + return; + + if(m_model) + { + // disconnect signals from old model + disconnect(m_model, 0, this, 0); + } + + m_model = model; + // connect signals from the model + connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex))); + connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelRowsAdded(QModelIndex,int,int))); + connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelRowsRemoved(QModelIndex,int,int))); + connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelColumnsAdded(QModelIndex,int,int))); + connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelColumnsRemoved(QModelIndex,int,int))); +} + +void QPieModelMapperPrivate::slicesAdded() +{ + // +} + +void QPieModelMapperPrivate::slicesRemoved() +{ + // +} + +void QPieModelMapperPrivate::sliceChanged() +{ + // +} + +void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) +{ + for (int row = topLeft.row(); row <= bottomRight.row(); row++) { + for (int column = topLeft.column(); column <= bottomRight.column(); column++) { + if (m_orientation == Qt::Vertical) + { + if ( topLeft.row() >= m_first && (m_count == - 1 || topLeft.row() < m_first + m_count)) { + if (topLeft.column() == m_mapValues) + m_series->slices().at(topLeft.row() - m_first)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); + if (topLeft.column() == m_mapLabels) + 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_mapValues) + m_series->slices().at(topLeft.column() - m_first)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); + if (topLeft.row() == m_mapLabels) + m_series->slices().at(topLeft.column() - m_first)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); + } + } + } + } +} + + +//void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end) +//{ +// Q_UNUSED(parent); +// if (m_mapper->orientation() == Qt::Vertical) +// insertData(start, end); +// else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie +// initializePieFromModel(); +//} + +//void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) +//{ +// Q_UNUSED(parent); +// if (m_mapper->orientation() == Qt::Vertical) +// removeData(start, end); +// else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie +// initializePieFromModel(); +//} + +//void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) +//{ +// Q_UNUSED(parent); +// if (m_mapper->orientation() == Qt::Horizontal) +// insertData(start, end); +// else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie +// initializePieFromModel(); +//} + +//void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) +//{ +// Q_UNUSED(parent); +// if (m_mapper->orientation() == Qt::Horizontal) +// removeData(start, end); +// else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie +// initializePieFromModel(); +//} + +//void QPieModelMapperPrivate::insertData(int start, int end) +//{ +// Q_Q(QPieSeries); +// if (m_count != -1 && start >= m_first + m_count) { +// return; +// } else { +// int addedCount = end - start + 1; +// if (m_count != -1 && addedCount > m_count) +// addedCount = m_count; +// int first = qMax(start, m_first); +// int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); +// for (int i = first; i <= last; i++) { +// QPieSlice *slice = new QPieSlice; +// if (m_mapper->orientation() == Qt::Vertical) { +// slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble()); +// slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString()); +// } else { +// slice->setValue(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble()); +// slice->setLabel(m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString()); +// } +// slice->setLabelVisible(); +// q->insert(i - m_first, slice); +// } +// if (m_count != -1 && m_series->slices().size() > m_count) +// for (int i = m_series->slices().size() - 1; i >= m_count; i--) +// q->remove(q->slices().at(i)); +// } +//} + +//void QPieModelMapperPrivate::removeData(int start, int end) +//{ +// Q_Q(QPieSeries); +// int removedCount = end - start + 1; +// if (m_count != -1 && start >= m_first + m_count) { +// return; +// } else { +// 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--) +// q->remove(q->slices().at(i - m_first)); + +// if (m_count != -1) { +// int itemsAvailable; // check how many are available to be added +// if (m_mapper->orientation() == Qt::Vertical) +// itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size(); +// else +// itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size(); +// int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled. +// int currentSize = m_series->slices().size(); +// if (toBeAdded > 0) +// for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) { +// QPieSlice *slice = new QPieSlice; +// if (m_mapper->orientation() == Qt::Vertical) { +// slice->setValue(m_model->data(m_model->index(i + m_first, m_mapValues), Qt::DisplayRole).toDouble()); +// slice->setLabel(m_model->data(m_model->index(i + m_first, m_mapLabels), Qt::DisplayRole).toString()); +// } else { +// slice->setValue(m_model->data(m_model->index(m_mapValues, i + m_first), Qt::DisplayRole).toDouble()); +// slice->setLabel(m_model->data(m_model->index(m_mapLabels, i + m_first), Qt::DisplayRole).toString()); +// } +// slice->setLabelVisible(); +// q->insert(i, slice); +// } +// } +// } +//} + +//void QPieModelMapperPrivate::initializePieFromModel() +//{ +// Q_Q(QPieSeries); + +// // clear current content +// q->clear(); + +// if (m_model == 0 || m_mapper == 0) +// return; + +// // check if mappings are set +// if (m_mapValues == -1 || m_mapLabels == -1) +// return; + +// // create the initial slices set +// if (m_mapper->orientation() == Qt::Vertical) { +// if (m_mapValues >= m_model->columnCount() || m_mapLabels >= 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++) +// q->append(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString()); +// } else { +// if (m_mapValues >= m_model->rowCount() || m_mapLabels >= 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++) +// q->append(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString()); +// } +// q->setLabelsVisible(true); +//} + +#include "moc_qpiemodelmapper_p.cpp" #include "moc_qpiemodelmapper.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/piechart/qpiemodelmapper.h b/src/piechart/qpiemodelmapper.h index e816c66..5daac4c 100644 --- a/src/piechart/qpiemodelmapper.h +++ b/src/piechart/qpiemodelmapper.h @@ -6,6 +6,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QPieModelMapperPrivate; + class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject { Q_OBJECT @@ -17,7 +19,10 @@ class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject Q_ENUMS(Qt::Orientation) public: - explicit QPieModelMapper(QObject *parent = 0); + QPieModelMapper(QObject *parent = 0); + +// QAbstractItemModel* model() const; +// void setModel(QAbstractItemModel *model); int first() const; void setFirst(int first); @@ -36,16 +41,10 @@ public: void reset(); -Q_SIGNALS: - void updated(); - private: - int m_first; - int m_count; - Qt::Orientation m_orientation; - int m_mapValues; - int m_mapLabels; - + QScopedPointer d_ptr; + Q_DECLARE_PRIVATE(QPieModelMapper) + friend class QPieSeriesPrivate; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/piechart/qpiemodelmapper_p.h b/src/piechart/qpiemodelmapper_p.h new file mode 100644 index 0000000..21b0219 --- /dev/null +++ b/src/piechart/qpiemodelmapper_p.h @@ -0,0 +1,58 @@ +#ifndef QPIEMODELMAPPER_P_H +#define QPIEMODELMAPPER_P_H + +#include "qpiemodelmapper.h" +#include + +class QModelIndex; +class QAbstractItemModel; + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QPieModelMapper; +class QPieSeries; + +class QPieModelMapperPrivate : public QObject +{ + Q_OBJECT + +public: + QPieModelMapperPrivate(QPieModelMapper *q); + +public Q_SLOTS: + // for the model + void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); +// void modelRowsAdded(QModelIndex parent, int start, int end); +// void modelRowsRemoved(QModelIndex parent, int start, int end); +// void modelColumnsAdded(QModelIndex parent, int start, int end); +// void modelColumnsRemoved(QModelIndex parent, int start, int end); + + // for the series + void slicesAdded(); + void slicesRemoved(); + void sliceChanged(); + +private: + void setModel(QAbstractItemModel *model); + +private: + int getSliceIndex(); + +private: + int m_first; + int m_count; + Qt::Orientation m_orientation; + int m_mapValues; + int m_mapLabels; + +private: + QPieSeries *m_series; + QAbstractItemModel *m_model; + QPieModelMapper *q_ptr; + Q_DECLARE_PUBLIC(QPieModelMapper) + friend class QPieSeriesPrivate; +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QPIEMODELMAPPER_P_H diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 0c4ff5a..f436a6c 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -28,6 +28,7 @@ #include "legendmarker_p.h" #include #include "qpiemodelmapper.h" +#include "qpiemodelmapper_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -469,30 +470,31 @@ qreal QPieSeries::sum() const void QPieSeries::setModel(QAbstractItemModel* model) { Q_D(QPieSeries); - // disconnect signals from old model - if(d->m_model) - { - disconnect(d->m_model, 0, this, 0); - } - - // set new model - if(model) - { - d->m_model = model; - // connect signals from the model - connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); - connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); - connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); - connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); - connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); - - if (d->m_mapper) - d->initializePieFromModel(); - } - else - { - d->m_model = 0; - } + d->setModel(model); + // // disconnect signals from old model + // if(d->m_model) + // { + // disconnect(d->m_model, 0, this, 0); + // } + + // // set new model + // if(model) + // { + // d->m_model = model; + // // connect signals from the model + //// connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); + //// connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); + //// connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); + //// connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); + //// connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); + + //// if (d->m_mapper) + //// d->initializePieFromModel(); + // } + // else + // { + // d->m_model = 0; + // } } void QPieSeries::setModelMapper(QPieModelMapper *mapper) @@ -506,10 +508,10 @@ void QPieSeries::setModelMapper(QPieModelMapper *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(initializePieFromModel())); - if (d->m_model) - d->initializePieFromModel(); + // if (d->m_model) + // d->initializePieFromModel(); } else { d->m_mapper = 0; } @@ -532,6 +534,7 @@ QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) : m_pieStartAngle(0), m_pieEndAngle(360), m_sum(0), + m_model(0), m_mapper(0) { @@ -608,191 +611,6 @@ void QPieSeriesPrivate::sliceHovered(bool state) emit q->hovered(slice, state); } -void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) -{ - if (m_mapper) { - for (int row = topLeft.row(); row <= bottomRight.row(); row++) { - for (int column = topLeft.column(); column <= bottomRight.column(); column++) { - if (m_mapper->orientation() == Qt::Vertical) - { - if ( topLeft.row() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.row() < m_mapper->first() + m_mapper->count())) { - if (topLeft.column() == m_mapper->mapValues()) - m_slices.at(topLeft.row() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); - if (topLeft.column() == m_mapper->mapLabels()) - m_slices.at(topLeft.row() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); - } - } - else - { - if (topLeft.column() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.column() < m_mapper->first() + m_mapper->count())) { - if (topLeft.row() == m_mapper->mapValues()) - m_slices.at(topLeft.column() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); - if (topLeft.row() == m_mapper->mapLabels()) - m_slices.at(topLeft.column() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); - } - } - } - } - } -} - - -void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end) -{ - Q_UNUSED(parent); - if (m_mapper) { - if (m_mapper->orientation() == Qt::Vertical) - insertData(start, end); - else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie - initializePieFromModel(); - } -} - -void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) -{ - Q_UNUSED(parent); - if (m_mapper) { - if (m_mapper->orientation() == Qt::Vertical) - removeData(start, end); - else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie - initializePieFromModel(); - } -} - -void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) -{ - Q_UNUSED(parent); - if (m_mapper) { - if (m_mapper->orientation() == Qt::Horizontal) - insertData(start, end); - else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie - initializePieFromModel(); - } -} - -void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) -{ - Q_UNUSED(parent); - if (m_mapper) { - if (m_mapper->orientation() == Qt::Horizontal) - removeData(start, end); - else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie - initializePieFromModel(); - } -} - -void QPieSeriesPrivate::insertData(int start, int end) -{ - Q_Q(QPieSeries); - if (m_mapper) { - if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) { - return; - } else { - int addedCount = end - start + 1; - if (m_mapper->count() != -1 && addedCount > m_mapper->count()) - addedCount = m_mapper->count(); - int first = qMax(start, m_mapper->first()); - int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); - for (int i = first; i <= last; i++) { - QPieSlice *slice = new QPieSlice; - if (m_mapper->orientation() == Qt::Vertical) { - slice->setValue(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble()); - slice->setLabel(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString()); - } else { - slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble()); - slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString()); - } - slice->setLabelVisible(); - q->insert(i - m_mapper->first(), slice); - } - if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count()) - for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--) - q->remove(q->slices().at(i)); - } - } -} - -void QPieSeriesPrivate::removeData(int start, int end) -{ - Q_Q(QPieSeries); - if (m_mapper) { - int removedCount = end - start + 1; - if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) { - return; - } else { - int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed - int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed. - int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed. - for (int i = last; i >= first; i--) - q->remove(q->slices().at(i - m_mapper->first())); - - if (m_mapper->count() != -1) { - int itemsAvailable; // check how many are available to be added - if (m_mapper->orientation() == Qt::Vertical) - itemsAvailable = m_model->rowCount() - m_mapper->first() - m_slices.size(); - else - itemsAvailable = m_model->columnCount() - m_mapper->first() - m_slices.size(); - int toBeAdded = qMin(itemsAvailable, m_mapper->count() - m_slices.size()); // add not more items than there is space left to be filled. - int currentSize = m_slices.size(); - if (toBeAdded > 0) - for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) { - QPieSlice *slice = new QPieSlice; - if (m_mapper->orientation() == Qt::Vertical) { - slice->setValue(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapValues()), Qt::DisplayRole).toDouble()); - slice->setLabel(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapLabels()), Qt::DisplayRole).toString()); - } else { - slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i + m_mapper->first()), Qt::DisplayRole).toDouble()); - slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString()); - } - slice->setLabelVisible(); - q->insert(i, slice); - } - } - } - } -} - -void QPieSeriesPrivate::initializePieFromModel() -{ - Q_Q(QPieSeries); - - // 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; - - // create the initial slices set - if (m_mapper->orientation() == Qt::Vertical) { - if (m_mapper->mapValues() >= m_model->columnCount() || m_mapper->mapLabels() >= m_model->columnCount()) - return; // mapped columns are not existing - - int sliceCount = 0; - if(m_mapper->count() == -1) - sliceCount = m_model->rowCount() - m_mapper->first(); - else - sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first()); - for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++) - q->append(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString(), m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble()); - } else { - if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount()) - return; // mapped columns are not existing - - int sliceCount = 0; - if(m_mapper->count() == -1) - sliceCount = m_model->columnCount() - m_mapper->first(); - else - sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first()); - for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++) - q->append(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString(), m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble()); - } - q->setLabelsVisible(true); -} - bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min) { // Remove rounding errors @@ -847,6 +665,21 @@ QList QPieSeriesPrivate::createLegendMarker(QLegend* legend) return markers; } +void QPieSeriesPrivate::setModel(QAbstractItemModel* model) +{ + Q_Q(QPieSeries); + QPieModelMapperPrivate *mapperPrivate = m_mapper->d_func(); + mapperPrivate->setModel(model); + if(mapperPrivate->m_series != q) + { + disconnect(mapperPrivate->m_series, 0, mapperPrivate, 0); + mapperPrivate->m_series = q; + connect(this, SIGNAL(added(QList)), mapperPrivate, SLOT(slicesAdded())); + connect(this, SIGNAL(removed(QList)), mapperPrivate, SLOT(slicesRemoved())); + connect(this, SIGNAL(modified()), mapperPrivate, SLOT(sliceChanged())); + } +} + #include "moc_qpieseries.cpp" #include "moc_qpieseries_p.cpp" diff --git a/src/piechart/qpieseries_p.h b/src/piechart/qpieseries_p.h index 157b2f8..259353c 100644 --- a/src/piechart/qpieseries_p.h +++ b/src/piechart/qpieseries_p.h @@ -54,20 +54,18 @@ public Q_SLOTS: void sliceChanged(); void sliceClicked(); void sliceHovered(bool state); - void initializePieFromModel(); - void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); - void modelRowsAdded(QModelIndex parent, int start, int end); - void modelRowsRemoved(QModelIndex parent, int start, int end); - void modelColumnsAdded(QModelIndex parent, int start, int end); - void modelColumnsRemoved(QModelIndex parent, int start, int end); +// void initializePieFromModel(); +// void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); +// void modelRowsAdded(QModelIndex parent, int start, int end); +// void modelRowsRemoved(QModelIndex parent, int start, int end); +// void modelColumnsAdded(QModelIndex parent, int start, int end); +// void modelColumnsRemoved(QModelIndex parent, int start, int end); bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0); private: - void doClear(); - void doRemove(QPieSlice* slice); - void doInsert(int index, QPieSlice* slice); - void insertData(int start, int end); - void removeData(int start, int end); + void setModel(QAbstractItemModel *model); +// void insertData(int start, int end); +// void removeData(int start, int end); public: QList m_slices;