From b3d46b8e0cda8e33aa3ef94c00e200a0916562e6 2012-05-28 12:30:58 From: Marek Rosa Date: 2012-05-28 12:30:58 Subject: [PATCH] XYModelMapper slots for model signals implemented --- diff --git a/src/xychart/qxymodelmapper.cpp b/src/xychart/qxymodelmapper.cpp index d66793c..8fed754 100644 --- a/src/xychart/qxymodelmapper.cpp +++ b/src/xychart/qxymodelmapper.cpp @@ -1,81 +1,383 @@ #include "qxymodelmapper.h" +#include "qxymodelmapper_p.h" +#include "qxyseries.h" +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE QXYModelMapper::QXYModelMapper(QObject *parent): QObject(parent), - m_first(0), - m_count(-1), - m_orientation(Qt::Vertical), - m_xSection(-1), - m_ySection(-1) + d_ptr(new QXYModelMapperPrivate(this)) +{ +} + +QAbstractItemModel* QXYModelMapper::model() const +{ + Q_D(const QXYModelMapper); + return d->m_model; +} + +void QXYModelMapper::setModel(QAbstractItemModel *model) +{ + if (model == 0) + return; + + Q_D(QXYModelMapper); + if (d->m_model) { + disconnect(d->m_model, 0, d, 0); + } + + d->m_model = model; + d->initializeXYFromModel(); + // 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))); +} + +QXYSeries* QXYModelMapper::series() const { + Q_D(const QXYModelMapper); + return d->m_series; +} + +void QXYModelMapper::setSeries(QXYSeries *series) +{ + Q_D(QXYModelMapper); + if (d->m_series) { + disconnect(d->m_series, 0, d, 0); + } + + if (series == 0) + return; + + d->m_series = series; + d->initializeXYFromModel(); + // connect the signals from the series + connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int))); + connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int))); + connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(pointReplaced(int))); } int QXYModelMapper::first() const { - return m_first; + Q_D(const QXYModelMapper); + return d->m_first; } void QXYModelMapper::setFirst(int first) { - m_first = qMax(first, 0); -// emit updated(); + Q_D(QXYModelMapper); + d->m_first = qMax(first, 0); + d->initializeXYFromModel(); } int QXYModelMapper::count() const { - return m_count; + Q_D(const QXYModelMapper); + return d->m_count; } void QXYModelMapper::setCount(int count) { - m_count = qMax(count, -1); -// emit updated(); + Q_D(QXYModelMapper); + d->m_count = qMax(count, -1); + d->initializeXYFromModel(); } Qt::Orientation QXYModelMapper::orientation() const { - return m_orientation; + Q_D(const QXYModelMapper); + return d->m_orientation; } void QXYModelMapper::setOrientation(Qt::Orientation orientation) { - m_orientation = orientation; -// emit updated(); + Q_D(QXYModelMapper); + d->m_orientation = orientation; + d->initializeXYFromModel(); } int QXYModelMapper::xSection() const { - return m_xSection; + Q_D(const QXYModelMapper); + return d->m_xSection; } void QXYModelMapper::setXSection(int xSection) { - m_xSection = xSection; -// emit updated(); + Q_D(QXYModelMapper); + d->m_xSection = xSection; + d->initializeXYFromModel(); } int QXYModelMapper::ySection() const { - return m_ySection; + Q_D(const QXYModelMapper); + return d->m_ySection; } void QXYModelMapper::setYSection(int ySection) { - m_ySection = ySection; -// emit updated(); + Q_D(QXYModelMapper); + d->m_ySection = ySection; + d->initializeXYFromModel(); } void QXYModelMapper::reset() { - m_first = 0; - m_count = -1; - m_orientation = Qt::Vertical; - m_xSection = -1; - m_ySection = -1; + Q_D(QXYModelMapper); + d->m_first = 0; + d->m_count = -1; + d->m_orientation = Qt::Vertical; + d->m_xSection = -1; + d->m_ySection = -1; + d->initializeXYFromModel(); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +QXYModelMapperPrivate::QXYModelMapperPrivate(QXYModelMapper *q) : + m_series(0), + m_model(0), + m_first(0), + m_count(-1), + m_orientation(Qt::Vertical), + m_xSection(-1), + m_ySection(-1), + m_seriesSignalsBlock(false), + m_modelSignalsBlock(false), + q_ptr(q) +{ +} + +void QXYModelMapperPrivate::blockModelSignals(bool block) +{ + m_modelSignalsBlock = block; +} + +void QXYModelMapperPrivate::blockSeriesSignals(bool block) +{ + m_seriesSignalsBlock = block; +} + +QModelIndex QXYModelMapperPrivate::xModelIndex(int xPos) +{ + if (m_count != -1 && xPos >= m_count) + return QModelIndex(); // invalid + + if (m_orientation == Qt::Vertical) + return m_model->index(xPos + m_first, m_xSection); + else + return m_model->index(m_xSection, xPos + m_first); +} + +QModelIndex QXYModelMapperPrivate::yModelIndex(int yPos) +{ + if (m_count != -1 && yPos >= m_count) + return QModelIndex(); // invalid + + if (m_orientation == Qt::Vertical) + return m_model->index(yPos + m_first, m_ySection); + else + return m_model->index(m_ySection, yPos + m_first); +} + +void QXYModelMapperPrivate::handlePointAdded(int pointPos) +{ + Q_UNUSED(pointPos) +} + +void QXYModelMapperPrivate::handlePointRemoved(int pointPos) +{ + Q_UNUSED(pointPos) +} + +void QXYModelMapperPrivate::handlePointReplaced(int pointPos) +{ + Q_UNUSED(pointPos) +} + +void QXYModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) +{ + if (m_modelSignalsBlock) + return; + + blockSeriesSignals(); + QModelIndex index; + QPointF oldPoint; + QPointF newPoint; + for (int row = topLeft.row(); row <= bottomRight.row(); row++) { + for (int column = topLeft.column(); column <= bottomRight.column(); column++) { + index = topLeft.sibling(row, column); + if (m_orientation == Qt::Vertical && (index.column() == m_xSection|| index.column() == m_ySection)) { + if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) { + oldPoint = m_series->points().at(index.row() - m_first); + newPoint.setX(m_model->data(m_model->index(index.row(), m_xSection)).toReal()); + newPoint.setY(m_model->data(m_model->index(index.row(), m_ySection)).toReal()); + } + } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) { + if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) { + oldPoint = m_series->points().at(index.column() - m_first); + newPoint.setX(m_model->data(m_model->index(m_xSection, index.column())).toReal()); + newPoint.setY(m_model->data(m_model->index(m_ySection, index.column())).toReal()); + } + } else { + continue; + } + m_series->replace(oldPoint, newPoint); + } + blockSeriesSignals(false); + } +} + +void QXYModelMapperPrivate::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_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy + initializeXYFromModel(); + blockSeriesSignals(false); +} + +void QXYModelMapperPrivate::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_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy + initializeXYFromModel(); + blockSeriesSignals(false); +} + +void QXYModelMapperPrivate::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_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy + initializeXYFromModel(); + blockSeriesSignals(false); +} + +void QXYModelMapperPrivate::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_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy + initializeXYFromModel(); + blockSeriesSignals(false); +} + +void QXYModelMapperPrivate::insertData(int start, int end) +{ + if (m_model == 0 || m_series == 0) + return; + + 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_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); + for (int i = first; i <= last; i++) { + QPointF point; + point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble()); + point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble()); + m_series->insert(i - m_first, point); + } + + // remove excess of slices (abouve m_count) + if (m_count != -1 && m_series->points().size() > m_count) + for (int i = m_series->points().size() - 1; i >= m_count; i--) { + m_series->remove(m_series->points().at(i)); + } + } +} + +void QXYModelMapperPrivate::removeData(int start, int end) +{ + if (m_model == 0 || m_series == 0) + return; + + int removedCount = end - start + 1; + if (m_count != -1 && start >= m_first + m_count) { + return; + } else { + int toRemove = qMin(m_series->points().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->points().size() + m_first - 1); // get the index of the last item that will be removed. + for (int i = last; i >= first; i--) { + m_series->remove(m_series->points().at(i - m_first)); + } + + if (m_count != -1) { + int itemsAvailable; // check how many are available to be added + if (m_orientation == Qt::Vertical) + itemsAvailable = m_model->rowCount() - m_first - m_series->points().size(); + else + itemsAvailable = m_model->columnCount() - m_first - m_series->points().size(); + int toBeAdded = qMin(itemsAvailable, m_count - m_series->points().size()); // add not more items than there is space left to be filled. + int currentSize = m_series->points().size(); + if (toBeAdded > 0) + for (int i = m_series->points().size(); i < currentSize + toBeAdded; i++) { + QPointF point; + point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble()); + point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble()); + m_series->insert(i, point); + } + } + } +} + +void QXYModelMapperPrivate::initializeXYFromModel() +{ + if (m_model == 0 || m_series == 0) + return; + + blockSeriesSignals(); + // clear current content + m_series->clear(); + + // create the initial slices set + int pointPos = 0; + QModelIndex xIndex = xModelIndex(pointPos); + QModelIndex yIndex = yModelIndex(pointPos); + while (xIndex.isValid() && yIndex.isValid()) { + QPointF point; + point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble()); + point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble()); + m_series->append(point); + pointPos++; + xIndex = xModelIndex(pointPos); + yIndex = yModelIndex(pointPos); + } + blockSeriesSignals(false); } #include "moc_qxymodelmapper.cpp" +#include "moc_qxymodelmapper_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/xychart/qxymodelmapper.h b/src/xychart/qxymodelmapper.h index 3d58044..34b03be 100644 --- a/src/xychart/qxymodelmapper.h +++ b/src/xychart/qxymodelmapper.h @@ -4,25 +4,37 @@ #include "qchartglobal.h" #include +class QAbstractItemModel; + QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QXYModelMapperPrivate; +class QXYSeries; + class QTCOMMERCIALCHART_EXPORT QXYModelMapper : public QObject { Q_OBJECT - Q_PROPERTY(int xSection READ xSection WRITE setXSection) - Q_PROPERTY(int ySection READ ySection WRITE setYSection) + Q_PROPERTY(QXYSeries *series READ series WRITE setSeries) + Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel) Q_PROPERTY(int first READ first WRITE setFirst) Q_PROPERTY(int count READ count WRITE setCount) - Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation) Q_ENUMS(Qt::Orientation) public: + QAbstractItemModel* model() const; + void setModel(QAbstractItemModel *model); + + QXYSeries* series() const; + void setSeries(QXYSeries *series); + int first() const; void setFirst(int first); int count() const; void setCount(int count); + void reset(); + protected: explicit QXYModelMapper(QObject *parent = 0); @@ -35,17 +47,9 @@ protected: int ySection() const; void setYSection(int ySection); - void reset(); - -Q_SIGNALS: - void updated(); - -private: - int m_first; - int m_count; - Qt::Orientation m_orientation; - int m_xSection; - int m_ySection; +protected: + QXYModelMapperPrivate * const d_ptr; + Q_DECLARE_PRIVATE(QXYModelMapper) }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/xychart/qxymodelmapper_p.h b/src/xychart/qxymodelmapper_p.h new file mode 100644 index 0000000..dc9e490 --- /dev/null +++ b/src/xychart/qxymodelmapper_p.h @@ -0,0 +1,64 @@ +#ifndef QXYMODELMAPPER_P_H +#define QXYMODELMAPPER_P_H + +#include "qxymodelmapper.h" +#include + +class QModelIndex; +class QAbstractItemModel; +class QPointF; + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QXYModelMapper; +class QXYSeries; + +class QXYModelMapperPrivate : public QObject +{ + Q_OBJECT + +public: + QXYModelMapperPrivate(QXYModelMapper *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 handlePointAdded(int pointPos); + void handlePointRemoved(int pointPos); + void handlePointReplaced(int pointPos); + + void initializeXYFromModel(); + +private: + QModelIndex xModelIndex(int xPos); + QModelIndex yModelIndex(int yPos); + void insertData(int start, int end); + void removeData(int start, int end); + void blockModelSignals(bool block = true); + void blockSeriesSignals(bool block = true); + +private: + QXYSeries *m_series; + QAbstractItemModel *m_model; + int m_first; + int m_count; + Qt::Orientation m_orientation; + int m_xSection; + int m_ySection; + bool m_seriesSignalsBlock; + bool m_modelSignalsBlock; + +private: + QXYModelMapper *q_ptr; + Q_DECLARE_PUBLIC(QXYModelMapper) +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QXYMODELMAPPER_P_H diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index 731298b..d1b7fec 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -162,6 +162,20 @@ void QXYSeries::removeAll() } } +void QXYSeries::insert(int index, const QPointF &point) +{ + Q_D(QXYSeries); + d->m_points.insert(index, point); + emit d->pointAdded(index); +} + +void QXYSeries::clear() +{ + Q_D(QXYSeries); + for (int i = d->m_points.size() - 1; i >= 0; i--) + remove(d->m_points.at(i)); +} + /*! \internal \a pos */ diff --git a/src/xychart/qxyseries.h b/src/xychart/qxyseries.h index 025e94a..9cff389 100644 --- a/src/xychart/qxyseries.h +++ b/src/xychart/qxyseries.h @@ -50,6 +50,8 @@ public: void remove(qreal x, qreal y); void remove(const QPointF &point); void removeAll(); + void insert(int index, const QPointF &point); + void clear(); int count() const; QList points() const; diff --git a/src/xychart/qxyseries_p.h b/src/xychart/qxyseries_p.h index 1b0de95..f9da140 100644 --- a/src/xychart/qxyseries_p.h +++ b/src/xychart/qxyseries_p.h @@ -50,10 +50,7 @@ Q_SIGNALS: void updated(); void pointReplaced(int index); void pointRemoved(int index); - void pointsRemoved(int start, int end); void pointAdded(int index); - void pointsAdded(int start, int end); - void reinitialized(); protected: QVector m_points; diff --git a/src/xychart/xychart.pri b/src/xychart/xychart.pri index 16cf7d8..d43fc8d 100644 --- a/src/xychart/xychart.pri +++ b/src/xychart/xychart.pri @@ -10,7 +10,8 @@ SOURCES += \ PRIVATE_HEADERS += \ $$PWD/xychart_p.h \ - $$PWD/qxyseries_p.h + $$PWD/qxyseries_p.h \ + $$PWD/qxymodelmapper_p.h PUBLIC_HEADERS += \ @@ -18,4 +19,3 @@ PUBLIC_HEADERS += \ $$PWD/qxymodelmapper.h \ $$PWD/qvxymodelmapper.h \ $$PWD/qhxymodelmapper.h - diff --git a/tests/tablemodelchart/tablewidget.cpp b/tests/tablemodelchart/tablewidget.cpp index fc0cc23..31fe02c 100644 --- a/tests/tablemodelchart/tablewidget.cpp +++ b/tests/tablemodelchart/tablewidget.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include "customtablemodel.h" #include #include @@ -91,7 +91,7 @@ TableWidget::TableWidget(QWidget *parent) connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3())); - // QLabel *spinBoxLabel = new QLabel("Rows affected:"); + QLabel *spinBoxLabel = new QLabel("Rows affected:"); // spin box for setting number of affected items (add, remove) m_linesCountSpinBox = new QSpinBox; @@ -100,8 +100,8 @@ TableWidget::TableWidget(QWidget *parent) // buttons layout QVBoxLayout* buttonsLayout = new QVBoxLayout; - // buttonsLayout->addWidget(spinBoxLabel); - // buttonsLayout->addWidget(m_linesCountSpinBox); + buttonsLayout->addWidget(spinBoxLabel); + buttonsLayout->addWidget(m_linesCountSpinBox); // buttonsLayout->addWidget(addRowAboveButton); buttonsLayout->addWidget(addRowBelowButton); buttonsLayout->addWidget(removeRowButton); @@ -185,20 +185,20 @@ void TableWidget::updateChartType(bool toggle) // m_chart->axisX()->setNiceNumbersEnabled(false); // m_chart->axisY()->setNiceNumbersEnabled(false); - // // renable axes of the chart (pie hides them) - // // x axis - // QAxis *axis = m_chart->axisX(); - // axis->setAxisVisible(true); - // axis->setGridLineVisible(true); - // axis->setLabelsVisible(true); + // renable axes of the chart (pie hides them) + // x axis + QAxis *axis = m_chart->axisX(); + axis->setAxisVisible(true); + axis->setGridLineVisible(true); + axis->setLabelsVisible(true); - // // y axis - // axis = m_chart->axisY(); - // axis->setAxisVisible(true); - // axis->setGridLineVisible(true); - // axis->setLabelsVisible(true); + // y axis + axis = m_chart->axisY(); + axis->setAxisVisible(true); + axis->setGridLineVisible(true); + axis->setLabelsVisible(true); - // m_model->clearMapping(); + m_model->clearMapping(); QString seriesColorHex = "#000000"; // QPen pen; @@ -208,21 +208,22 @@ void TableWidget::updateChartType(bool toggle) { // m_chart->setAnimationOptions(QChart::NoAnimation); - // // series 1 - // m_series = new QLineSeries; - // m_series->setModel(m_model); + // series 1 + m_series = new QLineSeries(this); - // QXYModelMapper *mapper = new QXYModelMapper; - // mapper->setMapX(0); - // mapper->setMapY(1); - // mapper->setFirst(3); + QVXYModelMapper *mapper = new QVXYModelMapper; + mapper->setModel(m_model); + mapper->setSeries(m_series); + mapper->setXColumn(0); + mapper->setYColumn(1); + mapper->setFirst(3); // mapper->setCount(4); - // m_series->setModelMapper(mapper); - // // m_series->setModelMapping(0,1, Qt::Vertical); - // // m_series->setModelMappingRange(3, 4); - // m_chart->addSeries(m_series); - // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); - // m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4)); + + // m_series->setModelMapping(0,1, Qt::Vertical); + // m_series->setModelMappingRange(3, 4); + m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4)); // // series 2 // m_series = new QLineSeries; @@ -354,7 +355,7 @@ void TableWidget::updateChartType(bool toggle) m_pieMapper->setLabelsColumn(7); m_pieMapper->setSeries(m_pieSeries); m_pieMapper->setModel(m_model); - m_pieMapper->setFirst(2); +// m_pieMapper->setFirst(2); // m_pieMapper->setCount(5); // pieSeries->setModelMapper(mapper); @@ -377,9 +378,9 @@ void TableWidget::updateChartType(bool toggle) m_pieMapper = new QVPieModelMapper; m_pieMapper->setValuesColumn(0); m_pieMapper->setLabelsColumn(7); - m_pieMapper->setSeries(m_pieSeries2); m_pieMapper->setModel(m_model); - m_pieMapper->setFirst(2); + m_pieMapper->setSeries(m_pieSeries2); +// m_pieMapper->setFirst(2); m_pieSeries2->setLabelsVisible(true); m_pieSeries2->setPieSize(0.35);