From 9a81cbf4c0cd766a6053e0f70fc50b01edf7aa3d Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 3 Oct 2022 11:56:48 +0800 Subject: [PATCH] Improve column updates granularity PR #17806. --- src/gui/torrentcontentmodel.cpp | 65 +++++++++++++++++++++++++-------- src/gui/torrentcontentmodel.h | 5 ++- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/src/gui/torrentcontentmodel.cpp b/src/gui/torrentcontentmodel.cpp index ff4d23ce2..de1042259 100644 --- a/src/gui/torrentcontentmodel.cpp +++ b/src/gui/torrentcontentmodel.cpp @@ -220,7 +220,12 @@ void TorrentContentModel::updateFilesProgress(const QVector &fp) // Update folders progress in the tree m_rootItem->recalculateProgress(); m_rootItem->recalculateAvailability(); - notifyModelUpdate(index(0, 0)); + + const QVector columns = + { + {TorrentContentModelItem::COL_PROGRESS, TorrentContentModelItem::COL_PROGRESS} + }; + notifySubtreeUpdated(index(0, 0), columns); } void TorrentContentModel::updateFilesPriorities(const QVector &fprio) @@ -233,7 +238,13 @@ void TorrentContentModel::updateFilesPriorities(const QVectorsetPriority(static_cast(fprio[i])); - notifyModelUpdate(index(0, 0)); + + const QVector columns = + { + {TorrentContentModelItem::COL_NAME, TorrentContentModelItem::COL_NAME}, + {TorrentContentModelItem::COL_PRIO, TorrentContentModelItem::COL_PRIO} + }; + notifySubtreeUpdated(index(0, 0), columns); } void TorrentContentModel::updateFilesAvailability(const QVector &fa) @@ -247,7 +258,12 @@ void TorrentContentModel::updateFilesAvailability(const QVector &fa) m_filesIndex[i]->setAvailability(fa[i]); // Update folders progress in the tree m_rootItem->recalculateProgress(); - notifyModelUpdate(index(0, 0)); + + const QVector columns = + { + {TorrentContentModelItem::COL_AVAILABILITY, TorrentContentModelItem::COL_AVAILABILITY} + }; + notifySubtreeUpdated(index(0, 0), columns); } QVector TorrentContentModel::getFilePriorities() const @@ -297,7 +313,12 @@ bool TorrentContentModel::setData(const QModelIndex &index, const QVariant &valu m_rootItem->recalculateProgress(); m_rootItem->recalculateAvailability(); - notifyModelUpdate(index); + const QVector columns = + { + {TorrentContentModelItem::COL_NAME, TorrentContentModelItem::COL_NAME}, + {TorrentContentModelItem::COL_PRIO, TorrentContentModelItem::COL_PRIO} + }; + notifySubtreeUpdated(index, columns); emit filteredFilesChanged(); return true; @@ -331,7 +352,13 @@ bool TorrentContentModel::setData(const QModelIndex &index, const QVariant &valu { item->setPriority(newPrio); - notifyModelUpdate(index); + const QVector columns = + { + {TorrentContentModelItem::COL_NAME, TorrentContentModelItem::COL_NAME}, + {TorrentContentModelItem::COL_PRIO, TorrentContentModelItem::COL_PRIO} + }; + notifySubtreeUpdated(index, columns); + if ((newPrio == BitTorrent::DownloadPriority::Ignored) || (currentPrio == BitTorrent::DownloadPriority::Ignored)) { @@ -564,20 +591,22 @@ void TorrentContentModel::setupModelData(const BitTorrent::AbstractFileStorage & emit layoutChanged(); } -void TorrentContentModel::notifyModelUpdate(const QModelIndex &index) +void TorrentContentModel::notifySubtreeUpdated(const QModelIndex &index, const QVector &columns) { - Q_ASSERT(index.isValid()); + // For best performance, `columns` entries should be arranged from left to right - const int lastColumnIndex = columnCount(index) - 1; + Q_ASSERT(index.isValid()); // emit itself - emit dataChanged(index.siblingAtColumn(0), index.siblingAtColumn(lastColumnIndex)); + for (const ColumnInterval &column : columns) + emit dataChanged(index.siblingAtColumn(column.first()), index.siblingAtColumn(column.last())); // propagate up the model QModelIndex parentIndex = parent(index); while (parentIndex.isValid()) { - emit dataChanged(parentIndex.siblingAtColumn(0), parentIndex.siblingAtColumn(lastColumnIndex)); + for (const ColumnInterval &column : columns) + emit dataChanged(parentIndex.siblingAtColumn(column.first()), parentIndex.siblingAtColumn(column.last())); parentIndex = parent(parentIndex); } @@ -592,19 +621,23 @@ void TorrentContentModel::notifyModelUpdate(const QModelIndex &index) const QModelIndex parent = parentIndexes.takeLast(); const int childCount = rowCount(parent); - const QModelIndex childTopLeft = this->index(0, 0, parent); - const QModelIndex childBottomRight = this->index((childCount - 1), lastColumnIndex, parent); + const QModelIndex child = this->index(0, 0, parent); // emit this generation - emit dataChanged(childTopLeft, childBottomRight); + for (const ColumnInterval &column : columns) + { + const QModelIndex childTopLeft = child.siblingAtColumn(column.first()); + const QModelIndex childBottomRight = child.sibling((childCount - 1), column.last()); + emit dataChanged(childTopLeft, childBottomRight); + } // check generations further down parentIndexes.reserve(childCount); for (int i = 0; i < childCount; ++i) { - const QModelIndex child = childTopLeft.siblingAtRow(i); - if (hasChildren(child)) - parentIndexes.push_back(child); + const QModelIndex sibling = child.siblingAtRow(i); + if (hasChildren(sibling)) + parentIndexes.push_back(sibling); } } } diff --git a/src/gui/torrentcontentmodel.h b/src/gui/torrentcontentmodel.h index 6505fc679..481cf2ead 100644 --- a/src/gui/torrentcontentmodel.h +++ b/src/gui/torrentcontentmodel.h @@ -31,6 +31,7 @@ #include #include +#include "base/indexrange.h" #include "torrentcontentmodelitem.h" class QFileIconProvider; @@ -80,7 +81,9 @@ signals: void filteredFilesChanged(); private: - void notifyModelUpdate(const QModelIndex &index); + using ColumnInterval = IndexInterval; + + void notifySubtreeUpdated(const QModelIndex &index, const QVector &columns); TorrentContentModelFolder *m_rootItem = nullptr; QVector m_filesIndex;