[network/ktorrent] plugins/downloadorder: Improved DownloadOrderModel

Jack Hill <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 2502a87e636938927199bf3e30708ae88d81e682 by Jack Hill.
Committed on 14/08/2026 at 21:01.
Pushed by jackh into branch 'master'.

Improved DownloadOrderModel

- Use checkIndex for improved index bounds checking
- Use dataChanged signals when searching for a torrent instead of
  resetting the model.
- Use model move functions instead of dataChanged. This means the
  selection model now gets updated automatically.
- Use layoutChanged signals when sorting model.

M  +13   -39   plugins/downloadorder/downloadorderdialog.cpp
M  +1    -1    plugins/downloadorder/downloadorderdialog.h
M  +19   -19   plugins/downloadorder/downloadordermodel.cpp

https://invent.kde.org/network/ktorrent/-/commit/2502a87e636938927199bf3e30708ae88d81e682

diff --git a/plugins/downloadorder/downloadorderdialog.cpp b/plugins/downloadorder/downloadorderdialog.cpp
index 9abd20c21..8e8e2f6f8 100644
--- a/plugins/downloadorder/downloadorderdialog.cpp
+++ b/plugins/downloadorder/downloadorderdialog.cpp
@@ -64,7 +64,7 @@ DownloadOrderDialog::DownloadOrderDialog(DownloadOrderPlugin *plugin, bt::Torren
     QSize s = KSharedConfig::openConfig()->group(QStringLiteral("DownloadOrderDialog")).readEntry("size", size());
     resize(s);
 
-    connect(m_order->selectionModel(), &QItemSelectionModel::selectionChanged, this, &DownloadOrderDialog::itemSelectionChanged);
+    connect(m_order->selectionModel(), &QItemSelectionModel::selectionChanged, this, &DownloadOrderDialog::updateMoveActions);
     connect(m_custom_order_enabled, &QCheckBox::toggled, this, &DownloadOrderDialog::customOrderEnableToggled);
     connect(m_search_files, &QLineEdit::textChanged, this, &DownloadOrderDialog::search);
 
@@ -110,14 +110,8 @@ void DownloadOrderDialog::moveUp()
     const auto top_index = *std::min_element(idx.cbegin(), idx.cend(), [](const auto &lhs, const auto &rhs) -> bool {
         return lhs.row() < rhs.row();
     });
-    const auto bottom_index = *std::max_element(idx.cbegin(), idx.cend(), [](const auto &lhs, const auto &rhs) -> bool {
-        return lhs.row() < rhs.row();
-    });
     model->moveUp(top_index.row(), idx.count());
-    if (top_index.row() > 0) {
-        QItemSelection sel(model->index(top_index.row() - 1), model->index(bottom_index.row() - 1));
-        m_order->selectionModel()->select(sel, QItemSelectionModel::ClearAndSelect);
-    }
+    updateMoveActions();
 }
 
 void DownloadOrderDialog::moveTop()
@@ -128,10 +122,7 @@ void DownloadOrderDialog::moveTop()
         return lhs.row() < rhs.row();
     });
     model->moveTop(top_index.row(), idx.count());
-    if (top_index.row() > 0) {
-        QItemSelection sel(model->index(0), model->index(idx.count() - 1));
-        m_order->selectionModel()->select(sel, QItemSelectionModel::ClearAndSelect);
-    }
+    updateMoveActions();
 }
 
 void DownloadOrderDialog::moveDown()
@@ -141,14 +132,8 @@ void DownloadOrderDialog::moveDown()
     const auto top_index = *std::min_element(idx.cbegin(), idx.cend(), [](const auto &lhs, const auto &rhs) -> bool {
         return lhs.row() < rhs.row();
     });
-    const auto bottom_index = *std::max_element(idx.cbegin(), idx.cend(), [](const auto &lhs, const auto &rhs) -> bool {
-        return lhs.row() < rhs.row();
-    });
     model->moveDown(top_index.row(), idx.count());
-    if (bottom_index.row() < (int)tor->getNumFiles() - 1) {
-        QItemSelection sel(model->index(top_index.row() + 1), model->index(bottom_index.row() + 1));
-        m_order->selectionModel()->select(sel, QItemSelectionModel::ClearAndSelect);
-    }
+    updateMoveActions();
 }
 
 void DownloadOrderDialog::moveBottom()
