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 +++++++++++++++++----- 1 file changed, 40 insertions(+), 10 deletions(-) (limited to 'src/datavisualization/data/baritemmodelhandler.cpp') 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)]); } } -- cgit v1.2.3