From e985c9beb8cdb70f1091757db00c2849a6c07202 Mon Sep 17 00:00:00 2001 From: okrc Date: Mon, 15 May 2023 01:07:18 +0800 Subject: [PATCH] fix behaviour on multiple selection --- SandboxiePlus/SandMan/Views/SbieView.cpp | 46 +++++++++++++++--------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/SandboxiePlus/SandMan/Views/SbieView.cpp b/SandboxiePlus/SandMan/Views/SbieView.cpp index e93cc642..6f0cc10a 100644 --- a/SandboxiePlus/SandMan/Views/SbieView.cpp +++ b/SandboxiePlus/SandMan/Views/SbieView.cpp @@ -888,27 +888,39 @@ void CSbieView::OnGroupAction(QAction* Action) return; // todo: fix behaviour on multiple selection - foreach(const QString& Name, GetSelectedGroups(true)) { - bool bFound = false; - retry: - for (auto I = m_Groups.begin(); I != m_Groups.end(); ++I) { - int pos = I->indexOf(Name); + QMap> GroupPositions; + bool bOutBounded = false; + auto FindPosition = [this, Offset, &bOutBounded, &GroupPositions](const QString& Name) -> bool { + foreach(const QString& Group, m_Groups.keys()) { + int pos = m_Groups[Group].indexOf(Name); if (pos != -1) { - if ((Offset < 0 && pos > Offset + 1) || (Offset > 0 && pos < I->count() - Offset)) { - QString Temp = I.value()[pos + Offset]; - I.value()[pos + Offset] = I.value()[pos]; - I.value()[pos] = Temp; - } - else // out of bounds - QApplication::beep(); - bFound = true; - break; + if (pos + Offset >= 0 && pos + Offset < m_Groups[Group].count()) + GroupPositions[Group].append(pos); + else + bOutBounded = true; + return true; } } - if (!bFound) { - bFound = true; + return false; + }; + foreach(const QString& Name, GetSelectedGroups(true)) { + if (!FindPosition(Name)) { m_Groups[""].prepend(Name); - goto retry; + FindPosition(Name); + } + if (bOutBounded) + break; + } + if (bOutBounded) + QApplication::beep(); + else { + foreach(const QString& Group, GroupPositions.keys()) { + std::sort(GroupPositions[Group].begin(), GroupPositions[Group].end(), [Offset](const int& a, const int& b) { + return Offset > 0 && a > b || Offset < 0 && a < b; + }); + foreach(const int ItemIndex, GroupPositions[Group]) { + m_Groups[Group].swapItemsAt(ItemIndex + Offset, ItemIndex); + } } } }