@@ -159,28 +144,17 @@ void DownloadOrderDialog::moveBottom()
         return lhs.row() < rhs.row();
     });
     model->moveBottom(top_index.row(), idx.count());
-    if (top_index.row() < (int)tor->getNumFiles() - 1) {
-        QItemSelection sel(model->index(tor->getNumFiles() - idx.size()), model->index(tor->getNumFiles() - 1));
-        m_order->selectionModel()->select(sel, QItemSelectionModel::ClearAndSelect);
-    }
+    updateMoveActions();
 }
 
-void DownloadOrderDialog::itemSelectionChanged(const QItemSelection &new_sel, const QItemSelection &old_sel)
+void DownloadOrderDialog::updateMoveActions()
 {
-    Q_UNUSED(old_sel);
-    if (new_sel.empty()) {
-        m_move_down->setEnabled(false);
-        m_move_up->setEnabled(false);
-        m_move_top->setEnabled(false);
-        m_move_down->setEnabled(false);
-    } else {
-        bool up_ok = new_sel.front().topLeft().row() > 0;
-        bool down_ok = new_sel.back().bottomRight().row() != (int)tor->getNumFiles() - 1;
-        m_move_up->setEnabled(up_ok);
-        m_move_top->setEnabled(up_ok);
-        m_move_down->setEnabled(down_ok);
-        m_move_bottom->setEnabled(down_ok);
-    }
+    const bool up_ok = !m_order->selectionModel()->isRowSelected(0);
+    const bool down_ok = !m_order->selectionModel()->isRowSelected(tor->getNumFiles() - 1);
+    m_move_up->setEnabled(up_ok);
+    m_move_top->setEnabled(up_ok);
+    m_move_down->setEnabled(down_ok);
+    m_move_bottom->setEnabled(down_ok);
 }
 
 void DownloadOrderDialog::customOrderEnableToggled(bool on)
@@ -193,7 +167,7 @@ void DownloadOrderDialog::customOrderEnableToggled(bool on)
         m_move_top->setEnabled(false);
         m_move_down->setEnabled(false);
     } else {
-        itemSelectionChanged(m_order->selectionModel()->selection(), QItemSelection());
+        updateMoveActions();
     }
 }
 
diff --git a/plugins/downloadorder/downloadorderdialog.h b/plugins/downloadorder/downloadorderdialog.h
index 7ee34e5ec..d69363729 100644
--- a/plugins/downloadorder/downloadorderdialog.h
+++ b/plugins/downloadorder/downloadorderdialog.h
@@ -36,7 +36,7 @@ private Q_SLOTS:
     void moveDown();
     void moveTop();
     void moveBottom();
-    void itemSelectionChanged(const QItemSelection &new_sel, const QItemSelection &old_sel);
+    void updateMoveActions();
     void customOrderEnableToggled(bool on);
     void search(const QString &text);
 
