From b79999a6bfbbbeebc0d18177d7e9138fcd0e57c3 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sun, 10 Dec 2017 11:14:45 +0100 Subject: [PATCH] Move some window title updates from Song into MainWindow Move the window title updates which are triggered due to a changed modify state from Song to MainWindow. Implementation details: Add a new signal modified to Song and connect the new slot method MainWindow::onSongModified to it. Currently only the window title is updated in the slot. It is only updated if the code is executed from the GUI main thread. This implementation was taken over from the original implementation of Song::setModified. The assumption here seems to be that the Song might also be set as modified from other threads (which is a bad design). Add a new private method Song::setModified(bool) and replace all direct manipulations of m_modified in Song by calls to this method. This is done to ensure that the signal can be emitted in all these cases. Make Song::setModified() delegates to Song::setModified(bool) for the same reason. Other changes: Slightly refactor MainWindow::resetWindowTitle to get rid of an unnecessary if statement. --- include/MainWindow.h | 1 + include/Song.h | 3 +++ src/core/Song.cpp | 38 ++++++++++++++++---------------------- src/gui/MainWindow.cpp | 24 +++++++++++++++++++----- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/include/MainWindow.h b/include/MainWindow.h index 845a872ce..ee0804b22 100644 --- a/include/MainWindow.h +++ b/include/MainWindow.h @@ -253,6 +253,7 @@ private slots: void onExportProjectTracks(); void onImportProject(); void onSongStopped(); + void onSongModified(); signals: void periodicUpdate(); diff --git a/include/Song.h b/include/Song.h index fc4625eed..c1ca62688 100644 --- a/include/Song.h +++ b/include/Song.h @@ -370,6 +370,8 @@ private: void processAutomations(const TrackList& tracks, MidiTime timeStart, fpp_t frames); + void setModified(bool value); + AutomationTrack * m_globalAutomationTrack; IntModel m_tempoModel; @@ -429,6 +431,7 @@ signals: void controllerRemoved( Controller * ); void updateSampleTracks(); void stopped(); + void modified(); } ; diff --git a/src/core/Song.cpp b/src/core/Song.cpp index 9dc37a0b4..a7f81bf46 100644 --- a/src/core/Song.cpp +++ b/src/core/Song.cpp @@ -451,6 +451,18 @@ void Song::processAutomations(const TrackList &tracklist, MidiTime timeStart, fp } } +void Song::setModified(bool value) +{ + if( !m_loadingProject ) + { + if (m_modified != value) + { + m_modified = value; + emit modified(); + } + } +} + std::pair Song::getExportEndpoints() const { if ( m_renderBetweenMarkers ) @@ -933,13 +945,8 @@ void Song::createNewProject() QCoreApplication::sendPostedEvents(); - m_modified = false; + setModified(false); m_loadOnLaunch = false; - - if( gui->mainWindow() ) - { - gui->mainWindow()->resetWindowTitle(); - } } @@ -1132,13 +1139,8 @@ void Song::loadProject( const QString & fileName ) } m_loadingProject = false; - m_modified = false; + setModified(false); m_loadOnLaunch = false; - - if( gui && gui->mainWindow() ) - { - gui->mainWindow()->resetWindowTitle(); - } } @@ -1184,7 +1186,7 @@ bool Song::guiSaveProject() if( saveResult ) { - m_modified = false; + setModified(false); } return saveResult; @@ -1289,15 +1291,7 @@ void Song::updateFramesPerTick() void Song::setModified() { - if( !m_loadingProject ) - { - m_modified = true; - if( gui != nullptr && gui->mainWindow() && - QThread::currentThread() == gui->mainWindow()->thread() ) - { - gui->mainWindow()->resetWindowTitle(); - } - } + setModified(true); } diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 954d9a5e8..651de1177 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -236,6 +236,8 @@ MainWindow::MainWindow() : this, SLOT( updatePlayPauseIcons() ) ); connect(Engine::getSong(), SIGNAL(stopped()), SLOT(onSongStopped())); + + connect(Engine::getSong(), SIGNAL(modified()), SLOT(onSongModified())); } @@ -672,24 +674,24 @@ SubWindow* MainWindow::addWindowedWidget(QWidget *w, Qt::WindowFlags windowFlags void MainWindow::resetWindowTitle() { - QString title = ""; + QString title(tr( "Untitled" )); + if( Engine::getSong()->projectFileName() != "" ) { title = QFileInfo( Engine::getSong()->projectFileName() ).completeBaseName(); } - if( title == "" ) - { - title = tr( "Untitled" ); - } + if( Engine::getSong()->isModified() ) { title += '*'; } + if( getSession() == Recover ) { title += " - " + tr( "Recover session. Please save your work!" ); } + setWindowTitle( title + " - " + tr( "LMMS %1" ).arg( LMMS_VERSION ) ); } @@ -1803,3 +1805,15 @@ void MainWindow::onSongStopped() } } } + +void MainWindow::onSongModified() +{ + // Only update the window title if the code is executed from the GUI main thread. + // The assumption seems to be that the Song can also be set as modified from other + // threads. This is not a good design! Copied from the original implementation of + // Song::setModified. + if(QThread::currentThread() == this->thread()) + { + this->resetWindowTitle(); + } +}