forked from qt-creator/qt-creator
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:
@@ -97,13 +97,13 @@ public:
|
|||||||
return type == TypeRemoved ? BehaviorSilent : IDocument::reloadBehavior(state, type);
|
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
|
QTC_CHECK(filePath == realFilePath); // The bineditor can do no autosaving
|
||||||
return openImpl(filePath);
|
return openImpl(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenResult openImpl(const FilePath &filePath, quint64 offset = 0);
|
Result<> openImpl(const FilePath &filePath, quint64 offset = 0);
|
||||||
|
|
||||||
void provideData(quint64 address);
|
void provideData(quint64 address);
|
||||||
|
|
||||||
@@ -2118,35 +2118,27 @@ bool BinEditorDocument::setContents(const QByteArray &contents)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
IDocument::OpenResult BinEditorDocument::openImpl(const FilePath &filePath, quint64 offset)
|
Result<> BinEditorDocument::openImpl(const FilePath &filePath, quint64 offset)
|
||||||
{
|
{
|
||||||
const qint64 size = filePath.fileSize();
|
const qint64 size = filePath.fileSize();
|
||||||
if (size < 0) {
|
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.
|
// FIXME: Was: file.errorString(), but we don't have a file anymore.
|
||||||
QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg);
|
return ResultError(Tr::tr("Cannot open %1: %2").arg(filePath.toUserOutput()));
|
||||||
return {OpenResult::CannotHandle, msg};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size == 0) {
|
if (size == 0)
|
||||||
QString msg = Tr::tr("The Binary Editor cannot open empty files.");
|
return ResultError(Tr::tr("The Binary Editor cannot open empty files."));
|
||||||
QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg);
|
|
||||||
return {OpenResult::CannotHandle, msg};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size / 16 >= qint64(1) << 31) {
|
// The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line.
|
||||||
// The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line.
|
if (size / 16 >= qint64(1) << 31)
|
||||||
QString msg = Tr::tr("The file is too big for the Binary Editor (max. 32GB).");
|
return ResultError(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};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (offset >= quint64(size))
|
if (offset >= quint64(size))
|
||||||
return OpenResult::CannotHandle;
|
return ResultError(Tr::tr("File offset too large"));
|
||||||
|
|
||||||
setFilePath(filePath);
|
setFilePath(filePath);
|
||||||
setSizes(offset, size);
|
setSizes(offset, size);
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BinEditorDocument::provideData(quint64 address)
|
void BinEditorDocument::provideData(quint64 address)
|
||||||
@@ -2179,8 +2171,8 @@ Result<> BinEditorDocument::reload(ReloadFlag flag, ChangeType type)
|
|||||||
return ResultOk;
|
return ResultOk;
|
||||||
emit aboutToReload();
|
emit aboutToReload();
|
||||||
clear();
|
clear();
|
||||||
const OpenResult result = openImpl(filePath());
|
const Result<> result = openImpl(filePath());
|
||||||
emit reloadFinished(result.code == OpenResult::Success);
|
emit reloadFinished(result.has_value());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -272,8 +272,7 @@ void ClangFormatConfigWidget::reopenClangFormatDocument()
|
|||||||
{
|
{
|
||||||
GuardLocker locker(m_ignoreChanges);
|
GuardLocker locker(m_ignoreChanges);
|
||||||
|
|
||||||
if (m_editor->document()->open(m_config->filePath(), m_config->filePath()).code
|
if (m_editor->document()->open(m_config->filePath(), m_config->filePath())) {
|
||||||
== Core::IDocument::OpenResult::Success) {
|
|
||||||
invokeMethodForLanguageClientManager("documentOpened",
|
invokeMethodForLanguageClientManager("documentOpened",
|
||||||
Q_ARG(Core::IDocument *, m_editor->document()));
|
Q_ARG(Core::IDocument *, m_editor->document()));
|
||||||
}
|
}
|
||||||
|
@@ -151,8 +151,8 @@ class JsonSettingsDocument : public Core::IDocument
|
|||||||
public:
|
public:
|
||||||
JsonSettingsDocument(QUndoStack *undoStack);
|
JsonSettingsDocument(QUndoStack *undoStack);
|
||||||
|
|
||||||
OpenResult open(const Utils::FilePath &filePath,
|
Result<> open(const Utils::FilePath &filePath,
|
||||||
const Utils::FilePath &realFilePath) override;
|
const Utils::FilePath &realFilePath) override;
|
||||||
|
|
||||||
Result<> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
|
Result<> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
|
||||||
|
|
||||||
@@ -380,25 +380,25 @@ JsonSettingsDocument::JsonSettingsDocument(QUndoStack *undoStack)
|
|||||||
m_ceSettings.setUndoStack(undoStack);
|
m_ceSettings.setUndoStack(undoStack);
|
||||||
}
|
}
|
||||||
|
|
||||||
IDocument::OpenResult JsonSettingsDocument::open(const FilePath &filePath,
|
Result<> JsonSettingsDocument::open(const FilePath &filePath,
|
||||||
const FilePath &realFilePath)
|
const FilePath &realFilePath)
|
||||||
{
|
{
|
||||||
if (!filePath.isReadableFile())
|
if (!filePath.isReadableFile())
|
||||||
return OpenResult::CannotHandle;
|
return ResultError(Tr::tr("File not readable"));
|
||||||
|
|
||||||
Result<QByteArray> contents = realFilePath.fileContents();
|
Result<QByteArray> contents = realFilePath.fileContents();
|
||||||
if (!contents)
|
if (!contents)
|
||||||
return {OpenResult::CannotHandle, contents.error()};
|
return ResultError(contents.error());
|
||||||
|
|
||||||
Result<Store> result = storeFromJson(*contents);
|
Result<Store> result = storeFromJson(*contents);
|
||||||
if (!result)
|
if (!result)
|
||||||
return {OpenResult::CannotHandle, result.error()};
|
return ResultError(result.error());
|
||||||
|
|
||||||
setFilePath(filePath);
|
setFilePath(filePath);
|
||||||
|
|
||||||
m_ceSettings.fromMap(*result);
|
m_ceSettings.fromMap(result.value());
|
||||||
emit settingsChanged();
|
emit settingsChanged();
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> JsonSettingsDocument::saveImpl(const FilePath &newFilePath, bool autoSave)
|
Result<> JsonSettingsDocument::saveImpl(const FilePath &newFilePath, bool autoSave)
|
||||||
|
@@ -903,15 +903,13 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const FilePath &file
|
|||||||
factory = factories.isEmpty() ? nullptr : factories.takeFirst();
|
factory = factories.isEmpty() ? nullptr : factories.takeFirst();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
IDocument::OpenResult openResult = editor->document()->open(filePath, realFp);
|
Result<> openResult = editor->document()->open(filePath, realFp);
|
||||||
if (openResult.code == IDocument::OpenResult::Success)
|
if (openResult)
|
||||||
break;
|
break;
|
||||||
errorString = openResult.error;
|
errorString = openResult.error();
|
||||||
overrideCursor.reset();
|
overrideCursor.reset();
|
||||||
delete editor;
|
delete editor;
|
||||||
editor = nullptr;
|
editor = nullptr;
|
||||||
// can happen e.g. when trying to open an completely empty .qrc file
|
|
||||||
QTC_CHECK(openResult.code == IDocument::OpenResult::CannotHandle);
|
|
||||||
} else {
|
} else {
|
||||||
QTC_ASSERT(factory->isExternalEditor(),
|
QTC_ASSERT(factory->isExternalEditor(),
|
||||||
factory = factories.isEmpty() ? nullptr : factories.takeFirst();
|
factory = factories.isEmpty() ? nullptr : factories.takeFirst();
|
||||||
@@ -2993,7 +2991,7 @@ void EditorManager::populateOpenWithMenu(QMenu *menu, const FilePath &filePath)
|
|||||||
menu->setEnabled(anyMatches);
|
menu->setEnabled(anyMatches);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditorManager::runWithTemporaryEditor(const Utils::FilePath &filePath,
|
void EditorManager::runWithTemporaryEditor(const FilePath &filePath,
|
||||||
const std::function<void (IEditor *)> &callback)
|
const std::function<void (IEditor *)> &callback)
|
||||||
{
|
{
|
||||||
const MimeType mt = mimeTypeForFile(filePath, MimeMatchMode::MatchDefaultAndRemote);
|
const MimeType mt = mimeTypeForFile(filePath, MimeMatchMode::MatchDefaultAndRemote);
|
||||||
@@ -3006,7 +3004,7 @@ void EditorManager::runWithTemporaryEditor(const Utils::FilePath &filePath,
|
|||||||
if (!editor)
|
if (!editor)
|
||||||
continue;
|
continue;
|
||||||
editor->document()->setTemporary(true);
|
editor->document()->setTemporary(true);
|
||||||
if (editor->document()->open(filePath, filePath).code != IDocument::OpenResult::Success)
|
if (!editor->document()->open(filePath, filePath))
|
||||||
continue;
|
continue;
|
||||||
callback(editor.get());
|
callback(editor.get());
|
||||||
break;
|
break;
|
||||||
|
@@ -71,7 +71,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\enum Core::IDocument::OpenResult
|
\enum Core::Utils::Result<>
|
||||||
|
|
||||||
The OpenResult enum describes whether a file was successfully opened.
|
The OpenResult enum describes whether a file was successfully opened.
|
||||||
|
|
||||||
@@ -315,11 +315,11 @@ Id IDocument::id() const
|
|||||||
\sa shouldAutoSave()
|
\sa shouldAutoSave()
|
||||||
\sa setFilePath()
|
\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(filePath)
|
||||||
Q_UNUSED(realFilePath)
|
Q_UNUSED(realFilePath)
|
||||||
return OpenResult::CannotHandle;
|
return ResultError(ResultUnimplemented);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -807,9 +807,4 @@ QString IDocument::uniqueDisplayName() const
|
|||||||
return d->uniqueDisplayName;
|
return d->uniqueDisplayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
IDocument::OpenResult::operator Result<>() const
|
|
||||||
{
|
|
||||||
return makeResult(code == Success, error);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
@@ -26,24 +26,6 @@ class CORE_EXPORT IDocument : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
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
|
// This enum must match the indexes of the reloadBehavior widget
|
||||||
// in generalsettings.ui
|
// in generalsettings.ui
|
||||||
enum ReloadSetting {
|
enum ReloadSetting {
|
||||||
@@ -78,7 +60,7 @@ public:
|
|||||||
void setId(Utils::Id id);
|
void setId(Utils::Id id);
|
||||||
Utils::Id id() const;
|
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);
|
Utils::Result<> save(const Utils::FilePath &filePath = {}, bool autoSave = false);
|
||||||
|
|
||||||
|
@@ -82,8 +82,8 @@ public:
|
|||||||
|
|
||||||
// Open file
|
// Open file
|
||||||
QScopedPointer<TextEditor::BaseTextEditor> editor(TextEditor::createPlainTextEditor());
|
QScopedPointer<TextEditor::BaseTextEditor> editor(TextEditor::createPlainTextEditor());
|
||||||
Core::IDocument::OpenResult res = editor->document()->open(document->filePath(), document->filePath());
|
Result<> res = editor->document()->open(document->filePath(), document->filePath());
|
||||||
QVERIFY(res.error.isEmpty());
|
QVERIFY(res.has_value());
|
||||||
|
|
||||||
// Set cursor position
|
// Set cursor position
|
||||||
QTextCursor cursor = editor->textCursor();
|
QTextCursor cursor = editor->textCursor();
|
||||||
|
@@ -43,27 +43,26 @@ FormWindowFile::FormWindowFile(QDesignerFormWindowInterface *form, QObject *pare
|
|||||||
m_resourceHandler, &ResourceHandler::updateResources);
|
m_resourceHandler, &ResourceHandler::updateResources);
|
||||||
}
|
}
|
||||||
|
|
||||||
IDocument::OpenResult FormWindowFile::open(const FilePath &filePath,
|
Result<> FormWindowFile::open(const FilePath &filePath, const FilePath &realFilePath)
|
||||||
const FilePath &realFilePath)
|
|
||||||
{
|
{
|
||||||
if (Designer::Constants::Internal::debug)
|
if (Designer::Constants::Internal::debug)
|
||||||
qDebug() << "FormWindowFile::open" << filePath.toUserOutput();
|
qDebug() << "FormWindowFile::open" << filePath.toUserOutput();
|
||||||
|
|
||||||
QDesignerFormWindowInterface *form = formWindow();
|
QDesignerFormWindowInterface *form = formWindow();
|
||||||
QTC_ASSERT(form, return OpenResult::CannotHandle);
|
QTC_ASSERT(form, return ResultError(ResultAssert));
|
||||||
|
|
||||||
if (filePath.isEmpty())
|
if (filePath.isEmpty())
|
||||||
return OpenResult::CannotHandle;
|
return ResultError("File name is empty"); // FIXME: Use something better
|
||||||
|
|
||||||
QString contents;
|
QString contents;
|
||||||
QString errorString;
|
QString errorString;
|
||||||
TextFileFormat::ReadResult readResult = read(filePath.absoluteFilePath(),
|
TextFileFormat::ReadResult readResult = read(filePath.absoluteFilePath(),
|
||||||
&contents,
|
&contents,
|
||||||
&errorString);
|
&errorString);
|
||||||
if (readResult == Utils::TextFileFormat::ReadEncodingError)
|
if (readResult == TextFileFormat::ReadEncodingError)
|
||||||
return {OpenResult::CannotHandle, errorString};
|
return ResultError(errorString);
|
||||||
if (readResult != Utils::TextFileFormat::ReadSuccess)
|
if (readResult != TextFileFormat::ReadSuccess)
|
||||||
return {OpenResult::CannotHandle, errorString};
|
return ResultError(errorString);
|
||||||
|
|
||||||
form->setFileName(filePath.absoluteFilePath().toUrlishString());
|
form->setFileName(filePath.absoluteFilePath().toUrlishString());
|
||||||
const QByteArray contentsBA = contents.toUtf8();
|
const QByteArray contentsBA = contents.toUtf8();
|
||||||
@@ -71,7 +70,7 @@ IDocument::OpenResult FormWindowFile::open(const FilePath &filePath,
|
|||||||
str.setData(contentsBA);
|
str.setData(contentsBA);
|
||||||
str.open(QIODevice::ReadOnly);
|
str.open(QIODevice::ReadOnly);
|
||||||
if (!form->setContents(&str, &errorString))
|
if (!form->setContents(&str, &errorString))
|
||||||
return {OpenResult::CannotHandle, errorString};
|
return ResultError(errorString);
|
||||||
form->setDirty(filePath != realFilePath);
|
form->setDirty(filePath != realFilePath);
|
||||||
|
|
||||||
syncXmlFromFormWindow();
|
syncXmlFromFormWindow();
|
||||||
@@ -79,7 +78,7 @@ IDocument::OpenResult FormWindowFile::open(const FilePath &filePath,
|
|||||||
setShouldAutoSave(false);
|
setShouldAutoSave(false);
|
||||||
resourceHandler()->updateProjectResources();
|
resourceHandler()->updateProjectResources();
|
||||||
|
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> FormWindowFile::saveImpl(const FilePath &filePath, bool autoSave)
|
Result<> FormWindowFile::saveImpl(const FilePath &filePath, bool autoSave)
|
||||||
@@ -201,8 +200,8 @@ Result<> FormWindowFile::reload(ReloadFlag flag, ChangeType type)
|
|||||||
return ResultOk;
|
return ResultOk;
|
||||||
} else {
|
} else {
|
||||||
emit aboutToReload();
|
emit aboutToReload();
|
||||||
const OpenResult result = open(filePath(), filePath());
|
const Result<> result = open(filePath(), filePath());
|
||||||
emit reloadFinished(result.code == OpenResult::Success);
|
emit reloadFinished(result.has_value());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -26,8 +26,8 @@ public:
|
|||||||
~FormWindowFile() override { }
|
~FormWindowFile() override { }
|
||||||
|
|
||||||
// IDocument
|
// IDocument
|
||||||
OpenResult open(const Utils::FilePath &filePath,
|
Utils::Result<> open(const Utils::FilePath &filePath,
|
||||||
const Utils::FilePath &realFilePath) override;
|
const Utils::FilePath &realFilePath) override;
|
||||||
QByteArray contents() const override;
|
QByteArray contents() const override;
|
||||||
bool setContents(const QByteArray &contents) override;
|
bool setContents(const QByteArray &contents) override;
|
||||||
bool shouldAutoSave() const override;
|
bool shouldAutoSave() const override;
|
||||||
|
@@ -265,7 +265,7 @@ Result<> DiffEditorDocument::reload(ReloadFlag flag, ChangeType type)
|
|||||||
return open(filePath(), filePath());
|
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
|
QTC_CHECK(filePath == realFilePath); // does not support autosave
|
||||||
beginReload();
|
beginReload();
|
||||||
@@ -274,7 +274,7 @@ IDocument::OpenResult DiffEditorDocument::open(const FilePath &filePath, const F
|
|||||||
ReadResult readResult = read(filePath, &patch, &errorString);
|
ReadResult readResult = read(filePath, &patch, &errorString);
|
||||||
if (readResult == TextFileFormat::ReadIOError
|
if (readResult == TextFileFormat::ReadIOError
|
||||||
|| readResult == TextFileFormat::ReadMemoryAllocationError) {
|
|| readResult == TextFileFormat::ReadMemoryAllocationError) {
|
||||||
return {OpenResult::CannotHandle, errorString};
|
return ResultError(errorString);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::optional<QList<FileData>> fileDataList = DiffUtils::readPatch(patch);
|
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)
|
if (!ok && readResult == TextFileFormat::ReadEncodingError)
|
||||||
ok = selectEncoding();
|
ok = selectEncoding();
|
||||||
if (!ok)
|
if (!ok)
|
||||||
return {OpenResult::CannotHandle, errorString};
|
return ResultError(errorString);
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DiffEditorDocument::selectEncoding()
|
bool DiffEditorDocument::selectEncoding()
|
||||||
|
@@ -62,8 +62,8 @@ public:
|
|||||||
bool isSaveAsAllowed() const override;
|
bool isSaveAsAllowed() const override;
|
||||||
void reload();
|
void reload();
|
||||||
Utils::Result<> reload(ReloadFlag flag, ChangeType type) override;
|
Utils::Result<> reload(ReloadFlag flag, ChangeType type) override;
|
||||||
OpenResult open(const Utils::FilePath &filePath,
|
Utils::Result<> open(const Utils::FilePath &filePath,
|
||||||
const Utils::FilePath &realFilePath) override;
|
const Utils::FilePath &realFilePath) override;
|
||||||
bool selectEncoding();
|
bool selectEncoding();
|
||||||
State state() const { return m_state; }
|
State state() const { return m_state; }
|
||||||
|
|
||||||
|
@@ -64,26 +64,26 @@ ImageViewerFile::~ImageViewerFile()
|
|||||||
cleanUp();
|
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
|
QTC_CHECK(filePath == realfilePath); // does not support auto save
|
||||||
OpenResult res = openImpl(filePath);
|
Result<> res = openImpl(filePath);
|
||||||
emit openFinished(res.code == OpenResult::Success);
|
emit openFinished(res.has_value());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath)
|
Result<> ImageViewerFile::openImpl(const FilePath &filePath)
|
||||||
{
|
{
|
||||||
cleanUp();
|
cleanUp();
|
||||||
|
|
||||||
if (!filePath.isReadableFile())
|
if (!filePath.isReadableFile())
|
||||||
return OpenResult::CannotHandle;
|
return ResultError(Tr::tr("File not readable"));
|
||||||
|
|
||||||
const QString &fileName = filePath.toUrlishString();
|
const QString &fileName = filePath.toUrlishString();
|
||||||
QByteArray format = QImageReader::imageFormat(fileName);
|
QByteArray format = QImageReader::imageFormat(fileName);
|
||||||
// if it is impossible to recognize a file format - file will not be open correctly
|
// if it is impossible to recognize a file format - file will not be open correctly
|
||||||
if (format.isEmpty())
|
if (format.isEmpty())
|
||||||
return {OpenResult::CannotHandle, Tr::tr("Image format not supported.")};
|
return ResultError(Tr::tr("Image format not supported."));
|
||||||
|
|
||||||
#ifndef QT_NO_SVG
|
#ifndef QT_NO_SVG
|
||||||
if (format.startsWith("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()))) {
|
if (!bound.isValid() || (qFuzzyIsNull(bound.width()) && qFuzzyIsNull(bound.height()))) {
|
||||||
delete m_tempSvgItem;
|
delete m_tempSvgItem;
|
||||||
m_tempSvgItem = nullptr;
|
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;
|
m_type = TypeSvg;
|
||||||
emit imageSizeChanged(m_tempSvgItem->boundingRect().size().toSize());
|
emit imageSizeChanged(m_tempSvgItem->boundingRect().size().toSize());
|
||||||
@@ -105,7 +105,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath)
|
|||||||
if (!m_movie->isValid()) {
|
if (!m_movie->isValid()) {
|
||||||
delete m_movie;
|
delete m_movie;
|
||||||
m_movie = nullptr;
|
m_movie = nullptr;
|
||||||
return {OpenResult::CannotHandle, Tr::tr("Failed to read image.")};
|
return ResultError(Tr::tr("Failed to read image."));
|
||||||
}
|
}
|
||||||
m_type = TypeMovie;
|
m_type = TypeMovie;
|
||||||
connect(m_movie, &QMovie::resized, this, &ImageViewerFile::imageSizeChanged);
|
connect(m_movie, &QMovie::resized, this, &ImageViewerFile::imageSizeChanged);
|
||||||
@@ -115,7 +115,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath)
|
|||||||
if (m_pixmap->isNull()) {
|
if (m_pixmap->isNull()) {
|
||||||
delete m_pixmap;
|
delete m_pixmap;
|
||||||
m_pixmap = nullptr;
|
m_pixmap = nullptr;
|
||||||
return {OpenResult::CannotHandle, Tr::tr("Failed to read image.")};
|
return ResultError(Tr::tr("Failed to read image."));
|
||||||
}
|
}
|
||||||
m_type = TypePixmap;
|
m_type = TypePixmap;
|
||||||
emit imageSizeChanged(m_pixmap->size());
|
emit imageSizeChanged(m_pixmap->size());
|
||||||
@@ -123,7 +123,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath)
|
|||||||
|
|
||||||
setFilePath(filePath);
|
setFilePath(filePath);
|
||||||
setMimeType(Utils::mimeTypeForFile(filePath).name());
|
setMimeType(Utils::mimeTypeForFile(filePath).name());
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::IDocument::ReloadBehavior ImageViewerFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
|
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)
|
if (flag == FlagIgnore)
|
||||||
return ResultOk;
|
return ResultOk;
|
||||||
emit aboutToReload();
|
emit aboutToReload();
|
||||||
const OpenResult result = openImpl(filePath());
|
const Result<> result = openImpl(filePath());
|
||||||
emit reloadFinished( result.code == OpenResult::Success);
|
emit reloadFinished(result.has_value());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -35,7 +35,8 @@ public:
|
|||||||
ImageViewerFile();
|
ImageViewerFile();
|
||||||
~ImageViewerFile() override;
|
~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;
|
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override;
|
||||||
Utils::Result<> reload(ReloadFlag flag, ChangeType type) override;
|
Utils::Result<> reload(ReloadFlag flag, ChangeType type) override;
|
||||||
@@ -54,7 +55,7 @@ signals:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void cleanUp();
|
void cleanUp();
|
||||||
OpenResult openImpl(const Utils::FilePath &filePath);
|
Utils::Result<> openImpl(const Utils::FilePath &filePath);
|
||||||
|
|
||||||
ImageType m_type = TypeInvalid;
|
ImageType m_type = TypeInvalid;
|
||||||
#ifndef QT_NO_SVG
|
#ifndef QT_NO_SVG
|
||||||
|
@@ -42,13 +42,10 @@ ModelDocument::~ModelDocument()
|
|||||||
delete d;
|
delete d;
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::IDocument::OpenResult ModelDocument::open(const FilePath &filePath,
|
Result<> ModelDocument::open(const FilePath &filePath, const FilePath &realFilePath)
|
||||||
const FilePath &realFilePath)
|
|
||||||
{
|
{
|
||||||
Q_UNUSED(filePath)
|
Q_UNUSED(filePath)
|
||||||
|
return load(realFilePath);
|
||||||
OpenResult result = load(realFilePath);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> ModelDocument::saveImpl(const FilePath &filePath, bool autoSave)
|
Result<> ModelDocument::saveImpl(const FilePath &filePath, bool autoSave)
|
||||||
@@ -111,7 +108,7 @@ ExtDocumentController *ModelDocument::documentController() const
|
|||||||
return d->documentController;
|
return d->documentController;
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::IDocument::OpenResult ModelDocument::load(const FilePath &fileName)
|
Result<> ModelDocument::load(const FilePath &fileName)
|
||||||
{
|
{
|
||||||
d->documentController = ModelEditorPlugin::modelsManager()->createModel(this);
|
d->documentController = ModelEditorPlugin::modelsManager()->createModel(this);
|
||||||
connect(d->documentController, &qmt::DocumentController::changed, this, &IDocument::changed);
|
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);
|
d->documentController->loadProject(fileName);
|
||||||
setFilePath(d->documentController->projectController()->project()->fileName());
|
setFilePath(d->documentController->projectController()->project()->fileName());
|
||||||
} catch (const qmt::FileNotFoundException &ex) {
|
} catch (const qmt::FileNotFoundException &ex) {
|
||||||
return {OpenResult::CannotHandle, ex.errorMessage()};
|
return ResultError(ex.errorMessage());
|
||||||
} catch (const qmt::Exception &ex) {
|
} catch (const qmt::Exception &ex) {
|
||||||
return {OpenResult::CannotHandle,
|
return ResultError(Tr::tr("Could not open \"%1\" for reading: %2.")
|
||||||
Tr::tr("Could not open \"%1\" for reading: %2.")
|
.arg(fileName.toUserOutput(), ex.errorMessage()));
|
||||||
.arg(fileName.toUserOutput(), ex.errorMessage())};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePath configPath = d->documentController->projectController()->project()->configPath();
|
FilePath configPath = d->documentController->projectController()->project()->configPath();
|
||||||
@@ -139,7 +135,7 @@ Core::IDocument::OpenResult ModelDocument::load(const FilePath &fileName)
|
|||||||
}
|
}
|
||||||
|
|
||||||
emit contentSet();
|
emit contentSet();
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ModelEditor::Internal
|
} // namespace ModelEditor::Internal
|
||||||
|
@@ -12,8 +12,7 @@ namespace ModelEditor::Internal {
|
|||||||
|
|
||||||
class ExtDocumentController;
|
class ExtDocumentController;
|
||||||
|
|
||||||
class ModelDocument :
|
class ModelDocument : public Core::IDocument
|
||||||
public Core::IDocument
|
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
class ModelDocumentPrivate;
|
class ModelDocumentPrivate;
|
||||||
@@ -26,8 +25,8 @@ signals:
|
|||||||
void contentSet();
|
void contentSet();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
OpenResult open(const Utils::FilePath &filePath,
|
Utils::Result<> open(const Utils::FilePath &filePath,
|
||||||
const Utils::FilePath &realFilePath) override;
|
const Utils::FilePath &realFilePath) override;
|
||||||
bool shouldAutoSave() const override;
|
bool shouldAutoSave() const override;
|
||||||
bool isModified() const override;
|
bool isModified() const override;
|
||||||
bool isSaveAsAllowed() const override;
|
bool isSaveAsAllowed() const override;
|
||||||
@@ -35,7 +34,7 @@ public:
|
|||||||
|
|
||||||
ExtDocumentController *documentController() const;
|
ExtDocumentController *documentController() const;
|
||||||
|
|
||||||
OpenResult load(const Utils::FilePath &fileName);
|
Utils::Result<> load(const Utils::FilePath &fileName);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Utils::Result<> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
|
Utils::Result<> saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
|
||||||
|
@@ -89,13 +89,13 @@ ResourceFile::~ResourceFile()
|
|||||||
clearPrefixList();
|
clearPrefixList();
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::IDocument::OpenResult ResourceFile::load()
|
Result<> ResourceFile::load()
|
||||||
{
|
{
|
||||||
m_error_message.clear();
|
m_error_message.clear();
|
||||||
|
|
||||||
if (m_filePath.isEmpty()) {
|
if (m_filePath.isEmpty()) {
|
||||||
m_error_message = Tr::tr("The file name is empty.");
|
m_error_message = Tr::tr("The file name is empty.");
|
||||||
return Core::IDocument::OpenResult::CannotHandle;
|
return ResultError(m_error_message);
|
||||||
}
|
}
|
||||||
|
|
||||||
clearPrefixList();
|
clearPrefixList();
|
||||||
@@ -108,7 +108,7 @@ Core::IDocument::OpenResult ResourceFile::load()
|
|||||||
QFile file(m_filePath.toUrlishString());
|
QFile file(m_filePath.toUrlishString());
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
m_error_message = file.errorString();
|
m_error_message = file.errorString();
|
||||||
return Core::IDocument::OpenResult::CannotHandle;
|
return ResultError(m_error_message);
|
||||||
}
|
}
|
||||||
QByteArray data = file.readAll();
|
QByteArray data = file.readAll();
|
||||||
// Detect line ending style
|
// Detect line ending style
|
||||||
@@ -122,7 +122,7 @@ Core::IDocument::OpenResult ResourceFile::load()
|
|||||||
if (!doc.setContent(data, &error_msg, &error_line, &error_col)) {
|
if (!doc.setContent(data, &error_msg, &error_line, &error_col)) {
|
||||||
m_error_message = Tr::tr("XML error on line %1, col %2: %3")
|
m_error_message = Tr::tr("XML error on line %1, col %2: %3")
|
||||||
.arg(error_line).arg(error_col).arg(error_msg);
|
.arg(error_line).arg(error_col).arg(error_msg);
|
||||||
return Core::IDocument::OpenResult::CannotHandle;
|
return ResultError(m_error_message);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -133,7 +133,7 @@ Core::IDocument::OpenResult ResourceFile::load()
|
|||||||
if (!doc.setContent(m_contents, &error_msg, &error_line, &error_col)) {
|
if (!doc.setContent(m_contents, &error_msg, &error_line, &error_col)) {
|
||||||
m_error_message = Tr::tr("XML error on line %1, col %2: %3")
|
m_error_message = Tr::tr("XML error on line %1, col %2: %3")
|
||||||
.arg(error_line).arg(error_col).arg(error_msg);
|
.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"));
|
QDomElement root = doc.firstChildElement(QLatin1String("RCC"));
|
||||||
if (root.isNull()) {
|
if (root.isNull()) {
|
||||||
m_error_message = Tr::tr("The <RCC> root element is missing.");
|
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"));
|
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
|
QString ResourceFile::contents() const
|
||||||
@@ -1078,11 +1078,11 @@ QModelIndex ResourceModel::deleteItem(const QModelIndex &idx)
|
|||||||
return index(file_idx, 0, prefix_model_idx);
|
return index(file_idx, 0, prefix_model_idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::IDocument::OpenResult ResourceModel::reload()
|
Result<> ResourceModel::reload()
|
||||||
{
|
{
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
Core::IDocument::OpenResult result = m_resource_file.load();
|
Result<> result = m_resource_file.load();
|
||||||
if (result.code == Core::IDocument::OpenResult::Success)
|
if (result.has_value())
|
||||||
setDirty(false);
|
setDirty(false);
|
||||||
endResetModel();
|
endResetModel();
|
||||||
return result;
|
return result;
|
||||||
|
@@ -107,7 +107,7 @@ public:
|
|||||||
void setFilePath(const Utils::FilePath &filePath) { m_filePath = filePath; }
|
void setFilePath(const Utils::FilePath &filePath) { m_filePath = filePath; }
|
||||||
Utils::FilePath filePath() const { return m_filePath; }
|
Utils::FilePath filePath() const { return m_filePath; }
|
||||||
|
|
||||||
Core::IDocument::OpenResult load();
|
Utils::Result<> load();
|
||||||
bool save();
|
bool save();
|
||||||
QString contents() const;
|
QString contents() const;
|
||||||
QString errorMessage() const { return m_error_message; }
|
QString errorMessage() const { return m_error_message; }
|
||||||
@@ -227,7 +227,7 @@ private:
|
|||||||
bool renameFile(const QString &fileName, const QString &newFileName);
|
bool renameFile(const QString &fileName, const QString &newFileName);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual Core::IDocument::OpenResult reload();
|
virtual Utils::Result<> reload();
|
||||||
virtual bool save();
|
virtual bool save();
|
||||||
QString contents() const { return m_resource_file.contents(); }
|
QString contents() const { return m_resource_file.contents(); }
|
||||||
|
|
||||||
|
@@ -48,7 +48,7 @@ class ResourceEditorDocument final : public IDocument
|
|||||||
public:
|
public:
|
||||||
ResourceEditorDocument(QObject *parent = nullptr);
|
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(); }
|
QString plainText() const { return m_model.contents(); }
|
||||||
QByteArray contents() const final { return m_model.contents().toUtf8(); }
|
QByteArray contents() const final { return m_model.contents().toUtf8(); }
|
||||||
bool setContents(const QByteArray &contents) final;
|
bool setContents(const QByteArray &contents) final;
|
||||||
@@ -179,8 +179,7 @@ ResourceEditorImpl::~ResourceEditorImpl()
|
|||||||
delete m_toolBar;
|
delete m_toolBar;
|
||||||
}
|
}
|
||||||
|
|
||||||
IDocument::OpenResult ResourceEditorDocument::open(const FilePath &filePath,
|
Result<> ResourceEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath)
|
||||||
const FilePath &realFilePath)
|
|
||||||
{
|
{
|
||||||
if (debugResourceEditorW)
|
if (debugResourceEditorW)
|
||||||
qDebug() << "ResourceEditorW::open: " << filePath;
|
qDebug() << "ResourceEditorW::open: " << filePath;
|
||||||
@@ -189,9 +188,9 @@ IDocument::OpenResult ResourceEditorDocument::open(const FilePath &filePath,
|
|||||||
|
|
||||||
m_model.setFilePath(realFilePath);
|
m_model.setFilePath(realFilePath);
|
||||||
|
|
||||||
OpenResult openResult = m_model.reload();
|
Result<> openResult = m_model.reload();
|
||||||
if (openResult.code != OpenResult::Success) {
|
if (!openResult) {
|
||||||
openResult.error = m_model.errorMessage();
|
openResult = ResultError(m_model.errorMessage()); // FIXME: Move to m_model
|
||||||
setBlockDirtyChanged(false);
|
setBlockDirtyChanged(false);
|
||||||
emit loaded(false);
|
emit loaded(false);
|
||||||
return openResult;
|
return openResult;
|
||||||
@@ -203,7 +202,7 @@ IDocument::OpenResult ResourceEditorDocument::open(const FilePath &filePath,
|
|||||||
m_shouldAutoSave = false;
|
m_shouldAutoSave = false;
|
||||||
|
|
||||||
emit loaded(true);
|
emit loaded(true);
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> ResourceEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
|
Result<> ResourceEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
|
||||||
@@ -246,7 +245,7 @@ bool ResourceEditorDocument::setContents(const QByteArray &contents)
|
|||||||
|
|
||||||
const FilePath originalFileName = m_model.filePath();
|
const FilePath originalFileName = m_model.filePath();
|
||||||
m_model.setFilePath(saver.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_model.setFilePath(originalFileName);
|
||||||
m_shouldAutoSave = false;
|
m_shouldAutoSave = false;
|
||||||
if (debugResourceEditorW)
|
if (debugResourceEditorW)
|
||||||
@@ -283,8 +282,8 @@ Result<> ResourceEditorDocument::reload(ReloadFlag flag, ChangeType type)
|
|||||||
if (flag == FlagIgnore)
|
if (flag == FlagIgnore)
|
||||||
return ResultOk;
|
return ResultOk;
|
||||||
emit aboutToReload();
|
emit aboutToReload();
|
||||||
const OpenResult result = open(filePath(), filePath());
|
const Result<> result = open(filePath(), filePath());
|
||||||
emit reloadFinished(result.code == OpenResult::Success);
|
emit reloadFinished(result.has_value());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -121,7 +121,7 @@ static bool addFilesToResource(const FilePath &resourceFile,
|
|||||||
*notAdded = filePaths;
|
*notAdded = filePaths;
|
||||||
|
|
||||||
ResourceFile file(resourceFile);
|
ResourceFile file(resourceFile);
|
||||||
if (file.load().code != IDocument::OpenResult::Success)
|
if (!file.load())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
int index = file.indexOfPrefix(prefix, lang);
|
int index = file.indexOfPrefix(prefix, lang);
|
||||||
@@ -272,7 +272,7 @@ static void compressTree(FolderNode *n)
|
|||||||
void ResourceTopLevelNode::addInternalNodes()
|
void ResourceTopLevelNode::addInternalNodes()
|
||||||
{
|
{
|
||||||
ResourceFile file(filePath(), m_contents);
|
ResourceFile file(filePath(), m_contents);
|
||||||
if (file.load().code != IDocument::OpenResult::Success)
|
if (!file.load())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QMap<PrefixFolderLang, FolderNode *> folderNodes;
|
QMap<PrefixFolderLang, FolderNode *> folderNodes;
|
||||||
@@ -382,7 +382,7 @@ RemovedFilesFromProject ResourceTopLevelNode::removeFiles(const FilePaths &fileP
|
|||||||
bool ResourceTopLevelNode::addPrefix(const QString &prefix, const QString &lang)
|
bool ResourceTopLevelNode::addPrefix(const QString &prefix, const QString &lang)
|
||||||
{
|
{
|
||||||
ResourceFile file(filePath());
|
ResourceFile file(filePath());
|
||||||
if (file.load().code != IDocument::OpenResult::Success)
|
if (!file.load())
|
||||||
return false;
|
return false;
|
||||||
int index = file.addPrefix(prefix, lang);
|
int index = file.addPrefix(prefix, lang);
|
||||||
if (index == -1)
|
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)
|
bool ResourceTopLevelNode::removePrefix(const QString &prefix, const QString &lang)
|
||||||
{
|
{
|
||||||
ResourceFile file(filePath());
|
ResourceFile file(filePath());
|
||||||
if (file.load().code != IDocument::OpenResult::Success)
|
if (!file.load())
|
||||||
return false;
|
return false;
|
||||||
for (int i = 0; i < file.prefixCount(); ++i) {
|
for (int i = 0; i < file.prefixCount(); ++i) {
|
||||||
if (file.prefix(i) == prefix
|
if (file.prefix(i) == prefix
|
||||||
@@ -411,7 +411,7 @@ bool ResourceTopLevelNode::removePrefix(const QString &prefix, const QString &la
|
|||||||
bool ResourceTopLevelNode::removeNonExistingFiles()
|
bool ResourceTopLevelNode::removeNonExistingFiles()
|
||||||
{
|
{
|
||||||
ResourceFile file(filePath());
|
ResourceFile file(filePath());
|
||||||
if (file.load().code != IDocument::OpenResult::Success)
|
if (!file.load())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
QFileInfo fi;
|
QFileInfo fi;
|
||||||
@@ -490,7 +490,7 @@ RemovedFilesFromProject ResourceFolderNode::removeFiles(const FilePaths &filePat
|
|||||||
if (notRemoved)
|
if (notRemoved)
|
||||||
*notRemoved = filePaths;
|
*notRemoved = filePaths;
|
||||||
ResourceFile file(m_topLevelNode->filePath());
|
ResourceFile file(m_topLevelNode->filePath());
|
||||||
if (file.load().code != IDocument::OpenResult::Success)
|
if (!file.load())
|
||||||
return RemovedFilesFromProject::Error;
|
return RemovedFilesFromProject::Error;
|
||||||
int index = file.indexOfPrefix(m_prefix, m_lang);
|
int index = file.indexOfPrefix(m_prefix, m_lang);
|
||||||
if (index == -1)
|
if (index == -1)
|
||||||
@@ -518,8 +518,7 @@ bool ResourceFolderNode::canRenameFile(const FilePath &oldFilePath, const FilePa
|
|||||||
bool fileEntryExists = false;
|
bool fileEntryExists = false;
|
||||||
ResourceFile file(m_topLevelNode->filePath());
|
ResourceFile file(m_topLevelNode->filePath());
|
||||||
|
|
||||||
int index = (file.load().code != IDocument::OpenResult::Success)
|
int index = file.load() ? file.indexOfPrefix(m_prefix, m_lang) : -1;
|
||||||
? -1 : file.indexOfPrefix(m_prefix, m_lang);
|
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
for (int j = 0; j < file.fileCount(index); ++j) {
|
for (int j = 0; j < file.fileCount(index); ++j) {
|
||||||
if (file.file(index, j) == oldFilePath.toUrlishString()) {
|
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)
|
bool ResourceFolderNode::renameFiles(const FilePairs &filesToRename, FilePaths *notRenamed)
|
||||||
{
|
{
|
||||||
ResourceFile file(m_topLevelNode->filePath());
|
ResourceFile file(m_topLevelNode->filePath());
|
||||||
if (file.load().code != IDocument::OpenResult::Success)
|
if (!file.load())
|
||||||
return false;
|
return false;
|
||||||
int index = file.indexOfPrefix(m_prefix, m_lang);
|
int index = file.indexOfPrefix(m_prefix, m_lang);
|
||||||
if (index == -1)
|
if (index == -1)
|
||||||
@@ -567,7 +566,7 @@ bool ResourceFolderNode::renameFiles(const FilePairs &filesToRename, FilePaths *
|
|||||||
bool ResourceFolderNode::renamePrefix(const QString &prefix, const QString &lang)
|
bool ResourceFolderNode::renamePrefix(const QString &prefix, const QString &lang)
|
||||||
{
|
{
|
||||||
ResourceFile file(m_topLevelNode->filePath());
|
ResourceFile file(m_topLevelNode->filePath());
|
||||||
if (file.load().code != IDocument::OpenResult::Success)
|
if (!file.load())
|
||||||
return false;
|
return false;
|
||||||
int index = file.indexOfPrefix(m_prefix, m_lang);
|
int index = file.indexOfPrefix(m_prefix, m_lang);
|
||||||
if (index == -1)
|
if (index == -1)
|
||||||
|
@@ -35,24 +35,23 @@ ScxmlEditorDocument::ScxmlEditorDocument(MainWidget *designWidget, QObject *pare
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::IDocument::OpenResult ScxmlEditorDocument::open(const FilePath &filePath,
|
Result<> ScxmlEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath)
|
||||||
const FilePath &realFilePath)
|
|
||||||
{
|
{
|
||||||
Q_UNUSED(realFilePath)
|
Q_UNUSED(realFilePath)
|
||||||
|
|
||||||
if (filePath.isEmpty())
|
if (filePath.isEmpty())
|
||||||
return OpenResult::CannotHandle;
|
return ResultError("File path is empty"); // FIXME: Use something better
|
||||||
|
|
||||||
if (!m_designWidget)
|
if (!m_designWidget)
|
||||||
return OpenResult::CannotHandle;
|
return ResultError(ResultAssert);
|
||||||
|
|
||||||
const FilePath &absoluteFilePath = filePath.absoluteFilePath();
|
const FilePath &absoluteFilePath = filePath.absoluteFilePath();
|
||||||
if (!m_designWidget->load(absoluteFilePath.toUrlishString()))
|
if (!m_designWidget->load(absoluteFilePath.toUrlishString()))
|
||||||
return {OpenResult::CannotHandle, m_designWidget->errorMessage()};
|
return ResultError(m_designWidget->errorMessage());
|
||||||
|
|
||||||
setFilePath(absoluteFilePath);
|
setFilePath(absoluteFilePath);
|
||||||
|
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> ScxmlEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
|
Result<> ScxmlEditorDocument::saveImpl(const FilePath &filePath, bool autoSave)
|
||||||
|
@@ -25,7 +25,8 @@ public:
|
|||||||
explicit ScxmlEditorDocument(Common::MainWidget *designWidget, QObject *parent = nullptr);
|
explicit ScxmlEditorDocument(Common::MainWidget *designWidget, QObject *parent = nullptr);
|
||||||
|
|
||||||
// IDocument
|
// 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 shouldAutoSave() const override;
|
||||||
bool isSaveAsAllowed() const override;
|
bool isSaveAsAllowed() const override;
|
||||||
bool isModified() const override;
|
bool isModified() const override;
|
||||||
|
@@ -28,11 +28,10 @@ ObjectsMapDocument::ObjectsMapDocument()
|
|||||||
connect(m_contentModel, &ObjectsMapModel::modelChanged, this, [this] { setModified(true); });
|
connect(m_contentModel, &ObjectsMapModel::modelChanged, this, [this] { setModified(true); });
|
||||||
}
|
}
|
||||||
|
|
||||||
IDocument::OpenResult ObjectsMapDocument::open(const FilePath &fileName,
|
Result<> ObjectsMapDocument::open(const FilePath &fileName, const FilePath &realFileName)
|
||||||
const FilePath &realFileName)
|
|
||||||
{
|
{
|
||||||
OpenResult result = openImpl(fileName, realFileName);
|
Result<> result = openImpl(fileName, realFileName);
|
||||||
if (result.code == OpenResult::Success) {
|
if (result.has_value()) {
|
||||||
setFilePath(fileName);
|
setFilePath(fileName);
|
||||||
setModified(fileName != realFileName);
|
setModified(fileName != realFileName);
|
||||||
}
|
}
|
||||||
@@ -77,11 +76,10 @@ Result<> ObjectsMapDocument::reload(IDocument::ReloadFlag flag, IDocument::Chang
|
|||||||
if (flag == FlagIgnore)
|
if (flag == FlagIgnore)
|
||||||
return ResultOk;
|
return ResultOk;
|
||||||
emit aboutToReload();
|
emit aboutToReload();
|
||||||
const OpenResult result = openImpl(filePath(), filePath());
|
const Result<> result = openImpl(filePath(), filePath());
|
||||||
const bool success = result.code == OpenResult::Success;
|
if (result.has_value())
|
||||||
if (success)
|
|
||||||
setModified(false);
|
setModified(false);
|
||||||
emit reloadFinished(success);
|
emit reloadFinished(result.has_value());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,28 +171,27 @@ QByteArray ObjectsMapDocument::contents() const
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
IDocument::OpenResult ObjectsMapDocument::openImpl(const FilePath &fileName,
|
Result<> ObjectsMapDocument::openImpl(const FilePath &fileName, const FilePath &realFileName)
|
||||||
const FilePath &realFileName)
|
|
||||||
{
|
{
|
||||||
if (fileName.isEmpty())
|
if (fileName.isEmpty())
|
||||||
return OpenResult::CannotHandle;
|
return ResultError("File name is empty"); // FIXME: Find somethong better
|
||||||
|
|
||||||
QByteArray text;
|
QByteArray text;
|
||||||
if (realFileName.fileName() == "objects.map") {
|
if (realFileName.fileName() == "objects.map") {
|
||||||
FileReader reader;
|
FileReader reader;
|
||||||
if (const Result<> res = reader.fetch(realFileName); !res)
|
if (const Result<> res = reader.fetch(realFileName); !res)
|
||||||
return {OpenResult::CannotHandle, res.error()};
|
return res;
|
||||||
|
|
||||||
text = reader.text();
|
text = reader.text();
|
||||||
} else {
|
} else {
|
||||||
const FilePath base = settings().squishPath();
|
const FilePath base = settings().squishPath();
|
||||||
if (base.isEmpty()) {
|
if (base.isEmpty()) {
|
||||||
return {OpenResult::CannotHandle, Tr::tr("Incomplete Squish settings. "
|
return ResultError(Tr::tr("Incomplete Squish settings. "
|
||||||
"Missing Squish installation path.")};
|
"Missing Squish installation path."));
|
||||||
}
|
}
|
||||||
const FilePath exe = base.pathAppended("lib/exec/objectmaptool").withExecutableSuffix();
|
const FilePath exe = base.pathAppended("lib/exec/objectmaptool").withExecutableSuffix();
|
||||||
if (!exe.isExecutableFile())
|
if (!exe.isExecutableFile())
|
||||||
return {OpenResult::CannotHandle, Tr::tr("objectmaptool not found.")};
|
return ResultError(Tr::tr("objectmaptool not found."));
|
||||||
|
|
||||||
|
|
||||||
Process objectMapReader;
|
Process objectMapReader;
|
||||||
@@ -206,8 +203,8 @@ IDocument::OpenResult ObjectsMapDocument::openImpl(const FilePath &fileName,
|
|||||||
text = objectMapReader.cleanedStdOut().toUtf8();
|
text = objectMapReader.cleanedStdOut().toUtf8();
|
||||||
}
|
}
|
||||||
if (!setContents(text))
|
if (!setContents(text))
|
||||||
return {OpenResult::CannotHandle, Tr::tr("Failure while parsing objects.map content.")};
|
return ResultError(Tr::tr("Failure while parsing objects.map content."));
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ObjectsMapDocument::writeFile(const Utils::FilePath &fileName) const
|
bool ObjectsMapDocument::writeFile(const Utils::FilePath &fileName) const
|
||||||
|
@@ -14,11 +14,12 @@ class ObjectsMapModel;
|
|||||||
class ObjectsMapDocument : public Core::IDocument
|
class ObjectsMapDocument : public Core::IDocument
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ObjectsMapDocument();
|
ObjectsMapDocument();
|
||||||
|
|
||||||
OpenResult open(const Utils::FilePath &fileName,
|
Utils::Result<> open(const Utils::FilePath &fileName,
|
||||||
const Utils::FilePath &realFileName) override;
|
const Utils::FilePath &realFileName) override;
|
||||||
Utils::FilePath fallbackSaveAsPath() const override;
|
Utils::FilePath fallbackSaveAsPath() const override;
|
||||||
QString fallbackSaveAsFileName() const override;
|
QString fallbackSaveAsFileName() const override;
|
||||||
bool isModified() const override { return m_isModified; }
|
bool isModified() const override { return m_isModified; }
|
||||||
@@ -35,8 +36,8 @@ protected:
|
|||||||
Utils::Result<> saveImpl(const Utils::FilePath &fileName, bool autoSave) override;
|
Utils::Result<> saveImpl(const Utils::FilePath &fileName, bool autoSave) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OpenResult openImpl(const Utils::FilePath &fileName,
|
Utils::Result<> openImpl(const Utils::FilePath &fileName,
|
||||||
const Utils::FilePath &realFileName);
|
const Utils::FilePath &realFileName);
|
||||||
bool buildObjectsMapTree(const QByteArray &contents);
|
bool buildObjectsMapTree(const QByteArray &contents);
|
||||||
bool writeFile(const Utils::FilePath &fileName) const;
|
bool writeFile(const Utils::FilePath &fileName) const;
|
||||||
void syncXMLFromEditor();
|
void syncXMLFromEditor();
|
||||||
|
@@ -754,11 +754,11 @@ bool TextDocument::isModified() const
|
|||||||
return d->m_document.isModified();
|
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);
|
emit aboutToOpen(filePath, realFilePath);
|
||||||
OpenResult result = openImpl(filePath, realFilePath, /*reload =*/ false);
|
const Result<> result = openImpl(filePath, realFilePath, /*reload =*/ false);
|
||||||
if (result.code == OpenResult::Success) {
|
if (result) {
|
||||||
setMimeType(Utils::mimeTypeForFile(filePath, MimeMatchMode::MatchDefaultAndRemote).name());
|
setMimeType(Utils::mimeTypeForFile(filePath, MimeMatchMode::MatchDefaultAndRemote).name());
|
||||||
setTabSettings(d->m_tabSettings);
|
setTabSettings(d->m_tabSettings);
|
||||||
emit openFinishedSuccessfully();
|
emit openFinishedSuccessfully();
|
||||||
@@ -766,9 +766,9 @@ IDocument::OpenResult TextDocument::open(const FilePath &filePath, const FilePat
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
IDocument::OpenResult TextDocument::openImpl(const FilePath &filePath,
|
Result<> TextDocument::openImpl(const FilePath &filePath,
|
||||||
const FilePath &realFilePath,
|
const FilePath &realFilePath,
|
||||||
bool reload)
|
bool reload)
|
||||||
{
|
{
|
||||||
QStringList content;
|
QStringList content;
|
||||||
QString errorString;
|
QString errorString;
|
||||||
@@ -820,15 +820,15 @@ IDocument::OpenResult TextDocument::openImpl(const FilePath &filePath,
|
|||||||
|
|
||||||
auto documentLayout =
|
auto documentLayout =
|
||||||
qobject_cast<TextDocumentLayout*>(d->m_document.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();
|
documentLayout->lastSaveRevision = d->m_autoSaveRevision = d->m_document.revision();
|
||||||
d->updateRevisions();
|
d->updateRevisions();
|
||||||
d->m_document.setModified(filePath != realFilePath);
|
d->m_document.setModified(filePath != realFilePath);
|
||||||
setFilePath(filePath);
|
setFilePath(filePath);
|
||||||
}
|
}
|
||||||
if (readResult == Utils::TextFileFormat::ReadIOError)
|
if (readResult == TextFileFormat::ReadIOError)
|
||||||
return {OpenResult::CannotHandle, errorString};
|
return ResultError(errorString);
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<> TextDocument::reload(const QByteArray &codec)
|
Result<> TextDocument::reload(const QByteArray &codec)
|
||||||
@@ -851,11 +851,11 @@ Result<> TextDocument::reload(const FilePath &realFilePath)
|
|||||||
if (documentLayout)
|
if (documentLayout)
|
||||||
documentLayout->documentAboutToReload(this); // removes text marks non-permanently
|
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)
|
if (documentLayout)
|
||||||
documentLayout->documentReloaded(this); // re-adds text marks
|
documentLayout->documentReloaded(this); // re-adds text marks
|
||||||
emit reloadFinished(result.code == OpenResult::Success);
|
emit reloadFinished(result.has_value());
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@@ -117,7 +117,8 @@ public:
|
|||||||
void setFallbackSaveAsPath(const Utils::FilePath &fallbackSaveAsPath);
|
void setFallbackSaveAsPath(const Utils::FilePath &fallbackSaveAsPath);
|
||||||
void setFallbackSaveAsFileName(const QString &fallbackSaveAsFileName);
|
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();
|
virtual Utils::Result<> reload();
|
||||||
Utils::Result<> reload(const Utils::FilePath &realFilePath);
|
Utils::Result<> reload(const Utils::FilePath &realFilePath);
|
||||||
|
|
||||||
@@ -169,9 +170,9 @@ protected:
|
|||||||
virtual void slotCodeStyleSettingsChanged(); // Used in CppEditorDocumet
|
virtual void slotCodeStyleSettingsChanged(); // Used in CppEditorDocumet
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OpenResult openImpl(const Utils::FilePath &filePath,
|
Utils::Result<> openImpl(const Utils::FilePath &filePath,
|
||||||
const Utils::FilePath &realFileName,
|
const Utils::FilePath &realFileName,
|
||||||
bool reload);
|
bool reload);
|
||||||
void cleanWhitespace(QTextCursor &cursor, bool inEntireDocument, bool cleanIndentation);
|
void cleanWhitespace(QTextCursor &cursor, bool inEntireDocument, bool cleanIndentation);
|
||||||
void ensureFinalNewLine(QTextCursor &cursor);
|
void ensureFinalNewLine(QTextCursor &cursor);
|
||||||
void modificationChanged(bool modified);
|
void modificationChanged(bool modified);
|
||||||
|
@@ -27,22 +27,22 @@ SubmitEditorFile::SubmitEditorFile(VcsBaseSubmitEditor *editor) :
|
|||||||
this, &IDocument::contentsChanged);
|
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())
|
if (filePath.isEmpty())
|
||||||
return OpenResult::CannotHandle;
|
return ResultError("File name is empty"); // FIXME: Use something better
|
||||||
|
|
||||||
FileReader reader;
|
FileReader reader;
|
||||||
if (const Result<> res = reader.fetch(realFilePath); !res)
|
if (const Result<> res = reader.fetch(realFilePath); !res)
|
||||||
return {OpenResult::CannotHandle, res.error()};
|
return res;
|
||||||
|
|
||||||
const QString text = QString::fromLocal8Bit(reader.text());
|
const QString text = QString::fromLocal8Bit(reader.text());
|
||||||
if (!m_editor->setFileContents(text.toUtf8()))
|
if (!m_editor->setFileContents(text.toUtf8()))
|
||||||
return OpenResult::CannotHandle;
|
return ResultError("Cannot set file contents"); // FIXME: Use something better
|
||||||
|
|
||||||
setFilePath(filePath.absoluteFilePath());
|
setFilePath(filePath.absoluteFilePath());
|
||||||
setModified(filePath != realFilePath);
|
setModified(filePath != realFilePath);
|
||||||
return OpenResult::Success;
|
return ResultOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray SubmitEditorFile::contents() const
|
QByteArray SubmitEditorFile::contents() const
|
||||||
|
@@ -17,7 +17,8 @@ class SubmitEditorFile : public Core::IDocument
|
|||||||
public:
|
public:
|
||||||
explicit SubmitEditorFile(VcsBaseSubmitEditor *editor);
|
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;
|
QByteArray contents() const override;
|
||||||
bool setContents(const QByteArray &contents) override;
|
bool setContents(const QByteArray &contents) override;
|
||||||
|
Reference in New Issue
Block a user