From 024bef7ded405e02b442bfe4f5bb99eda79deb52 Mon Sep 17 00:00:00 2001 From: RickyRister <42636155+RickyRister@users.noreply.github.com> Date: Wed, 25 Dec 2024 19:34:24 -0800 Subject: [PATCH] add local rename button to replays tab (#5331) --- cockatrice/src/client/tabs/tab_replays.cpp | 34 ++++++++++++++++++++++ cockatrice/src/client/tabs/tab_replays.h | 3 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/cockatrice/src/client/tabs/tab_replays.cpp b/cockatrice/src/client/tabs/tab_replays.cpp index 954d9934a..b53e68989 100644 --- a/cockatrice/src/client/tabs/tab_replays.cpp +++ b/cockatrice/src/client/tabs/tab_replays.cpp @@ -78,6 +78,9 @@ TabReplays::TabReplays(TabSupervisor *_tabSupervisor, AbstractClient *_client) : aOpenLocalReplay->setIcon(QPixmap("theme:icons/view")); connect(aOpenLocalReplay, SIGNAL(triggered()), this, SLOT(actOpenLocalReplay())); connect(localDirView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(actOpenLocalReplay())); + aRenameLocal = new QAction(this); + aRenameLocal->setIcon(QPixmap("theme:icons/pencil")); + connect(aRenameLocal, &QAction::triggered, this, &TabReplays::actRenameLocal); aNewLocalFolder = new QAction(this); aNewLocalFolder->setIcon(qApp->style()->standardIcon(QStyle::SP_FileDialogNewFolder)); connect(aNewLocalFolder, &QAction::triggered, this, &TabReplays::actNewLocalFolder); @@ -100,6 +103,7 @@ TabReplays::TabReplays(TabSupervisor *_tabSupervisor, AbstractClient *_client) : connect(aDeleteRemoteReplay, SIGNAL(triggered()), this, SLOT(actDeleteRemoteReplay())); leftToolBar->addAction(aOpenLocalReplay); + leftToolBar->addAction(aRenameLocal); leftToolBar->addAction(aNewLocalFolder); leftToolBar->addAction(aDeleteLocalReplay); rightToolBar->addAction(aOpenRemoteReplay); @@ -159,6 +163,36 @@ void TabReplays::actOpenLocalReplay() } } +void TabReplays::actRenameLocal() +{ + QModelIndexList curLefts = localDirView->selectionModel()->selectedRows(); + for (const auto &curLeft : curLefts) { + const QFileInfo info = localDirModel->fileInfo(curLeft); + + const QString oldName = info.baseName(); + const QString title = info.isDir() ? tr("Rename local folder") : tr("Rename local file"); + + bool ok; + QString newName = QInputDialog::getText(this, title, tr("New name:"), QLineEdit::Normal, oldName, &ok); + if (!ok) { // terminate all remaining selections if user cancels + return; + } + if (newName.isEmpty() || oldName == newName) { + continue; + } + + QString newFileName = newName; + if (!info.suffix().isEmpty()) { + newFileName += "." + info.suffix(); + } + const QString newFilePath = QFileInfo(info.dir(), newFileName).filePath(); + + if (!QFile::rename(info.filePath(), newFilePath)) { + QMessageBox::critical(this, tr("Error"), tr("Rename failed")); + } + } +} + void TabReplays::actNewLocalFolder() { QModelIndex curLeft = localDirView->selectionModel()->currentIndex(); diff --git a/cockatrice/src/client/tabs/tab_replays.h b/cockatrice/src/client/tabs/tab_replays.h index cf7a76e4f..565ec8604 100644 --- a/cockatrice/src/client/tabs/tab_replays.h +++ b/cockatrice/src/client/tabs/tab_replays.h @@ -25,13 +25,14 @@ private: RemoteReplayList_TreeWidget *serverDirView; QGroupBox *leftGroupBox, *rightGroupBox; - QAction *aOpenLocalReplay, *aNewLocalFolder, *aDeleteLocalReplay; + QAction *aOpenLocalReplay, *aRenameLocal, *aNewLocalFolder, *aDeleteLocalReplay; QAction *aOpenRemoteReplay, *aDownload, *aKeep, *aDeleteRemoteReplay; void downloadNodeAtIndex(const QModelIndex &curLeft, const QModelIndex &curRight); private slots: void actLocalDoubleClick(const QModelIndex &curLeft); + void actRenameLocal(); void actOpenLocalReplay(); void actNewLocalFolder(); void actDeleteLocalReplay();