From 420042bb5759fecf4f1db7e465ddf3d977633b8a Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Mon, 9 Aug 2021 11:37:45 -0700 Subject: [PATCH] project: added revert function This can be triggered either with File > Revert or by re-opening an already open project Fixes #1710 --- app/core.cpp | 58 ++++++++++++++++++++++++++++-- app/core.h | 7 ++++ app/window/mainwindow/mainmenu.cpp | 3 ++ app/window/mainwindow/mainmenu.h | 1 + 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/app/core.cpp b/app/core.cpp index dc41884a8..290393867 100644 --- a/app/core.cpp +++ b/app/core.cpp @@ -833,6 +833,42 @@ void Core::SaveUnrecoveredList() } } +bool Core::RevertProjectInternal(Project *p, bool by_opening_existing) +{ + if (p->filename().isEmpty()) { + QMessageBox::critical(main_window_, tr("Revert"), + tr("This project has not yet been saved, therefore there is no last saved state to revert to.")); + } else { + QString msg; + + if (by_opening_existing) { + msg = tr("The project \"%1\" is already open. By re-opening it, the project will revert to " + "its last saved state. Any unsaved changes will be lost. Do you wish to continue?").arg(p->filename()); + } else { + msg = tr("This will revert the project \"%1\" back to its last saved state. " + "All unsaved changes will be lost. Do you wish to continue?").arg(p->name()); + } + + if (QMessageBox::question(main_window_, tr("Revert"), msg, QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok) { + // Copy filename because CloseProject is going to delete `p` + QString filename = p->filename(); + + // Close project without prompting to save it + CloseProjectBehavior b = kCloseProjectDontSave; + CloseProject(p, false, b); + + // NOTE: `p` will be deleted now, so don't try accessing it + + // Re-open project at the filename + OpenProjectInternal(filename); + + return true; + } + } + + return false; +} + void Core::SaveAutorecovery() { if (Config::Current()[QStringLiteral("AutorecoveryEnabled")].toBool()) { @@ -1009,6 +1045,15 @@ bool Core::SaveAllProjects() return true; } +void Core::RevertActiveProject() +{ + Project *p = GetActiveProject(); + + if (p) { + RevertProjectInternal(p, false); + } +} + bool Core::CloseActiveProject() { return CloseProject(GetActiveProject(), true); @@ -1194,9 +1239,18 @@ void Core::OpenProjectInternal(const QString &filename, bool recovery_project) { // See if this project is open already foreach (Project* p, open_projects_) { - if (p->filename() == filename) { + // Comparing QFileInfos will handle case insensitivity and both slash directions on platforms + // where this is necessary (not naming any names *cough* Windows) + if (QFileInfo(p->filename()) == QFileInfo(filename)) { // This project is already open - AddOpenProject(p); + bool reverted = RevertProjectInternal(p, true); + + if (!reverted) { + // Calling this will focus attention to the project that the user just tried to re-open + AddOpenProject(p); + } + + // Don't do anything else return; } } diff --git a/app/core.h b/app/core.h index 67fb6a95d..a85a0d17e 100644 --- a/app/core.h +++ b/app/core.h @@ -329,6 +329,11 @@ public slots: */ bool SaveAllProjects(); + /** + * @brief Revert project to last saved state (basically close and open it) + */ + void RevertActiveProject(); + /** * @brief Closes the active project * @@ -506,6 +511,8 @@ private: void SaveUnrecoveredList(); + bool RevertProjectInternal(Project *p, bool by_opening_existing); + /** * @brief Internal main window object */ diff --git a/app/window/mainwindow/mainmenu.cpp b/app/window/mainwindow/mainmenu.cpp index 6b90a62f7..62452ca19 100644 --- a/app/window/mainwindow/mainmenu.cpp +++ b/app/window/mainwindow/mainmenu.cpp @@ -58,6 +58,8 @@ MainMenu::MainMenu(MainWindow *parent) : file_save_as_item_ = file_menu_->AddItem("saveprojas", Core::instance(), &Core::SaveActiveProjectAs, "Ctrl+Shift+S"); file_save_all_item_ = file_menu_->AddItem("saveallproj", Core::instance(), &Core::SaveAllProjects); file_menu_->addSeparator(); + file_revert_item_ = file_menu_->AddItem("revert", Core::instance(), &Core::RevertActiveProject, "Ctrl+F12"); + file_menu_->addSeparator(); file_import_item_ = file_menu_->AddItem("import", Core::instance(), &Core::DialogImportShow, "Ctrl+I"); file_menu_->addSeparator(); file_export_menu_ = new Menu(file_menu_); @@ -679,6 +681,7 @@ void MainMenu::Retranslate() file_open_recent_menu_->setTitle(tr("Open &Recent")); file_open_recent_clear_item_->setText(tr("&Clear Recent List")); file_save_all_item_->setText(tr("Sa&ve All Projects")); + file_revert_item_->setText(tr("Revert")); file_import_item_->setText(tr("&Import...")); file_export_menu_->setTitle(tr("&Export")); file_export_media_item_->setText(tr("&Media...")); diff --git a/app/window/mainwindow/mainmenu.h b/app/window/mainwindow/mainmenu.h index ceffb7ac2..ff038e64a 100644 --- a/app/window/mainwindow/mainmenu.h +++ b/app/window/mainwindow/mainmenu.h @@ -203,6 +203,7 @@ private: QAction* file_save_item_; QAction* file_save_as_item_; QAction* file_save_all_item_; + QAction* file_revert_item_; QAction* file_import_item_; Menu* file_export_menu_; QAction* file_export_media_item_;