From ea51795e8c571513e1ff583350ab8649bc857fc2 Mon Sep 17 00:00:00 2001 From: Fushan Wen Date: Mon, 12 Sep 2022 13:32:20 +0800 Subject: [PATCH] libtaskmanager: consolidate group parent adjacent to the moved task If the task exchanges position with a group parent, the group parent also needs to be consolidated. Test: 1. Open many Konsole windows 2. Open Firefox (One window) 3. Now the task order is [Firefox] [Konsole Group Parent] 4. Move Firefox to the right of Konsole group parent. 5. Open group dialog of Konsole group parent 6. Move the first Konsole window down to the second position 7. Before this change, Firefox and Konsole group parent will exchange positions, which is incorrect. After this change, the Konsole task in the group dialog can be moved as expected. --- libtaskmanager/tasksmodel.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/libtaskmanager/tasksmodel.cpp b/libtaskmanager/tasksmodel.cpp index 3c5aff484..06697abef 100644 --- a/libtaskmanager/tasksmodel.cpp +++ b/libtaskmanager/tasksmodel.cpp @@ -1698,6 +1698,17 @@ bool TasksModel::move(int row, int newPos, const QModelIndex &parent) // Translate to sort map indices. const QModelIndex &groupingRowIndex = mapToSource(index(row, 0, parent)); const QModelIndex &preFilterRowIndex = d->preFilterIndex(groupingRowIndex); + + const bool groupNotDisabled = !parent.isValid() && groupMode() != GroupDisabled; + QModelIndex adjacentGroupingRowIndex; // Also consolidate the adjacent group parent + if (groupNotDisabled) { + if (newPos > row && row + 1 < rowCount(parent)) { + adjacentGroupingRowIndex = mapToSource(index(row + 1, 0, parent) /* task on the right */); + } else if (newPos < row && row - 1 >= 0) { + adjacentGroupingRowIndex = mapToSource(index(row - 1, 0, parent) /* task on the left */); + } + } + row = d->sortedPreFilterRows.indexOf(preFilterRowIndex.row()); newPos = d->sortedPreFilterRows.indexOf(d->preFilterIndex(mapToSource(index(newPos, 0, parent))).row()); @@ -1705,8 +1716,16 @@ bool TasksModel::move(int row, int newPos, const QModelIndex &parent) d->sortedPreFilterRows.move(row, newPos); // If we moved a group parent, consolidate sort map for children. - if (!parent.isValid() && groupMode() != GroupDisabled && d->groupingProxyModel->rowCount(groupingRowIndex)) { - d->consolidateManualSortMapForGroup(groupingRowIndex); + if (groupNotDisabled) { + if (d->groupingProxyModel->rowCount(groupingRowIndex)) { + d->consolidateManualSortMapForGroup(groupingRowIndex); + } + // Special case: Before moving, the task at newPos is a group parent + // Before moving: [Task] [Group parent] + // After moving: [Group parent (not consolidated yet)] [Task] + if (adjacentGroupingRowIndex.isValid() && d->groupingProxyModel->rowCount(adjacentGroupingRowIndex)) { + d->consolidateManualSortMapForGroup(adjacentGroupingRowIndex); + } } endMoveRows();