From dcb83cfdc0e6c8e92df0ca2aacfd34c0ca276e2e Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Fri, 2 May 2014 13:02:57 +0300 Subject: Optimize single item changes in bar/surface item models. We are only able to optimize this in cases where rows and columns of the model are directly mapped to rows and columns of the data proxy. In other cases we do not know if the new values of the changed data item in the model actually specify the same row/column in our data proxy as the previous values. Task-number: QTRD-2190 Change-Id: Ie014469ac894474900e5cfd6d91fd1a60353b1f7 Reviewed-by: Titta Heikkala --- src/datavisualization/data/baritemmodelhandler.cpp | 50 +++++++++++--- src/datavisualization/data/baritemmodelhandler_p.h | 6 ++ .../data/scatteritemmodelhandler.cpp | 6 +- .../data/surfaceitemmodelhandler.cpp | 78 +++++++++++++++++----- .../data/surfaceitemmodelhandler_p.h | 7 ++ src/datavisualization/engine/bars3dcontroller.cpp | 6 +- src/datavisualization/engine/bars3drenderer.cpp | 13 +++- 7 files changed, 133 insertions(+), 33 deletions(-) (limited to 'src/datavisualization') diff --git a/src/datavisualization/data/baritemmodelhandler.cpp b/src/datavisualization/data/baritemmodelhandler.cpp index 4f44fe1d..3b02c1e3 100644 --- a/src/datavisualization/data/baritemmodelhandler.cpp +++ b/src/datavisualization/data/baritemmodelhandler.cpp @@ -26,7 +26,9 @@ BarItemModelHandler::BarItemModelHandler(QItemModelBarDataProxy *proxy, QObject : AbstractItemModelHandler(parent), m_proxy(proxy), m_proxyArray(0), - m_columnCount(0) + m_columnCount(0), + m_valueRole(noRoleIndex), + m_rotationRole(noRoleIndex) { } @@ -34,6 +36,34 @@ BarItemModelHandler::~BarItemModelHandler() { } +void BarItemModelHandler::handleDataChanged(const QModelIndex &topLeft, + const QModelIndex &bottomRight, + const QVector &roles) +{ + // Do nothing if full reset already pending + if (!m_fullReset) { + if (!m_proxy->useModelCategories()) { + // If the data model doesn't directly map rows and columns, we cannot optimize + AbstractItemModelHandler::handleDataChanged(topLeft, bottomRight, roles); + } else { + int startRow = qMin(topLeft.row(), bottomRight.row()); + int endRow = qMax(topLeft.row(), bottomRight.row()); + int startCol = qMin(topLeft.column(), bottomRight.column()); + int endCol = qMax(topLeft.column(), bottomRight.column()); + + for (int i = startRow; i <= endRow; i++) { + for (int j = startCol; j <= endCol; j++) { + QBarDataItem item; + item.setValue(m_itemModel->index(i, j).data(m_valueRole).toFloat()); + if (m_rotationRole != noRoleIndex) + item.setRotation(m_itemModel->index(i, j).data(m_rotationRole).toFloat()); + m_proxy->setItem(i, j, item); + } + } + } + } +} + // Resolve entire item model into QBarDataArray. void BarItemModelHandler::resolveModel() { @@ -54,8 +84,8 @@ void BarItemModelHandler::resolveModel() QHash roleHash = m_itemModel->roleNames(); // Default value role to display role if no mapping - int valueRole = roleHash.key(m_proxy->valueRole().toLatin1(), Qt::DisplayRole); - int rotationRole = roleHash.key(m_proxy->rotationRole().toLatin1(), noRoleIndex); + m_valueRole = roleHash.key(m_proxy->valueRole().toLatin1(), Qt::DisplayRole); + m_rotationRole = roleHash.key(m_proxy->rotationRole().toLatin1(), noRoleIndex); int rowCount = m_itemModel->rowCount(); int columnCount = m_itemModel->columnCount(); @@ -71,9 +101,9 @@ void BarItemModelHandler::resolveModel() for (int i = 0; i < rowCount; i++) { QBarDataRow &newProxyRow = *m_proxyArray->at(i); for (int j = 0; j < columnCount; j++) { - newProxyRow[j].setValue(m_itemModel->index(i, j).data(valueRole).toReal()); - if (rotationRole != noRoleIndex) - newProxyRow[j].setRotation(m_itemModel->index(i, j).data(rotationRole).toReal()); + newProxyRow[j].setValue(m_itemModel->index(i, j).data(m_valueRole).toFloat()); + if (m_rotationRole != noRoleIndex) + newProxyRow[j].setRotation(m_itemModel->index(i, j).data(m_rotationRole).toFloat()); } } // Generate labels from headers if using model rows/columns @@ -104,9 +134,9 @@ void BarItemModelHandler::resolveModel() QModelIndex index = m_itemModel->index(i, j); QString rowRoleStr = index.data(rowRole).toString(); QString columnRoleStr = index.data(columnRole).toString(); - itemValueMap[rowRoleStr][columnRoleStr] = index.data(valueRole).toReal(); - if (rotationRole != noRoleIndex) - itemRotationMap[rowRoleStr][columnRoleStr] = index.data(rotationRole).toReal(); + itemValueMap[rowRoleStr][columnRoleStr] = index.data(m_valueRole).toFloat(); + if (m_rotationRole != noRoleIndex) + itemRotationMap[rowRoleStr][columnRoleStr] = index.data(m_rotationRole).toFloat(); if (generateRows && !rowListHash.value(rowRoleStr, false)) { rowListHash.insert(rowRoleStr, true); rowList << rowRoleStr; @@ -142,7 +172,7 @@ void BarItemModelHandler::resolveModel() QBarDataRow &newProxyRow = *m_proxyArray->at(i); for (int j = 0; j < columnList.size(); j++) { newProxyRow[j].setValue(itemValueMap[rowKey][columnList.at(j)]); - if (rotationRole != noRoleIndex) + if (m_rotationRole != noRoleIndex) newProxyRow[j].setRotation(itemRotationMap[rowKey][columnList.at(j)]); } } diff --git a/src/datavisualization/data/baritemmodelhandler_p.h b/src/datavisualization/data/baritemmodelhandler_p.h index 7bf7b0a1..737e0055 100644 --- a/src/datavisualization/data/baritemmodelhandler_p.h +++ b/src/datavisualization/data/baritemmodelhandler_p.h @@ -41,12 +41,18 @@ public: BarItemModelHandler(QItemModelBarDataProxy *proxy, QObject *parent = 0); virtual ~BarItemModelHandler(); +public slots: + virtual void handleDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, + const QVector &roles = QVector ()); + protected: void virtual resolveModel(); QItemModelBarDataProxy *m_proxy; // Not owned QBarDataArray *m_proxyArray; // Not owned int m_columnCount; + int m_valueRole; + int m_rotationRole; }; QT_END_NAMESPACE_DATAVISUALIZATION diff --git a/src/datavisualization/data/scatteritemmodelhandler.cpp b/src/datavisualization/data/scatteritemmodelhandler.cpp index 08ed12f3..280f2a23 100644 --- a/src/datavisualization/data/scatteritemmodelhandler.cpp +++ b/src/datavisualization/data/scatteritemmodelhandler.cpp @@ -25,7 +25,11 @@ static const int noRoleIndex = -1; ScatterItemModelHandler::ScatterItemModelHandler(QItemModelScatterDataProxy *proxy, QObject *parent) : AbstractItemModelHandler(parent), m_proxy(proxy), - m_proxyArray(0) + m_proxyArray(0), + m_xPosRole(noRoleIndex), + m_yPosRole(noRoleIndex), + m_zPosRole(noRoleIndex), + m_rotationRole(noRoleIndex) { } diff --git a/src/datavisualization/data/surfaceitemmodelhandler.cpp b/src/datavisualization/data/surfaceitemmodelhandler.cpp index f4383dbf..584ddf28 100644 --- a/src/datavisualization/data/surfaceitemmodelhandler.cpp +++ b/src/datavisualization/data/surfaceitemmodelhandler.cpp @@ -25,7 +25,10 @@ static const int noRoleIndex = -1; SurfaceItemModelHandler::SurfaceItemModelHandler(QItemModelSurfaceDataProxy *proxy, QObject *parent) : AbstractItemModelHandler(parent), m_proxy(proxy), - m_proxyArray(0) + m_proxyArray(0), + m_xPosRole(noRoleIndex), + m_yPosRole(noRoleIndex), + m_zPosRole(noRoleIndex) { } @@ -33,6 +36,45 @@ SurfaceItemModelHandler::~SurfaceItemModelHandler() { } +void SurfaceItemModelHandler::handleDataChanged(const QModelIndex &topLeft, + const QModelIndex &bottomRight, + const QVector &roles) +{ + // Do nothing if full reset already pending + if (!m_fullReset) { + if (!m_proxy->useModelCategories()) { + // If the data model doesn't directly map rows and columns, we cannot optimize + AbstractItemModelHandler::handleDataChanged(topLeft, bottomRight, roles); + } else { + int startRow = qMin(topLeft.row(), bottomRight.row()); + int endRow = qMax(topLeft.row(), bottomRight.row()); + int startCol = qMin(topLeft.column(), bottomRight.column()); + int endCol = qMax(topLeft.column(), bottomRight.column()); + + for (int i = startRow; i <= endRow; i++) { + for (int j = startCol; j <= endCol; j++) { + QSurfaceDataItem item; + const QSurfaceDataItem *oldItem = m_proxy->itemAt(i, j); + float xPos; + float zPos; + if (m_xPosRole != noRoleIndex) + xPos = m_itemModel->index(i, j).data(m_xPosRole).toFloat(); + else + xPos = oldItem->x(); + if (m_zPosRole != noRoleIndex) + zPos = m_itemModel->index(i, j).data(m_zPosRole).toFloat(); + else + zPos = oldItem->z(); + item.setPosition(QVector3D(xPos, + m_itemModel->index(i, j).data(m_yPosRole).toFloat(), + zPos)); + m_proxy->setItem(i, j, item); + } + } + } + } +} + // Resolve entire item model into QSurfaceDataArray. void SurfaceItemModelHandler::resolveModel() { @@ -52,9 +94,9 @@ void SurfaceItemModelHandler::resolveModel() QHash roleHash = m_itemModel->roleNames(); // Default to display role if no mapping - int xPosRole = roleHash.key(m_proxy->xPosRole().toLatin1(), noRoleIndex); - int yPosRole = roleHash.key(m_proxy->yPosRole().toLatin1(), Qt::DisplayRole); - int zPosRole = roleHash.key(m_proxy->zPosRole().toLatin1(), noRoleIndex); + m_xPosRole = roleHash.key(m_proxy->xPosRole().toLatin1(), noRoleIndex); + m_yPosRole = roleHash.key(m_proxy->yPosRole().toLatin1(), Qt::DisplayRole); + m_zPosRole = roleHash.key(m_proxy->zPosRole().toLatin1(), noRoleIndex); int rowCount = m_itemModel->rowCount(); int columnCount = m_itemModel->columnCount(); @@ -70,10 +112,10 @@ void SurfaceItemModelHandler::resolveModel() for (int i = 0; i < rowCount; i++) { QSurfaceDataRow &newProxyRow = *m_proxyArray->at(i); for (int j = 0; j < columnCount; j++) { - float xPos = j; - float zPos = i; - if (xPosRole != noRoleIndex) { - xPos = m_itemModel->index(i, j).data(xPosRole).toFloat(); + float xPos = float(j); + float zPos = float(i); + if (m_xPosRole != noRoleIndex) { + xPos = m_itemModel->index(i, j).data(m_xPosRole).toFloat(); } else { QString header = m_itemModel->headerData(j, Qt::Horizontal).toString(); bool ok = false; @@ -82,8 +124,8 @@ void SurfaceItemModelHandler::resolveModel() xPos = headerValue; } - if (zPosRole != noRoleIndex) { - zPos = m_itemModel->index(i, j).data(zPosRole).toFloat(); + if (m_zPosRole != noRoleIndex) { + zPos = m_itemModel->index(i, j).data(m_zPosRole).toFloat(); } else { QString header = m_itemModel->headerData(i, Qt::Vertical).toString(); bool ok = false; @@ -94,17 +136,17 @@ void SurfaceItemModelHandler::resolveModel() newProxyRow[j].setPosition( QVector3D(xPos, - m_itemModel->index(i, j).data(yPosRole).toFloat(), + m_itemModel->index(i, j).data(m_yPosRole).toFloat(), zPos)); } } } else { int rowRole = roleHash.key(m_proxy->rowRole().toLatin1()); int columnRole = roleHash.key(m_proxy->columnRole().toLatin1()); - if (xPosRole == noRoleIndex) - xPosRole = columnRole; - if (zPosRole == noRoleIndex) - zPosRole = rowRole; + if (m_xPosRole == noRoleIndex) + m_xPosRole = columnRole; + if (m_zPosRole == noRoleIndex) + m_zPosRole = rowRole; bool generateRows = m_proxy->autoRowCategories(); bool generateColumns = m_proxy->autoColumnCategories(); @@ -124,9 +166,9 @@ void SurfaceItemModelHandler::resolveModel() QModelIndex index = m_itemModel->index(i, j); QString rowRoleStr = index.data(rowRole).toString(); QString columnRoleStr = index.data(columnRole).toString(); - QVector3D itemPos(index.data(xPosRole).toReal(), - index.data(yPosRole).toReal(), - index.data(zPosRole).toReal()); + QVector3D itemPos(index.data(m_xPosRole).toFloat(), + index.data(m_yPosRole).toFloat(), + index.data(m_zPosRole).toFloat()); itemValueMap[rowRoleStr][columnRoleStr] = itemPos; if (generateRows && !rowListHash.value(rowRoleStr, false)) { rowListHash.insert(rowRoleStr, true); diff --git a/src/datavisualization/data/surfaceitemmodelhandler_p.h b/src/datavisualization/data/surfaceitemmodelhandler_p.h index ae426433..dbed0a60 100644 --- a/src/datavisualization/data/surfaceitemmodelhandler_p.h +++ b/src/datavisualization/data/surfaceitemmodelhandler_p.h @@ -41,11 +41,18 @@ public: SurfaceItemModelHandler(QItemModelSurfaceDataProxy *proxy, QObject *parent = 0); virtual ~SurfaceItemModelHandler(); +public slots: + virtual void handleDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, + const QVector &roles = QVector ()); + protected: void virtual resolveModel(); QItemModelSurfaceDataProxy *m_proxy; // Not owned QSurfaceDataArray *m_proxyArray; // Not owned + int m_xPosRole; + int m_yPosRole; + int m_zPosRole; }; QT_END_NAMESPACE_DATAVISUALIZATION diff --git a/src/datavisualization/engine/bars3dcontroller.cpp b/src/datavisualization/engine/bars3dcontroller.cpp index 99fa8223..35b24218 100644 --- a/src/datavisualization/engine/bars3dcontroller.cpp +++ b/src/datavisualization/engine/bars3dcontroller.cpp @@ -148,7 +148,6 @@ void Bars3DController::handleRowsChanged(int startIndex, int count) if (!oldChangeCount) m_changedRows.reserve(count); - int selectedRow = m_selectedBar.x(); for (int i = 0; i < count; i++) { bool newItem = true; int candidate = startIndex + i; @@ -162,7 +161,7 @@ void Bars3DController::handleRowsChanged(int startIndex, int count) if (newItem) { ChangeRow newChangeItem = {series, candidate}; m_changedRows.append(newChangeItem); - if (series == m_selectedBarSeries && selectedRow == candidate) + if (series == m_selectedBarSeries && m_selectedBar.x() == candidate) series->d_ptr->markItemLabelDirty(); } } @@ -516,7 +515,8 @@ void Bars3DController::setSelectedBar(const QPoint &position, QBar3DSeries *seri adjustSelectionPosition(pos, series); if (selectionMode().testFlag(QAbstract3DGraph::SelectionSlice)) { - // If the selected bar is outside data window, or there is no visible selected bar, disable slicing + // If the selected bar is outside data window, or there is no visible selected bar, + // disable slicing. if (pos.x() < m_axisZ->min() || pos.x() > m_axisZ->max() || pos.y() < m_axisX->min() || pos.y() > m_axisX->max() || !series->isVisible()) { diff --git a/src/datavisualization/engine/bars3drenderer.cpp b/src/datavisualization/engine/bars3drenderer.cpp index ab7bb4ca..f93d20b6 100644 --- a/src/datavisualization/engine/bars3drenderer.cpp +++ b/src/datavisualization/engine/bars3drenderer.cpp @@ -337,8 +337,14 @@ void Bars3DRenderer::updateRows(const QVector &rows if (!cache->isVisible() && !cache->dataDirty()) cache->setDataDirty(true); } - if (cache->isVisible()) + if (cache->isVisible()) { updateRenderRow(dataArray->at(row), cache->renderArray()[row - minRow]); + if (m_cachedIsSlicingActivated + && cache == m_selectedSeriesCache + && m_selectedBarPos.x() == row) { + m_selectionDirty = true; // Need to update slice view + } + } } } @@ -370,6 +376,11 @@ void Bars3DRenderer::updateItems(const QVector &it if (cache->isVisible()) { updateRenderItem(dataArray->at(row)->at(col), cache->renderArray()[row - minRow][col - minCol]); + if (m_cachedIsSlicingActivated + && cache == m_selectedSeriesCache + && m_selectedBarPos == QPoint(row, col)) { + m_selectionDirty = true; // Need to update slice view + } } } } -- cgit v1.2.3