From 779fcdb18b0315182df922218d1827295dffd718 Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 20 Sep 2024 12:49:55 +0200 Subject: [PATCH] Core: Return expected_str from IDocument::reload() ... and adjust callers. Change-Id: Iee41f06e8f6e0019aecff67ce06a6f22ec1a45ee Reviewed-by: Eike Ziller --- src/plugins/bineditor/bineditorplugin.cpp | 13 +++++---- src/plugins/coreplugin/documentmanager.cpp | 25 ++++++++-------- .../editormanager/editormanager.cpp | 7 +++-- src/plugins/coreplugin/idocument.cpp | 5 ++-- src/plugins/coreplugin/idocument.h | 2 +- src/plugins/designer/formwindowfile.cpp | 13 +++++---- src/plugins/designer/formwindowfile.h | 2 +- src/plugins/diffeditor/diffeditordocument.cpp | 21 +++++++------- src/plugins/diffeditor/diffeditordocument.h | 2 +- .../diffeditor/diffeditorwidgetcontroller.cpp | 3 +- .../genericprojectmanager/genericproject.cpp | 7 ++--- src/plugins/imageviewer/imageviewerfile.cpp | 16 ++++++---- src/plugins/imageviewer/imageviewerfile.h | 2 +- src/plugins/modeleditor/modeldocument.cpp | 18 +++++------- src/plugins/modeleditor/modeldocument.h | 2 +- src/plugins/projectexplorer/project.cpp | 5 ++-- src/plugins/projectexplorer/taskfile.cpp | 10 +++++-- src/plugins/projectexplorer/taskfile.h | 2 +- .../qmakeprojectmanager/qmakeparsernodes.cpp | 7 +++-- .../qmakeprojectmanager/qmakeproject.cpp | 5 ++-- src/plugins/resourceeditor/resourceeditor.cpp | 13 +++++---- src/plugins/resourceeditor/resourcenode.cpp | 7 ++--- .../scxmleditor/scxmleditordocument.cpp | 13 +++++---- src/plugins/scxmleditor/scxmleditordocument.h | 2 +- src/plugins/squish/objectsmapdocument.cpp | 14 +++++---- src/plugins/squish/objectsmapdocument.h | 2 +- src/plugins/texteditor/textdocument.cpp | 29 ++++++++++--------- src/plugins/texteditor/textdocument.h | 8 ++--- src/plugins/texteditor/texteditor.cpp | 5 ++-- 29 files changed, 138 insertions(+), 122 deletions(-) diff --git a/src/plugins/bineditor/bineditorplugin.cpp b/src/plugins/bineditor/bineditorplugin.cpp index fdf923f6c1f..81880283686 100644 --- a/src/plugins/bineditor/bineditorplugin.cpp +++ b/src/plugins/bineditor/bineditorplugin.cpp @@ -120,7 +120,7 @@ public: bool isSaveAsAllowed() const final { return true; } - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final; + Utils::expected_str reload(ReloadFlag flag, ChangeType type) final; Utils::expected_str saveImpl(const Utils::FilePath &filePath, bool autoSave) final; void fetchData(quint64 address) const { if (m_fetchDataHandler) m_fetchDataHandler(address); } @@ -2182,16 +2182,19 @@ bool BinEditorDocument::isModified() const return m_undoStack.size() != m_unmodifiedState; } -bool BinEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) +expected_str BinEditorDocument::reload(ReloadFlag flag, ChangeType type) { Q_UNUSED(type) if (flag == FlagIgnore) - return true; + return {}; emit aboutToReload(); clear(); - const bool success = (openImpl(errorString, filePath()) == OpenResult::Success); + QString errorString; + const bool success = (openImpl(&errorString, filePath()) == OpenResult::Success); emit reloadFinished(success); - return success; + if (!success) + return make_unexpected(errorString); + return {}; } expected_str BinEditorDocument::saveImpl(const FilePath &filePath, bool autoSave) diff --git a/src/plugins/coreplugin/documentmanager.cpp b/src/plugins/coreplugin/documentmanager.cpp index 89a4d24976b..b0489b3e757 100644 --- a/src/plugins/coreplugin/documentmanager.cpp +++ b/src/plugins/coreplugin/documentmanager.cpp @@ -1188,19 +1188,18 @@ void DocumentManager::checkForReload() removeFileInfo(document); addFileInfos({document}); - bool success = true; - QString errorString; + expected_str success; // we've got some modification document->checkPermissions(); // check if it's contents or permissions: if (!type) { // Only permission change - success = true; + success = {}; // now we know it's a content change or file was removed } else if (defaultBehavior == IDocument::ReloadUnmodified && type == IDocument::TypeContents && !document->isModified()) { // content change, but unmodified (and settings say to reload in this case) - success = document->reload(&errorString, IDocument::FlagReload, *type); + success = document->reload(IDocument::FlagReload, *type); // file was removed or it's a content change and the default behavior for // unmodified files didn't kick in } else if (defaultBehavior == IDocument::ReloadUnmodified && type == IDocument::TypeRemoved @@ -1210,7 +1209,7 @@ void DocumentManager::checkForReload() documentsToClose << document; } else if (defaultBehavior == IDocument::IgnoreAll) { // content change or removed, but settings say ignore - success = document->reload(&errorString, IDocument::FlagIgnore, *type); + success = document->reload(IDocument::FlagIgnore, *type); // either the default behavior is to always ask, // or the ReloadUnmodified default behavior didn't kick in, // so do whatever the IDocument wants us to do @@ -1221,16 +1220,16 @@ void DocumentManager::checkForReload() if (type == IDocument::TypeRemoved) documentsToClose << document; else - success = document->reload(&errorString, IDocument::FlagReload, *type); + success = document->reload(IDocument::FlagReload, *type); // IDocument wants us to ask } else if (type == IDocument::TypeContents) { // content change, IDocument wants to ask user if (previousReloadAnswer == ReloadNone || previousReloadAnswer == ReloadNoneAndDiff) { // answer already given, ignore - success = document->reload(&errorString, IDocument::FlagIgnore, IDocument::TypeContents); + success = document->reload(IDocument::FlagIgnore, IDocument::TypeContents); } else if (previousReloadAnswer == ReloadAll) { // answer already given, reload - success = document->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents); + success = document->reload(IDocument::FlagReload, IDocument::TypeContents); } else { // Ask about content change previousReloadAnswer = reloadPrompt(document->filePath(), document->isModified(), @@ -1239,12 +1238,12 @@ void DocumentManager::checkForReload() switch (previousReloadAnswer) { case ReloadAll: case ReloadCurrent: - success = document->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents); + success = document->reload(IDocument::FlagReload, IDocument::TypeContents); break; case ReloadSkipCurrent: case ReloadNone: case ReloadNoneAndDiff: - success = document->reload(&errorString, IDocument::FlagIgnore, IDocument::TypeContents); + success = document->reload(IDocument::FlagIgnore, IDocument::TypeContents); break; case CloseCurrent: documentsToClose << document; @@ -1288,10 +1287,10 @@ void DocumentManager::checkForReload() } } if (!success) { + QString errorString = success.error(); if (errorString.isEmpty()) - errorStrings << Tr::tr("Cannot reload %1").arg(document->filePath().toUserOutput()); - else - errorStrings << errorString; + errorString = Tr::tr("Cannot reload %1").arg(document->filePath().toUserOutput()); + errorStrings << errorString; } d->m_blockedIDocument = nullptr; diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index 2367a8cb15e..594530b742a 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -2630,9 +2630,10 @@ void EditorManagerPrivate::revertToSaved(IDocument *document) return; } } - QString errorString; - if (!document->reload(&errorString, IDocument::FlagReload, IDocument::TypeContents)) - QMessageBox::critical(ICore::dialogParent(), ::Core::Tr::tr("File Error"), errorString); + + const expected_str res = document->reload(IDocument::FlagReload, IDocument::TypeContents); + if (!res) + QMessageBox::critical(ICore::dialogParent(), ::Core::Tr::tr("File Error"), res.error()); } void EditorManagerPrivate::autoSuspendDocuments() diff --git a/src/plugins/coreplugin/idocument.cpp b/src/plugins/coreplugin/idocument.cpp index 84495188491..3587ae69ba7 100644 --- a/src/plugins/coreplugin/idocument.cpp +++ b/src/plugins/coreplugin/idocument.cpp @@ -467,12 +467,11 @@ IDocument::ReloadBehavior IDocument::reloadBehavior(ChangeTrigger trigger, Chang \sa reloadFinished() \sa changed() */ -bool IDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) +Utils::expected_str IDocument::reload(ReloadFlag flag, ChangeType type) { - Q_UNUSED(errorString) Q_UNUSED(flag) Q_UNUSED(type) - return true; + return {}; } /*! diff --git a/src/plugins/coreplugin/idocument.h b/src/plugins/coreplugin/idocument.h index 02d306282af..28bcd58afcc 100644 --- a/src/plugins/coreplugin/idocument.h +++ b/src/plugins/coreplugin/idocument.h @@ -100,7 +100,7 @@ public: void setSuspendAllowed(bool value); virtual ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const; - virtual bool reload(QString *errorString, ReloadFlag flag, ChangeType type); + virtual Utils::expected_str reload(ReloadFlag flag, ChangeType type); void checkPermissions(); diff --git a/src/plugins/designer/formwindowfile.cpp b/src/plugins/designer/formwindowfile.cpp index 9138cee5e64..43f9aef84ea 100644 --- a/src/plugins/designer/formwindowfile.cpp +++ b/src/plugins/designer/formwindowfile.cpp @@ -189,11 +189,11 @@ bool FormWindowFile::isSaveAsAllowed() const return true; } -bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType type) +expected_str FormWindowFile::reload(ReloadFlag flag, ChangeType type) { if (flag == FlagIgnore) { if (!m_formWindow || type != TypeContents) - return true; + return {}; const bool wasModified = m_formWindow->isDirty(); { Utils::GuardLocker locker(m_modificationChangedGuard); @@ -203,13 +203,16 @@ bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType ty } if (!wasModified) updateIsModified(); - return true; + return {}; } else { emit aboutToReload(); + QString errorString; const bool success - = (open(errorString, filePath(), filePath()) == OpenResult::Success); + = (open(&errorString, filePath(), filePath()) == OpenResult::Success); emit reloadFinished(success); - return success; + if (!success) + return make_unexpected(errorString); + return {}; } } diff --git a/src/plugins/designer/formwindowfile.h b/src/plugins/designer/formwindowfile.h index 55690ed77da..25736034d0a 100644 --- a/src/plugins/designer/formwindowfile.h +++ b/src/plugins/designer/formwindowfile.h @@ -33,7 +33,7 @@ public: bool shouldAutoSave() const override; bool isModified() const override; bool isSaveAsAllowed() const override; - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; + Utils::expected_str reload(ReloadFlag flag, ChangeType type) override; QString fallbackSaveAsFileName() const override; bool supportsCodec(const QTextCodec *codec) const override; diff --git a/src/plugins/diffeditor/diffeditordocument.cpp b/src/plugins/diffeditor/diffeditordocument.cpp index 2768dacddd9..7140ed52114 100644 --- a/src/plugins/diffeditor/diffeditordocument.cpp +++ b/src/plugins/diffeditor/diffeditordocument.cpp @@ -261,20 +261,22 @@ expected_str DiffEditorDocument::saveImpl(const FilePath &filePath, bool a void DiffEditorDocument::reload() { - if (m_controller) { + if (m_controller) m_controller->requestReload(); - } else { - QString errorMessage; - reload(&errorMessage, Core::IDocument::FlagReload, Core::IDocument::TypeContents); - } + else + reload(Core::IDocument::FlagReload, Core::IDocument::TypeContents); } -bool DiffEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) +expected_str DiffEditorDocument::reload(ReloadFlag flag, ChangeType type) { Q_UNUSED(type) if (flag == FlagIgnore) - return true; - return open(errorString, filePath(), filePath()) == OpenResult::Success; + return {}; + QString errorString; + bool success = open(&errorString, filePath(), filePath()) == OpenResult::Success; + if (!success) + return make_unexpected(errorString); + return {}; } Core::IDocument::OpenResult DiffEditorDocument::open(QString *errorString, const FilePath &filePath, @@ -314,8 +316,7 @@ bool DiffEditorDocument::selectEncoding() switch (result.action) { case CodecSelectorResult::Reload: { setCodec(result.codec); - QString errorMessage; - return reload(&errorMessage, Core::IDocument::FlagReload, Core::IDocument::TypeContents); + return bool(reload(Core::IDocument::FlagReload, Core::IDocument::TypeContents)); } case CodecSelectorResult::Save: setCodec(result.codec); diff --git a/src/plugins/diffeditor/diffeditordocument.h b/src/plugins/diffeditor/diffeditordocument.h index 512fb60b2d5..3d175ac85c6 100644 --- a/src/plugins/diffeditor/diffeditordocument.h +++ b/src/plugins/diffeditor/diffeditordocument.h @@ -61,7 +61,7 @@ public: bool isSaveAsAllowed() const override; void reload(); - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; + Utils::expected_str reload(ReloadFlag flag, ChangeType type) override; OpenResult open(QString *errorString, const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override; bool selectEncoding(); diff --git a/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp b/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp index 9decc5f1a60..2d8a5d46227 100644 --- a/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp +++ b/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp @@ -191,8 +191,7 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch), FilePath::fromString(contentsCopyDir), 0, patchAction)) { - QString errorString; - if (textDocument->reload(&errorString, FilePath::fromString(contentsCopyFileName))) + if (textDocument->reload(FilePath::fromString(contentsCopyFileName))) m_document->reload(); } } diff --git a/src/plugins/genericprojectmanager/genericproject.cpp b/src/plugins/genericprojectmanager/genericproject.cpp index f352ecaa72a..4a7242aa00d 100644 --- a/src/plugins/genericprojectmanager/genericproject.cpp +++ b/src/plugins/genericprojectmanager/genericproject.cpp @@ -84,7 +84,7 @@ public: return BehaviorSilent; } - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final; + expected_str reload(ReloadFlag flag, ChangeType type) final; private: GenericProject *m_project = nullptr; @@ -695,15 +695,14 @@ void GenericProject::configureAsExampleProject(Kit *kit) setup(infoList); } -bool GenericProjectFile::reload(QString *errorString, IDocument::ReloadFlag flag, IDocument::ChangeType type) +expected_str GenericProjectFile::reload(IDocument::ReloadFlag flag, IDocument::ChangeType type) { - Q_UNUSED(errorString) Q_UNUSED(flag) Q_UNUSED(type) if (Target *t = m_project->activeTarget()) static_cast(t->buildSystem())->refresh(m_options); - return true; + return {}; } void GenericProject::editFilesTriggered() diff --git a/src/plugins/imageviewer/imageviewerfile.cpp b/src/plugins/imageviewer/imageviewerfile.cpp index 1d73a7fbeb4..006595d2a4e 100644 --- a/src/plugins/imageviewer/imageviewerfile.cpp +++ b/src/plugins/imageviewer/imageviewerfile.cpp @@ -25,6 +25,8 @@ #include #endif +using namespace Utils; + namespace ImageViewer::Internal { class MovieItem : public QObject, public QGraphicsPixmapItem @@ -144,17 +146,19 @@ Core::IDocument::ReloadBehavior ImageViewerFile::reloadBehavior(ChangeTrigger st return BehaviorAsk; } -bool ImageViewerFile::reload(QString *errorString, - Core::IDocument::ReloadFlag flag, - Core::IDocument::ChangeType type) +expected_str ImageViewerFile::reload(Core::IDocument::ReloadFlag flag, + Core::IDocument::ChangeType type) { Q_UNUSED(type) if (flag == FlagIgnore) - return true; + return {}; emit aboutToReload(); - bool success = (openImpl(errorString, filePath()) == OpenResult::Success); + QString errorString; + bool success = (openImpl(&errorString, filePath()) == OpenResult::Success); emit reloadFinished(success); - return success; + if (!success) + return make_unexpected(errorString); + return {}; } QMovie *ImageViewerFile::movie() const diff --git a/src/plugins/imageviewer/imageviewerfile.h b/src/plugins/imageviewer/imageviewerfile.h index 26ed01a94fe..37b5fe9da85 100644 --- a/src/plugins/imageviewer/imageviewerfile.h +++ b/src/plugins/imageviewer/imageviewerfile.h @@ -39,7 +39,7 @@ public: const Utils::FilePath &realFilePath) override; ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override; - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; + Utils::expected_str reload(ReloadFlag flag, ChangeType type) override; QMovie *movie() const; diff --git a/src/plugins/modeleditor/modeldocument.cpp b/src/plugins/modeleditor/modeldocument.cpp index 9143301e26d..0b3d80cda02 100644 --- a/src/plugins/modeleditor/modeldocument.cpp +++ b/src/plugins/modeleditor/modeldocument.cpp @@ -11,8 +11,6 @@ #include "qmt/config/configcontroller.h" #include "qmt/infrastructure/ioexceptions.h" -#include "qmt/model_controller/modelcontroller.h" -#include "qmt/model/mdiagram.h" #include "qmt/project_controller/projectcontroller.h" #include "qmt/project/project.h" @@ -91,24 +89,22 @@ bool ModelDocument::isSaveAsAllowed() const return true; } -bool ModelDocument::reload(QString *errorString, Core::IDocument::ReloadFlag flag, - Core::IDocument::ChangeType type) +Utils::expected_str ModelDocument::reload(Core::IDocument::ReloadFlag flag, + Core::IDocument::ChangeType type) { Q_UNUSED(type) if (flag == FlagIgnore) - return true; + return {}; try { d->documentController->loadProject(filePath()); } catch (const qmt::FileNotFoundException &ex) { - *errorString = ex.errorMessage(); - return false; + return make_unexpected(ex.errorMessage()); } catch (const qmt::Exception &ex) { - *errorString = Tr::tr("Could not open \"%1\" for reading: %2.") - .arg(filePath().toUserOutput(), ex.errorMessage()); - return false; + return make_unexpected(Tr::tr("Could not open \"%1\" for reading: %2.") + .arg(filePath().toUserOutput(), ex.errorMessage())); } emit contentSet(); - return true; + return {}; } ExtDocumentController *ModelDocument::documentController() const diff --git a/src/plugins/modeleditor/modeldocument.h b/src/plugins/modeleditor/modeldocument.h index e53c161445e..6e672c4d3d8 100644 --- a/src/plugins/modeleditor/modeldocument.h +++ b/src/plugins/modeleditor/modeldocument.h @@ -33,7 +33,7 @@ public: bool shouldAutoSave() const override; bool isModified() const override; bool isSaveAsAllowed() const override; - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; + Utils::expected_str reload(ReloadFlag flag, ChangeType type) override; ExtDocumentController *documentController() const; diff --git a/src/plugins/projectexplorer/project.cpp b/src/plugins/projectexplorer/project.cpp index 7336b676513..274c35ae1d1 100644 --- a/src/plugins/projectexplorer/project.cpp +++ b/src/plugins/projectexplorer/project.cpp @@ -145,14 +145,13 @@ public: return BehaviorSilent; } - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final + expected_str reload(ReloadFlag flag, ChangeType type) final { - Q_UNUSED(errorString) Q_UNUSED(flag) Q_UNUSED(type) emit m_project->projectFileIsDirty(filePath()); - return true; + return {}; } private: diff --git a/src/plugins/projectexplorer/taskfile.cpp b/src/plugins/projectexplorer/taskfile.cpp index 1c95254e2ef..c85a8def83a 100644 --- a/src/plugins/projectexplorer/taskfile.cpp +++ b/src/plugins/projectexplorer/taskfile.cpp @@ -45,15 +45,19 @@ Core::IDocument::ReloadBehavior TaskFile::reloadBehavior(ChangeTrigger state, Ch return BehaviorSilent; } -bool TaskFile::reload(QString *errorString, ReloadFlag flag, ChangeType type) +expected_str TaskFile::reload(ReloadFlag flag, ChangeType type) { Q_UNUSED(flag) if (type == TypeRemoved) { deleteLater(); - return true; + return {}; } - return load(errorString, filePath()); + QString errorString; + bool success = load(&errorString, filePath()); + if (!success) + return make_unexpected(errorString); + return {}; } static Task::TaskType typeFrom(const QString &typeName) diff --git a/src/plugins/projectexplorer/taskfile.h b/src/plugins/projectexplorer/taskfile.h index 74ed5ca01b0..1d5772cf811 100644 --- a/src/plugins/projectexplorer/taskfile.h +++ b/src/plugins/projectexplorer/taskfile.h @@ -24,7 +24,7 @@ public: TaskFile(QObject *parent); ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override; - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; + Utils::expected_str reload(ReloadFlag flag, ChangeType type) override; bool load(QString *errorString, const Utils::FilePath &fileName); diff --git a/src/plugins/qmakeprojectmanager/qmakeparsernodes.cpp b/src/plugins/qmakeprojectmanager/qmakeparsernodes.cpp index a851e690146..9474c8ce159 100644 --- a/src/plugins/qmakeprojectmanager/qmakeparsernodes.cpp +++ b/src/plugins/qmakeprojectmanager/qmakeparsernodes.cpp @@ -929,9 +929,10 @@ void QmakePriFile::save(const QStringList &lines) QStringList errorStrings; Core::IDocument *document = Core::DocumentModel::documentForFilePath(filePath()); if (document) { - QString errorString; - if (!document->reload(&errorString, Core::IDocument::FlagReload, Core::IDocument::TypeContents)) - errorStrings << errorString; + expected_str res = + document->reload(Core::IDocument::FlagReload, Core::IDocument::TypeContents); + if (!res) + errorStrings << res.error(); } if (!errorStrings.isEmpty()) QMessageBox::warning(Core::ICore::dialogParent(), Tr::tr("File Error"), diff --git a/src/plugins/qmakeprojectmanager/qmakeproject.cpp b/src/plugins/qmakeprojectmanager/qmakeproject.cpp index 971a5115a4c..5e25a908829 100644 --- a/src/plugins/qmakeprojectmanager/qmakeproject.cpp +++ b/src/plugins/qmakeprojectmanager/qmakeproject.cpp @@ -101,14 +101,13 @@ public: Q_UNUSED(type) return BehaviorSilent; } - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override + expected_str reload(ReloadFlag flag, ChangeType type) override { - Q_UNUSED(errorString) Q_UNUSED(flag) Q_UNUSED(type) if (m_priFile) m_priFile->scheduleUpdate(); - return true; + return {}; } void setPriFile(QmakePriFile *priFile) { m_priFile = priFile; } diff --git a/src/plugins/resourceeditor/resourceeditor.cpp b/src/plugins/resourceeditor/resourceeditor.cpp index e428a6e1baf..08fcef967fe 100644 --- a/src/plugins/resourceeditor/resourceeditor.cpp +++ b/src/plugins/resourceeditor/resourceeditor.cpp @@ -56,7 +56,7 @@ public: bool shouldAutoSave() const final { return m_shouldAutoSave; } bool isModified() const final { return m_model.dirty(); } bool isSaveAsAllowed() const final { return true; } - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) final; + expected_str reload(ReloadFlag flag, ChangeType type) final; void setFilePath(const FilePath &newName) final; void setBlockDirtyChanged(bool value) { m_blockDirtyChanged = value; } @@ -280,15 +280,18 @@ void ResourceEditorImpl::restoreState(const QByteArray &state) m_resourceEditor->restoreState(splitterState); } -bool ResourceEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) +expected_str ResourceEditorDocument::reload(ReloadFlag flag, ChangeType type) { Q_UNUSED(type) if (flag == FlagIgnore) - return true; + return {}; emit aboutToReload(); - const bool success = (open(errorString, filePath(), filePath()) == OpenResult::Success); + QString errorString; + const bool success = (open(&errorString, filePath(), filePath()) == OpenResult::Success); emit reloadFinished(success); - return success; + if (!success) + return make_unexpected(errorString); + return {}; } void ResourceEditorDocument::dirtyChanged(bool dirty) diff --git a/src/plugins/resourceeditor/resourcenode.cpp b/src/plugins/resourceeditor/resourcenode.cpp index 2d32c320493..f593faecc15 100644 --- a/src/plugins/resourceeditor/resourcenode.cpp +++ b/src/plugins/resourceeditor/resourcenode.cpp @@ -47,14 +47,13 @@ public: return BehaviorSilent; } - bool reload(QString *, ReloadFlag, ChangeType type) final + expected_str reload(ReloadFlag, ChangeType) final { - Q_UNUSED(type) FolderNode *parent = m_node->parentFolderNode(); - QTC_ASSERT(parent, return false); + QTC_ASSERT(parent, return make_unexpected(QString())); parent->replaceSubtree(m_node, std::make_unique( m_node->filePath(), parent->filePath(), m_node->contents())); - return true; + return {}; } private: diff --git a/src/plugins/scxmleditor/scxmleditordocument.cpp b/src/plugins/scxmleditor/scxmleditordocument.cpp index e0f0adac86e..3e71cf2b3d0 100644 --- a/src/plugins/scxmleditor/scxmleditordocument.cpp +++ b/src/plugins/scxmleditor/scxmleditordocument.cpp @@ -110,16 +110,19 @@ bool ScxmlEditorDocument::isModified() const return m_designWidget && m_designWidget->isDirty(); } -bool ScxmlEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) +Utils::expected_str ScxmlEditorDocument::reload(ReloadFlag flag, ChangeType type) { Q_UNUSED(type) if (flag == FlagIgnore) - return true; + return {}; emit aboutToReload(); - emit reloadRequested(errorString, filePath().toString()); - const bool success = errorString->isEmpty(); + QString errorString; + emit reloadRequested(&errorString, filePath().toString()); + const bool success = errorString.isEmpty(); emit reloadFinished(success); - return success; + if (!success) + return make_unexpected(errorString); + return {}; } bool ScxmlEditorDocument::supportsCodec(const QTextCodec *codec) const diff --git a/src/plugins/scxmleditor/scxmleditordocument.h b/src/plugins/scxmleditor/scxmleditordocument.h index 5c6b8f1a3f4..d45c80ccd15 100644 --- a/src/plugins/scxmleditor/scxmleditordocument.h +++ b/src/plugins/scxmleditor/scxmleditordocument.h @@ -33,7 +33,7 @@ public: bool shouldAutoSave() const override; bool isSaveAsAllowed() const override; bool isModified() const override; - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; + Utils::expected_str reload(ReloadFlag flag, ChangeType type) override; bool supportsCodec(const QTextCodec *codec) const override; // Internal diff --git a/src/plugins/squish/objectsmapdocument.cpp b/src/plugins/squish/objectsmapdocument.cpp index 7d168049125..a1e0df8033b 100644 --- a/src/plugins/squish/objectsmapdocument.cpp +++ b/src/plugins/squish/objectsmapdocument.cpp @@ -74,19 +74,21 @@ void ObjectsMapDocument::setModified(bool modified) emit changed(); } -bool ObjectsMapDocument::reload(QString *errorString, - Core::IDocument::ReloadFlag flag, - Core::IDocument::ChangeType type) +expected_str ObjectsMapDocument::reload(Core::IDocument::ReloadFlag flag, + Core::IDocument::ChangeType type) { Q_UNUSED(type); if (flag == FlagIgnore) - return true; + return {}; emit aboutToReload(); - const bool success = (openImpl(errorString, filePath(), filePath()) == OpenResult::Success); + QString errorString; + const bool success = (openImpl(&errorString, filePath(), filePath()) == OpenResult::Success); if (success) setModified(false); emit reloadFinished(success); - return success; + if (!success) + return make_unexpected(errorString); + return {}; } bool ObjectsMapDocument::buildObjectsMapTree(const QByteArray &contents) diff --git a/src/plugins/squish/objectsmapdocument.h b/src/plugins/squish/objectsmapdocument.h index aa87f864d72..a9a74ece6c2 100644 --- a/src/plugins/squish/objectsmapdocument.h +++ b/src/plugins/squish/objectsmapdocument.h @@ -26,7 +26,7 @@ public: bool isModified() const override { return m_isModified; } void setModified(bool modified); bool isSaveAsAllowed() const override { return true; } - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; + Utils::expected_str reload(ReloadFlag flag, ChangeType type) override; bool shouldAutoSave() const override { return true; } bool setContents(const QByteArray &contents) override; diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index afd35a49888..198438012c0 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -791,19 +791,19 @@ Core::IDocument::OpenResult TextDocument::openImpl(QString *errorString, return OpenResult::Success; } -bool TextDocument::reload(QString *errorString, QTextCodec *codec) +expected_str TextDocument::reload(QTextCodec *codec) { - QTC_ASSERT(codec, return false); + QTC_ASSERT(codec, return make_unexpected(QString("No codec given"))); setCodec(codec); - return reload(errorString); + return reload(); } -bool TextDocument::reload(QString *errorString) +expected_str TextDocument::reload() { - return reload(errorString, filePath()); + return reload(filePath()); } -bool TextDocument::reload(QString *errorString, const FilePath &realFilePath) +expected_str TextDocument::reload(const FilePath &realFilePath) { emit aboutToReload(); auto documentLayout = @@ -811,13 +811,16 @@ bool TextDocument::reload(QString *errorString, const FilePath &realFilePath) if (documentLayout) documentLayout->documentAboutToReload(this); // removes text marks non-permanently - bool success = openImpl(errorString, filePath(), realFilePath, /*reload =*/true) + QString errorString; + bool success = openImpl(&errorString, filePath(), realFilePath, /*reload =*/true) == OpenResult::Success; if (documentLayout) documentLayout->documentReloaded(this); // re-adds text marks emit reloadFinished(success); - return success; + if (!success) + return make_unexpected(errorString); + return {}; } bool TextDocument::setPlainText(const QString &text) @@ -834,24 +837,24 @@ bool TextDocument::setPlainText(const QString &text) return true; } -bool TextDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type) +expected_str TextDocument::reload(ReloadFlag flag, ChangeType type) { if (flag == FlagIgnore) { if (type != TypeContents) - return true; + return {}; const bool wasModified = document()->isModified(); { - Utils::GuardLocker locker(d->m_modificationChangedGuard); + GuardLocker locker(d->m_modificationChangedGuard); // hack to ensure we clean the clear state in QTextDocument document()->setModified(false); document()->setModified(true); } if (!wasModified) modificationChanged(true); - return true; + return {}; } - return reload(errorString); + return reload(); } void TextDocument::resetSyntaxHighlighter(const std::function &creator) diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index 300d5654a2a..67a0fc8b772 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -105,7 +105,7 @@ public: bool shouldAutoSave() const override; bool isModified() const override; bool isSaveAsAllowed() const override; - bool reload(QString *errorString, ReloadFlag flag, ChangeType type) override; + Utils::expected_str reload(ReloadFlag flag, ChangeType type) override; void setFilePath(const Utils::FilePath &newName) override; ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override; @@ -117,8 +117,8 @@ public: OpenResult open(QString *errorString, const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override; - virtual bool reload(QString *errorString); - bool reload(QString *errorString, const Utils::FilePath &realFilePath); + virtual Utils::expected_str reload(); + Utils::expected_str reload(const Utils::FilePath &realFilePath); bool setPlainText(const QString &text); QTextDocument *document() const; @@ -127,7 +127,7 @@ public: void resetSyntaxHighlighter(const SyntaxHighLighterCreator &creator); SyntaxHighlighter *syntaxHighlighter() const; - bool reload(QString *errorString, QTextCodec *codec); + Utils::expected_str reload(QTextCodec *codec); void cleanWhitespace(const QTextCursor &cursor); virtual void triggerPendingUpdates(); diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index b63e2a8ef98..a0050d9658b 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -1862,9 +1862,8 @@ void TextEditorWidget::selectEncoding() const CodecSelectorResult result = Core::askForCodec(Core::ICore::dialogParent(), doc); switch (result.action) { case Core::CodecSelectorResult::Reload: { - QString errorString; - if (!doc->reload(&errorString, result.codec)) { - QMessageBox::critical(this, Tr::tr("File Error"), errorString); + if (expected_str res = doc->reload(result.codec); !res) { + QMessageBox::critical(this, Tr::tr("File Error"), res.error()); break; } break;