From 7fa62266a961c8826347302021de0337aadee3d5 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sat, 9 Dec 2017 23:43:40 +0100 Subject: [PATCH] Move showing of save result dialog out of Song Move the showing of save result dialogs, e.g. "The project XYZ is now saved.", from the class Song into the class MainWindow. Implementation details: Add three new methods guiSaveProject, guiSaveProjectAs and handleSaveResult to MainWindow. The first two correspond to the methods with the same name in Song which don't do anything GUI related anymore. The GUI related actions are instead implemented in the two new methods in MainWindow. The method handleSaveResult shows the dialogs for successful and failed saves, updates the list of recent files and the title bar of the main window. This commit also fixes a problem in Song::guiSaveProject where a failed saved without a GUI would still have returned true, i.e. success. --- include/MainWindow.h | 3 +++ src/core/Song.cpp | 34 +++++++++++++--------------------- src/gui/MainWindow.cpp | 41 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/include/MainWindow.h b/include/MainWindow.h index bd2d8f28d..d60788a32 100644 --- a/include/MainWindow.h +++ b/include/MainWindow.h @@ -197,6 +197,9 @@ private: void refocus(); void exportProject(bool multiExport = false); + void handleSaveResult(QString const & filename, bool songSavedSuccessfully); + bool guiSaveProject(); + bool guiSaveProjectAs( const QString & filename ); QMdiArea * m_workspace; diff --git a/src/core/Song.cpp b/src/core/Song.cpp index ef4e1da16..2d25c3d24 100644 --- a/src/core/Song.cpp +++ b/src/core/Song.cpp @@ -51,7 +51,6 @@ #include "ProjectJournal.h" #include "ProjectNotes.h" #include "SongEditor.h" -#include "TextFloat.h" #include "TimeLineWidget.h" #include "PeakController.h" @@ -1186,50 +1185,43 @@ bool Song::saveProjectFile( const QString & filename ) -// save current song and update the gui +// Save the current song bool Song::guiSaveProject() { DataFile dataFile( DataFile::SongProject ); m_fileName = dataFile.nameWithExtension( m_fileName ); - if( saveProjectFile( m_fileName ) && gui != nullptr ) + + bool const saveResult = saveProjectFile( m_fileName ); + + if( saveResult ) { - TextFloat::displayMessage( tr( "Project saved" ), - tr( "The project %1 is now saved." - ).arg( m_fileName ), - embed::getIconPixmap( "project_save", 24, 24 ), - 2000 ); - ConfigManager::inst()->addRecentlyOpenedProject( m_fileName ); m_modified = false; - gui->mainWindow()->resetWindowTitle(); - } - else if( gui != nullptr ) - { - TextFloat::displayMessage( tr( "Project NOT saved." ), - tr( "The project %1 was not saved!" ).arg( - m_fileName ), - embed::getIconPixmap( "error" ), 4000 ); - return false; } - return true; + return saveResult; } -// save current song in given filename +// Save the current song with the given filename bool Song::guiSaveProjectAs( const QString & _file_name ) { QString o = m_oldFileName; m_oldFileName = m_fileName; m_fileName = _file_name; - if( guiSaveProject() == false ) + + if(!guiSaveProject()) { + // Saving failed. Restore old filenames. m_fileName = m_oldFileName; m_oldFileName = o; + return false; } + m_oldFileName = m_fileName; + return true; } diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 3818543d2..8adc538fe 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -62,6 +62,7 @@ #include "SetupDialog.h" #include "SideBar.h" #include "SongEditor.h" +#include "TextFloat.h" #include "ToolButton.h" #include "ToolPlugin.h" #include "VersionedSaveDialog.h" @@ -948,7 +949,7 @@ bool MainWindow::saveProject() } else { - Engine::getSong()->guiSaveProject(); + this->guiSaveProject(); if( getSession() == Recover ) { sessionCleanup(); @@ -1000,7 +1001,7 @@ bool MainWindow::saveProjectAs() } } } - Engine::getSong()->guiSaveProjectAs( fname ); + this->guiSaveProjectAs( fname ); if( getSession() == Recover ) { sessionCleanup(); @@ -1025,7 +1026,7 @@ bool MainWindow::saveProjectAsNewVersion() do VersionedSaveDialog::changeFileNameVersion( fileName, true ); while ( QFile( fileName ).exists() ); - Engine::getSong()->guiSaveProjectAs( fileName ); + this->guiSaveProjectAs( fileName ); return true; } } @@ -1695,6 +1696,40 @@ void MainWindow::exportProject(bool multiExport) } } +void MainWindow::handleSaveResult(QString const & filename, bool songSavedSuccessfully) +{ + if (songSavedSuccessfully) + { + TextFloat::displayMessage( tr( "Project saved" ), tr( "The project %1 is now saved.").arg( filename ), + embed::getIconPixmap( "project_save", 24, 24 ), 2000 ); + ConfigManager::inst()->addRecentlyOpenedProject(filename); + resetWindowTitle(); + } + else + { + TextFloat::displayMessage( tr( "Project NOT saved." ), tr( "The project %1 was not saved!" ).arg(filename), + embed::getIconPixmap( "error" ), 4000 ); + } +} + +bool MainWindow::guiSaveProject() +{ + Song * song = Engine::getSong(); + bool const songSaveResult = song->guiSaveProject(); + handleSaveResult(song->projectFileName(), songSaveResult); + + return songSaveResult; +} + +bool MainWindow::guiSaveProjectAs( const QString & filename ) +{ + Song * song = Engine::getSong(); + bool const songSaveResult = song->guiSaveProjectAs(filename); + handleSaveResult(filename, songSaveResult); + + return songSaveResult; +} + void MainWindow::onExportProject() { this->exportProject();