diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index 998d93e..2af123b 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -52,6 +52,7 @@ void LineChartItem::setLayout(QVector& points) { if(points.size()==0) { + m_path = QPainterPath(); XYChartItem::setLayout(points); return; } diff --git a/src/scatterchart/scatterchartitem.cpp b/src/scatterchart/scatterchartitem.cpp index b275d23..e713225 100644 --- a/src/scatterchart/scatterchartitem.cpp +++ b/src/scatterchart/scatterchartitem.cpp @@ -105,6 +105,7 @@ void ScatterChartItem::setLayout(QVector& points) { if(points.size()==0) { + deletePoints(m_items.childItems().count()); XYChartItem::setLayout(points); return; } diff --git a/src/splinechart/qsplineseries.cpp b/src/splinechart/qsplineseries.cpp index c369a01..df1e417 100644 --- a/src/splinechart/qsplineseries.cpp +++ b/src/splinechart/qsplineseries.cpp @@ -220,16 +220,28 @@ void QSplineSeriesPrivate::updateControlPoints() } } -void QSplineSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end) +void QSplineSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end) { updateControlPoints(); - QXYSeriesPrivate::modelDataAdded(parent, start, end); + QXYSeriesPrivate::modelRowsAdded(parent, start, end); } -void QSplineSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end) +void QSplineSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) { updateControlPoints(); - QXYSeriesPrivate::modelDataRemoved(parent, start, end); + QXYSeriesPrivate::modelRowsRemoved(parent, start, end); +} + +void QSplineSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) +{ + updateControlPoints(); + QXYSeriesPrivate::modelColumnsAdded(parent, start, end); +} + +void QSplineSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) +{ + updateControlPoints(); + QXYSeriesPrivate::modelColumnsRemoved(parent, start, end); } Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter) diff --git a/src/splinechart/qsplineseries_p.h b/src/splinechart/qsplineseries_p.h index 93a2bc8..f369d42 100644 --- a/src/splinechart/qsplineseries_p.h +++ b/src/splinechart/qsplineseries_p.h @@ -46,8 +46,10 @@ public Q_SLOTS: void updateControlPoints(); protected Q_SLOTS: - void modelDataAdded(QModelIndex parent, int start, int end); - void modelDataRemoved(QModelIndex parent, int start, int end); + 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); private: void calculateControlPoints(); diff --git a/src/splinechart/splinechartitem.cpp b/src/splinechart/splinechartitem.cpp index 22eeb8d..c5faf3c 100644 --- a/src/splinechart/splinechartitem.cpp +++ b/src/splinechart/splinechartitem.cpp @@ -80,12 +80,16 @@ QPointF SplineChartItem::calculateGeometryControlPoint(int index) const void SplineChartItem::setLayout(QVector &points) { // Dummy implementation because of a bug in Clang compiler + if (points.size() < 2) { + m_path = QPainterPath(); + } XYChartItem::setLayout(points); } void SplineChartItem::setLayout(QVector &points, QVector &controlPoints) { if ((points.size()<2) || (controlPoints.size()<2)) { + m_path = QPainterPath(); XYChartItem::setLayout(points); m_controlPoints=controlPoints; return; diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index 6fd5031..d92b1d3 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -174,6 +174,8 @@ QList QXYSeries::points() const QList result; if (d->m_mapOrientation == Qt::Vertical){ // consecutive data is read from model's column + if (d->m_mapX >= d->m_model->columnCount() || d->m_mapY >= d->m_model->columnCount()) + return result; // mapped columns are not existing for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) { qreal x = d->m_model->data(d->m_model->index(i, d->m_mapX), Qt::DisplayRole).toReal(); @@ -184,6 +186,9 @@ QList QXYSeries::points() const } else{ // consecutive data is read from model's row + if (d->m_mapX >= d->m_model->rowCount() || d->m_mapY >= d->m_model->rowCount()) + return result; // mapped rows are not existing + for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) { qreal x = d->m_model->data(d->m_model->index(d->m_mapX, i), Qt::DisplayRole).toReal(); qreal y = d->m_model->data(d->m_model->index(d->m_mapY, i), Qt::DisplayRole).toReal(); @@ -208,14 +213,18 @@ int QXYSeries::count() const if (d->m_mapOrientation == Qt::Vertical) { // data is in a column. Return the number of mapped items if the model's column have enough items // or the number of items that can be mapped - if (d->m_mapCount != -1) + if (d->m_mapX >= d->m_model->columnCount() || d->m_mapY >= d->m_model->columnCount()) + return 0; // mapped columns are not existing + else if (d->m_mapCount != -1) return qMin(d->m_mapCount, qMax(d->m_model->rowCount() - d->m_mapFirst, 0)); else return qMax(d->m_model->rowCount() - d->m_mapFirst, 0); } else { // data is in a row. Return the number of mapped items if the model's row have enough items // or the number of items that can be mapped - if (d->m_mapCount != -1) + if (d->m_mapX >= d->m_model->rowCount() || d->m_mapY >= d->m_model->rowCount()) + return 0; // mapped rows are not existing + else if (d->m_mapCount != -1) return qMin(d->m_mapCount, qMax(d->m_model->columnCount() - d->m_mapFirst, 0)); else return qMax(d->m_model->columnCount() - d->m_mapFirst, 0); @@ -357,13 +366,13 @@ void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientat // connect the signals from the model connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); - if (d->m_mapOrientation == Qt::Vertical) { - connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int))); - connect(d->m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int))); - } else { - connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int))); - connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int))); - } +// if (d->m_mapOrientation == Qt::Vertical) { + 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))); +// } else { + 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))); +// } } void QXYSeries::setModelMappingRange(int first, int count) @@ -426,7 +435,7 @@ void QXYSeriesPrivate::scaleDomain(Domain& domain) maxY = qMax(maxY, y); } - domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); + domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); } QList QXYSeriesPrivate::createLegendMarker(QLegend* legend) @@ -456,16 +465,40 @@ void QXYSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight } -void QXYSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end) +void QXYSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end) +{ + Q_UNUSED(parent); + if (m_mapOrientation == Qt::Vertical) + emit pointsAdded(start, end); + else if (start <= m_mapX || start <= m_mapY) + emit reinitialized(); +} + +void QXYSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) +{ + Q_UNUSED(parent); + if (m_mapOrientation == Qt::Vertical) + emit pointsRemoved(start, end); + else if (start <= m_mapX || start <= m_mapY) + emit reinitialized(); +} + +void QXYSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) { Q_UNUSED(parent); - emit pointsAdded(start, end); + if (m_mapOrientation == Qt::Horizontal) + emit pointsAdded(start, end); + else if (start <= m_mapX || start <= m_mapY) + emit reinitialized(); } -void QXYSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end) +void QXYSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) { Q_UNUSED(parent); - emit pointsRemoved(start, end); + if (m_mapOrientation == Qt::Horizontal) + emit pointsRemoved(start, end); + else if (start <= m_mapX || start <= m_mapY) + emit reinitialized(); } #include "moc_qxyseries.cpp" diff --git a/src/xychart/qxyseries_p.h b/src/xychart/qxyseries_p.h index 2c1683d..d25bddd 100644 --- a/src/xychart/qxyseries_p.h +++ b/src/xychart/qxyseries_p.h @@ -48,8 +48,14 @@ public: protected Q_SLOTS: void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); - virtual void modelDataAdded(QModelIndex parent, int start, int end); - virtual void modelDataRemoved(QModelIndex parent, int start, int end); + virtual void modelRowsAdded(QModelIndex parent, int start, int end); + virtual void modelRowsRemoved(QModelIndex parent, int start, int end); + virtual void modelColumnsAdded(QModelIndex parent, int start, int end); + virtual void modelColumnsRemoved(QModelIndex parent, int start, int end); + +private: + void insertData(int start, int end); + void removeData(int start, int end); Q_SIGNALS: void updated(); @@ -58,8 +64,7 @@ Q_SIGNALS: 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/xychartitem.cpp b/src/xychart/xychartitem.cpp index 2eef246..037f946 100644 --- a/src/xychart/xychartitem.cpp +++ b/src/xychart/xychartitem.cpp @@ -44,6 +44,7 @@ XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int))); connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int))); connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int))); + connect(series->d_func(),SIGNAL(reinitialized()),this,SLOT(handleReinitialized())); connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF))); } @@ -211,6 +212,16 @@ void XYChartItem::handlePointReplaced(int index) update(); } +void XYChartItem::handleReinitialized() +{ + QVector points = calculateGeometryPoints(); + if (points.isEmpty()) + setLayout(points); + else + updateLayout(m_points,points); + update(); +} + void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) { m_minX=minX; diff --git a/src/xychart/xychartitem_p.h b/src/xychart/xychartitem_p.h index 897596f..1e9bbc6 100644 --- a/src/xychart/xychartitem_p.h +++ b/src/xychart/xychartitem_p.h @@ -47,6 +47,7 @@ public Q_SLOTS: void handlePointRemoved(int index); void handlePointsRemoved(int start, int end); void handlePointReplaced(int index); + void handleReinitialized(); void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); void handleGeometryChanged(const QRectF &size); diff --git a/test/tablemodelchart/tablewidget.cpp b/test/tablemodelchart/tablewidget.cpp index dc98059..8dac25e 100644 --- a/test/tablemodelchart/tablewidget.cpp +++ b/test/tablemodelchart/tablewidget.cpp @@ -368,9 +368,10 @@ void TableWidget::updateChartType(bool toggle) } - if (!m_barRadioButton->isChecked()) + if (!m_barRadioButton->isChecked()) { m_chart->axisX()->setRange(0, 500); - // m_chart->axisY()->setRange(0, 120); + m_chart->axisY()->setRange(0, 220); + } m_chart->legend()->setVisible(true); // repaint table view colors