From 418edc7471f629b38aa26f3cd4c8301f21dfe44d Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Sat, 29 Jun 2024 21:57:59 +0300 Subject: [PATCH] Apply bulk changes to correct content widget items PR #21006. Closes #21001. --- src/gui/torrentcontentwidget.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/gui/torrentcontentwidget.cpp b/src/gui/torrentcontentwidget.cpp index 6e6e9d051..59c401871 100644 --- a/src/gui/torrentcontentwidget.cpp +++ b/src/gui/torrentcontentwidget.cpp @@ -56,6 +56,19 @@ #include "gui/macutilities.h" #endif +namespace +{ + QList toPersistentIndexes(const QModelIndexList &indexes) + { + QList persistentIndexes; + persistentIndexes.reserve(indexes.size()); + for (const QModelIndex &index : indexes) + persistentIndexes.emplaceBack(index); + + return persistentIndexes; + } +} + TorrentContentWidget::TorrentContentWidget(QWidget *parent) : QTreeView(parent) { @@ -229,9 +242,9 @@ void TorrentContentWidget::keyPressEvent(QKeyEvent *event) const Qt::CheckState state = (static_cast(value.toInt()) == Qt::Checked) ? Qt::Unchecked : Qt::Checked; - const QModelIndexList selection = selectionModel()->selectedRows(TorrentContentModelItem::COL_NAME); + const QList selection = toPersistentIndexes(selectionModel()->selectedRows(TorrentContentModelItem::COL_NAME)); - for (const QModelIndex &index : selection) + for (const QPersistentModelIndex &index : selection) model()->setData(index, state, Qt::CheckStateRole); } @@ -258,10 +271,10 @@ void TorrentContentWidget::renameSelectedFile() void TorrentContentWidget::applyPriorities(const BitTorrent::DownloadPriority priority) { - const QModelIndexList selectedRows = selectionModel()->selectedRows(0); - for (const QModelIndex &index : selectedRows) + const QList selectedRows = toPersistentIndexes(selectionModel()->selectedRows(Priority)); + for (const QPersistentModelIndex &index : selectedRows) { - model()->setData(index.sibling(index.row(), Priority), static_cast(priority)); + model()->setData(index, static_cast(priority)); } } @@ -271,7 +284,7 @@ void TorrentContentWidget::applyPrioritiesByOrder() // a download priority that will apply to each item. The number of groups depends on how // many "download priority" are available to be assigned - const QModelIndexList selectedRows = selectionModel()->selectedRows(0); + const QList selectedRows = toPersistentIndexes(selectionModel()->selectedRows(Priority)); const qsizetype priorityGroups = 3; const auto priorityGroupSize = std::max((selectedRows.length() / priorityGroups), 1); @@ -293,8 +306,8 @@ void TorrentContentWidget::applyPrioritiesByOrder() break; } - const QModelIndex &index = selectedRows[i]; - model()->setData(index.sibling(index.row(), Priority), static_cast(priority)); + const QPersistentModelIndex &index = selectedRows[i]; + model()->setData(index, static_cast(priority)); } }