Commit 4f927e4c authored by Eike Ziller's avatar Eike Ziller
Browse files

Move "open" from IEditor to IDocument



For non-editor documents it currently is not used, but for editors it
makes more sense to have that on the document instead of the editor.
Most actual implementations of "open" were done in the documents already
anyhow, because it is needed for reloading.

Change-Id: I29d4df2078995cbe80172b51a9bebeecb3afad3c
Reviewed-by: default avatarDavid Schulz <david.schulz@theqtcompany.com>
parent c916d250
......@@ -65,11 +65,6 @@ AndroidManifestEditor::AndroidManifestEditor(AndroidManifestEditorWidget *editor
setWidget(editorWidget);
}
bool AndroidManifestEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
return widget()->open(errorString, fileName, realFileName);
}
QWidget *AndroidManifestEditor::toolBar()
{
return m_toolBar;
......
......@@ -54,7 +54,6 @@ class AndroidManifestEditor : public Core::IEditor
public:
explicit AndroidManifestEditor(AndroidManifestEditorWidget *editorWidget);
bool open(QString *errorString, const QString &fileName, const QString &realFileName) override;
QWidget *toolBar() override;
AndroidManifestEditorWidget *widget() const override;
Core::IDocument *document() override;
......
......@@ -123,6 +123,8 @@ AndroidManifestEditorWidget::AndroidManifestEditorWidget()
connect(m_textEditorWidget->document(), SIGNAL(contentsChanged()),
this, SLOT(startParseCheck()));
connect(m_textEditorWidget->textDocument(), &TextEditor::TextDocument::reloadFinished,
this, [this](bool success) { if (success) updateAfterFileLoad(); });
connect(m_textEditorWidget->textDocument(), &TextEditor::TextDocument::openFinishedSuccessfully,
this, &AndroidManifestEditorWidget::updateAfterFileLoad);
}
......@@ -502,18 +504,8 @@ void AndroidManifestEditorWidget::updateTargetComboBox()
m_targetLineEdit->addItems(items);
}
bool AndroidManifestEditorWidget::open(QString *errorString, const QString &fileName, const QString &realFileName)
void AndroidManifestEditorWidget::updateAfterFileLoad()
{
bool result = m_textEditorWidget->open(errorString, fileName, realFileName);
updateAfterFileLoad(result);
return result;
}
void AndroidManifestEditorWidget::updateAfterFileLoad(bool success)
{
if (!success)
return;
QString error;
int errorLine;
int errorColumn;
......
......@@ -96,8 +96,6 @@ public:
explicit AndroidManifestEditorWidget();
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
bool isModified() const;
EditorPage activePage() const;
......@@ -135,7 +133,7 @@ private:
bool syncToWidgets();
void syncToWidgets(const QDomDocument &doc);
void syncToEditor();
void updateAfterFileLoad(bool success);
void updateAfterFileLoad();
bool checkDocument(const QDomDocument &doc, QString *errorMessage,
int *errorLine, int *errorColumn);
......
......@@ -265,7 +265,14 @@ public:
}
}
bool open(QString *errorString, const QString &fileName, quint64 offset = 0) {
bool open(QString *errorString, const QString &fileName, const QString &realFileName)
{
QTC_CHECK(fileName == realFileName); // The bineditor can do no autosaving
return openImpl(errorString, fileName);
}
bool openImpl(QString *errorString, const QString &fileName, quint64 offset = 0)
{
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
file.close();
......@@ -326,7 +333,7 @@ private slots:
void provideNewRange(quint64 offset)
{
open(0, filePath().toString(), offset);
openImpl(0, filePath().toString(), offset);
}
public:
......@@ -356,7 +363,7 @@ public:
emit aboutToReload();
int cPos = m_widget->cursorPosition();
m_widget->clear();
const bool success = open(errorString, filePath().toString());
const bool success = openImpl(errorString, filePath().toString());
m_widget->setCursorPosition(cPos);
emit reloadFinished(success);
return success;
......@@ -409,10 +416,6 @@ public:
delete m_widget;
}
bool open(QString *errorString, const QString &fileName, const QString &realFileName) override {
QTC_ASSERT(fileName == realFileName, return false); // The bineditor can do no autosaving
return m_file->open(errorString, fileName);
}
IDocument *document() override { return m_file; }
QWidget *toolBar() override { return m_toolBar; }
......
......@@ -612,7 +612,7 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const QString &fileN
}
QString errorString;
if (editor->open(&errorString, fn, realFn))
if (editor->document()->open(&errorString, fn, realFn))
break;
overrideCursor.reset();
......
......@@ -51,7 +51,6 @@ public:
bool duplicateSupported() const;
void setDuplicateSupported(bool duplicateSupported);
virtual bool open(QString *errorString, const QString &fileName, const QString &realFileName) = 0;
virtual IDocument *document() = 0;
virtual IEditor *duplicate() { return 0; }
......
......@@ -117,6 +117,26 @@ Id IDocument::id() const
return d->id;
}
/*!
* Used to load a file if this document is part of an IEditor implementation, when the editor
* is opened.
* If the editor is opened from an auto save file, \a realFileName is the name of the auto save
* that should be loaded, and \a fileName is the file name of the resulting file.
* In that case, the contents of the auto save file should be loaded, the file name of the
* IDocument should be set to \a fileName, and the document state be set to modified.
* If the editor is opened from a regular file, \a fileName and \a realFileName are the same.
* Use \a errorString to return an error message, if this document can not handle the
* file contents.
* Returns true on success, false if an error occurred.
*/
bool IDocument::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
Q_UNUSED(errorString)
Q_UNUSED(fileName)
Q_UNUSED(realFileName)
return false;
}
/*!
Used for example by EditorManager::openEditorWithContents() to set the contents
of this document.
......
......@@ -85,6 +85,9 @@ public:
void setId(Id id);
Id id() const;
// required to be re-implemented for documents of IEditors
virtual bool open(QString *errorString, const QString &fileName, const QString &realFileName);
virtual bool save(QString *errorString, const QString &fileName = QString(), bool autoSave = false) = 0;
virtual bool setContents(const QByteArray &contents);
......
......@@ -111,7 +111,7 @@ public:
QScopedPointer<TextEditor::BaseTextEditor> editor(
TextEditor::PlainTextEditorFactory::createPlainTextEditor());
QString error;
editor->open(&error, document->fileName(), document->fileName());
editor->document()->open(&error, document->fileName(), document->fileName());
QVERIFY(error.isEmpty());
// Set cursor position
......
......@@ -53,11 +53,6 @@ FormWindowEditor::~FormWindowEditor()
{
}
bool FormWindowEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
return formWindowFile()->open(errorString, fileName, realFileName);
}
QWidget *FormWindowEditor::toolBar()
{
return 0;
......
......@@ -54,7 +54,6 @@ public:
FormWindowEditor();
~FormWindowEditor() override;
bool open(QString *errorString, const QString &fileName, const QString &realFileName) override;
QWidget *toolBar() override;
bool isDesignModePreferred() const override;
......
......@@ -52,9 +52,9 @@ public:
explicit FormWindowFile(QDesignerFormWindowInterface *form, QObject *parent = 0);
~FormWindowFile() override { }
bool open(QString *errorString, const QString &fileName, const QString &realFileName) override;
// IDocument
bool open(QString *errorString, const QString &fileName,
const QString &realFileName) override;
bool save(QString *errorString, const QString &fileName, bool autoSave) override;
bool setContents(const QByteArray &contents) override;
bool shouldAutoSave() const override;
......
......@@ -364,14 +364,6 @@ Core::IEditor *DiffEditor::duplicate()
return editor;
}
bool DiffEditor::open(QString *errorString,
const QString &fileName,
const QString &realFileName)
{
Q_UNUSED(realFileName)
return m_document->open(errorString, fileName);
}
Core::IDocument *DiffEditor::document()
{
return m_document.data();
......
......@@ -63,12 +63,7 @@ public:
~DiffEditor() override;
Core::IEditor *duplicate() override;
bool open(QString *errorString,
const QString &fileName,
const QString &realFileName) override;
Core::IDocument *document() override;
QWidget *toolBar() override;
private slots:
......
......@@ -235,12 +235,14 @@ bool DiffEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeTyp
Q_UNUSED(type)
if (flag == FlagIgnore)
return true;
return open(errorString, filePath().toString());
return open(errorString, filePath().toString(), filePath().toString());
}
bool DiffEditorDocument::open(QString *errorString, const QString &fileName)
bool DiffEditorDocument::open(QString *errorString, const QString &fileName,
const QString &realFileName)
{
QTC_ASSERT(errorString, return false);
QTC_ASSERT(fileName == realFileName, return false); // does not support autosave
beginReload();
QString patch;
if (read(fileName, &patch, errorString) != TextFileFormat::ReadSuccess)
......
......@@ -77,7 +77,7 @@ public:
bool save(QString *errorString, const QString &fileName, bool autoSave);
void reload();
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
bool open(QString *errorString, const QString &fileName);
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
QString plainText() const;
......
......@@ -295,8 +295,9 @@ void GitEditorWidget::addDiffActions(QMenu *menu, const DiffChunk &chunk)
});
}
bool GitEditorWidget::open(QString *errorString, const QString &fileName, const QString &realFileName)
void GitEditorWidget::aboutToOpen(const QString &fileName, const QString &realFileName)
{
Q_UNUSED(realFileName)
Core::Id editorId = textDocument()->id();
if (editorId == Git::Constants::GIT_COMMIT_TEXT_EDITOR_ID
|| editorId == Git::Constants::GIT_REBASE_EDITOR_ID) {
......@@ -306,7 +307,6 @@ bool GitEditorWidget::open(QString *errorString, const QString &fileName, const
textDocument()->setCodec(
GitPlugin::instance()->client()->encoding(gitPath, "i18n.commitEncoding"));
}
return VcsBaseEditorWidget::open(errorString, fileName, realFileName);
}
QString GitEditorWidget::decorateVersion(const QString &revision) const
......
......@@ -62,7 +62,7 @@ private:
void init() override;
void resetChange(const QByteArray &resetType);
void addDiffActions(QMenu *menu, const VcsBase::DiffChunk &chunk) override;
bool open(QString *errorString, const QString &fileName, const QString &realFileName) override;
void aboutToOpen(const QString &fileName, const QString &realFileName) override;
QSet<QString> annotationChanges() const override;
QString changeUnderCursor(const QTextCursor &) const override;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const override;
......
......@@ -142,6 +142,8 @@ void ImageViewer::ctor()
this, &ImageViewer::playToggled);
connect(d->file.data(), &ImageViewerFile::imageSizeChanged,
this, &ImageViewer::imageSizeUpdated);
connect(d->file.data(), &ImageViewerFile::openFinished,
d->imageView, &ImageView::createScene);
connect(d->file.data(), &ImageViewerFile::aboutToReload,
d->imageView, &ImageView::reset);
connect(d->file.data(), &ImageViewerFile::reloadFinished,
......@@ -159,14 +161,6 @@ ImageViewer::~ImageViewer()
delete d;
}
bool ImageViewer::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
if (!d->file->open(errorString, fileName, realFileName))
return false;
d->imageView->createScene();
return true;
}
Core::IDocument *ImageViewer::document()
{
return d->file.data();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment