Disable "Save All" when there are no documents to be saved

Fixes: QTCREATORBUG-22072
Change-Id: I6c84e0004d1ada27bfcec59f509d066f1b03ca2c
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Ville Nummela
2020-03-19 15:19:11 +02:00
parent 3b995f7623
commit 1194340b85
4 changed files with 38 additions and 12 deletions

View File

@@ -30,6 +30,9 @@
#include "idocumentfactory.h" #include "idocumentfactory.h"
#include "coreconstants.h" #include "coreconstants.h"
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/diffservice.h> #include <coreplugin/diffservice.h>
#include <coreplugin/dialogs/filepropertiesdialog.h> #include <coreplugin/dialogs/filepropertiesdialog.h>
#include <coreplugin/dialogs/readonlyfilesdialog.h> #include <coreplugin/dialogs/readonlyfilesdialog.h>
@@ -165,6 +168,8 @@ public:
void checkOnNextFocusChange(); void checkOnNextFocusChange();
void onApplicationFocusChange(); void onApplicationFocusChange();
void registerSaveAllAction();
QMap<QString, FileState> m_states; // filePathKey -> FileState QMap<QString, FileState> m_states; // filePathKey -> FileState
QSet<QString> m_changedFiles; // watched file paths collected from file watcher notifications QSet<QString> m_changedFiles; // watched file paths collected from file watcher notifications
QList<IDocument *> m_documentsWithoutWatch; QList<IDocument *> m_documentsWithoutWatch;
@@ -188,6 +193,8 @@ public:
// signal // signal
// That makes the code easier // That makes the code easier
IDocument *m_blockedIDocument = nullptr; IDocument *m_blockedIDocument = nullptr;
QAction *m_saveAllAction;
}; };
static DocumentManager *m_instance; static DocumentManager *m_instance;
@@ -231,7 +238,20 @@ void DocumentManagerPrivate::onApplicationFocusChange()
m_instance->checkForReload(); 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 // we do not want to do too much directly in the focus change event, so queue the connection
connect(qApp, connect(qApp,
@@ -348,6 +368,7 @@ void DocumentManager::addDocuments(const QList<IDocument *> &documents, bool add
m_instance, &DocumentManager::documentDestroyed); m_instance, &DocumentManager::documentDestroyed);
connect(document, &IDocument::filePathChanged, connect(document, &IDocument::filePathChanged,
m_instance, &DocumentManager::filePathChanged); m_instance, &DocumentManager::filePathChanged);
connect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll);
d->m_documentsWithoutWatch.append(document); d->m_documentsWithoutWatch.append(document);
} }
} }
@@ -360,6 +381,7 @@ void DocumentManager::addDocuments(const QList<IDocument *> &documents, bool add
connect(document, &QObject::destroyed, m_instance, &DocumentManager::documentDestroyed); connect(document, &QObject::destroyed, m_instance, &DocumentManager::documentDestroyed);
connect(document, &IDocument::filePathChanged, connect(document, &IDocument::filePathChanged,
m_instance, &DocumentManager::filePathChanged); m_instance, &DocumentManager::filePathChanged);
connect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll);
addFileInfo(document); addFileInfo(document);
} }
} }
@@ -473,6 +495,11 @@ void DocumentManager::filePathChanged(const FilePath &oldName, const FilePath &n
emit m_instance->documentRenamed(doc, oldName.toString(), newName.toString()); 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 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 (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, &IDocument::changed, m_instance, &DocumentManager::checkForNewFileName);
} }
disconnect(document, &QObject::destroyed, m_instance, &DocumentManager::documentDestroyed); disconnect(document, &QObject::destroyed, m_instance, &DocumentManager::documentDestroyed);
disconnect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll);
return addWatcher; return addWatcher;
} }
@@ -739,6 +767,7 @@ bool DocumentManager::saveDocument(IDocument *document, const QString &fileName,
addDocument(document, addWatcher); addDocument(document, addWatcher);
unexpectFileChange(effName); unexpectFileChange(effName);
m_instance->updateSaveAll();
return ret; return ret;
} }
@@ -1501,6 +1530,11 @@ void DocumentManager::notifyFilesChangedInternally(const QStringList &files)
emit m_instance->filesChangedInternally(files); emit m_instance->filesChangedInternally(files);
} }
void DocumentManager::registerSaveAllAction()
{
d->registerSaveAllAction();
}
// -------------- FileChangeBlocker // -------------- FileChangeBlocker
/*! /*!

View File

@@ -162,6 +162,8 @@ private:
void checkForReload(); void checkForReload();
void changedFile(const QString &file); void changedFile(const QString &file);
void filePathChanged(const Utils::FilePath &oldName, const Utils::FilePath &newName); void filePathChanged(const Utils::FilePath &oldName, const Utils::FilePath &newName);
void updateSaveAll();
static void registerSaveAllAction();
friend class Core::Internal::MainWindow; friend class Core::Internal::MainWindow;
friend class Core::Internal::DocumentManagerPrivate; friend class Core::Internal::DocumentManagerPrivate;

View File

@@ -549,11 +549,7 @@ void MainWindow::registerDefaultActions()
mfile->addAction(cmd, Constants::G_FILE_SAVE); mfile->addAction(cmd, Constants::G_FILE_SAVE);
// SaveAll Action // SaveAll Action
m_saveAllAction = new QAction(tr("Save A&ll"), this); DocumentManager::registerSaveAllAction();
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);
// Print Action // Print Action
icon = QIcon::fromTheme(QLatin1String("document-print")); icon = QIcon::fromTheme(QLatin1String("document-print"));
@@ -897,11 +893,6 @@ void MainWindow::setFocusToEditor()
EditorManagerPrivate::doEscapeKeyFocusMoveMagic(); EditorManagerPrivate::doEscapeKeyFocusMoveMagic();
} }
void MainWindow::saveAll()
{
DocumentManager::saveAllModifiedDocumentsSilently();
}
void MainWindow::exit() void MainWindow::exit()
{ {
// this function is most likely called from a user action // this function is most likely called from a user action

View File

@@ -121,7 +121,6 @@ private:
void openFile(); void openFile();
void aboutToShowRecentFiles(); void aboutToShowRecentFiles();
void setFocusToEditor(); void setFocusToEditor();
void saveAll();
void aboutQtCreator(); void aboutQtCreator();
void aboutPlugins(); void aboutPlugins();
void updateFocusWidget(QWidget *old, QWidget *now); void updateFocusWidget(QWidget *old, QWidget *now);