From 6ada347ec46b47ededfb0121dbe98bd6dbb51bad Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 11 Mar 2020 00:20:40 -0500 Subject: [PATCH] [nostalgia/studio] Cleanup save hooks and add unsaved changes marker to tabs --- src/nostalgia/studio/lib/editor.cpp | 8 +++--- src/nostalgia/studio/lib/editor.hpp | 13 +++++----- src/nostalgia/studio/mainwindow.cpp | 40 ++++++++++++++++++++++------- src/nostalgia/studio/mainwindow.hpp | 2 ++ 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/nostalgia/studio/lib/editor.cpp b/src/nostalgia/studio/lib/editor.cpp index 3f91288c..658a69e8 100644 --- a/src/nostalgia/studio/lib/editor.cpp +++ b/src/nostalgia/studio/lib/editor.cpp @@ -13,15 +13,12 @@ namespace nostalgia::studio { Editor::Editor(QWidget *parent): QWidget(parent) { } -void Editor::saveItem() { -} - QUndoStack *Editor::undoStack() { return nullptr; } void Editor::save() { - save(); + saveItem(); setUnsavedChanges(false); } @@ -30,4 +27,7 @@ void Editor::setUnsavedChanges(bool uc) { emit unsavedChangesUpdate(uc); } +void Editor::saveItem() { +} + } diff --git a/src/nostalgia/studio/lib/editor.hpp b/src/nostalgia/studio/lib/editor.hpp index 959b43cd..6795351d 100644 --- a/src/nostalgia/studio/lib/editor.hpp +++ b/src/nostalgia/studio/lib/editor.hpp @@ -18,7 +18,7 @@ namespace nostalgia::studio { class NOSTALGIASTUDIO_EXPORT Editor: public QWidget { Q_OBJECT - private: + private: bool m_unsavedChanges = false; public: @@ -31,11 +31,6 @@ class NOSTALGIASTUDIO_EXPORT Editor: public QWidget { */ virtual QString itemName() = 0; - /** - * Save changes to item being edited. - */ - virtual void saveItem(); - /** * Returns the undo stack holding changes to the item being edited. */ @@ -52,6 +47,12 @@ class NOSTALGIASTUDIO_EXPORT Editor: public QWidget { */ void setUnsavedChanges(bool); + protected: + /** + * Save changes to item being edited. + */ + virtual void saveItem(); + signals: void unsavedChangesUpdate(bool); diff --git a/src/nostalgia/studio/mainwindow.cpp b/src/nostalgia/studio/mainwindow.cpp index c7685c0c..cf48d00f 100644 --- a/src/nostalgia/studio/mainwindow.cpp +++ b/src/nostalgia/studio/mainwindow.cpp @@ -303,7 +303,11 @@ void MainWindow::openProject(QString projectPath) { // reopen tabs auto openTabs = readTabs(); for (auto t : openTabs) { - openFile(t, true); + try { + openFile(t, true); + } catch (ox::Error err) { + oxTrace("nostalgia::studio::MainWindow::openProject") << "Error opening tab:" << err; + } } qInfo() << "Open project:" << projectPath; } @@ -316,7 +320,7 @@ void MainWindow::closeProject() { m_tabs->removeTab(0); delete tab; } - + if (m_ctx.project) { disconnect(m_ctx.project, SIGNAL(updated(QString)), m_oxfsView, SLOT(updateFile(QString))); @@ -471,14 +475,32 @@ void MainWindow::moveTab(int from, int to) { void MainWindow::changeTab(int idx) { auto tab = dynamic_cast(m_tabs->widget(idx)); - disconnect(m_currentEditor, &Editor::unsavedChangesUpdate, m_saveAction, &QAction::setEnabled); - m_currentEditor = tab; - connect(m_currentEditor, &Editor::unsavedChangesUpdate, m_saveAction, &QAction::setEnabled); - if (!tab) { - m_undoGroup.setActiveStack(nullptr); - return; + if (m_currentEditor) { + disconnect(m_currentEditor, &Editor::unsavedChangesUpdate, m_saveAction, &QAction::setEnabled); + disconnect(m_currentEditor, &Editor::unsavedChangesUpdate, this, &MainWindow::markUnsavedChanges); + } + m_currentEditor = tab; + if (m_currentEditor) { + connect(m_currentEditor, &Editor::unsavedChangesUpdate, m_saveAction, &QAction::setEnabled); + connect(m_currentEditor, &Editor::unsavedChangesUpdate, this, &MainWindow::markUnsavedChanges); + m_undoGroup.setActiveStack(tab->undoStack()); + } else { + m_undoGroup.setActiveStack(nullptr); + } +} + +void MainWindow::markUnsavedChanges(bool unsavedChanges) { + auto idx = m_tabs->indexOf(m_currentEditor); + if (idx > -1) { + const auto path = m_currentEditor->itemName(); + const auto lastSlash = path.lastIndexOf('/') + 1; + const auto tabName = path.mid(lastSlash); + if (unsavedChanges) { + m_tabs->setTabText(idx, tabName + "*"); + } else { + m_tabs->setTabText(idx, tabName); + } } - m_undoGroup.setActiveStack(tab->undoStack()); } void MainWindow::showImportWizard() { diff --git a/src/nostalgia/studio/mainwindow.hpp b/src/nostalgia/studio/mainwindow.hpp index b8b032e8..5c11123c 100644 --- a/src/nostalgia/studio/mainwindow.hpp +++ b/src/nostalgia/studio/mainwindow.hpp @@ -150,6 +150,8 @@ class MainWindow: public QMainWindow { void changeTab(int idx); + void markUnsavedChanges(bool); + void showNewWizard(); void showImportWizard();