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

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();
......
......@@ -55,7 +55,6 @@ public:
explicit ImageViewer(QWidget *parent = 0);
~ImageViewer() override;
bool open(QString *errorString, const QString &fileName, const QString &realFileName) override;
Core::IDocument *document() override;
QWidget *toolBar() override;
......
......@@ -88,7 +88,13 @@ ImageViewerFile::~ImageViewerFile()
bool ImageViewerFile::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
QTC_CHECK(fileName == realFileName); // does not support auto save
bool success = openImpl(errorString, fileName);
emit openFinished(success);
return success;
}
bool ImageViewerFile::openImpl(QString *errorString, const QString &fileName)
{
cleanUp();
m_type = TypeInvalid;
......@@ -160,7 +166,7 @@ bool ImageViewerFile::reload(QString *errorString,
return true;
}
emit aboutToReload();
bool success = open(errorString, filePath().toString(), filePath().toString());
bool success = openImpl(errorString, filePath().toString());
emit reloadFinished(success);
return success;
}
......
......@@ -87,11 +87,13 @@ public:
void updateVisibility();
signals:
void openFinished(bool success);
void imageSizeChanged(const QSize &size);
void isPausedChanged(bool paused);
private:
void cleanUp();
bool openImpl(QString *errorString, const QString &fileName);
ImageType m_type = TypeInvalid;
#ifndef QT_NO_SVG
......
......@@ -120,11 +120,6 @@ ResourceEditorW::~ResourceEditorW()
delete m_toolBar;
}
bool ResourceEditorW::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
return m_resourceDocument->open(errorString, fileName, realFileName);
}
bool ResourceEditorDocument::open(QString *errorString, const QString &fileName,
const QString &realFileName)
{
......
......@@ -56,8 +56,8 @@ class ResourceEditorDocument
public:
ResourceEditorDocument(QObject *parent = 0);
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
//IDocument
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
bool save(QString *errorString, const QString &fileName, bool autoSave);
QString plainText() const;
bool setContents(const QByteArray &contents);
......@@ -95,7 +95,6 @@ public:
~ResourceEditorW() override;
// IEditor
bool open(QString *errorString, const QString &fileName, const QString &realFileName) override;
Core::IDocument *document() override { return m_resourceDocument; }
QWidget *toolBar() override;
......
......@@ -94,11 +94,12 @@ bool TaskFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
deleteLater();
return true;
}
return open(errorString, filePath().toString());
return open(errorString, filePath().toString(), filePath().toString());
}
bool TaskFile::open(QString *errorString, const QString &fileName)
bool TaskFile::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
Q_UNUSED(realFileName)
setFilePath(Utils::FileName::fromString(fileName));
return TaskListPlugin::loadFile(errorString, m_baseDir, fileName);
}
......
......@@ -55,7 +55,7 @@ public:
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
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 baseDir() const;
void setBaseDir(const QString &base);
......
......@@ -177,7 +177,7 @@ IDocument *TaskListPlugin::openTasks(const QString &base, const QString &fileNam
file->setBaseDir(base);
QString errorString;
if (!file->open(&errorString, fileName)) {
if (!file->open(&errorString, fileName, fileName)) {
QMessageBox::critical(ICore::mainWindow(), tr("File Error"), errorString);
delete file;
return 0;
......
......@@ -44,6 +44,7 @@
#include <texteditor/generichighlighter/highlighter.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/documentmodel.h>
#include <utils/mimetypes/mimedatabase.h>
#include <QApplication>
#include <QDir>
......@@ -558,6 +559,19 @@ void TextDocument::checkPermissions()
}
bool TextDocument::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
emit aboutToOpen(fileName, realFileName);
bool success = openImpl(errorString, fileName, realFileName);
if (success) {
Utils::MimeDatabase mdb;
setMimeType(mdb.mimeTypeForFile(fileName).name());
emit openFinishedSuccessfully();
return true;
}
return false;
}
bool TextDocument::openImpl(QString *errorString, const QString &fileName, const QString &realFileName)
{
QStringList content;
......@@ -620,7 +634,7 @@ bool TextDocument::reload(QString *errorString)
if (documentLayout)
marks = documentLayout->documentClosing(); // removes text marks non-permanently
bool success = open(errorString, filePath().toString(), filePath().toString());
bool success = openImpl(errorString, filePath().toString(), filePath().toString());
if (documentLayout)
documentLayout->documentReloaded(marks, this); // re-adds text marks
......
......@@ -117,7 +117,7 @@ public:
void setDefaultPath(const QString &defaultPath);
void setSuggestedFileName(const QString &suggestedFileName);
virtual bool open(QString *errorString, const QString &fileName, const QString &realFileName);
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
virtual bool reload(QString *errorString);
bool setPlainText(const QString &text);
......@@ -138,6 +138,8 @@ public slots:
void setFontSettings(const TextEditor::FontSettings &fontSettings);
signals:
void aboutToOpen(const QString &fileName, const QString &realFileName);
void openFinishedSuccessfully();
void contentsChanged();
void tabSettingsChanged();
void fontSettingsChanged();
......@@ -146,6 +148,7 @@ protected slots:
virtual void applyFontSettings();
private:
bool openImpl(QString *errorString, const QString &fileName, const QString &realFileName);
void cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, bool inEntireDocument);
void ensureFinalNewLine(QTextCursor &cursor);
......
......@@ -681,6 +681,10 @@ void TextEditorWidgetPrivate::ctor(const QSharedPointer<TextDocument> &doc)
updateCannotDecodeInfo();
QObject::connect(m_document.data(), &TextDocument::aboutToOpen,
q, &TextEditorWidget::aboutToOpen);
QObject::connect(m_document.data(), &TextDocument::openFinishedSuccessfully,
q, &TextEditorWidget::openFinishedSuccessfully);
connect(m_fileEncodingLabel, &LineColumnLabel::clicked,
q, &TextEditorWidget::selectEncoding);
connect(m_document->document(), &QTextDocument::modificationChanged,
......@@ -972,17 +976,6 @@ void TextEditorWidgetPrivate::updateCannotDecodeInfo()
}
}
bool TextEditorWidget::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
if (d->m_document->open(errorString, fileName, realFileName)) {
moveCursor(QTextCursor::Start);
d->updateCannotDecodeInfo();
updateTextCodecLabel();
return true;
}
return false;
}
/*
Collapses the first comment in a file, if there is only whitespace above
*/
......@@ -1014,6 +1007,19 @@ TextDocument *TextEditorWidget::textDocument() const
return d->m_document.data();
}
void TextEditorWidget::aboutToOpen(const QString &fileName, const QString &realFileName)
{
Q_UNUSED(fileName)
Q_UNUSED(realFileName)
}
void TextEditorWidget::openFinishedSuccessfully()
{
moveCursor(QTextCursor::Start);
d->updateCannotDecodeInfo();
updateTextCodecLabel();
}
TextDocumentPtr TextEditorWidget::textDocumentPtr() const
{
return d->m_document;
......@@ -7142,15 +7148,6 @@ QString TextEditorWidget::foldReplacementText(const QTextBlock &) const
return QLatin1String("...");
}
bool BaseTextEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
if (!editorWidget()->open(errorString, fileName, realFileName))
return false;
Utils::MimeDatabase mdb;
textDocument()->setMimeType(mdb.mimeTypeForFile(fileName).name());
return true;
}
QByteArray BaseTextEditor::saveState() const
{
return editorWidget()->saveState();
......
......@@ -150,7 +150,6 @@ public:
// IEditor
Core::IDocument *document() override;
bool open(QString *errorString, const QString &fileName, const QString &realFileName) override;
IEditor *duplicate() override;
......@@ -211,8 +210,9 @@ public:
TextDocument *textDocument() const;
QSharedPointer<TextDocument> textDocumentPtr() const;
virtual void aboutToOpen(const QString &fileName, const QString &realFileName);
virtual void openFinishedSuccessfully();
// IEditor
virtual bool open(QString *errorString, const QString &fileName, const QString &realFileName);
QByteArray saveState() const;
bool restoreState(const QByteArray &state);
void gotoLine(int line, int column = 0, bool centerLine = true);
......
......@@ -57,6 +57,24 @@ SubmitEditorFile::SubmitEditorFile(const VcsBaseSubmitEditorParameters *paramete
setTemporary(true);
}
bool SubmitEditorFile::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
if (fileName.isEmpty())
return false;
FileReader reader;
if (!reader.fetch(realFileName, QIODevice::Text, errorString))
return false;
const QString text = QString::fromLocal8Bit(reader.data());
if (!m_editor->setFileContents(text.toUtf8()))
return false;
setFilePath(FileName::fromString(fileName));
setModified(fileName != realFileName);
return true;
}
bool SubmitEditorFile::setContents(const QByteArray &contents)
{
return m_editor->setFileContents(contents);
......
......@@ -47,6 +47,7 @@ public:
explicit SubmitEditorFile(const VcsBaseSubmitEditorParameters *parameters,
VcsBaseSubmitEditor *parent = 0);
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
bool setContents(const QByteArray &contents);
QString defaultPath() const { return QString(); }
QString suggestedFileName() const { return QString(); }
......
......@@ -362,24 +362,6 @@ void VcsBaseSubmitEditor::slotDescriptionChanged()
{
}
bool VcsBaseSubmitEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
if (fileName.isEmpty())
return false;
FileReader reader;
if (!reader.fetch(realFileName, QIODevice::Text, errorString))
return false;
const QString text = QString::fromLocal8Bit(reader.data());
if (!setFileContents(text.toUtf8()))
return false;
d->m_file->setFilePath(FileName::fromString(fileName));
d->m_file->setModified(fileName != realFileName);
return true;
}
Core::IDocument *VcsBaseSubmitEditor::document()
{
return d->m_file;
......
......@@ -113,8 +113,6 @@ public:
QString checkScriptWorkingDirectory() const;
void setCheckScriptWorkingDirectory(const QString &);
// Core::IEditor
bool open(QString *errorString, const QString &fileName, const QString &realFileName) override;
Core::IDocument *document() override;
QWidget *toolBar() override;
......
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