support multi-select for local decks in deck storage tab (#5314)

* allow multi-select

* support multi upload

* support multi open deck

* support multi delete deck
This commit is contained in:
RickyRister 2024-12-23 17:41:15 -08:00 committed by GitHub
parent 0234a70bfd
commit a40d8092ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 17 deletions

View file

@ -41,6 +41,7 @@ TabDeckStorage::TabDeckStorage(TabSupervisor *_tabSupervisor, AbstractClient *_c
localDirView->setColumnHidden(1, true);
localDirView->setRootIndex(localDirModel->index(localDirModel->rootPath(), 0));
localDirView->setSortingEnabled(true);
localDirView->setSelectionMode(QAbstractItemView::ExtendedSelection);
localDirView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
localDirView->header()->setSortIndicator(0, Qt::AscendingOrder);
@ -149,30 +150,45 @@ QString TabDeckStorage::getTargetPath() const
void TabDeckStorage::actOpenLocalDeck()
{
QModelIndex curLeft = localDirView->selectionModel()->currentIndex();
if (localDirModel->isDir(curLeft))
return;
QString filePath = localDirModel->filePath(curLeft);
QModelIndexList curLefts = localDirView->selectionModel()->selectedRows();
for (const auto &curLeft : curLefts) {
if (localDirModel->isDir(curLeft))
return;
QString filePath = localDirModel->filePath(curLeft);
DeckLoader deckLoader;
if (!deckLoader.loadFromFile(filePath, DeckLoader::CockatriceFormat))
return;
DeckLoader deckLoader;
if (!deckLoader.loadFromFile(filePath, DeckLoader::CockatriceFormat))
return;
emit openDeckEditor(&deckLoader);
emit openDeckEditor(&deckLoader);
}
}
void TabDeckStorage::actUpload()
{
QModelIndex curLeft = localDirView->selectionModel()->currentIndex();
if (localDirModel->isDir(curLeft))
QModelIndexList curLefts = localDirView->selectionModel()->selectedRows();
if (curLefts.isEmpty()) {
return;
}
QString targetPath = getTargetPath();
if (targetPath.length() > MAX_NAME_LENGTH) {
qCritical() << "target path to upload to is too long" << targetPath;
return;
}
QString filePath = localDirModel->filePath(curLeft);
for (const auto &curLeft : curLefts) {
if (localDirModel->isDir(curLeft)) {
continue;
}
QString filePath = localDirModel->filePath(curLeft);
uploadDeck(filePath, targetPath);
}
}
void TabDeckStorage::uploadDeck(const QString &filePath, const QString &targetPath)
{
QFile deckFile(filePath);
QFileInfo deckFileInfo(deckFile);
@ -228,16 +244,22 @@ void TabDeckStorage::uploadFinished(const Response &r, const CommandContainer &c
void TabDeckStorage::actDeleteLocalDeck()
{
QModelIndex curLeft = localDirView->selectionModel()->currentIndex();
if (localDirModel->isDir(curLeft))
return;
const QModelIndexList curLefts = localDirView->selectionModel()->selectedRows();
if (QMessageBox::warning(this, tr("Delete local file"),
tr("Are you sure you want to delete \"%1\"?").arg(localDirModel->fileName(curLeft)),
auto isDir = [&](const auto &curLeft) { return localDirModel->isDir(curLeft); };
if (curLefts.isEmpty() || std::all_of(curLefts.begin(), curLefts.end(), isDir)) {
return;
}
if (QMessageBox::warning(this, tr("Delete local file"), tr("Are you sure you want to delete the selected files?"),
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;
localDirModel->remove(curLeft);
for (const auto &curLeft : curLefts) {
if (!localDirModel->isDir(curLeft)) {
localDirModel->remove(curLeft);
}
}
}
void TabDeckStorage::actOpenRemoteDeck()

View file

@ -28,6 +28,9 @@ private:
QAction *aOpenLocalDeck, *aUpload, *aDeleteLocalDeck, *aOpenRemoteDeck, *aDownload, *aNewFolder, *aDeleteRemoteDeck;
QString getTargetPath() const;
void uploadDeck(const QString &filePath, const QString &targetPath);
private slots:
void actOpenLocalDeck();