Apply bulk changes to correct content widget items

PR #21006.
Closes #21001.
This commit is contained in:
Vladimir Golovnev 2024-06-29 21:57:59 +03:00 committed by Vladimir Golovnev (Glassez)
parent efa517ea90
commit 92db0170d5
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7

View file

@ -56,6 +56,19 @@
#include "gui/macutilities.h" #include "gui/macutilities.h"
#endif #endif
namespace
{
QList<QPersistentModelIndex> toPersistentIndexes(const QModelIndexList &indexes)
{
QList<QPersistentModelIndex> persistentIndexes;
persistentIndexes.reserve(indexes.size());
for (const QModelIndex &index : indexes)
persistentIndexes.append(index);
return persistentIndexes;
}
}
TorrentContentWidget::TorrentContentWidget(QWidget *parent) TorrentContentWidget::TorrentContentWidget(QWidget *parent)
: QTreeView(parent) : QTreeView(parent)
{ {
@ -219,9 +232,9 @@ void TorrentContentWidget::keyPressEvent(QKeyEvent *event)
const Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked) const Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked)
? Qt::Unchecked : Qt::Checked; ? Qt::Unchecked : Qt::Checked;
const QModelIndexList selection = selectionModel()->selectedRows(TorrentContentModelItem::COL_NAME); const QList<QPersistentModelIndex> selection = toPersistentIndexes(selectionModel()->selectedRows(TorrentContentModelItem::COL_NAME));
for (const QModelIndex &index : selection) for (const QPersistentModelIndex &index : selection)
model()->setData(index, state, Qt::CheckStateRole); model()->setData(index, state, Qt::CheckStateRole);
} }
@ -248,10 +261,10 @@ void TorrentContentWidget::renameSelectedFile()
void TorrentContentWidget::applyPriorities(const BitTorrent::DownloadPriority priority) void TorrentContentWidget::applyPriorities(const BitTorrent::DownloadPriority priority)
{ {
const QModelIndexList selectedRows = selectionModel()->selectedRows(0); const QList<QPersistentModelIndex> selectedRows = toPersistentIndexes(selectionModel()->selectedRows(Priority));
for (const QModelIndex &index : selectedRows) for (const QPersistentModelIndex &index : selectedRows)
{ {
model()->setData(index.sibling(index.row(), Priority), static_cast<int>(priority)); model()->setData(index, static_cast<int>(priority));
} }
} }
@ -261,7 +274,7 @@ void TorrentContentWidget::applyPrioritiesByOrder()
// a download priority that will apply to each item. The number of groups depends on how // a download priority that will apply to each item. The number of groups depends on how
// many "download priority" are available to be assigned // many "download priority" are available to be assigned
const QModelIndexList selectedRows = selectionModel()->selectedRows(0); const QList<QPersistentModelIndex> selectedRows = toPersistentIndexes(selectionModel()->selectedRows(Priority));
const qsizetype priorityGroups = 3; const qsizetype priorityGroups = 3;
const auto priorityGroupSize = std::max<qsizetype>((selectedRows.length() / priorityGroups), 1); const auto priorityGroupSize = std::max<qsizetype>((selectedRows.length() / priorityGroups), 1);
@ -283,8 +296,8 @@ void TorrentContentWidget::applyPrioritiesByOrder()
break; break;
} }
const QModelIndex &index = selectedRows[i]; const QPersistentModelIndex &index = selectedRows[i];
model()->setData(index.sibling(index.row(), Priority), static_cast<int>(priority)); model()->setData(index, static_cast<int>(priority));
} }
} }