diff --git a/plugins/downloadorder/downloadordermodel.cpp b/plugins/downloadorder/downloadordermodel.cpp
index 40b2b4b95..46a5655a3 100644
--- a/plugins/downloadorder/downloadordermodel.cpp
+++ b/plugins/downloadorder/downloadordermodel.cpp
@@ -48,7 +48,7 @@ int DownloadOrderModel::rowCount(const QModelIndex &parent) const
 
 QVariant DownloadOrderModel::data(const QModelIndex &index, int role) const
 {
-    if (!index.isValid()) {
+    if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) {
         return QVariant();
     }
 
@@ -76,31 +76,29 @@ QVariant DownloadOrderModel::data(const QModelIndex &index, int role) const
 
 QModelIndex DownloadOrderModel::find(const QString &text)
 {
-    beginResetModel();
     current_search_text = text;
+    Q_EMIT dataChanged(index(0), index(rowCount({}) - 1), {Qt::FontRole});
+
     for (Uint32 i = 0; i < tor->getNumFiles(); i++) {
         if (tor->getTorrentFile(i).getUserModifiedPath().contains(current_search_text, Qt::CaseInsensitive)) {
-            endResetModel();
             return index(i);
         }
     }
 
-    endResetModel();
     return QModelIndex();
 }
 
 void DownloadOrderModel::clearHighLights()
 {
-    beginResetModel();
     current_search_text.clear();
-    endResetModel();
+    Q_EMIT dataChanged(index(0, 0), index(rowCount({}) - 1), {Qt::FontRole});
 }
 
 Qt::ItemFlags DownloadOrderModel::flags(const QModelIndex &index) const
 {
     Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
 
-    if (index.isValid()) {
+    if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) {
         return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
     } else {
         return Qt::ItemIsDropEnabled | defaultFlags;
@@ -161,6 +159,7 @@ bool DownloadOrderModel::dropMimeData(const QMimeData *data, Qt::DropAction acti
     QList<Uint32> files;
     in >> files;
 
+    Q_EMIT layoutAboutToBeChanged({}, LayoutChangeHint::VerticalSortHint);
     // remove all files from order which are in the dragged list
     int r = 0;
     for (QList<Uint32>::iterator i = order.begin(); i != order.end();) {
@@ -182,6 +181,7 @@ bool DownloadOrderModel::dropMimeData(const QMimeData *data, Qt::DropAction acti
         order.insert(begin_row, file);
         begin_row++;
     }
+    Q_EMIT layoutChanged();
     return true;
 }
 
@@ -191,11 +191,11 @@ void DownloadOrderModel::moveUp(int row, int count)
         return;
     }
 
+    beginMoveRows({}, row, row + count - 1, {}, row - 1);
     for (int i = 0; i < count; i++) {
         order.swapItemsAt(row + i, row + i - 1);
     }
-
-    Q_EMIT dataChanged(createIndex(row - 1, 0), createIndex(row + count, 0));
+    endMoveRows();
 }
 
 void DownloadOrderModel::moveTop(int row, int count)
@@ -204,14 +204,14 @@ void DownloadOrderModel::moveTop(int row, int count)
         return;
     }
 
+    beginMoveRows({}, row, row + count - 1, {}, 0);
     QList<Uint32> tmp;
     for (int i = 0; i < count; i++) {
         tmp.append(order.takeAt(row));
     }
 
-    beginResetModel();
     order = tmp + order;
-    endResetModel();
+    endMoveRows();
 }
 
 void DownloadOrderModel::moveDown(int row, int count)
@@ -220,11 +220,11 @@ void DownloadOrderModel::moveDown(int row, int count)
         return;
     }
 
+    beginMoveRows({}, row, row + count - 1, {}, row + count + 1);
     for (int i = count - 1; i >= 0; i--) {
         order.swapItemsAt(row + i, row + i + 1);
     }
-
-    Q_EMIT dataChanged(createIndex(row, 0), createIndex(row + count + 1, 0));
+    endMoveRows();
 }
 
 void DownloadOrderModel::moveBottom(int row, int count)
@@ -233,14 +233,14 @@ void DownloadOrderModel::moveBottom(int row, int count)
         return;
     }
 
+    beginMoveRows({}, row, row + count - 1, {}, rowCount({}));
     QList<Uint32> tmp;
     for (int i = 0; i < count; i++) {
         tmp.append(order.takeAt(row));
     }
 
-    beginResetModel();
     order = order + tmp;
-    endResetModel();
+    endMoveRows();
 }
 
 struct NameCompare {
@@ -259,9 +259,9 @@ struct NameCompare {
 
 void DownloadOrderModel::sortByName()
 {
-    beginResetModel();
+    Q_EMIT layoutAboutToBeChanged({}, LayoutChangeHint::VerticalSortHint);
     std::sort(order.begin(), order.end(), NameCompare(tor));
-    endResetModel();
+    Q_EMIT layoutChanged();
 }
 
 struct AlbumTrackCompare {
@@ -309,9 +309,9 @@ struct AlbumTrackCompare {
 
 void DownloadOrderModel::sortByAlbumTrackOrder()
 {
-    beginResetModel();
+    Q_EMIT layoutAboutToBeChanged({}, LayoutChangeHint::VerticalSortHint);
     std::sort(order.begin(), order.end(), AlbumTrackCompare(tor));
-    endResetModel();
+    Q_EMIT layoutChanged();
 }
 
 struct SeasonEpisodeCompare {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.