Core: Replace IDocument::OpenResult by Utils::Result<>

After the preceding change, the semantics were the same.

The need to specify new error messages in some places indicates that
errors were not very descriptive in some places before. For now,
use some untranslated strings in these cases, the idea is to centrally
bundle them via constants to ResultError similar to
ResultNotImplemented.

Change-Id: I6388cc43e0a540068ea73117a91fa2db982fbefa
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2025-04-16 14:33:11 +02:00
parent d8c9c9fbe2
commit cf4f32df77
27 changed files with 159 additions and 200 deletions

View File

@@ -97,13 +97,13 @@ public:
return type == TypeRemoved ? BehaviorSilent : IDocument::reloadBehavior(state, type);
}
OpenResult open(const FilePath &filePath, const FilePath &realFilePath) final
Result<> open(const FilePath &filePath, const FilePath &realFilePath) final
{
QTC_CHECK(filePath == realFilePath); // The bineditor can do no autosaving
return openImpl(filePath);
}
OpenResult openImpl(const FilePath &filePath, quint64 offset = 0);
Result<> openImpl(const FilePath &filePath, quint64 offset = 0);
void provideData(quint64 address);
@@ -2118,35 +2118,27 @@ bool BinEditorDocument::setContents(const QByteArray &contents)
return true;
}
IDocument::OpenResult BinEditorDocument::openImpl(const FilePath &filePath, quint64 offset)
Result<> BinEditorDocument::openImpl(const FilePath &filePath, quint64 offset)
{
const qint64 size = filePath.fileSize();
if (size < 0) {
QString msg = Tr::tr("Cannot open %1: %2").arg(filePath.toUserOutput(), Tr::tr("File Error"));
// FIXME: Was: file.errorString(), but we don't have a file anymore.
QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg);
return {OpenResult::CannotHandle, msg};
return ResultError(Tr::tr("Cannot open %1: %2").arg(filePath.toUserOutput()));
}
if (size == 0) {
QString msg = Tr::tr("The Binary Editor cannot open empty files.");
QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg);
return {OpenResult::CannotHandle, msg};
}
if (size == 0)
return ResultError(Tr::tr("The Binary Editor cannot open empty files."));
if (size / 16 >= qint64(1) << 31) {
// The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line.
QString msg = Tr::tr("The file is too big for the Binary Editor (max. 32GB).");
QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg);
return {OpenResult::CannotHandle, msg};
}
// The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line.
if (size / 16 >= qint64(1) << 31)
return ResultError(Tr::tr("The file is too big for the Binary Editor (max. 32GB)."));
if (offset >= quint64(size))
return OpenResult::CannotHandle;
return ResultError(Tr::tr("File offset too large"));
setFilePath(filePath);
setSizes(offset, size);
return OpenResult::Success;
return ResultOk;
}
void BinEditorDocument::provideData(quint64 address)
@@ -2179,8 +2171,8 @@ Result<> BinEditorDocument::reload(ReloadFlag flag, ChangeType type)
return ResultOk;
emit aboutToReload();
clear();
const OpenResult result = openImpl(filePath());
emit reloadFinished(result.code == OpenResult::Success);
const Result<> result = openImpl(filePath());
emit reloadFinished(result.has_value());
return result;
}

View File

