Launcher: Support extended selection for directories

pull/3236/head
Alexei Kotov 4 months ago
parent 5711a395af
commit 073e4639bf

@ -83,6 +83,20 @@ namespace
for (const auto& subdir : currentDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot))
contentSubdirs(subdir.canonicalFilePath(), dirs);
}
QList<QPair<int, QListWidgetItem*>> sortedSelectedItems(QListWidget* list, bool reverse = false)
{
QList<QPair<int, QListWidgetItem*>> 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<QPair<int, QListWidgetItem*>> 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<QListWidgetItem*> selectedItems = ui.archiveListWidget->selectedItems();
QList<QPair<int, QListWidgetItem*>> sortedItems;
for (QListWidgetItem* selectedItem : selectedItems)
{
int selectedRow = ui.archiveListWidget->row(selectedItem);
sortedItems.append(qMakePair(selectedRow, selectedItem));
}
if (step > 0)
const QList<QPair<int, QListWidgetItem*>> 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; });
}
else
{
std::sort(sortedItems.begin(), sortedItems.end(), [](auto a, auto b) { return a.first < b.first; });
}
for (auto i : sortedItems)
{
if (!moveArchive(i.second, step))
int selectedRow = sourceList->row(i.second);
int newRow = selectedRow + step;
if (selectedRow == -1 || newRow < 0 || newRow > sourceList->count() - 1)
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;
if (!(sourceList->item(newRow)->flags() & Qt::ItemIsEnabled))
break;
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)

@ -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);

@ -55,6 +55,9 @@
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
</widget>
</item>
<item row="0" column="1">

Loading…
Cancel
Save