From 073e4639bf6d19efd55f336ee696463f58299733 Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Thu, 29 Aug 2024 04:15:07 +0300 Subject: [PATCH] Launcher: Support extended selection for directories --- apps/launcher/datafilespage.cpp | 98 ++++++++++++++----------------- apps/launcher/datafilespage.hpp | 4 +- apps/launcher/ui/datafilespage.ui | 3 + 3 files changed, 49 insertions(+), 56 deletions(-) diff --git a/apps/launcher/datafilespage.cpp b/apps/launcher/datafilespage.cpp index 35e743e13f..7c52caabe7 100644 --- a/apps/launcher/datafilespage.cpp +++ b/apps/launcher/datafilespage.cpp @@ -83,6 +83,20 @@ namespace for (const auto& subdir : currentDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) contentSubdirs(subdir.canonicalFilePath(), dirs); } + + QList> sortedSelectedItems(QListWidget* list, bool reverse = false) + { + QList> sortedItems; + for (QListWidgetItem* item : list->selectedItems()) + sortedItems.append(qMakePair(list->row(item), item)); + + if (reverse) + std::sort(sortedItems.begin(), sortedItems.end(), [](auto a, auto b) { return a.first > b.first; }); + else + std::sort(sortedItems.begin(), sortedItems.end(), [](auto a, auto b) { return a.first < b.first; }); + + return sortedItems; + } } namespace Launcher @@ -164,11 +178,15 @@ Launcher::DataFilesPage::DataFilesPage(const Files::ConfigurationManager& cfg, C connect(mCloneProfileDialog->lineEdit(), &LineEdit::textChanged, this, &DataFilesPage::updateCloneProfileOkButton); connect(ui.directoryAddSubdirsButton, &QPushButton::released, this, [this]() { this->addSubdirectories(true); }); connect(ui.directoryInsertButton, &QPushButton::released, this, [this]() { this->addSubdirectories(false); }); - connect(ui.directoryUpButton, &QPushButton::released, this, [this]() { this->moveDirectory(-1); }); - connect(ui.directoryDownButton, &QPushButton::released, this, [this]() { this->moveDirectory(1); }); + connect(ui.directoryUpButton, &QPushButton::released, this, + [this]() { this->moveSources(ui.directoryListWidget, -1); }); + connect(ui.directoryDownButton, &QPushButton::released, this, + [this]() { this->moveSources(ui.directoryListWidget, 1); }); connect(ui.directoryRemoveButton, &QPushButton::released, this, &DataFilesPage::removeDirectory); - connect(ui.archiveUpButton, &QPushButton::released, this, [this]() { this->moveArchives(-1); }); - connect(ui.archiveDownButton, &QPushButton::released, this, [this]() { this->moveArchives(1); }); + connect( + ui.archiveUpButton, &QPushButton::released, this, [this]() { this->moveSources(ui.archiveListWidget, -1); }); + connect( + ui.archiveDownButton, &QPushButton::released, this, [this]() { this->moveSources(ui.archiveListWidget, 1); }); connect(ui.directoryListWidget->model(), &QAbstractItemModel::rowsMoved, this, &DataFilesPage::sortDirectories); connect(ui.archiveListWidget->model(), &QAbstractItemModel::rowsMoved, this, &DataFilesPage::sortArchives); @@ -688,7 +706,17 @@ void Launcher::DataFilesPage::updateCloneProfileOkButton(const QString& text) void Launcher::DataFilesPage::addSubdirectories(bool append) { - int selectedRow = append ? ui.directoryListWidget->count() : ui.directoryListWidget->currentRow(); + int selectedRow = -1; + if (append) + { + selectedRow = ui.directoryListWidget->count(); + } + else + { + const QList> sortedItems = sortedSelectedItems(ui.directoryListWidget); + if (!sortedItems.isEmpty()) + selectedRow = sortedItems.first().first; + } if (selectedRow == -1) return; @@ -788,21 +816,6 @@ void Launcher::DataFilesPage::sortArchives() } } -void Launcher::DataFilesPage::moveDirectory(int step) -{ - int selectedRow = ui.directoryListWidget->currentRow(); - int newRow = selectedRow + step; - if (selectedRow == -1 || newRow < 0 || newRow > ui.directoryListWidget->count() - 1) - return; - - if (!(ui.directoryListWidget->item(newRow)->flags() & Qt::ItemIsEnabled)) - return; - - const auto item = ui.directoryListWidget->takeItem(selectedRow); - ui.directoryListWidget->insertItem(newRow, item); - ui.directoryListWidget->setCurrentRow(newRow); -} - void Launcher::DataFilesPage::removeDirectory() { for (const auto& path : ui.directoryListWidget->selectedItems()) @@ -836,44 +849,23 @@ void Launcher::DataFilesPage::slotCheckMultiSelectedItems() setCheckStateForMultiSelectedItems(true); } -void Launcher::DataFilesPage::moveArchives(int step) +void Launcher::DataFilesPage::moveSources(QListWidget* sourceList, int step) { - QList selectedItems = ui.archiveListWidget->selectedItems(); - QList> sortedItems; - - for (QListWidgetItem* selectedItem : selectedItems) - { - int selectedRow = ui.archiveListWidget->row(selectedItem); - sortedItems.append(qMakePair(selectedRow, selectedItem)); - } - - if (step > 0) - { - std::sort(sortedItems.begin(), sortedItems.end(), [](auto a, auto b) { return a.first > b.first; }); - } - else + const QList> sortedItems = sortedSelectedItems(sourceList, step > 0); + for (const auto& i : sortedItems) { - std::sort(sortedItems.begin(), sortedItems.end(), [](auto a, auto b) { return a.first < b.first; }); - } + int selectedRow = sourceList->row(i.second); + int newRow = selectedRow + step; + if (selectedRow == -1 || newRow < 0 || newRow > sourceList->count() - 1) + break; - for (auto i : sortedItems) - { - if (!moveArchive(i.second, step)) + if (!(sourceList->item(newRow)->flags() & Qt::ItemIsEnabled)) break; - } -} -bool Launcher::DataFilesPage::moveArchive(QListWidgetItem* listItem, int step) -{ - int selectedRow = ui.archiveListWidget->row(listItem); - int newRow = selectedRow + step; - if (selectedRow == -1 || newRow < 0 || newRow > ui.archiveListWidget->count() - 1) - return false; - - QListWidgetItem* item = ui.archiveListWidget->takeItem(selectedRow); - ui.archiveListWidget->insertItem(newRow, item); - ui.archiveListWidget->setCurrentRow(newRow); - return true; + const auto item = sourceList->takeItem(selectedRow); + sourceList->insertItem(newRow, item); + sourceList->setCurrentRow(newRow); + } } void Launcher::DataFilesPage::addArchive(const QString& name, Qt::CheckState selected, int row) diff --git a/apps/launcher/datafilespage.hpp b/apps/launcher/datafilespage.hpp index 1b92354dab..7e347c0844 100644 --- a/apps/launcher/datafilespage.hpp +++ b/apps/launcher/datafilespage.hpp @@ -76,8 +76,7 @@ namespace Launcher void sortDirectories(); void sortArchives(); void removeDirectory(); - void moveArchives(int step); - void moveDirectory(int step); + void moveSources(QListWidget* sourceList, int step); void slotShowArchiveContextMenu(const QPoint& pos); void slotCheckMultiSelectedItems(); @@ -128,7 +127,6 @@ namespace Launcher void addArchive(const QString& name, Qt::CheckState selected, int row = -1); void addArchivesFromDir(const QString& dir); - bool moveArchive(QListWidgetItem* listItem, int step); void buildView(); void buildArchiveContextMenu(); void setCheckStateForMultiSelectedItems(bool checked); diff --git a/apps/launcher/ui/datafilespage.ui b/apps/launcher/ui/datafilespage.ui index b212b85227..1f537b1cc8 100644 --- a/apps/launcher/ui/datafilespage.ui +++ b/apps/launcher/ui/datafilespage.ui @@ -55,6 +55,9 @@ QAbstractItemView::InternalMove + + QAbstractItemView::ExtendedSelection +