Commit fade19a9 authored by Eike Ziller's avatar Eike Ziller

Move IEditor::createNew to IDocument::setContents

The method is for setting the contents, so it belongs to the document,
and should be named correspondingly.

Change-Id: I40363dc08f11268f530885b512e4a88e8b10d096
Reviewed-by: default avatarFriedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 0ba2b9b3
......@@ -195,6 +195,14 @@ public:
return QLatin1String(Constants::C_BINEDITOR_MIMETYPE);
}
bool setContents(const QByteArray &contents)
{
if (!contents.isEmpty())
return false;
m_widget->clear();
return true;
}
bool save(QString *errorString, const QString &fn, bool autoSave)
{
QTC_ASSERT(!autoSave, return true); // bineditor does not support autosave - it would be a bit expensive
......@@ -342,11 +350,6 @@ public:
delete m_widget;
}
bool createNew(const QString & /* contents */ = QString()) {
m_widget->clear();
m_file->setFilePath(QString());
return true;
}
bool open(QString *errorString, const QString &fileName, const QString &realFileName) {
QTC_ASSERT(fileName == realFileName, return false); // The bineditor can do no autosaving
return m_file->open(errorString, fileName);
......
......@@ -929,7 +929,7 @@ void ClearCasePlugin::ccDiffWithPred(const QString &workingDir, const QStringLis
if (files.count() == 1) {
// Show in the same editor if diff has been executed before
if (Core::IEditor *existingEditor = VcsBase::VcsBaseEditorWidget::locateEditorByTag(tag)) {
existingEditor->createNew(result);
existingEditor->document()->setContents(result.toUtf8());
Core::EditorManager::activateEditor(existingEditor);
setDiffBaseDirectory(existingEditor, workingDir);
return;
......@@ -1189,7 +1189,7 @@ void ClearCasePlugin::history(const QString &workingDir,
const QString id = VcsBase::VcsBaseEditorWidget::getTitleId(workingDir, files);
const QString tag = VcsBase::VcsBaseEditorWidget::editorTag(VcsBase::LogOutput, workingDir, files);
if (Core::IEditor *editor = VcsBase::VcsBaseEditorWidget::locateEditorByTag(tag)) {
editor->createNew(response.stdOut);
editor->document()->setContents(response.stdOut.toUtf8());
Core::EditorManager::activateEditor(editor);
} else {
const QString title = QString::fromLatin1("cc history %1").arg(id);
......@@ -1301,7 +1301,7 @@ void ClearCasePlugin::vcsAnnotate(const QString &workingDir, const QString &file
const QStringList files = QStringList(file);
const QString tag = VcsBase::VcsBaseEditorWidget::editorTag(VcsBase::AnnotateOutput, workingDir, files);
if (Core::IEditor *editor = VcsBase::VcsBaseEditorWidget::locateEditorByTag(tag)) {
editor->createNew(res);
editor->document()->setContents(res.toUtf8());
VcsBase::VcsBaseEditorWidget::gotoLineOfEditor(editor, lineNumber);
Core::EditorManager::activateEditor(editor);
} else {
......@@ -1338,7 +1338,7 @@ void ClearCasePlugin::describe(const QString &source, const QString &changeNr)
// the common usage pattern of continuously changing and diffing a file
const QString tag = VcsBase::VcsBaseEditorWidget::editorTag(VcsBase::DiffOutput, source, QStringList(), changeNr);
if (Core::IEditor *editor = VcsBase::VcsBaseEditorWidget::locateEditorByTag(tag)) {
editor->createNew(description);
editor->document()->setContents(description.toUtf8());
Core::EditorManager::activateEditor(editor);
} else {
const QString title = QString::fromLatin1("cc describe %1").arg(id);
......@@ -1397,7 +1397,7 @@ Core::IEditor *ClearCasePlugin::showOutputInEditor(const QString& title, const Q
qDebug() << "ClearCasePlugin::showOutputInEditor" << title << id.name()
<< "Size= " << output.size() << " Type=" << editorType << debugCodec(codec);
QString s = title;
Core::IEditor *editor = Core::EditorManager::openEditorWithContents(id, &s, output);
Core::IEditor *editor = Core::EditorManager::openEditorWithContents(id, &s, output.toUtf8());
connect(editor, SIGNAL(annotateRevisionRequested(QString,QString,int)),
this, SLOT(annotateVersion(QString,QString,int)));
ClearCaseEditor *e = qobject_cast<ClearCaseEditor*>(editor->widget());
......
......@@ -1627,7 +1627,7 @@ QStringList EditorManager::getOpenFileNames() const
IEditor *EditorManager::openEditorWithContents(const Id &editorId,
QString *titlePattern,
const QString &contents)
const QByteArray &contents)
{
if (debugEditorManager)
qDebug() << Q_FUNC_INFO << editorId.name() << titlePattern << contents;
......@@ -1669,7 +1669,7 @@ IEditor *EditorManager::openEditorWithContents(const Id &editorId,
return 0;
}
if (!edt->createNew(contents)) {
if (!edt->document()->setContents(contents)) {
QApplication::restoreOverrideCursor();
delete edt;
edt = 0;
......
......@@ -115,7 +115,7 @@ public:
const Id &editorId = Id(), OpenEditorFlags flags = 0,
bool *newEditor = 0);
static IEditor *openEditorWithContents(const Id &editorId,
QString *titlePattern = 0, const QString &contents = QString());
QString *titlePattern = 0, const QByteArray &contents = QByteArray());
static bool openExternalEditor(const QString &fileName, const Id &editorId);
......
......@@ -47,7 +47,6 @@ public:
IEditor(QObject *parent = 0) : IContext(parent) {}
virtual ~IEditor() {}
virtual bool createNew(const QString &contents = QString()) = 0;
virtual bool open(QString *errorString, const QString &fileName, const QString &realFileName) = 0;
virtual IDocument *document() = 0;
virtual Core::Id id() const = 0;
......
......@@ -57,6 +57,18 @@ IDocument::~IDocument()
delete m_infoBar;
}
/*!
Used for example by EditorManager::openEditorWithContents() to set the contents
of this document.
Returns if setting the contents was successful.
The base implementation does nothing and returns false.
*/
bool IDocument::setContents(const QByteArray &contents)
{
Q_UNUSED(contents)
return false;
}
IDocument::ReloadBehavior IDocument::reloadBehavior(ChangeTrigger state, ChangeType type) const
{
if (type == TypePermissions)
......
......@@ -82,6 +82,8 @@ public:
virtual ~IDocument();
virtual bool save(QString *errorString, const QString &fileName = QString(), bool autoSave = false) = 0;
virtual bool setContents(const QByteArray &contents);
QString filePath() const { return m_filePath; }
virtual void setFilePath(const QString &filePath);
QString displayName() const;
......
......@@ -591,7 +591,7 @@ void CvsPlugin::cvsDiff(const CvsDiffParameters &p)
// Show in the same editor if diff has been executed before
const QString tag = VcsBaseEditorWidget::editorTag(DiffOutput, p.workingDir, p.files);
if (IEditor *existingEditor = VcsBaseEditorWidget::locateEditorByTag(tag)) {
existingEditor->createNew(output);
existingEditor->document()->setContents(output.toUtf8());
EditorManager::activateEditor(existingEditor);
setDiffBaseDirectory(existingEditor, p.workingDir);
return;
......@@ -859,7 +859,7 @@ void CvsPlugin::filelog(const QString &workingDir,
// the common usage pattern of continuously changing and diffing a file
const QString tag = VcsBaseEditorWidget::editorTag(LogOutput, workingDir, files);
if (Core::IEditor *editor = VcsBaseEditorWidget::locateEditorByTag(tag)) {
editor->createNew(response.stdOut);
editor->document()->setContents(response.stdOut.toUtf8());
Core::EditorManager::activateEditor(editor);
} else {
const QString title = QString::fromLatin1("cvs log %1").arg(id);
......@@ -1001,7 +1001,7 @@ void CvsPlugin::annotate(const QString &workingDir, const QString &file,
const QString tag = VcsBaseEditorWidget::editorTag(AnnotateOutput, workingDir, QStringList(file), revision);
if (IEditor *editor = VcsBaseEditorWidget::locateEditorByTag(tag)) {
editor->createNew(response.stdOut);
editor->document()->setContents(response.stdOut.toUtf8());
VcsBaseEditorWidget::gotoLineOfEditor(editor, lineNumber);
EditorManager::activateEditor(editor);
} else {
......@@ -1197,7 +1197,7 @@ bool CvsPlugin::describe(const QString &repositoryPath,
// the common usage pattern of continuously changing and diffing a file
const QString commitId = entries.front().revisions.front().commitId;
if (IEditor *editor = VcsBaseEditorWidget::locateEditorByTag(commitId)) {
editor->createNew(output);
editor->document()->setContents(output.toUtf8());
EditorManager::activateEditor(editor);
setDiffBaseDirectory(editor, repositoryPath);
} else {
......@@ -1270,7 +1270,7 @@ IEditor *CvsPlugin::showOutputInEditor(const QString& title, const QString &outp
qDebug() << "CVSPlugin::showOutputInEditor" << title << id.name()
<< "source=" << source << "Size= " << output.size() << " Type=" << editorType << debugCodec(codec);
QString s = title;
IEditor *editor = EditorManager::openEditorWithContents(id, &s, output);
IEditor *editor = EditorManager::openEditorWithContents(id, &s, output.toUtf8());
connect(editor, SIGNAL(annotateRevisionRequested(QString,QString,int)),
this, SLOT(vcsAnnotate(QString,QString,int)));
CvsEditor *e = qobject_cast<CvsEditor*>(editor->widget());
......
......@@ -2530,7 +2530,7 @@ void DebuggerPluginPrivate::openTextEditor(const QString &titlePattern0,
return;
QString titlePattern = titlePattern0;
IEditor *editor = EditorManager::openEditorWithContents(
CC::K_DEFAULT_TEXT_EDITOR_ID, &titlePattern, contents);
CC::K_DEFAULT_TEXT_EDITOR_ID, &titlePattern, contents.toUtf8());
QTC_ASSERT(editor, return);
EditorManager::activateEditor(editor, EditorManager::IgnoreNavigationHistory);
}
......
......@@ -115,7 +115,7 @@ void SourceAgent::setContent(const QString &filePath, const QString &content)
d->editor = qobject_cast<ITextEditor *>(
EditorManager::openEditorWithContents(
CppEditor::Constants::CPPEDITOR_ID,
&titlePattern, content));
&titlePattern, content.toUtf8()));
QTC_ASSERT(d->editor, return);
d->editor->document()->setProperty(Debugger::Constants::OPENED_BY_DEBUGGER, true);
......
......@@ -38,16 +38,10 @@
#include <utils/qtcassert.h>
#if QT_VERSION >= 0x050000
# include <QDesignerFormWindowInterface>
# include <QBuffer>
#else
# include "qt_private/formwindowbase_p.h"
#endif
#include <QBuffer>
#include <QDebug>
#include <QDesignerFormWindowInterface>
#include <QFileInfo>
#include <QApplication>
namespace Designer {
......@@ -74,47 +68,6 @@ FormWindowEditor::~FormWindowEditor()
delete d;
}
bool FormWindowEditor::createNew(const QString &contents)
{
if (Designer::Constants::Internal::debug)
qDebug() << "FormWindowEditor::createNew" << contents.size();
syncXmlEditor(QString());
QDesignerFormWindowInterface *form = d->m_widget->formWindowFile()->formWindow();
QTC_ASSERT(form, return false);
if (contents.isEmpty())
return false;
// If we have an override cursor, reset it over Designer loading,
// should it pop up messages about missing resources or such.
const bool hasOverrideCursor = QApplication::overrideCursor();
QCursor overrideCursor;
if (hasOverrideCursor) {
overrideCursor = QCursor(*QApplication::overrideCursor());
QApplication::restoreOverrideCursor();
}
#if QT_VERSION >= 0x050000
const bool success = form->setContents(contents);
#else
form->setContents(contents);
const bool success = form->mainContainer() != 0;
#endif
if (hasOverrideCursor)
QApplication::setOverrideCursor(overrideCursor);
if (!success)
return false;
syncXmlEditor(contents);
d->m_widget->formWindowFile()->setFilePath(QString());
d->m_widget->formWindowFile()->setShouldAutoSave(false);
return true;
}
void FormWindowEditor::slotOpen(QString *errorString, const QString &fileName)
{
open(errorString, fileName, fileName);
......@@ -152,7 +105,7 @@ bool FormWindowEditor::open(QString *errorString, const QString &fileName, const
return false;
#endif
form->setDirty(fileName != realFileName);
syncXmlEditor(contents);
d->m_widget->formWindowFile()->syncXmlFromFormWindow();
d->m_widget->formWindowFile()->setFilePath(absfileName);
d->m_widget->formWindowFile()->setShouldAutoSave(false);
......@@ -167,12 +120,7 @@ void FormWindowEditor::syncXmlEditor()
{
if (Designer::Constants::Internal::debug)
qDebug() << "FormWindowEditor::syncXmlEditor" << d->m_widget->formWindowFile()->filePath();
syncXmlEditor(contents());
}
void FormWindowEditor::syncXmlEditor(const QString &contents)
{
editorWidget()->document()->setPlainText(contents);
d->m_widget->formWindowFile()->syncXmlFromFormWindow();
}
Core::Id FormWindowEditor::id() const
......@@ -187,17 +135,7 @@ QWidget *FormWindowEditor::toolBar()
QString FormWindowEditor::contents() const
{
#if QT_VERSION >= 0x050000 // TODO: No warnings about spacers here
const QDesignerFormWindowInterface *fw = d->m_widget->formWindowFile()->formWindow();
QTC_ASSERT(fw, return QString());
return fw->contents();
#else
// No warnings about spacers here
const qdesigner_internal::FormWindowBase *fw =
qobject_cast<const qdesigner_internal::FormWindowBase *>(d->m_widget->formWindowFile()->formWindow());
QTC_ASSERT(fw, return QString());
return fw->fileContents();
#endif
return d->m_widget->formWindowFile()->formWindowContents();
}
bool FormWindowEditor::isDesignModePreferred() const
......
......@@ -66,7 +66,6 @@ public:
virtual ~FormWindowEditor();
// IEditor
virtual bool createNew(const QString &contents = QString());
virtual bool open(QString *errorString, const QString &fileName, const QString &realFileName);
virtual Core::Id id() const;
......@@ -84,8 +83,6 @@ private slots:
void slotOpen(QString *errorString, const QString &fileName);
private:
void syncXmlEditor(const QString &contents);
FormWindowEditorPrivate *d;
};
......
......@@ -32,13 +32,16 @@
#include <utils/qtcassert.h>
#include <QApplication>
#include <QDesignerFormWindowInterface>
#include <QDesignerFormWindowManagerInterface>
#include <QDesignerFormEditorInterface>
#if QT_VERSION < 0x050000
# include "qt_private/qsimpleresource_p.h"
# include "qt_private/formwindowbase_p.h"
#endif
#include <QTextDocument>
#include <QUndoStack>
#include <QFileInfo>
......@@ -103,6 +106,45 @@ bool FormWindowFile::save(QString *errorString, const QString &name, bool autoSa
return true;
}
bool FormWindowFile::setContents(const QByteArray &contents)
{
if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << contents.size();
document()->setPlainText(QString());
QTC_ASSERT(m_formWindow, return false);
if (contents.isEmpty())
return false;
// If we have an override cursor, reset it over Designer loading,
// should it pop up messages about missing resources or such.
const bool hasOverrideCursor = QApplication::overrideCursor();
QCursor overrideCursor;
if (hasOverrideCursor) {
overrideCursor = QCursor(*QApplication::overrideCursor());
QApplication::restoreOverrideCursor();
}
#if QT_VERSION >= 0x050000
const bool success = m_formWindow->setContents(QString::fromUtf8(contents));
#else
m_formWindow->setContents(QString::fromUtf8(contents));
const bool success = m_formWindow->mainContainer() != 0;
#endif
if (hasOverrideCursor)
QApplication::setOverrideCursor(overrideCursor);
if (!success)
return false;
syncXmlFromFormWindow();
setShouldAutoSave(false);
return true;
}
void FormWindowFile::setFilePath(const QString &newName)
{
m_formWindow->setFileName(newName);
......@@ -184,6 +226,25 @@ QDesignerFormWindowInterface *FormWindowFile::formWindow() const
return m_formWindow;
}
void FormWindowFile::syncXmlFromFormWindow()
{
document()->setPlainText(formWindowContents());
}
QString FormWindowFile::formWindowContents() const
{
#if QT_VERSION >= 0x050000 // TODO: No warnings about spacers here
QTC_ASSERT(m_formWindow, return QString());
return m_formWindow->contents();
#else
// No warnings about spacers here
const qdesigner_internal::FormWindowBase *fw =
qobject_cast<const qdesigner_internal::FormWindowBase *>(m_formWindow);
QTC_ASSERT(fw, return QString());
return fw->fileContents();
#endif
}
void FormWindowFile::slotFormWindowRemoved(QDesignerFormWindowInterface *w)
{
// Release formwindow as soon as the FormWindowManager removes
......
......@@ -51,14 +51,15 @@ public:
~FormWindowFile() { }
// IDocument
virtual bool save(QString *errorString, const QString &fileName, bool autoSave);
virtual bool shouldAutoSave() const;
virtual bool isModified() const;
virtual bool isSaveAsAllowed() const;
bool save(QString *errorString, const QString &fileName, bool autoSave);
bool setContents(const QByteArray &contents);
bool shouldAutoSave() const;
bool isModified() const;
bool isSaveAsAllowed() const;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
virtual QString defaultPath() const;
virtual QString suggestedFileName() const;
virtual QString mimeType() const;
QString defaultPath() const;
QString suggestedFileName() const;
QString mimeType() const;
// Internal
void setSuggestedFileName(const QString &fileName);
......@@ -66,6 +67,8 @@ public:
bool writeFile(const QString &fileName, QString *errorString) const;
QDesignerFormWindowInterface *formWindow() const;
void syncXmlFromFormWindow();
QString formWindowContents() const;
signals:
// Internal
......
......@@ -67,12 +67,6 @@ DiffEditor::~DiffEditor()
delete m_widget;
}
bool DiffEditor::createNew(const QString &contents)
{
Q_UNUSED(contents)
return true;
}
bool DiffEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
Q_UNUSED(errorString)
......
......@@ -60,7 +60,6 @@ public:
void clear(const QString &message);
// Core::IEditor
bool createNew(const QString &contents);
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
Core::IDocument *document();
Core::Id id() const;
......
......@@ -46,6 +46,12 @@ DiffEditorFile::DiffEditorFile(const QString &mimeType, QObject *parent) :
setTemporary(true);
}
bool DiffEditorFile::setContents(const QByteArray &contents)
{
Q_UNUSED(contents);
return false;
}
void DiffEditorFile::setModified(bool modified)
{
if (m_modified == modified)
......
......@@ -48,6 +48,7 @@ public:
explicit DiffEditorFile(const QString &mimeType,
QObject *parent = 0);
bool setContents(const QByteArray &contents);
QString defaultPath() const { return QString(); }
QString suggestedFileName() const { return QString(); }
......
......@@ -103,7 +103,7 @@ void DiffEditorPlugin::diff()
//: Editor title
QString title = tr("Diff \"%1\", \"%2\"").arg(fileName1).arg(fileName2);
DiffEditor *editor = qobject_cast<DiffEditor *>
(Core::EditorManager::openEditorWithContents(editorId, &title, QString()));
(Core::EditorManager::openEditorWithContents(editorId, &title));
if (!editor)
return;
......
......@@ -42,10 +42,6 @@ QT_END_NAMESPACE
namespace DiffEditor {
namespace Internal {
class DiffEditorFile;
}
class DIFFEDITOR_EXPORT DiffShowEditor : public DiffEditor
{
Q_OBJECT
......
......@@ -867,7 +867,7 @@ VcsBase::VcsBaseEditorWidget *GitClient::findExistingVCSEditor(const char *regis
// Exists already
Core::EditorManager::activateEditor(outputEditor);
outputEditor->createNew(m_msgWait);
outputEditor->document()->setContents(m_msgWait.toUtf8());
rc = VcsBase::VcsBaseEditorWidget::getVcsBaseEditor(outputEditor);
return rc;
......@@ -880,14 +880,14 @@ DiffEditor::DiffEditor *GitClient::findExistingOrOpenNewDiffEditor(const char *r
if (outputEditor) {
// Exists already
Core::EditorManager::activateEditor(outputEditor);
outputEditor->createNew(m_msgWait);
outputEditor->document()->setContents(m_msgWait.toUtf8());
}
DiffEditor::DiffEditor *editor = qobject_cast<DiffEditor::DiffEditor *>(outputEditor);
if (!editor) {
QString title = titlePattern;
editor = qobject_cast<DiffEditor::DiffEditor *>(
Core::EditorManager::openEditorWithContents(editorId, &title, m_msgWait));
Core::EditorManager::openEditorWithContents(editorId, &title, m_msgWait.toUtf8()));
editor->document()->setProperty(registerDynamicProperty, dynamicPropertyValue);
Core::EditorManager::activateEditor(editor);
}
......@@ -913,7 +913,8 @@ VcsBase::VcsBaseEditorWidget *GitClient::createVcsEditor(const Core::Id &id,
QTC_CHECK(!findExistingVCSEditor(registerDynamicProperty, dynamicPropertyValue));
// Create new, set wait message, set up with source and codec
Core::IEditor *outputEditor = Core::EditorManager::openEditorWithContents(id, &title, m_msgWait);
Core::IEditor *outputEditor = Core::EditorManager::openEditorWithContents(id, &title,
m_msgWait.toUtf8());
outputEditor->document()->setProperty(registerDynamicProperty, dynamicPropertyValue);
rc = VcsBase::VcsBaseEditorWidget::getVcsBaseEditor(outputEditor);
connect(rc, SIGNAL(annotateRevisionRequested(QString,QString,int)),
......
......@@ -119,12 +119,6 @@ ImageViewer::~ImageViewer()
delete d;
}
bool ImageViewer::createNew(const QString &contents)
{
Q_UNUSED(contents)
return false;
}
bool ImageViewer::open(QString *errorString, const QString &fileName, const QString &realFileName)
{
if (!d->imageView->openFile(realFileName)) {
......
......@@ -54,7 +54,6 @@ public:
explicit ImageViewer(QWidget *parent = 0);
~ImageViewer();
bool createNew(const QString &contents = QString());
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
Core::IDocument *document();
Core::Id id() const;
......
......@@ -72,6 +72,12 @@ bool ImageViewerFile::save(QString *errorString, const QString &fileName, bool a
return false;
}
bool ImageViewerFile::setContents(const QByteArray &contents)
{
Q_UNUSED(contents);
return false;
}
QString ImageViewerFile::defaultPath() const
{
return QString();
......
......@@ -46,6 +46,7 @@ public:
explicit ImageViewerFile(ImageViewer *parent = 0);
bool save(QString *errorString, const QString &fileName, bool autoSave);
bool setContents(const QByteArray &contents);
QString defaultPath() const;
QString suggestedFileName() const;
......
......@@ -1166,7 +1166,7 @@ Core::IEditor *PerforcePlugin::showOutputInEditor(const QString &title, const QS
qDebug() << "PerforcePlugin::showOutputInEditor" << title << id.name()
<< "Size= " << output.size() << " Type=" << editorType << debugCodec(codec);
QString s = title;
Core::IEditor *editor = Core::EditorManager::openEditorWithContents(id, &s, output);
Core::IEditor *editor = Core::EditorManager::openEditorWithContents(id, &s, output.toUtf8());
connect(editor, SIGNAL(annotateRevisionRequested(QString,QString,int)),
this, SLOT(vcsAnnotate(QString,QString,int)));
PerforceEditor *e = qobject_cast<PerforceEditor*>(editor->widget());
......@@ -1260,7 +1260,7 @@ void PerforcePlugin::p4Diff(const PerforceDiffParameters &p)
return;
if (existingEditor) {
existingEditor->createNew(result.stdOut);