[nostalgia/studio] Cleanup save hooks and add unsaved changes marker to tabs
This commit is contained in:
		| @@ -13,15 +13,12 @@ namespace nostalgia::studio { | |||||||
| Editor::Editor(QWidget *parent): QWidget(parent) { | Editor::Editor(QWidget *parent): QWidget(parent) { | ||||||
| } | } | ||||||
|  |  | ||||||
| void Editor::saveItem() { |  | ||||||
| } |  | ||||||
|  |  | ||||||
| QUndoStack *Editor::undoStack() { | QUndoStack *Editor::undoStack() { | ||||||
| 	return nullptr; | 	return nullptr; | ||||||
| } | } | ||||||
|  |  | ||||||
| void Editor::save() { | void Editor::save() { | ||||||
|     save(); |     saveItem(); | ||||||
|     setUnsavedChanges(false); |     setUnsavedChanges(false); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -30,4 +27,7 @@ void Editor::setUnsavedChanges(bool uc) { | |||||||
|     emit unsavedChangesUpdate(uc); |     emit unsavedChangesUpdate(uc); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void Editor::saveItem() { | ||||||
|  | } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -18,7 +18,7 @@ namespace nostalgia::studio { | |||||||
| class NOSTALGIASTUDIO_EXPORT Editor: public QWidget { | class NOSTALGIASTUDIO_EXPORT Editor: public QWidget { | ||||||
| 	Q_OBJECT | 	Q_OBJECT | ||||||
|  |  | ||||||
| 	private:	 | 	private: | ||||||
| 		bool m_unsavedChanges = false; | 		bool m_unsavedChanges = false; | ||||||
|  |  | ||||||
| 	public: | 	public: | ||||||
| @@ -31,11 +31,6 @@ class NOSTALGIASTUDIO_EXPORT Editor: public QWidget { | |||||||
| 		 */ | 		 */ | ||||||
| 		virtual QString itemName() = 0; | 		virtual QString itemName() = 0; | ||||||
|  |  | ||||||
| 		/** |  | ||||||
| 		 * Save changes to item being edited. |  | ||||||
| 		 */ |  | ||||||
| 		virtual void saveItem(); |  | ||||||
|  |  | ||||||
| 		/** | 		/** | ||||||
| 		 * Returns the undo stack holding changes to the item being edited. | 		 * Returns the undo stack holding changes to the item being edited. | ||||||
| 		 */ | 		 */ | ||||||
| @@ -52,6 +47,12 @@ class NOSTALGIASTUDIO_EXPORT Editor: public QWidget { | |||||||
| 		 */ | 		 */ | ||||||
| 		void setUnsavedChanges(bool); | 		void setUnsavedChanges(bool); | ||||||
|  |  | ||||||
|  | 	protected: | ||||||
|  | 		/** | ||||||
|  | 		 * Save changes to item being edited. | ||||||
|  | 		 */ | ||||||
|  | 		virtual void saveItem(); | ||||||
|  |  | ||||||
| 	signals: | 	signals: | ||||||
| 		void unsavedChangesUpdate(bool); | 		void unsavedChangesUpdate(bool); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -303,7 +303,11 @@ void MainWindow::openProject(QString projectPath) { | |||||||
| 	// reopen tabs | 	// reopen tabs | ||||||
| 	auto openTabs = readTabs(); | 	auto openTabs = readTabs(); | ||||||
| 	for (auto t : openTabs) { | 	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; | 	qInfo() << "Open project:" << projectPath; | ||||||
| } | } | ||||||
| @@ -316,7 +320,7 @@ void MainWindow::closeProject() { | |||||||
| 		m_tabs->removeTab(0); | 		m_tabs->removeTab(0); | ||||||
| 		delete tab; | 		delete tab; | ||||||
| 	} | 	} | ||||||
| 	 |  | ||||||
| 	if (m_ctx.project) { | 	if (m_ctx.project) { | ||||||
| 		disconnect(m_ctx.project, SIGNAL(updated(QString)), m_oxfsView, SLOT(updateFile(QString))); | 		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) { | void MainWindow::changeTab(int idx) { | ||||||
| 	auto tab = dynamic_cast<studio::Editor*>(m_tabs->widget(idx)); | 	auto tab = dynamic_cast<studio::Editor*>(m_tabs->widget(idx)); | ||||||
| 	disconnect(m_currentEditor, &Editor::unsavedChangesUpdate, m_saveAction, &QAction::setEnabled); | 	if (m_currentEditor) { | ||||||
| 	m_currentEditor = tab; | 		disconnect(m_currentEditor, &Editor::unsavedChangesUpdate, m_saveAction, &QAction::setEnabled); | ||||||
| 	connect(m_currentEditor, &Editor::unsavedChangesUpdate, m_saveAction, &QAction::setEnabled); | 		disconnect(m_currentEditor, &Editor::unsavedChangesUpdate, this, &MainWindow::markUnsavedChanges); | ||||||
| 	if (!tab) { | 	} | ||||||
| 		m_undoGroup.setActiveStack(nullptr); | 	m_currentEditor = tab; | ||||||
| 		return; | 	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() { | void MainWindow::showImportWizard() { | ||||||
|   | |||||||
| @@ -150,6 +150,8 @@ class MainWindow: public QMainWindow { | |||||||
|  |  | ||||||
| 		void changeTab(int idx); | 		void changeTab(int idx); | ||||||
|  |  | ||||||
|  | 		void markUnsavedChanges(bool); | ||||||
|  |  | ||||||
| 		void showNewWizard(); | 		void showNewWizard(); | ||||||
|  |  | ||||||
| 		void showImportWizard(); | 		void showImportWizard(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user