From 4ba6faec3f57d55c5c8644eab79652e5158c371b Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 15 Jul 2013 14:12:05 +0200 Subject: [PATCH] Designer: Remove some redirections. Instead of keeping a separate plain text editor and document, the FormWindowEditor and FormWindowFile are directly derived from PlainTextEditor and BaseTextDocument respectively. Change-Id: I4319904dea769ec31900061bc7c3a3c5c22e0e8a Reviewed-by: Friedemann Kleint Reviewed-by: Eike Ziller --- src/plugins/designer/designer.pro | 8 +- src/plugins/designer/designer.qbs | 4 +- ...editor.cpp => designerxmleditorwidget.cpp} | 22 ++++-- ...rxmleditor.h => designerxmleditorwidget.h} | 16 ++-- src/plugins/designer/formeditorfactory.cpp | 2 +- src/plugins/designer/formeditorw.cpp | 4 +- src/plugins/designer/formwindoweditor.cpp | 75 +++++-------------- src/plugins/designer/formwindoweditor.h | 28 +++---- src/plugins/designer/formwindowfile.cpp | 6 +- src/plugins/designer/formwindowfile.h | 8 +- 10 files changed, 72 insertions(+), 101 deletions(-) rename src/plugins/designer/{designerxmleditor.cpp => designerxmleditorwidget.cpp} (75%) rename src/plugins/designer/{designerxmleditor.h => designerxmleditorwidget.h} (84%) diff --git a/src/plugins/designer/designer.pro b/src/plugins/designer/designer.pro index 09648d581ee..adf2c06229f 100644 --- a/src/plugins/designer/designer.pro +++ b/src/plugins/designer/designer.pro @@ -36,12 +36,12 @@ HEADERS += formeditorplugin.h \ formwizarddialog.h \ codemodelhelpers.h \ designer_export.h \ - designerxmleditor.h \ designercontext.h \ formeditorstack.h \ editordata.h \ resourcehandler.h \ - qtdesignerformclasscodegenerator.h + qtdesignerformclasscodegenerator.h \ + designerxmleditorwidget.h SOURCES += formeditorplugin.cpp \ formeditorfactory.cpp \ @@ -56,11 +56,11 @@ SOURCES += formeditorplugin.cpp \ formtemplatewizardpage.cpp \ formwizarddialog.cpp \ codemodelhelpers.cpp \ - designerxmleditor.cpp \ designercontext.cpp \ formeditorstack.cpp \ resourcehandler.cpp \ - qtdesignerformclasscodegenerator.cpp + qtdesignerformclasscodegenerator.cpp \ + designerxmleditorwidget.cpp RESOURCES += designer.qrc diff --git a/src/plugins/designer/designer.qbs b/src/plugins/designer/designer.qbs index 36102c3efc9..e839776b676 100644 --- a/src/plugins/designer/designer.qbs +++ b/src/plugins/designer/designer.qbs @@ -28,8 +28,8 @@ QtcPlugin { "designerconstants.h", "designercontext.cpp", "designercontext.h", - "designerxmleditor.cpp", - "designerxmleditor.h", + "designerxmleditorwidget.cpp", + "designerxmleditorwidget.h", "editordata.h", "editorwidget.cpp", "editorwidget.h", diff --git a/src/plugins/designer/designerxmleditor.cpp b/src/plugins/designer/designerxmleditorwidget.cpp similarity index 75% rename from src/plugins/designer/designerxmleditor.cpp rename to src/plugins/designer/designerxmleditorwidget.cpp index b98d3c3f1b6..202f65c5651 100644 --- a/src/plugins/designer/designerxmleditor.cpp +++ b/src/plugins/designer/designerxmleditorwidget.cpp @@ -27,7 +27,7 @@ ** ****************************************************************************/ -#include "designerxmleditor.h" +#include "designerxmleditorwidget.h" #include "formwindoweditor.h" #include "designerconstants.h" @@ -37,26 +37,34 @@ namespace Designer { namespace Internal { -DesignerXmlEditor::DesignerXmlEditor(QDesignerFormWindowInterface *form, - QWidget *parent) : +DesignerXmlEditorWidget::DesignerXmlEditorWidget(QDesignerFormWindowInterface *form, + QWidget *parent) : TextEditor::PlainTextEditorWidget(parent), - m_designerEditor(new FormWindowEditor(this, form)) + m_file(new FormWindowFile(form, this)), + m_designerEditor(new FormWindowEditor(this)) { + setBaseTextDocument(m_file); setReadOnly(true); + configure(m_file->mimeType()); } -TextEditor::BaseTextEditor *DesignerXmlEditor::createEditor() +TextEditor::BaseTextEditor *DesignerXmlEditorWidget::createEditor() { if (Designer::Constants::Internal::debug) qDebug() << "DesignerXmlEditor::createEditableInterface()"; - return m_designerEditor->textEditor(); + return m_designerEditor; } -FormWindowEditor *DesignerXmlEditor::designerEditor() const +FormWindowEditor *DesignerXmlEditorWidget::designerEditor() const { return m_designerEditor; } +Internal::FormWindowFile *DesignerXmlEditorWidget::formWindowFile() const +{ + return m_file.data(); +} + } // namespace Internal } // namespace Designer diff --git a/src/plugins/designer/designerxmleditor.h b/src/plugins/designer/designerxmleditorwidget.h similarity index 84% rename from src/plugins/designer/designerxmleditor.h rename to src/plugins/designer/designerxmleditorwidget.h index 9e97b3ccdfb..9c0780d7056 100644 --- a/src/plugins/designer/designerxmleditor.h +++ b/src/plugins/designer/designerxmleditorwidget.h @@ -27,11 +27,15 @@ ** ****************************************************************************/ -#ifndef DESIGNERXMLEDITOR_H -#define DESIGNERXMLEDITOR_H +#ifndef DESIGNERXMLEDITORWIDGET_H +#define DESIGNERXMLEDITORWIDGET_H + +#include "formwindowfile.h" #include +#include + QT_BEGIN_NAMESPACE class QDesignerFormWindowInterface; QT_END_NAMESPACE @@ -49,23 +53,25 @@ namespace Internal { * Internally manages a FormWindowEditor and uses the plain text * editable embedded in it. */ -class DesignerXmlEditor : public TextEditor::PlainTextEditorWidget +class DesignerXmlEditorWidget : public TextEditor::PlainTextEditorWidget { Q_OBJECT public: - explicit DesignerXmlEditor(QDesignerFormWindowInterface *form, + explicit DesignerXmlEditorWidget(QDesignerFormWindowInterface *form, QWidget *parent = 0); FormWindowEditor *designerEditor() const; + Internal::FormWindowFile *formWindowFile() const; protected: virtual TextEditor::BaseTextEditor *createEditor(); private: + QSharedPointer m_file; FormWindowEditor *m_designerEditor; }; } // Internal } // Designer -#endif // DESIGNERXMLEDITOR_H +#endif // DESIGNERXMLEDITORWIDGET_H diff --git a/src/plugins/designer/formeditorfactory.cpp b/src/plugins/designer/formeditorfactory.cpp index 3da34f486a4..6425eb33a16 100644 --- a/src/plugins/designer/formeditorfactory.cpp +++ b/src/plugins/designer/formeditorfactory.cpp @@ -31,7 +31,7 @@ #include "formeditorw.h" #include "formwindoweditor.h" #include "editordata.h" -#include "designerxmleditor.h" +#include "designerxmleditorwidget.h" #include #include diff --git a/src/plugins/designer/formeditorw.cpp b/src/plugins/designer/formeditorw.cpp index 27c90a10b15..3a7c49e91a2 100644 --- a/src/plugins/designer/formeditorw.cpp +++ b/src/plugins/designer/formeditorw.cpp @@ -34,7 +34,7 @@ #include "editorwidget.h" #include "editordata.h" #include "qtcreatorintegration.h" -#include "designerxmleditor.h" +#include "designerxmleditorwidget.h" #include "designercontext.h" #include "resourcehandler.h" #include @@ -710,7 +710,7 @@ EditorData FormEditorW::createEditor(QWidget *parent) qdesigner_internal::FormWindowBase::setupDefaultAction(form); #endif data.widgetHost = new SharedTools::WidgetHost( /* parent */ 0, form); - DesignerXmlEditor *xmlEditor = new DesignerXmlEditor(form, parent); + DesignerXmlEditorWidget *xmlEditor = new DesignerXmlEditorWidget(form, parent); TextEditor::TextEditorSettings::instance()->initializeEditor(xmlEditor); data.formWindowEditor = xmlEditor->designerEditor(); connect(data.widgetHost, SIGNAL(formWindowSizeChanged(int,int)), diff --git a/src/plugins/designer/formwindoweditor.cpp b/src/plugins/designer/formwindoweditor.cpp index 834c7a2a801..60c83dd585b 100644 --- a/src/plugins/designer/formwindoweditor.cpp +++ b/src/plugins/designer/formwindoweditor.cpp @@ -31,7 +31,7 @@ #include "formwindowfile.h" #include "designerconstants.h" #include "resourcehandler.h" -#include "designerxmleditor.h" +#include "designerxmleditorwidget.h" #include #include @@ -53,27 +53,20 @@ namespace Designer { struct FormWindowEditorPrivate { - FormWindowEditorPrivate(Internal::DesignerXmlEditor *editor, - QDesignerFormWindowInterface *form) - : m_textEditor(editor), m_file(form) - {} - - TextEditor::PlainTextEditor m_textEditor; - Internal::FormWindowFile m_file; + Internal::DesignerXmlEditorWidget *m_widget; }; -FormWindowEditor::FormWindowEditor(Internal::DesignerXmlEditor *editor, - QDesignerFormWindowInterface *form, - QObject *parent) : - Core::IEditor(parent), - d(new FormWindowEditorPrivate(editor, form)) +FormWindowEditor::FormWindowEditor(Internal::DesignerXmlEditorWidget *editor) : + TextEditor::PlainTextEditor(editor), + d(new FormWindowEditorPrivate) { + d->m_widget = editor; setContext(Core::Context(Designer::Constants::K_DESIGNER_XML_EDITOR_ID, Designer::Constants::C_DESIGNER_XML_EDITOR)); - setWidget(d->m_textEditor.widget()); // Revert to saved/load externally modified files. - connect(&d->m_file, SIGNAL(reload(QString*,QString)), this, SLOT(slotOpen(QString*,QString))); + connect(d->m_widget->formWindowFile(), SIGNAL(reloadRequested(QString*,QString)), + this, SLOT(slotOpen(QString*,QString)), Qt::DirectConnection); } FormWindowEditor::~FormWindowEditor() @@ -88,7 +81,7 @@ bool FormWindowEditor::createNew(const QString &contents) syncXmlEditor(QString()); - QDesignerFormWindowInterface *form = d->m_file.formWindow(); + QDesignerFormWindowInterface *form = d->m_widget->formWindowFile()->formWindow(); QTC_ASSERT(form, return false); if (contents.isEmpty()) @@ -117,8 +110,8 @@ bool FormWindowEditor::createNew(const QString &contents) return false; syncXmlEditor(contents); - d->m_file.setFilePath(QString()); - d->m_file.setShouldAutoSave(false); + d->m_widget->formWindowFile()->setFilePath(QString()); + d->m_widget->formWindowFile()->setShouldAutoSave(false); return true; } @@ -132,7 +125,7 @@ bool FormWindowEditor::open(QString *errorString, const QString &fileName, const if (Designer::Constants::Internal::debug) qDebug() << "FormWindowEditor::open" << fileName; - QDesignerFormWindowInterface *form = d->m_file.formWindow(); + QDesignerFormWindowInterface *form = d->m_widget->formWindowFile()->formWindow(); QTC_ASSERT(form, return false); if (fileName.isEmpty()) @@ -142,7 +135,7 @@ bool FormWindowEditor::open(QString *errorString, const QString &fileName, const const QString absfileName = fi.absoluteFilePath(); QString contents; - if (d->m_file.read(absfileName, &contents, errorString) != Utils::TextFileFormat::ReadSuccess) + if (d->m_widget->formWindowFile()->read(absfileName, &contents, errorString) != Utils::TextFileFormat::ReadSuccess) return false; form->setFileName(absfileName); @@ -161,8 +154,8 @@ bool FormWindowEditor::open(QString *errorString, const QString &fileName, const form->setDirty(fileName != realFileName); syncXmlEditor(contents); - d->m_file.setFilePath(absfileName); - d->m_file.setShouldAutoSave(false); + d->m_widget->formWindowFile()->setFilePath(absfileName); + d->m_widget->formWindowFile()->setShouldAutoSave(false); if (Internal::ResourceHandler *rh = form->findChild()) rh->updateResources(); @@ -173,22 +166,13 @@ bool FormWindowEditor::open(QString *errorString, const QString &fileName, const void FormWindowEditor::syncXmlEditor() { if (Designer::Constants::Internal::debug) - qDebug() << "FormWindowEditor::syncXmlEditor" << d->m_file.filePath(); + qDebug() << "FormWindowEditor::syncXmlEditor" << d->m_widget->formWindowFile()->filePath(); syncXmlEditor(contents()); } void FormWindowEditor::syncXmlEditor(const QString &contents) { - d->m_textEditor.editorWidget()->setPlainText(contents); - d->m_textEditor.editorWidget()->setReadOnly(true); - d->m_textEditor.document()->setDisplayName(d->m_file.displayName()); - static_cast - (d->m_textEditor.editorWidget())->configure(document()->mimeType()); -} - -Core::IDocument *FormWindowEditor::document() -{ - return &d->m_file; + editorWidget()->document()->setPlainText(contents); } Core::Id FormWindowEditor::id() const @@ -196,16 +180,6 @@ Core::Id FormWindowEditor::id() const return Core::Id(Designer::Constants::K_DESIGNER_XML_EDITOR_ID); } -QByteArray FormWindowEditor::saveState() const -{ - return d->m_textEditor.saveState(); -} - -bool FormWindowEditor::restoreState(const QByteArray &state) -{ - return d->m_textEditor.restoreState(state); -} - QWidget *FormWindowEditor::toolBar() { return 0; @@ -214,27 +188,18 @@ QWidget *FormWindowEditor::toolBar() QString FormWindowEditor::contents() const { #if QT_VERSION >= 0x050000 // TODO: No warnings about spacers here - const QDesignerFormWindowInterface *fw = d->m_file.formWindow(); + 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(d->m_file.formWindow()); + const qdesigner_internal::FormWindowBase *fw = + qobject_cast(d->m_widget->formWindowFile()->formWindow()); QTC_ASSERT(fw, return QString()); return fw->fileContents(); #endif } -TextEditor::BaseTextDocument *FormWindowEditor::textDocument() -{ - return qobject_cast(d->m_textEditor.document()); -} - -TextEditor::PlainTextEditor *FormWindowEditor::textEditor() -{ - return &d->m_textEditor; -} - bool FormWindowEditor::isDesignModePreferred() const { return true; diff --git a/src/plugins/designer/formwindoweditor.h b/src/plugins/designer/formwindoweditor.h index 3cba63b2c33..8188bcb736f 100644 --- a/src/plugins/designer/formwindoweditor.h +++ b/src/plugins/designer/formwindoweditor.h @@ -32,6 +32,7 @@ #include "designer_export.h" #include +#include QT_BEGIN_NAMESPACE class QDesignerFormWindowInterface; @@ -45,36 +46,30 @@ namespace TextEditor { namespace Designer { namespace Internal { - class DesignerXmlEditor; + class DesignerXmlEditorWidget; } struct FormWindowEditorPrivate; -// The actual Core::IEditor belonging to Qt Designer. Uses FormWindowFile -// as the Core::IDocument to do the isModified() handling, -// which needs to be done by Qt Designer. -// However, to make the read-only XML text editor work, -// a TextEditor::PlainTextEditorEditable (IEditor) is also required. -// It is aggregated and some functions are delegated to it. +// IEditor that is used for the QDesignerFormWindowInterface +// It is a read-only PlainTextEditor that shows the XML of the form. +// DesignerXmlEditorWidget is the corresponding PlainTextEditorWidget, +// FormWindowFile the corresponding BaseTextDocument. +// The content from the QDesignerFormWindowInterface is synced to the +// content of the XML editor. -class DESIGNER_EXPORT FormWindowEditor : public Core::IEditor +class DESIGNER_EXPORT FormWindowEditor : public TextEditor::PlainTextEditor { Q_PROPERTY(QString contents READ contents) Q_OBJECT public: - explicit FormWindowEditor(Internal::DesignerXmlEditor *editor, - QDesignerFormWindowInterface *form, - QObject *parent = 0); + explicit FormWindowEditor(Internal::DesignerXmlEditorWidget *editor); virtual ~FormWindowEditor(); // IEditor virtual bool createNew(const QString &contents = QString()); virtual bool open(QString *errorString, const QString &fileName, const QString &realFileName); - virtual Core::IDocument *document(); virtual Core::Id id() const; - virtual QByteArray saveState() const; - virtual bool restoreState(const QByteArray &state); - virtual QWidget *toolBar(); virtual bool isDesignModePreferred() const; @@ -82,9 +77,6 @@ public: // For uic code model support QString contents() const; - TextEditor::BaseTextDocument *textDocument(); - TextEditor::PlainTextEditor *textEditor(); - public slots: void syncXmlEditor(); diff --git a/src/plugins/designer/formwindowfile.cpp b/src/plugins/designer/formwindowfile.cpp index b12d3f4cd4e..3a91695dfdd 100644 --- a/src/plugins/designer/formwindowfile.cpp +++ b/src/plugins/designer/formwindowfile.cpp @@ -49,12 +49,12 @@ namespace Designer { namespace Internal { FormWindowFile::FormWindowFile(QDesignerFormWindowInterface *form, QObject *parent) - : Core::TextDocument(parent), - m_mimeType(QLatin1String(Designer::Constants::FORM_MIMETYPE)), + : m_mimeType(QLatin1String(Designer::Constants::FORM_MIMETYPE)), m_shouldAutoSave(false), m_formWindow(form), m_isModified(false) { + setParent(parent); // Designer needs UTF-8 regardless of settings. setCodec(QTextCodec::codecForName("UTF-8")); connect(m_formWindow->core()->formWindowManager(), SIGNAL(formWindowRemoved(QDesignerFormWindowInterface*)), @@ -141,7 +141,7 @@ bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType ty emit changed(); } else { emit aboutToReload(); - emit reload(errorString, filePath()); + emit reloadRequested(errorString, filePath()); const bool success = errorString->isEmpty(); emit reloadFinished(success); return success; diff --git a/src/plugins/designer/formwindowfile.h b/src/plugins/designer/formwindowfile.h index 6fdca217b1e..351e1357e14 100644 --- a/src/plugins/designer/formwindowfile.h +++ b/src/plugins/designer/formwindowfile.h @@ -30,7 +30,7 @@ #ifndef FORMWINDOWFILE_H #define FORMWINDOWFILE_H -#include +#include #include @@ -42,12 +42,13 @@ QT_END_NAMESPACE namespace Designer { namespace Internal { -class FormWindowFile : public Core::TextDocument +class FormWindowFile : public TextEditor::BaseTextDocument { Q_OBJECT public: explicit FormWindowFile(QDesignerFormWindowInterface *form, QObject *parent = 0); + ~FormWindowFile() { } // IDocument virtual bool save(QString *errorString, const QString &fileName, bool autoSave); @@ -68,8 +69,7 @@ public: signals: // Internal - void reload(QString *errorString, const QString &); - void setDisplayName(const QString &); + void reloadRequested(QString *errorString, const QString &); public slots: void setFilePath(const QString &);