From 9efd7b220dc4d218b289a59075cd96c2964aa4bc 2012-04-19 06:47:24 From: Marek Rosa Date: 2012-04-19 06:47:24 Subject: [PATCH] Fixes to model support --- diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index b3a8339..972bf5d 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -591,12 +591,14 @@ void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end) Q_UNUSED(parent); Q_Q(QPieSeries); // series uses model as a data sourceupda - int addedCount = end - start + 1; if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) { return; } else { + int addedCount = end - start + 1; + if (m_mapCount != -1 && addedCount > m_mapCount) + addedCount = m_mapCount; int first = qMax(start, m_mapFirst); - int last = qMin(first + addedCount - 1, end); + int last = qMin(first + addedCount - 1, m_mapOrientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); for (int i = first; i <= last; i++) { QPieSlice *slice = new QPieSlice; if (m_mapOrientation == Qt::Vertical) { @@ -679,17 +681,21 @@ void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end) } // else if (m_mapCount == -1) { +// // first find how many items can actually be removed +// int toRemove = qMin(m_slices.size(), removedCount); +// // get the index of the first item that will be removed. // int first = qMax(start, m_mapFirst); -// int last = qMax(end, m_mapFirst + removedCount - 1); -// for (int i = last; i >= first; i--) +// // get the index of the last item that will be removed. +// int last = first + toRemove; +// for (int i = last - 1; i >= first; i--) // q->remove(q->slices().at(i - m_mapFirst)); // } -// else { -// int toRemove = qMin(m_slices.size() - 1, removedCount); -// int first = qMax(start, m_mapFirst); -// int last = qMax(end, m_mapFirst + toRemove - 1); + // else { + // int toRemove = qMin(m_slices.size() - 1, removedCount); + // int first = qMax(start, m_mapFirst); + // int last = qMax(end, m_mapFirst + toRemove - 1); -// } + // } // removing items from unlimited map else if (m_mapCount == -1 && start >= m_mapFirst) { @@ -698,7 +704,8 @@ void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end) // handlePointRemoved(i - m_mapFirst); } else if (m_mapCount == - 1 && start < m_mapFirst) { // not all removed items - for (int i = m_mapFirst + removedCount - 1; i >= m_mapFirst; i--) + int lastExisting = qMin(m_mapFirst + m_slices.size() - 1, m_mapFirst + removedCount - 1); + for (int i = lastExisting; i >= m_mapFirst; i--) q->remove(q->slices().at(i - m_mapFirst)); // handlePointRemoved(i - m_mapFirst); } @@ -706,11 +713,11 @@ void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end) // removing items from limited map else if (start >= m_mapFirst) { // -// int sizeAfter = m_slices.size(); + // int sizeAfter = m_slices.size(); int lastExisting = qMin(m_mapFirst + m_slices.size() - 1, end); for (int i = lastExisting; i >= start; i--) { q->remove(q->slices().at(i - m_mapFirst)); -// sizeAfter--; + // sizeAfter--; // handlePointRemoved(i - m_mapFirst); } diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index 500c221..5862dd2 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -174,6 +174,7 @@ void QXYSeries::removeAll() */ QList QXYSeries::points() const { +// Q_ASSERT(false); Q_D(const QXYSeries); if (d->m_model) { QList result; diff --git a/src/xychart/xychartitem.cpp b/src/xychart/xychartitem.cpp index 2555f7b..c649042 100644 --- a/src/xychart/xychartitem.cpp +++ b/src/xychart/xychartitem.cpp @@ -129,52 +129,68 @@ void XYChartItem::handlePointsAdded(int start, int end) if (m_series->model() == 0) { for (int i = start; i <= end; i++) handlePointAdded(i); + } else if (m_series->mapCount() != -1 && start >= m_series->mapFirst() + m_series->mapCount()) { + return; } else { - // series uses model as a data source - int first = m_series->mapFirst(); - int count = m_series->mapCount(); int addedCount = end - start + 1; - if (count != -1 && start >= first + count) { - return; - } - - // adding items to unlimited map - else if (count == -1 && start >= first) { - for (int i = start; i <= end; i++) - handlePointAdded(i - first); - } else if (count == - 1 && start < first) { - // not all newly added items - for (int i = first; i < first + addedCount; i++) - handlePointAdded(i - first); - } - // commented out code below does the same thing, but its more confusing. - // } else if (count == -1) { - // int begin = qMax(start, first); - // for (int i = begin; i < begin + (end - start + 1); i++) - // handlePointAdded(i - first); - // } - - // adding items to limited map - else if (start >= first) { - // remove the items that will no longer fit into the map - // int toRemove = addedCount - (count - points().size()); - for (int i = start; i <= end; i++) { - handlePointAdded(i - first); - } - if (m_points.size() > count) - for (int i = m_points.size() - 1; i >= count; i--) - handlePointRemoved(i); - // update(); - } else { - // - for (int i = first; i < first + addedCount; i++) { - handlePointAdded(i - first); - } - if (m_points.size() > count) - for (int i = m_points.size() - 1; i >= count; i--) - handlePointRemoved(i); + if (m_series->mapCount() != -1 && addedCount > m_series->mapCount()) + addedCount = m_series->mapCount(); + int first = qMax(start, m_series->mapFirst()); + int last = qMin(first + addedCount - 1, m_series->mapOrientation() == Qt::Vertical ? m_series->model()->rowCount() - 1 : m_series->model()->columnCount() - 1); + for (int i = first; i <= last; i++) { + handlePointAdded(i - m_series->mapFirst()); } + if (m_series->mapCount() != -1 && m_points.size() > m_series->mapCount()) + for (int i = m_points.size() - 1; i >= m_series->mapCount(); i--) + handlePointRemoved(i); } + + // else { + // // series uses model as a data source + // int first = m_series->mapFirst(); + // int count = m_series->mapCount(); + // int addedCount = end - start + 1; + // if (count != -1 && start >= first + count) { + // return; + // } + + // // adding items to unlimited map + // else if (count == -1 && start >= first) { + // for (int i = start; i <= end; i++) + // handlePointAdded(i - first); + // } else if (count == - 1 && start < first) { + // // not all newly added items + // for (int i = first; i < first + addedCount; i++) + // handlePointAdded(i - first); + // } + // // commented out code below does the same thing, but its more confusing. + // // } else if (count == -1) { + // // int begin = qMax(start, first); + // // for (int i = begin; i < begin + (end - start + 1); i++) + // // handlePointAdded(i - first); + // // } + + // // adding items to limited map + // else if (start >= first) { + // // remove the items that will no longer fit into the map + // // int toRemove = addedCount - (count - points().size()); + // for (int i = start; i <= end; i++) { + // handlePointAdded(i - first); + // } + // if (m_points.size() > count) + // for (int i = m_points.size() - 1; i >= count; i--) + // handlePointRemoved(i); + // // update(); + // } else { + // // + // for (int i = first; i < first + addedCount; i++) { + // handlePointAdded(i - first); + // } + // if (m_points.size() > count) + // for (int i = m_points.size() - 1; i >= count; i--) + // handlePointRemoved(i); + // } + // } } void XYChartItem::handlePointRemoved(int index) @@ -211,7 +227,8 @@ void XYChartItem::handlePointsRemoved(int start, int end) handlePointRemoved(i - first); } else if (count == - 1 && start < first) { // not all removed items - for (int i = first + removedCount - 1; i >= first; i--) + int lastExisting = qMin(first + m_points.size() - 1, first + removedCount - 1); + for (int i = lastExisting; i >= first; i--) handlePointRemoved(i - first); } diff --git a/test/tablemodelchart/tablewidget.cpp b/test/tablemodelchart/tablewidget.cpp index 305413e..7fe009f 100644 --- a/test/tablemodelchart/tablewidget.cpp +++ b/test/tablemodelchart/tablewidget.cpp @@ -53,7 +53,7 @@ TableWidget::TableWidget(QWidget *parent) // tableView->setItemDelegate(new QStyledItemDelegate); m_chart = new QChart; m_chart->legend()->setVisible(true); - m_chart->setAnimationOptions(QChart::SeriesAnimations); +// m_chart->setAnimationOptions(QChart::SeriesAnimations); m_chartView = new QChartView(m_chart); m_chartView->setRenderHint(QPainter::Antialiasing); m_chartView->setMinimumSize(640, 480);