@@ -272,8 +272,7 @@ void ClangFormatConfigWidget::reopenClangFormatDocument()
{
GuardLocker locker(m_ignoreChanges);
if (m_editor->document()->open(m_config->filePath(), m_config->filePath()).code
== Core::IDocument::OpenResult::Success) {
if (m_editor->document()->open(m_config->filePath(), m_config->filePath())) {
invokeMethodForLanguageClientManager("documentOpened",
Q_ARG(Core::IDocument *, m_editor->document()));
}

View File

@@ -151,8 +151,8 @@ class JsonSettingsDocument : public Core::IDocument
public:
JsonSettingsDocument(QUndoStack *undoStack);
OpenResult open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
Result<> open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
Result<> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
@@ -380,25 +380,25 @@ JsonSettingsDocument::JsonSettingsDocument(QUndoStack *undoStack)
m_ceSettings.setUndoStack(undoStack);
}
IDocument::OpenResult JsonSettingsDocument::open(const FilePath &filePath,
const FilePath &realFilePath)
Result<> JsonSettingsDocument::open(const FilePath &filePath,
const FilePath &realFilePath)
{
if (!filePath.isReadableFile())
return OpenResult::CannotHandle;
return ResultError(Tr::tr("File not readable"));
Result<QByteArray> contents = realFilePath.fileContents();
if (!contents)
return {OpenResult::CannotHandle, contents.error()};
return ResultError(contents.error());
Result<Store> result = storeFromJson(*contents);
if (!result)
return {OpenResult::CannotHandle, result.error()};
return ResultError(result.error());
setFilePath(filePath);
m_ceSettings.fromMap(*result);
m_ceSettings.fromMap(result.value());
emit settingsChanged();
return OpenResult::Success;
return ResultOk;
}
Result<> JsonSettingsDocument::saveImpl(const FilePath &newFilePath, bool autoSave)

View File

@@ -903,15 +903,13 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const FilePath &file
factory = factories.isEmpty() ? nullptr : factories.takeFirst();
continue;
}
IDocument::OpenResult openResult = editor->document()->open(filePath, realFp);
if (openResult.code == IDocument::OpenResult::Success)
Result<> openResult = editor->document()->open(filePath, realFp);
if (openResult)
break;
errorString = openResult.error;
errorString = openResult.error();
overrideCursor.reset();
delete editor;
editor = nullptr;
// can happen e.g. when trying to open an completely empty .qrc file
QTC_CHECK(openResult.code == IDocument::OpenResult::CannotHandle);
} else {
QTC_ASSERT(factory->isExternalEditor(),
factory = factories.isEmpty() ? nullptr : factories.takeFirst();
@@ -2993,7 +2991,7 @@ void EditorManager::populateOpenWithMenu(QMenu *menu, const FilePath &filePath)
menu->setEnabled(anyMatches);
}
void EditorManager::runWithTemporaryEditor(const Utils::FilePath &filePath,
void EditorManager::runWithTemporaryEditor(const FilePath &filePath,
const std::function<void (IEditor *)> &callback)
{
const MimeType mt = mimeTypeForFile(filePath, MimeMatchMode::MatchDefaultAndRemote);
@@ -3006,7 +3004,7 @@ void EditorManager::runWithTemporaryEditor(const Utils::FilePath &filePath,
if (!editor)
continue;
editor->document()->setTemporary(true);
if (editor->document()->open(filePath, filePath).code != IDocument::OpenResult::Success)
if (!editor->document()->open(filePath, filePath))
continue;
callback(editor.get());
break;

View File

@@ -71,7 +71,7 @@
*/
/*!
\enum Core::IDocument::OpenResult
\enum Core::Utils::Result<>
The OpenResult enum describes whether a file was successfully opened.
@@ -315,11 +315,11 @@ Id IDocument::id() const
\sa shouldAutoSave()
\sa setFilePath()
*/
IDocument::OpenResult IDocument::open(const FilePath &filePath, const FilePath &realFilePath)
Result<> IDocument::open(const FilePath &filePath, const FilePath &realFilePath)
{
Q_UNUSED(filePath)
Q_UNUSED(realFilePath)
return OpenResult::CannotHandle;
return ResultError(ResultUnimplemented);
}
/*!
@@ -807,9 +807,4 @@ QString IDocument::uniqueDisplayName() const
return d->uniqueDisplayName;
}
IDocument::OpenResult::operator Result<>() const
{
return makeResult(code == Success, error);
}
} // namespace Core

View File

@@ -26,24 +26,6 @@ class CORE_EXPORT IDocument : public QObject
Q_OBJECT
public:
class CORE_EXPORT OpenResult
{
public:
enum Code {
Success,
CannotHandle
};
OpenResult() = default;
OpenResult(Code code) : code(code) {}
OpenResult(Code code, const QString &error) : code(code), error(error) {}
operator Utils::Result<>() const;
Code code;
QString error;
};
// This enum must match the indexes of the reloadBehavior widget
// in generalsettings.ui
enum ReloadSetting {
@@ -78,7 +60,7 @@ public:
void setId(Utils::Id id);
Utils::Id id() const;
virtual OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath);
virtual Utils::Result<> open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath);
Utils::Result<> save(const Utils::FilePath &filePath = {}, bool autoSave = false);

View File

@@ -82,8 +82,8 @@ public:
// Open file
QScopedPointer<TextEditor::BaseTextEditor> editor(TextEditor::createPlainTextEditor());
Core::IDocument::OpenResult res = editor->document()->open(document->filePath(), document->filePath());
QVERIFY(res.error.isEmpty());
Result<> res = editor->document()->open(document->filePath(), document->filePath());
QVERIFY(res.has_value());
// Set cursor position
QTextCursor cursor = editor->textCursor();

View File

@@ -43,27 +43,26 @@ FormWindowFile::FormWindowFile(QDesignerFormWindowInterface *form, QObject *pare
m_resourceHandler, &ResourceHandler::updateResources);
}
IDocument::OpenResult FormWindowFile::open(const FilePath &filePath,
const FilePath &realFilePath)
Result<> FormWindowFile::open(const FilePath &filePath, const FilePath &realFilePath)
{
if (Designer::Constants::Internal::debug)
qDebug() << "FormWindowFile::open" << filePath.toUserOutput();
QDesignerFormWindowInterface *form = formWindow();
QTC_ASSERT(form, return OpenResult::CannotHandle);
QTC_ASSERT(form, return ResultError(ResultAssert));
if (filePath.isEmpty())
return OpenResult::CannotHandle;
return ResultError("File name is empty"); // FIXME: Use something better
QString contents;
QString errorString;
TextFileFormat::ReadResult readResult = read(filePath.absoluteFilePath(),
&contents,
&errorString);
if (readResult == Utils::TextFileFormat::ReadEncodingError)
return {OpenResult::CannotHandle, errorString};
if (readResult != Utils::TextFileFormat::ReadSuccess)
return {OpenResult::CannotHandle, errorString};
if (readResult == TextFileFormat::ReadEncodingError)
return ResultError(errorString);
if (readResult != TextFileFormat::ReadSuccess)
return ResultError(errorString);
form->setFileName(filePath.absoluteFilePath().toUrlishString());
const QByteArray contentsBA = contents.toUtf8();
@@ -71,7 +70,7 @@ IDocument::OpenResult FormWindowFile::open(const FilePath &filePath,
str.setData(contentsBA);
str.open(QIODevice::ReadOnly);
if (!form->setContents(&str, &errorString))
return {OpenResult::CannotHandle, errorString};
return ResultError(errorString);
form->setDirty(filePath != realFilePath);
syncXmlFromFormWindow();
@@ -79,7 +78,7 @@ IDocument::OpenResult FormWindowFile::open(const FilePath &filePath,
setShouldAutoSave(false);
resourceHandler()->updateProjectResources();
return OpenResult::Success;
return ResultOk;
}
Result<> FormWindowFile::saveImpl(const FilePath &filePath, bool autoSave)
@@ -201,8 +200,8 @@ Result<> FormWindowFile::reload(ReloadFlag flag, ChangeType type)
return ResultOk;
} else {
emit aboutToReload();
const OpenResult result = open(filePath(), filePath());
emit reloadFinished(result.code == OpenResult::Success);
const Result<> result = open(filePath(), filePath());
emit reloadFinished(result.has_value());
return result;
}
}

View File

@@ -26,8 +26,8 @@ public:
~FormWindowFile() override { }
// IDocument
OpenResult open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
Utils::Result<> open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
QByteArray contents() const override;
bool setContents(const QByteArray &contents) override;
bool shouldAutoSave() const override;

View File

@@ -265,7 +265,7 @@ Result<> DiffEditorDocument::reload(ReloadFlag flag, ChangeType type)
return open(filePath(), filePath());
}
IDocument::OpenResult DiffEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath)
Result<> DiffEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath)
{
QTC_CHECK(filePath == realFilePath); // does not support autosave
beginReload();
@@ -274,7 +274,7 @@ IDocument::OpenResult DiffEditorDocument::open(const FilePath &filePath, const F
ReadResult readResult = read(filePath, &patch, &errorString);
if (readResult == TextFileFormat::ReadIOError
|| readResult == TextFileFormat::ReadMemoryAllocationError) {
return {OpenResult::CannotHandle, errorString};
return ResultError(errorString);
}
const std::optional<QList<FileData>> fileDataList = DiffUtils::readPatch(patch);
@@ -294,8 +294,8 @@ IDocument::OpenResult DiffEditorDocument::open(const FilePath &filePath, const F
if (!ok && readResult == TextFileFormat::ReadEncodingError)
ok = selectEncoding();
if (!ok)
return {OpenResult::CannotHandle, errorString};
return OpenResult::Success;
return ResultError(errorString);
return ResultOk;
}
bool DiffEditorDocument::selectEncoding()

View File

@@ -62,8 +62,8 @@ public:
bool isSaveAsAllowed() const override;
void reload();
Utils::Result<> reload(ReloadFlag flag, ChangeType type) override;
OpenResult open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
Utils::Result<> open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
bool selectEncoding();
State state() const { return m_state; }

View File

@@ -64,26 +64,26 @@ ImageViewerFile::~ImageViewerFile()
cleanUp();
}
IDocument::OpenResult ImageViewerFile::open(const FilePath &filePath, const FilePath &realfilePath)
Result<> ImageViewerFile::open(const FilePath &filePath, const FilePath &realfilePath)
{
QTC_CHECK(filePath == realfilePath); // does not support auto save
OpenResult res = openImpl(filePath);
emit openFinished(res.code == OpenResult::Success);
Result<> res = openImpl(filePath);
emit openFinished(res.has_value());
return res;
}
IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath)
Result<> ImageViewerFile::openImpl(const FilePath &filePath)
{
cleanUp();
if (!filePath.isReadableFile())
return OpenResult::CannotHandle;
return ResultError(Tr::tr("File not readable"));
const QString &fileName = filePath.toUrlishString();
QByteArray format = QImageReader::imageFormat(fileName);
// if it is impossible to recognize a file format - file will not be open correctly
if (format.isEmpty())
return {OpenResult::CannotHandle, Tr::tr("Image format not supported.")};
return ResultError(Tr::tr("Image format not supported."));
#ifndef QT_NO_SVG
if (format.startsWith("svg")) {
@@ -92,7 +92,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath)
if (!bound.isValid() || (qFuzzyIsNull(bound.width()) && qFuzzyIsNull(bound.height()))) {
delete m_tempSvgItem;
m_tempSvgItem = nullptr;
return {OpenResult::CannotHandle, Tr::tr("Failed to read SVG image.")};
return ResultError(Tr::tr("Failed to read SVG image."));
}
m_type = TypeSvg;
emit imageSizeChanged(m_tempSvgItem->boundingRect().size().toSize());
@@ -105,7 +105,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath)
if (!m_movie->isValid()) {
delete m_movie;
m_movie = nullptr;
return {OpenResult::CannotHandle, Tr::tr("Failed to read image.")};
return ResultError(Tr::tr("Failed to read image."));
}
m_type = TypeMovie;
connect(m_movie, &QMovie::resized, this, &ImageViewerFile::imageSizeChanged);
@@ -115,7 +115,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath)
if (m_pixmap->isNull()) {
delete m_pixmap;
m_pixmap = nullptr;
return {OpenResult::CannotHandle, Tr::tr("Failed to read image.")};
return ResultError(Tr::tr("Failed to read image."));
}
m_type = TypePixmap;
emit imageSizeChanged(m_pixmap->size());
@@ -123,7 +123,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath)
setFilePath(filePath);
setMimeType(Utils::mimeTypeForFile(filePath).name());
return OpenResult::Success;
return ResultOk;
}
Core::IDocument::ReloadBehavior ImageViewerFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
@@ -141,8 +141,8 @@ Result<> ImageViewerFile::reload(IDocument::ReloadFlag flag, IDocument::ChangeTy
if (flag == FlagIgnore)
return ResultOk;
emit aboutToReload();
const OpenResult result = openImpl(filePath());
emit reloadFinished( result.code == OpenResult::Success);
const Result<> result = openImpl(filePath());
emit reloadFinished(result.has_value());
return result;
}

View File

@@ -35,7 +35,8 @@ public:
ImageViewerFile();
~ImageViewerFile() override;
OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override;
Utils::Result<> open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override;
Utils::Result<> reload(ReloadFlag flag, ChangeType type) override;
@@ -54,7 +55,7 @@ signals:
private:
void cleanUp();
OpenResult openImpl(const Utils::FilePath &filePath);
Utils::Result<> openImpl(const Utils::FilePath &filePath);
ImageType m_type = TypeInvalid;
#ifndef QT_NO_SVG

View File

@@ -42,13 +42,10 @@ ModelDocument::~ModelDocument()
delete d;
}
Core::IDocument::OpenResult ModelDocument::open(const FilePath &filePath,
const FilePath &realFilePath)
Result<> ModelDocument::open(const FilePath &filePath, const FilePath &realFilePath)
{
Q_UNUSED(filePath)
OpenResult result = load(realFilePath);
return result;
return load(realFilePath);
}
Result<> ModelDocument::saveImpl(const FilePath &filePath, bool autoSave)
@@ -111,7 +108,7 @@ ExtDocumentController *ModelDocument::documentController() const
return d->documentController;
}
Core::IDocument::OpenResult ModelDocument::load(const FilePath &fileName)
Result<> ModelDocument::load(const FilePath &fileName)
{
d->documentController = ModelEditorPlugin::modelsManager()->createModel(this);
connect(d->documentController, &qmt::DocumentController::changed, this, &IDocument::changed);
@@ -120,11 +117,10 @@ Core::IDocument::OpenResult ModelDocument::load(const FilePath &fileName)
d->documentController->loadProject(fileName);
setFilePath(d->documentController->projectController()->project()->fileName());
} catch (const qmt::FileNotFoundException &ex) {
return {OpenResult::CannotHandle, ex.errorMessage()};
return ResultError(ex.errorMessage());
} catch (const qmt::Exception &ex) {
return {OpenResult::CannotHandle,
Tr::tr("Could not open \"%1\" for reading: %2.")
.arg(fileName.toUserOutput(), ex.errorMessage())};
return ResultError(Tr::tr("Could not open \"%1\" for reading: %2.")
.arg(fileName.toUserOutput(), ex.errorMessage()));
}
FilePath configPath = d->documentController->projectController()->project()->configPath();
@@ -139,7 +135,7 @@ Core::IDocument::OpenResult ModelDocument::load(const FilePath &fileName)
}
emit contentSet();
return OpenResult::Success;
return ResultOk;
}
} // namespace ModelEditor::Internal

View File

@@ -12,8 +12,7 @@ namespace ModelEditor::Internal {
class ExtDocumentController;
class ModelDocument :
public Core::IDocument
class ModelDocument : public Core::IDocument
{
Q_OBJECT
class ModelDocumentPrivate;
@@ -26,8 +25,8 @@ signals:
void contentSet();
public:
OpenResult open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
Utils::Result<> open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
bool shouldAutoSave() const override;
bool isModified() const override;
bool isSaveAsAllowed() const override;
@@ -35,7 +34,7 @@ public:
ExtDocumentController *documentController() const;
OpenResult load(const Utils::FilePath &fileName);
Utils::Result<> load(const Utils::FilePath &fileName);
protected:
Utils::Result<> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;

View File

@@ -89,13 +89,13 @@ ResourceFile::~ResourceFile()
clearPrefixList();
}
Core::IDocument::OpenResult ResourceFile::load()
Result<> ResourceFile::load()
{
m_error_message.clear();
if (m_filePath.isEmpty()) {
m_error_message = Tr::tr("The file name is empty.");
return Core::IDocument::OpenResult::CannotHandle;
return ResultError(m_error_message);
}
clearPrefixList();
@@ -108,7 +108,7 @@ Core::IDocument::OpenResult ResourceFile::load()
QFile file(m_filePath.toUrlishString());
if (!file.open(QIODevice::ReadOnly)) {
m_error_message = file.errorString();
return Core::IDocument::OpenResult::CannotHandle;
return ResultError(m_error_message);
}
QByteArray data = file.readAll();
// Detect line ending style
@@ -122,7 +122,7 @@ Core::IDocument::OpenResult ResourceFile::load()
if (!doc.setContent(data, &error_msg, &error_line, &error_col)) {
m_error_message = Tr::tr("XML error on line %1, col %2: %3")
.arg(error_line).arg(error_col).arg(error_msg);
return Core::IDocument::OpenResult::CannotHandle;
return ResultError(m_error_message);
}
} else {
@@ -133,7 +133,7 @@ Core::IDocument::OpenResult ResourceFile::load()
if (!doc.setContent(m_contents, &error_msg, &error_line, &error_col)) {
m_error_message = Tr::tr("XML error on line %1, col %2: %3")
.arg(error_line).arg(error_col).arg(error_msg);
return Core::IDocument::OpenResult::CannotHandle;
return ResultError(m_error_message);
}
}
@@ -141,7 +141,7 @@ Core::IDocument::OpenResult ResourceFile::load()
QDomElement root = doc.firstChildElement(QLatin1String("RCC"));
if (root.isNull()) {
m_error_message = Tr::tr("The <RCC> root element is missing.");
return Core::IDocument::OpenResult::CannotHandle;
return ResultError(m_error_message);
}
QDomElement relt = root.firstChildElement(QLatin1String("qresource"));
@@ -174,7 +174,7 @@ Core::IDocument::OpenResult ResourceFile::load()
}
}
return Core::IDocument::OpenResult::Success;
return ResultOk;
}
QString ResourceFile::contents() const
@@ -1078,11 +1078,11 @@ QModelIndex ResourceModel::deleteItem(const QModelIndex &idx)
return index(file_idx, 0, prefix_model_idx);
}
Core::IDocument::OpenResult ResourceModel::reload()
Result<> ResourceModel::reload()
{
beginResetModel();
Core::IDocument::OpenResult result = m_resource_file.load();
if (result.code == Core::IDocument::OpenResult::Success)
Result<> result = m_resource_file.load();
if (result.has_value())
setDirty(false);
endResetModel();
return result;

View File

@@ -107,7 +107,7 @@ public:
void setFilePath(const Utils::FilePath &filePath) { m_filePath = filePath; }
Utils::FilePath filePath() const { return m_filePath; }
Core::IDocument::OpenResult load();
Utils::Result<> load();
bool save();
QString contents() const;
QString errorMessage() const { return m_error_message; }
@@ -227,7 +227,7 @@ private:
bool renameFile(const QString &fileName, const QString &newFileName);
public:
virtual Core::IDocument::OpenResult reload();
virtual Utils::Result<> reload();
virtual bool save();
QString contents() const { return m_resource_file.contents(); }

View File

@@ -48,7 +48,7 @@ class ResourceEditorDocument final : public IDocument
public:
ResourceEditorDocument(QObject *parent = nullptr);
OpenResult open(const FilePath &filePath, const FilePath &realFilePath) final;
Result<> open(const FilePath &filePath, const FilePath &realFilePath) final;
QString plainText() const { return m_model.contents(); }
QByteArray contents() const final { return m_model.contents().toUtf8(); }
bool setContents(const QByteArray &contents) final;
@@ -179,8 +179,7 @@ ResourceEditorImpl::~ResourceEditorImpl()
delete m_toolBar;
}
IDocument::OpenResult ResourceEditorDocument::open(const FilePath &filePath,
const FilePath &realFilePath)
Result<> ResourceEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath)
{
if (debugResourceEditorW)
qDebug() << "ResourceEditorW::open: " << filePath;
@@ -189,9 +188,9 @@ IDocument::OpenResult ResourceEditorDocument::open(const FilePath &filePath,
m_model.setFilePath(realFilePath);
OpenResult openResult = m_model.reload();
if (openResult.code != OpenResult::Success) {
openResult.error = m_model.errorMessage();
Result<> openResult = m_model.reload();
if (!openResult) {
openResult = ResultError(m_model.errorMessage()); // FIXME: Move to m_model
setBlockDirtyChanged(false);
emit loaded(false);
return openResult;
@@ -203,7 +202,7 @@ IDocument::OpenResult ResourceEditorDocument::open(const FilePath &filePath,
m_shouldAutoSave = false;
emit loaded(true);
return OpenResult::Success;
return ResultOk;
}
Result<> ResourceEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
@@ -246,7 +245,7 @@ bool ResourceEditorDocument::setContents(const QByteArray &contents)
const FilePath originalFileName = m_model.filePath();
m_model.setFilePath(saver.filePath());
const bool success = (m_model.reload().code == OpenResult::Success);
const bool success = (m_model.reload().has_value());
m_model.setFilePath(originalFileName);
m_shouldAutoSave = false;
if (debugResourceEditorW)
@@ -283,8 +282,8 @@ Result<> ResourceEditorDocument::reload(ReloadFlag flag, ChangeType type)
if (flag == FlagIgnore)
return ResultOk;
emit aboutToReload();
const OpenResult result = open(filePath(), filePath());
emit reloadFinished(result.code == OpenResult::Success);
const Result<> result = open(filePath(), filePath());
emit reloadFinished(result.has_value());
return result;
}

View File

@@ -121,7 +121,7 @@ static bool addFilesToResource(const FilePath &resourceFile,
*notAdded = filePaths;
ResourceFile file(resourceFile);
if (file.load().code != IDocument::OpenResult::Success)
if (!file.load())
return false;
int index = file.indexOfPrefix(prefix, lang);
@@ -272,7 +272,7 @@ static void compressTree(FolderNode *n)
void ResourceTopLevelNode::addInternalNodes()
{
ResourceFile file(filePath(), m_contents);
if (file.load().code != IDocument::OpenResult::Success)
if (!file.load())
return;
QMap<PrefixFolderLang, FolderNode *> folderNodes;
@@ -382,7 +382,7 @@ RemovedFilesFromProject ResourceTopLevelNode::removeFiles(const FilePaths &fileP
bool ResourceTopLevelNode::addPrefix(const QString &prefix, const QString &lang)
{
ResourceFile file(filePath());
if (file.load().code != IDocument::OpenResult::Success)
if (!file.load())
return false;
int index = file.addPrefix(prefix, lang);
if (index == -1)
@@ -395,7 +395,7 @@ bool ResourceTopLevelNode::addPrefix(const QString &prefix, const QString &lang)
bool ResourceTopLevelNode::removePrefix(const QString &prefix, const QString &lang)
{
ResourceFile file(filePath());
if (file.load().code != IDocument::OpenResult::Success)
if (!file.load())
return false;
for (int i = 0; i < file.prefixCount(); ++i) {
if (file.prefix(i) == prefix
@@ -411,7 +411,7 @@ bool ResourceTopLevelNode::removePrefix(const QString &prefix, const QString &la
bool ResourceTopLevelNode::removeNonExistingFiles()
{
ResourceFile file(filePath());
if (file.load().code != IDocument::OpenResult::Success)
if (!file.load())
return false;
QFileInfo fi;
@@ -490,7 +490,7 @@ RemovedFilesFromProject ResourceFolderNode::removeFiles(const FilePaths &filePat
if (notRemoved)
*notRemoved = filePaths;
ResourceFile file(m_topLevelNode->filePath());
if (file.load().code != IDocument::OpenResult::Success)
if (!file.load())
return RemovedFilesFromProject::Error;
int index = file.indexOfPrefix(m_prefix, m_lang);
if (index == -1)
@@ -518,8 +518,7 @@ bool ResourceFolderNode::canRenameFile(const FilePath &oldFilePath, const FilePa
bool fileEntryExists = false;
ResourceFile file(m_topLevelNode->filePath());
int index = (file.load().code != IDocument::OpenResult::Success)
? -1 : file.indexOfPrefix(m_prefix, m_lang);
int index = file.load() ? file.indexOfPrefix(m_prefix, m_lang) : -1;
if (index != -1) {
for (int j = 0; j < file.fileCount(index); ++j) {
if (file.file(index, j) == oldFilePath.toUrlishString()) {
@@ -535,7 +534,7 @@ bool ResourceFolderNode::canRenameFile(const FilePath &oldFilePath, const FilePa
bool ResourceFolderNode::renameFiles(const FilePairs &filesToRename, FilePaths *notRenamed)
{
ResourceFile file(m_topLevelNode->filePath());
if (file.load().code != IDocument::OpenResult::Success)
if (!file.load())
return false;
int index = file.indexOfPrefix(m_prefix, m_lang);
if (index == -1)
@@ -567,7 +566,7 @@ bool ResourceFolderNode::renameFiles(const FilePairs &filesToRename, FilePaths *
bool ResourceFolderNode::renamePrefix(const QString &prefix, const QString &lang)
{
ResourceFile file(m_topLevelNode->filePath());
if (file.load().code != IDocument::OpenResult::Success)
if (!file.load())
return false;
int index = file.indexOfPrefix(m_prefix, m_lang);
if (index == -1)

View File

@@ -35,24 +35,23 @@ ScxmlEditorDocument::ScxmlEditorDocument(MainWidget *designWidget, QObject *pare
});
}
Core::IDocument::OpenResult ScxmlEditorDocument::open(const FilePath &filePath,
const FilePath &realFilePath)
Result<> ScxmlEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath)
{
Q_UNUSED(realFilePath)
if (filePath.isEmpty())
return OpenResult::CannotHandle;
return ResultError("File path is empty"); // FIXME: Use something better
if (!m_designWidget)
return OpenResult::CannotHandle;
return ResultError(ResultAssert);
const FilePath &absoluteFilePath = filePath.absoluteFilePath();
if (!m_designWidget->load(absoluteFilePath.toUrlishString()))
return {OpenResult::CannotHandle, m_designWidget->errorMessage()};
return ResultError(m_designWidget->errorMessage());
setFilePath(absoluteFilePath);
return OpenResult::Success;
return ResultOk;
}
Result<> ScxmlEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)

View File

@@ -25,7 +25,8 @@ public:
explicit ScxmlEditorDocument(Common::MainWidget *designWidget, QObject *parent = nullptr);
// IDocument
OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override;
Utils::Result<> open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
bool shouldAutoSave() const override;
bool isSaveAsAllowed() const override;
bool isModified() const override;

View File

@@ -28,11 +28,10 @@ ObjectsMapDocument::ObjectsMapDocument()
connect(m_contentModel, &ObjectsMapModel::modelChanged, this, [this] { setModified(true); });
}
IDocument::OpenResult ObjectsMapDocument::open(const FilePath &fileName,
const FilePath &realFileName)
Result<> ObjectsMapDocument::open(const FilePath &fileName, const FilePath &realFileName)
{
OpenResult result = openImpl(fileName, realFileName);
if (result.code == OpenResult::Success) {
Result<> result = openImpl(fileName, realFileName);
if (result.has_value()) {
setFilePath(fileName);
setModified(fileName != realFileName);
}
@@ -77,11 +76,10 @@ Result<> ObjectsMapDocument::reload(IDocument::ReloadFlag flag, IDocument::Chang
if (flag == FlagIgnore)
return ResultOk;
emit aboutToReload();
const OpenResult result = openImpl(filePath(), filePath());
const bool success = result.code == OpenResult::Success;
if (success)
const Result<> result = openImpl(filePath(), filePath());
if (result.has_value())
setModified(false);
emit reloadFinished(success);
emit reloadFinished(result.has_value());
return result;
}
@@ -173,28 +171,27 @@ QByteArray ObjectsMapDocument::contents() const
return result;
}
IDocument::OpenResult ObjectsMapDocument::openImpl(const FilePath &fileName,
const FilePath &realFileName)
Result<> ObjectsMapDocument::openImpl(const FilePath &fileName, const FilePath &realFileName)
{
if (fileName.isEmpty())
return OpenResult::CannotHandle;
return ResultError("File name is empty"); // FIXME: Find somethong better
QByteArray text;
if (realFileName.fileName() == "objects.map") {
FileReader reader;
if (const Result<> res = reader.fetch(realFileName); !res)
return {OpenResult::CannotHandle, res.error()};
return res;
text = reader.text();
} else {
const FilePath base = settings().squishPath();
if (base.isEmpty()) {
return {OpenResult::CannotHandle, Tr::tr("Incomplete Squish settings. "
"Missing Squish installation path.")};
return ResultError(Tr::tr("Incomplete Squish settings. "
"Missing Squish installation path."));
}
const FilePath exe = base.pathAppended("lib/exec/objectmaptool").withExecutableSuffix();
if (!exe.isExecutableFile())
return {OpenResult::CannotHandle, Tr::tr("objectmaptool not found.")};
return ResultError(Tr::tr("objectmaptool not found."));
Process objectMapReader;
@@ -206,8 +203,8 @@ IDocument::OpenResult ObjectsMapDocument::openImpl(const FilePath &fileName,
text = objectMapReader.cleanedStdOut().toUtf8();
}
if (!setContents(text))
return {OpenResult::CannotHandle, Tr::tr("Failure while parsing objects.map content.")};
return OpenResult::Success;
return ResultError(Tr::tr("Failure while parsing objects.map content."));
return ResultOk;
}
bool ObjectsMapDocument::writeFile(const Utils::FilePath &fileName) const

View File

@@ -14,11 +14,12 @@ class ObjectsMapModel;
class ObjectsMapDocument : public Core::IDocument
{
Q_OBJECT
public:
ObjectsMapDocument();
OpenResult open(const Utils::FilePath &fileName,
const Utils::FilePath &realFileName) override;
Utils::Result<> open(const Utils::FilePath &fileName,
const Utils::FilePath &realFileName) override;
Utils::FilePath fallbackSaveAsPath() const override;
QString fallbackSaveAsFileName() const override;
bool isModified() const override { return m_isModified; }
@@ -35,8 +36,8 @@ protected:
Utils::Result<> saveImpl(const Utils::FilePath &fileName, bool autoSave) override;
private:
OpenResult openImpl(const Utils::FilePath &fileName,
const Utils::FilePath &realFileName);
Utils::Result<> openImpl(const Utils::FilePath &fileName,
const Utils::FilePath &realFileName);
bool buildObjectsMapTree(const QByteArray &contents);
bool writeFile(const Utils::FilePath &fileName) const;
void syncXMLFromEditor();

View File

@@ -754,11 +754,11 @@ bool TextDocument::isModified() const
return d->m_document.isModified();
}
IDocument::OpenResult TextDocument::open(const FilePath &filePath, const FilePath &realFilePath)
Result<> TextDocument::open(const FilePath &filePath, const FilePath &realFilePath)
{
emit aboutToOpen(filePath, realFilePath);
OpenResult result = openImpl(filePath, realFilePath, /*reload =*/ false);
if (result.code == OpenResult::Success) {
const Result<> result = openImpl(filePath, realFilePath, /*reload =*/ false);
if (result) {
setMimeType(Utils::mimeTypeForFile(filePath, MimeMatchMode::MatchDefaultAndRemote).name());
setTabSettings(d->m_tabSettings);
emit openFinishedSuccessfully();
@@ -766,9 +766,9 @@ IDocument::OpenResult TextDocument::open(const FilePath &filePath, const FilePat
return result;
}
IDocument::OpenResult TextDocument::openImpl(const FilePath &filePath,
const FilePath &realFilePath,
bool reload)
Result<> TextDocument::openImpl(const FilePath &filePath,
const FilePath &realFilePath,
bool reload)
{
QStringList content;
QString errorString;
@@ -820,15 +820,15 @@ IDocument::OpenResult TextDocument::openImpl(const FilePath &filePath,
auto documentLayout =
qobject_cast<TextDocumentLayout*>(d->m_document.documentLayout());
QTC_ASSERT(documentLayout, return OpenResult::CannotHandle);
QTC_ASSERT(documentLayout, return ResultError(ResultAssert));
documentLayout->lastSaveRevision = d->m_autoSaveRevision = d->m_document.revision();
d->updateRevisions();
d->m_document.setModified(filePath != realFilePath);
setFilePath(filePath);
}
if (readResult == Utils::TextFileFormat::ReadIOError)
return {OpenResult::CannotHandle, errorString};
return OpenResult::Success;
if (readResult == TextFileFormat::ReadIOError)
return ResultError(errorString);
return ResultOk;
}
Result<> TextDocument::reload(const QByteArray &codec)
@@ -851,11 +851,11 @@ Result<> TextDocument::reload(const FilePath &realFilePath)
if (documentLayout)
documentLayout->documentAboutToReload(this); // removes text marks non-permanently
const OpenResult result = openImpl(filePath(), realFilePath, /*reload =*/true);
const Result<> result = openImpl(filePath(), realFilePath, /*reload =*/true);
if (documentLayout)
documentLayout->documentReloaded(this); // re-adds text marks
emit reloadFinished(result.code == OpenResult::Success);
emit reloadFinished(result.has_value());
return result;
}

View File

@@ -117,7 +117,8 @@ public:
void setFallbackSaveAsPath(const Utils::FilePath &fallbackSaveAsPath);
void setFallbackSaveAsFileName(const QString &fallbackSaveAsFileName);
OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override;
Utils::Result<> open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
virtual Utils::Result<> reload();
Utils::Result<> reload(const Utils::FilePath &realFilePath);
@@ -169,9 +170,9 @@ protected:
virtual void slotCodeStyleSettingsChanged(); // Used in CppEditorDocumet
private:
OpenResult openImpl(const Utils::FilePath &filePath,
const Utils::FilePath &realFileName,
bool reload);
Utils::Result<> openImpl(const Utils::FilePath &filePath,
const Utils::FilePath &realFileName,
bool reload);
void cleanWhitespace(QTextCursor &cursor, bool inEntireDocument, bool cleanIndentation);
void ensureFinalNewLine(QTextCursor &cursor);
void modificationChanged(bool modified);

View File

@@ -27,22 +27,22 @@ SubmitEditorFile::SubmitEditorFile(VcsBaseSubmitEditor *editor) :
this, &IDocument::contentsChanged);
}
IDocument::OpenResult SubmitEditorFile::open(const FilePath &filePath, const FilePath &realFilePath)
Result<> SubmitEditorFile::open(const FilePath &filePath, const FilePath &realFilePath)
{
if (filePath.isEmpty())
return OpenResult::CannotHandle;
return ResultError("File name is empty"); // FIXME: Use something better
FileReader reader;
if (const Result<> res = reader.fetch(realFilePath); !res)
return {OpenResult::CannotHandle, res.error()};
return res;
const QString text = QString::fromLocal8Bit(reader.text());
if (!m_editor->setFileContents(text.toUtf8()))
return OpenResult::CannotHandle;
return ResultError("Cannot set file contents"); // FIXME: Use something better
setFilePath(filePath.absoluteFilePath());
setModified(filePath != realFilePath);
return OpenResult::Success;
return ResultOk;
}
QByteArray SubmitEditorFile::contents() const

View File

@@ -17,7 +17,8 @@ class SubmitEditorFile : public Core::IDocument
public:
explicit SubmitEditorFile(VcsBaseSubmitEditor *editor);
OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override;
Utils::Result<> open(const Utils::FilePath &filePath,
const Utils::FilePath &realFilePath) override;
QByteArray contents() const override;
bool setContents(const QByteArray &contents) override;