diff --git a/src/barchart/barchart.pri b/src/barchart/barchart.pri index b85acab..e0c0f89 100644 --- a/src/barchart/barchart.pri +++ b/src/barchart/barchart.pri @@ -12,7 +12,9 @@ SOURCES += \ $$PWD/qstackedbarseries.cpp \ $$PWD/qgroupedbarseries.cpp \ $$PWD/stackedbarchartitem.cpp \ - $$PWD/qbarmodelmapper.cpp + $$PWD/qbarmodelmapper.cpp \ + $$PWD/qvbarmodelmapper.cpp \ + $$PWD/qhbarmodelmapper.cpp PRIVATE_HEADERS += \ $$PWD/bar_p.h \ @@ -33,4 +35,6 @@ PUBLIC_HEADERS += \ $$PWD/qpercentbarseries.h \ $$PWD/qstackedbarseries.h \ $$PWD/qgroupedbarseries.h \ - $$PWD/qbarmodelmapper.h + $$PWD/qbarmodelmapper.h \ + $$PWD/qvbarmodelmapper.h \ + $$PWD/qhbarmodelmapper.h diff --git a/src/barchart/qbarmodelmapper.cpp b/src/barchart/qbarmodelmapper.cpp index c082b7c..6d2c7f9 100644 --- a/src/barchart/qbarmodelmapper.cpp +++ b/src/barchart/qbarmodelmapper.cpp @@ -208,6 +208,10 @@ void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex botto { Q_UNUSED(topLeft) Q_UNUSED(bottomRight) + + if (m_model == 0 || m_series == 0) + return; + if (m_modelSignalsBlock) return; diff --git a/src/barchart/qhbarmodelmapper.cpp b/src/barchart/qhbarmodelmapper.cpp new file mode 100644 index 0000000..c8fee85 --- /dev/null +++ b/src/barchart/qhbarmodelmapper.cpp @@ -0,0 +1,42 @@ +#include "qhbarmodelmapper.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QHBarModelMapper::QHBarModelMapper(QObject *parent) : + QBarModelMapper(parent) +{ +} + +int QHBarModelMapper::firstBarRow() const +{ + return QBarModelMapper::firstBarSection(); +} + +void QHBarModelMapper::setFirstBarRow(int firstBarRow) +{ + return QBarModelMapper::setFirstBarSection(firstBarRow); +} + +int QHBarModelMapper::lastBarRow() const +{ + return QBarModelMapper::lastBarSection(); +} + +void QHBarModelMapper::setLastBarRow(int lastBarRow) +{ + return QBarModelMapper::setLastBarSection(lastBarRow); +} + +int QHBarModelMapper::categoriesRow() const +{ + return QBarModelMapper::categoriesSection(); +} + +void QHBarModelMapper::setCategoriesRow(int categoriesRow) +{ + return QBarModelMapper::setCategoriesSection(categoriesRow); +} + +#include "moc_qhbarmodelmapper.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qhbarmodelmapper.h b/src/barchart/qhbarmodelmapper.h new file mode 100644 index 0000000..e069497 --- /dev/null +++ b/src/barchart/qhbarmodelmapper.h @@ -0,0 +1,30 @@ +#ifndef QHBARMODELMAPPER_H +#define QHBARMODELMAPPER_H + +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QTCOMMERCIALCHART_EXPORT QHBarModelMapper : public QBarModelMapper +{ + Q_OBJECT + Q_PROPERTY(int firstBarRow READ firstBarRow WRITE setFirstBarRow) + Q_PROPERTY(int lastBarRow READ lastBarRow WRITE setLastBarRow) + +public: + explicit QHBarModelMapper(QObject *parent = 0); + + int firstBarRow() const; + void setFirstBarRow(int firstBarRow); + + int lastBarRow() const; + void setLastBarRow(int lastBarRow); + + int categoriesRow() const; + void setCategoriesRow(int categoriesRow); + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QHBARMODELMAPPER_H diff --git a/src/barchart/qvbarmodelmapper.cpp b/src/barchart/qvbarmodelmapper.cpp new file mode 100644 index 0000000..206be46 --- /dev/null +++ b/src/barchart/qvbarmodelmapper.cpp @@ -0,0 +1,42 @@ +#include "qvbarmodelmapper.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QVBarModelMapper::QVBarModelMapper(QObject *parent) : + QBarModelMapper(parent) +{ +} + +int QVBarModelMapper::firstBarColumn() const +{ + return QBarModelMapper::firstBarSection(); +} + +void QVBarModelMapper::setFirstBarColumn(int firstBarColumn) +{ + return QBarModelMapper::setFirstBarSection(firstBarColumn); +} + +int QVBarModelMapper::lastBarColumn() const +{ + return QBarModelMapper::lastBarSection(); +} + +void QVBarModelMapper::setLastBarColumn(int lastBarColumn) +{ + return QBarModelMapper::setLastBarSection(lastBarColumn); +} + +int QVBarModelMapper::categoriesColumn() const +{ + return QBarModelMapper::categoriesSection(); +} + +void QVBarModelMapper::setCategoriesColumn(int categoriesColumn) +{ + return QBarModelMapper::setCategoriesSection(categoriesColumn); +} + +#include "moc_qvbarmodelmapper.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qvbarmodelmapper.h b/src/barchart/qvbarmodelmapper.h new file mode 100644 index 0000000..0b74d39 --- /dev/null +++ b/src/barchart/qvbarmodelmapper.h @@ -0,0 +1,30 @@ +#ifndef QVBARMODELMAPPER_H +#define QVBARMODELMAPPER_H + +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QTCOMMERCIALCHART_EXPORT QVBarModelMapper : public QBarModelMapper +{ + Q_OBJECT + Q_PROPERTY(int firstBarColumn READ firstBarColumn WRITE setFirstBarColumn) + Q_PROPERTY(int lastBarColumn READ lastBarColumn WRITE setLastBarColumn) + +public: + explicit QVBarModelMapper(QObject *parent = 0); + + int firstBarColumn() const; + void setFirstBarColumn(int firstBarColumn); + + int lastBarColumn() const; + void setLastBarColumn(int lastBarColumn); + + int categoriesColumn() const; + void setCategoriesColumn(int categoriesColumn); + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QVBARMODELMAPPER_H diff --git a/src/piechart/qpiemodelmapper.cpp b/src/piechart/qpiemodelmapper.cpp index 39f7c24..767dbd0 100644 --- a/src/piechart/qpiemodelmapper.cpp +++ b/src/piechart/qpiemodelmapper.cpp @@ -12,6 +12,12 @@ QPieModelMapper::QPieModelMapper(QObject *parent) : { } +QPieModelMapper::~QPieModelMapper() +{ +// Q_D(QPieModelMapper); +// disconnect(d->m_model, 0, d, 0); +} + QAbstractItemModel* QPieModelMapper::model() const { Q_D(const QPieModelMapper); diff --git a/src/piechart/qpiemodelmapper.h b/src/piechart/qpiemodelmapper.h index ce0af45..a3d7938 100644 --- a/src/piechart/qpiemodelmapper.h +++ b/src/piechart/qpiemodelmapper.h @@ -42,6 +42,7 @@ class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject protected: QPieModelMapper(QObject *parent = 0); + ~QPieModelMapper(); public: QAbstractItemModel* model() const; diff --git a/src/xychart/qxymodelmapper.cpp b/src/xychart/qxymodelmapper.cpp index 4b12f3b..634b7fc 100644 --- a/src/xychart/qxymodelmapper.cpp +++ b/src/xychart/qxymodelmapper.cpp @@ -11,6 +11,13 @@ QXYModelMapper::QXYModelMapper(QObject *parent): { } +QXYModelMapper::~QXYModelMapper() +{ + Q_D(QXYModelMapper); + disconnect(d->m_model, 0, d, 0); +// disconnect(d->m_series, 0, d, 0); +} + QAbstractItemModel* QXYModelMapper::model() const { Q_D(const QXYModelMapper); @@ -360,9 +367,9 @@ void QXYModelMapperPrivate::removeData(int start, int end) 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 toRemove = qMin(m_series->count(), 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. + int last = qMin(first + toRemove - 1, m_series->count() + 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)); } @@ -370,13 +377,13 @@ void QXYModelMapperPrivate::removeData(int start, int end) 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(); + itemsAvailable = m_model->rowCount() - m_first - m_series->count(); 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(); + itemsAvailable = m_model->columnCount() - m_first - m_series->count(); + int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled. + int currentSize = m_series->count(); if (toBeAdded > 0) - for (int i = m_series->points().size(); i < currentSize + toBeAdded; i++) { + for (int i = m_series->count(); 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()); diff --git a/src/xychart/qxymodelmapper.h b/src/xychart/qxymodelmapper.h index 34b03be..57783fe 100644 --- a/src/xychart/qxymodelmapper.h +++ b/src/xychart/qxymodelmapper.h @@ -37,6 +37,7 @@ public: protected: explicit QXYModelMapper(QObject *parent = 0); + ~QXYModelMapper(); Qt::Orientation orientation() const; void setOrientation(Qt::Orientation orientation); diff --git a/tests/tablemodelchart/tablewidget.cpp b/tests/tablemodelchart/tablewidget.cpp index 124507e..dfdd0ce 100644 --- a/tests/tablemodelchart/tablewidget.cpp +++ b/tests/tablemodelchart/tablewidget.cpp @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include #include @@ -44,7 +44,12 @@ #include TableWidget::TableWidget(QWidget *parent) - : QWidget(parent) + : QWidget(parent), + m_series(0), + m_mapper(0), + m_model(0), + m_pieMapper(0), + m_pieMapper2(0) // specialPie(0) { setGeometry(1900, 100, 1000, 600); @@ -130,13 +135,13 @@ TableWidget::TableWidget(QWidget *parent) connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); - m_lineRadioButton->setChecked(true); + m_barRadioButton->setChecked(true); // radio buttons layout QVBoxLayout* radioLayout = new QVBoxLayout; radioLayout->addWidget(m_lineRadioButton); radioLayout->addWidget(m_splineRadioButton); - // radioLayout->addWidget(m_scatterRadioButton); + radioLayout->addWidget(m_scatterRadioButton); radioLayout->addWidget(m_pieRadioButton); // radioLayout->addWidget(m_areaRadioButton); radioLayout->addWidget(m_barRadioButton); @@ -186,8 +191,28 @@ void TableWidget::updateChartType(bool toggle) if (toggle) { // specialPie = 0; m_chart->removeAllSeries(); + m_series = 0; // m_chart->axisX()->setNiceNumbersEnabled(false); // m_chart->axisY()->setNiceNumbersEnabled(false); + if (m_mapper) { + m_mapper->deleteLater(); + m_mapper = 0; + } + + if (m_pieMapper) { + m_pieMapper->deleteLater(); + m_pieMapper = 0; + } + + if (m_pieMapper2) { + m_pieMapper2->deleteLater(); + m_pieMapper2 = 0; + } + + // if (m_series) { + // delete m_series; + // m_series = 0; + // } // renable axes of the chart (pie hides them) // x axis @@ -213,15 +238,15 @@ void TableWidget::updateChartType(bool toggle) m_chart->setAnimationOptions(QChart::NoAnimation); // series 1 - m_series = new QLineSeries(this); + m_series = new QLineSeries; - 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_mapper = new QVXYModelMapper; + m_mapper->setModel(m_model); + m_mapper->setSeries(m_series); + m_mapper->setXColumn(0); + m_mapper->setYColumn(1); + m_mapper->setFirst(3); + m_mapper->setCount(4); // m_series->setModelMapping(0,1, Qt::Vertical); // m_series->setModelMappingRange(3, 4); @@ -266,17 +291,17 @@ void TableWidget::updateChartType(bool toggle) // series 1 m_series = new QSplineSeries; -// m_series->setModel(m_model); + // m_series->setModel(m_model); - QVXYModelMapper *mapper = new QVXYModelMapper; - mapper->setSeries(m_series); - mapper->setModel(m_model); - mapper->setXColumn(0); - mapper->setYColumn(1); - mapper->setFirst(0); - mapper->setCount(-1); + m_mapper = new QVXYModelMapper; + m_mapper->setSeries(m_series); + m_mapper->setModel(m_model); + m_mapper->setXColumn(0); + m_mapper->setYColumn(1); + m_mapper->setFirst(0); + m_mapper->setCount(-1); -// m_series->setModelMapper(mapper); + // m_series->setModelMapper(mapper); m_chart->addSeries(m_series); seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); @@ -313,44 +338,45 @@ void TableWidget::updateChartType(bool toggle) // m_chart->addSeries(m_series); // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); // m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000)); - } - // else if (m_scatterRadioButton->isChecked()) - // { - // m_chart->setAnimationOptions(QChart::NoAnimation); - - // // series 1 - // m_series = new QScatterSeries; - // m_series->setModel(m_model); - // m_series->setModelMapping(0,1, Qt::Vertical); - // // m_series->setModelMappingRange(2, 0); - // // series->setModelMapping(0,1, Qt::Horizontal); - // m_chart->addSeries(m_series); - - // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); - // m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000)); - - // // series 2 - // m_series = new QScatterSeries; - // m_series->setModel(m_model); - // m_series->setModelMapping(2,3, Qt::Vertical); - // // m_series->setModelMappingRange(1, 6); - // // series->setModelMapping(2,3, Qt::Horizontal); - // m_chart->addSeries(m_series); - - // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); - // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6)); - - // // series 3 - // m_series = new QScatterSeries; - // m_series->setModel(m_model); - // m_series->setModelMapping(4,5, Qt::Vertical); - // // series->setModelMapping(4,5, Qt::Horizontal); - // m_chart->addSeries(m_series); - // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); - // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000)); - // } - else if (m_pieRadioButton->isChecked()) + } else if (m_scatterRadioButton->isChecked()) { + m_chart->setAnimationOptions(QChart::NoAnimation); + + // series 1 + m_series = new QScatterSeries; + + m_mapper = new QVXYModelMapper; + m_mapper->setSeries(m_series); + m_mapper->setModel(m_model); + m_mapper->setXColumn(0); + m_mapper->setYColumn(1); + m_mapper->setFirst(0); + m_mapper->setCount(-1); + + m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000)); + + // // series 2 + // m_series = new QScatterSeries; + // m_series->setModel(m_model); + // m_series->setModelMapping(2,3, Qt::Vertical); + // // m_series->setModelMappingRange(1, 6); + // // series->setModelMapping(2,3, Qt::Horizontal); + // m_chart->addSeries(m_series); + + // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); + // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6)); + + // // series 3 + // m_series = new QScatterSeries; + // m_series->setModel(m_model); + // m_series->setModelMapping(4,5, Qt::Vertical); + // // series->setModelMapping(4,5, Qt::Horizontal); + // m_chart->addSeries(m_series); + // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); + // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000)); + } else if (m_pieRadioButton->isChecked()) { m_chart->setAnimationOptions(QChart::SeriesAnimations); // pie 1 @@ -381,12 +407,12 @@ void TableWidget::updateChartType(bool toggle) // pie 2 m_pieSeries2 = new QPieSeries; - m_pieMapper = new QVPieModelMapper; - m_pieMapper->setValuesColumn(0); - m_pieMapper->setLabelsColumn(7); - m_pieMapper->setModel(m_model); - m_pieMapper->setSeries(m_pieSeries2); - m_pieMapper->setFirst(2); + m_pieMapper2 = new QVPieModelMapper; + m_pieMapper2->setValuesColumn(0); + m_pieMapper2->setLabelsColumn(7); + m_pieMapper2->setModel(m_model); + m_pieMapper2->setSeries(m_pieSeries2); + m_pieMapper2->setFirst(2); m_pieSeries2->setLabelsVisible(true); m_pieSeries2->setPieSize(0.35); @@ -441,25 +467,31 @@ void TableWidget::updateChartType(bool toggle) // } else if (m_barRadioButton->isChecked()) { - // m_chart->setAnimationOptions(QChart::SeriesAnimations); - - // QGroupedBarSeries* barSeries = new QGroupedBarSeries(); - // barSeries->setCategories(QStringList()); - // barSeries->setModel(m_model); - // // barSeries->setModelMappingRange(2, 5); - //// barSeries->setModelMapping(5, 2, 4, Qt::Vertical); - - // QBarModelMapper *mapper = new QBarModelMapper; - // mapper->setMapCategories(5); - // mapper->setMapBarBottom(2); - // mapper->setMapBarTop(4); - // barSeries->setModelMapper(mapper); - // m_chart->addSeries(barSeries); - // QList barsets = barSeries->barSets(); - // for (int i = 0; i < barsets.count(); i++) { - // seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper(); - // m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000)); - // } + m_chart->setAnimationOptions(QChart::SeriesAnimations); + + QGroupedBarSeries* barSeries = new QGroupedBarSeries(); +// barSeries->setCategories(QStringList()); +// barSeries->setModel(m_model); + // barSeries->setModelMappingRange(2, 5); + // barSeries->setModelMapping(5, 2, 4, Qt::Vertical); + + int first = 3; + int count = 4; + QVBarModelMapper *mapper = new QVBarModelMapper; + mapper->setCategoriesSection(5); + mapper->setFirstBarSection(2); + mapper->setLastBarSection(4); + mapper->setFirst(first); + mapper->setCount(count); + mapper->setSeries(barSeries); + mapper->setModel(m_model); +// barSeries->setModelMapper(mapper); + m_chart->addSeries(barSeries); + QList barsets = barSeries->barSets(); + for (int i = 0; i < barsets.count(); i++) { + seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(2 + i, first, 1, count)); + } } diff --git a/tests/tablemodelchart/tablewidget.h b/tests/tablemodelchart/tablewidget.h index 831a38e..ecf33cd 100644 --- a/tests/tablemodelchart/tablewidget.h +++ b/tests/tablemodelchart/tablewidget.h @@ -27,6 +27,7 @@ //#include "qxyseries.h" #include #include +#include class CustomTableModel; class QTableView; @@ -61,6 +62,7 @@ public: QChartView* m_chartView; QChart* m_chart; QXYSeries* m_series; + QVXYModelMapper *m_mapper; CustomTableModel* m_model; QTableView* m_tableView; QRadioButton* m_lineRadioButton;