diff --git a/src/plugins/coreplugin/documentmanager.cpp b/src/plugins/coreplugin/documentmanager.cpp index 5e82969cbbd..68f0b4674c0 100644 --- a/src/plugins/coreplugin/documentmanager.cpp +++ b/src/plugins/coreplugin/documentmanager.cpp @@ -30,6 +30,9 @@ #include "idocumentfactory.h" #include "coreconstants.h" +#include +#include +#include #include #include #include @@ -165,6 +168,8 @@ public: void checkOnNextFocusChange(); void onApplicationFocusChange(); + void registerSaveAllAction(); + QMap m_states; // filePathKey -> FileState QSet m_changedFiles; // watched file paths collected from file watcher notifications QList m_documentsWithoutWatch; @@ -188,6 +193,8 @@ public: // signal // That makes the code easier IDocument *m_blockedIDocument = nullptr; + + QAction *m_saveAllAction; }; static DocumentManager *m_instance; @@ -231,7 +238,20 @@ void DocumentManagerPrivate::onApplicationFocusChange() m_instance->checkForReload(); } -DocumentManagerPrivate::DocumentManagerPrivate() +void DocumentManagerPrivate::registerSaveAllAction() +{ + ActionContainer *mfile = ActionManager::actionContainer(Constants::M_FILE); + Command *cmd = ActionManager::registerAction(m_saveAllAction, Constants::SAVEALL); + cmd->setDefaultKeySequence(QKeySequence(useMacShortcuts ? QString() : tr("Ctrl+Shift+S"))); + mfile->addAction(cmd, Constants::G_FILE_SAVE); + m_saveAllAction->setEnabled(false); + connect(m_saveAllAction, &QAction::triggered, []() { + DocumentManager::saveAllModifiedDocumentsSilently(); + }); +} + +DocumentManagerPrivate::DocumentManagerPrivate() : + m_saveAllAction(new QAction(tr("Save A&ll"), this)) { // we do not want to do too much directly in the focus change event, so queue the connection connect(qApp, @@ -348,6 +368,7 @@ void DocumentManager::addDocuments(const QList &documents, bool add m_instance, &DocumentManager::documentDestroyed); connect(document, &IDocument::filePathChanged, m_instance, &DocumentManager::filePathChanged); + connect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll); d->m_documentsWithoutWatch.append(document); } } @@ -360,6 +381,7 @@ void DocumentManager::addDocuments(const QList &documents, bool add connect(document, &QObject::destroyed, m_instance, &DocumentManager::documentDestroyed); connect(document, &IDocument::filePathChanged, m_instance, &DocumentManager::filePathChanged); + connect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll); addFileInfo(document); } } @@ -473,6 +495,11 @@ void DocumentManager::filePathChanged(const FilePath &oldName, const FilePath &n emit m_instance->documentRenamed(doc, oldName.toString(), newName.toString()); } +void DocumentManager::updateSaveAll() +{ + d->m_saveAllAction->setEnabled(!modifiedDocuments().empty()); +} + /*! Adds \a document to the collection. If \a addWatcher is \c true (the default), the document's file is added to a file system watcher @@ -509,6 +536,7 @@ bool DocumentManager::removeDocument(IDocument *document) disconnect(document, &IDocument::changed, m_instance, &DocumentManager::checkForNewFileName); } disconnect(document, &QObject::destroyed, m_instance, &DocumentManager::documentDestroyed); + disconnect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll); return addWatcher; } @@ -739,6 +767,7 @@ bool DocumentManager::saveDocument(IDocument *document, const QString &fileName, addDocument(document, addWatcher); unexpectFileChange(effName); + m_instance->updateSaveAll(); return ret; } @@ -1501,6 +1530,11 @@ void DocumentManager::notifyFilesChangedInternally(const QStringList &files) emit m_instance->filesChangedInternally(files); } +void DocumentManager::registerSaveAllAction() +{ + d->registerSaveAllAction(); +} + // -------------- FileChangeBlocker /*! diff --git a/src/plugins/coreplugin/documentmanager.h b/src/plugins/coreplugin/documentmanager.h index 66ca54cd2e1..9e11d7cf424 100644 --- a/src/plugins/coreplugin/documentmanager.h +++ b/src/plugins/coreplugin/documentmanager.h @@ -162,6 +162,8 @@ private: void checkForReload(); void changedFile(const QString &file); void filePathChanged(const Utils::FilePath &oldName, const Utils::FilePath &newName); + void updateSaveAll(); + static void registerSaveAllAction(); friend class Core::Internal::MainWindow; friend class Core::Internal::DocumentManagerPrivate; diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp index a9a51d8b655..2a9d4edeee9 100644 --- a/src/plugins/coreplugin/mainwindow.cpp +++ b/src/plugins/coreplugin/mainwindow.cpp @@ -549,11 +549,7 @@ void MainWindow::registerDefaultActions() mfile->addAction(cmd, Constants::G_FILE_SAVE); // SaveAll Action - m_saveAllAction = new QAction(tr("Save A&ll"), this); - cmd = ActionManager::registerAction(m_saveAllAction, Constants::SAVEALL); - cmd->setDefaultKeySequence(QKeySequence(useMacShortcuts ? QString() : tr("Ctrl+Shift+S"))); - mfile->addAction(cmd, Constants::G_FILE_SAVE); - connect(m_saveAllAction, &QAction::triggered, this, &MainWindow::saveAll); + DocumentManager::registerSaveAllAction(); // Print Action icon = QIcon::fromTheme(QLatin1String("document-print")); @@ -897,11 +893,6 @@ void MainWindow::setFocusToEditor() EditorManagerPrivate::doEscapeKeyFocusMoveMagic(); } -void MainWindow::saveAll() -{ - DocumentManager::saveAllModifiedDocumentsSilently(); -} - void MainWindow::exit() { // this function is most likely called from a user action diff --git a/src/plugins/coreplugin/mainwindow.h b/src/plugins/coreplugin/mainwindow.h index 0d7b533086a..554e7b62068 100644 --- a/src/plugins/coreplugin/mainwindow.h +++ b/src/plugins/coreplugin/mainwindow.h @@ -121,7 +121,6 @@ private: void openFile(); void aboutToShowRecentFiles(); void setFocusToEditor(); - void saveAll(); void aboutQtCreator(); void aboutPlugins(); void updateFocusWidget(QWidget *old, QWidget *now);