diff options
author | hjk <hjk121@nokiamail.com> | 2014-08-22 12:14:06 +0200 |
---|---|---|
committer | hjk <hjk121@nokiamail.com> | 2014-08-22 15:17:23 +0200 |
commit | 5d41421533bbccad172f106fa235147a31cf3aad (patch) | |
tree | e19233b09d1a8cd6d7e1a88ac49a4b63247158ad /src/plugins/texteditor/basetexteditor.cpp | |
parent | bb8d0d2181fd7cd2f0850e558c4ca897f65acaff (diff) |
TextEditor: Introduce a BaseTextEditorFactory
The BaseTextEditorFactory will keep a collection of all
relevant creation functions (document, editor, widget,
indenter, syntax highlighter) and will use them to
create or duplicate base text editors in a uniform way.
Change-Id: I255b519e35cf1c28d5e198242f99316951a0235e
Reviewed-by: Christian Stenger <christian.stenger@digia.com>
Diffstat (limited to 'src/plugins/texteditor/basetexteditor.cpp')
-rw-r--r-- | src/plugins/texteditor/basetexteditor.cpp | 111 |
1 files changed, 100 insertions, 11 deletions
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 2015b131d2..8f8473ece9 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -49,6 +49,7 @@ #include "circularclipboardassist.h" #include "highlighterutils.h" #include "basetexteditor.h" + #include <texteditor/codeassist/codeassistant.h> #include <texteditor/codeassist/defaultassistinterface.h> #include <texteditor/generichighlighter/context.h> @@ -227,15 +228,16 @@ private: class BaseTextEditorPrivate { public: - BaseTextEditorPrivate() - {} + BaseTextEditorPrivate() {} - BaseTextEditor::EditorCreator m_editorCreator; - BaseTextEditor::DocumentCreator m_documentCreator; - BaseTextEditor::WidgetCreator m_widgetCreator; + BaseTextDocumentCreator m_documentCreator; + BaseTextEditorWidgetCreator m_editorWidgetCreator; + BaseTextEditorCreator m_editorCreator; CommentDefinition m_commentDefinition; std::function<CompletionAssistProvider *()> m_completionAssistProvider; + + QPointer<BaseTextEditorFactory> m_origin; }; class BaseTextEditorWidgetPrivate : public QObject @@ -6572,19 +6574,19 @@ BaseTextEditor::~BaseTextEditor() delete d; } -void BaseTextEditor::setEditorCreator(const BaseTextEditor::EditorCreator &creator) +void BaseTextEditor::setEditorCreator(const BaseTextEditorCreator &creator) { d->m_editorCreator = creator; } -void BaseTextEditor::setDocumentCreator(const BaseTextEditor::DocumentCreator &creator) +void BaseTextEditor::setDocumentCreator(const BaseTextDocumentCreator &creator) { d->m_documentCreator = creator; } -void BaseTextEditor::setWidgetCreator(const BaseTextEditor::WidgetCreator &creator) +void BaseTextEditor::setWidgetCreator(const BaseTextEditorWidgetCreator &creator) { - d->m_widgetCreator = creator; + d->m_editorWidgetCreator = creator; } BaseTextDocument *BaseTextEditor::textDocument() @@ -7163,6 +7165,10 @@ void BaseTextEditorWidget::setupAsPlainEditor() IEditor *BaseTextEditor::duplicate() { + // Use new standard setup if that's available. + if (d->m_origin) + return d->m_origin->duplicateTextEditor(this); + // Use standard setup if that's available. if (d->m_editorCreator) { BaseTextEditor *editor = d->m_editorCreator(); @@ -7192,8 +7198,9 @@ QWidget *BaseTextEditor::widget() const BaseTextEditorWidget *BaseTextEditor::ensureWidget() const { if (m_widget.isNull()) { - QTC_ASSERT(d->m_widgetCreator, return 0); - BaseTextEditorWidget *widget = d->m_widgetCreator(); + QTC_ASSERT(!d->m_origin, return 0); // New style always sets it. + QTC_ASSERT(d->m_editorWidgetCreator, return 0); + BaseTextEditorWidget *widget = d->m_editorWidgetCreator(); auto that = const_cast<BaseTextEditor *>(this); widget->d->m_editor = that; that->m_widget = widget; @@ -7206,12 +7213,94 @@ BaseTextDocumentPtr BaseTextEditor::ensureDocument() { BaseTextEditorWidget *widget = ensureWidget(); if (widget->d->m_document.isNull()) { + QTC_ASSERT(!d->m_origin, return BaseTextDocumentPtr()); // New style always sets it. QTC_ASSERT(d->m_documentCreator, return BaseTextDocumentPtr()); widget->setTextDocument(BaseTextDocumentPtr(d->m_documentCreator())); } return widget->textDocumentPtr(); } + +// +// BaseTextEditorFactory +// + +BaseTextEditorFactory::BaseTextEditorFactory(QObject *parent) + : IEditorFactory(parent) +{ +} + +void BaseTextEditorFactory::setDocumentCreator(const BaseTextDocumentCreator &creator) +{ + m_documentCreator = creator; +} + +void BaseTextEditorFactory::setEditorWidgetCreator(const BaseTextEditorWidgetCreator &creator) +{ + m_widgetCreator = creator; +} + +void BaseTextEditorFactory::setEditorCreator(const BaseTextEditorCreator &creator) +{ + m_editorCreator = creator; +} + +void BaseTextEditorFactory::setIndenterCreator(const IndenterCreator &creator) +{ + m_indenterCreator = creator; +} + +void BaseTextEditorFactory::setSyntaxHighlighterCreator(const SyntaxHighLighterCreator &creator) +{ + m_syntaxHighlighterCreator = creator; +} + +void BaseTextEditorFactory::setEditorActionHandlers(Id contextId, uint optionalActions) +{ + new TextEditorActionHandler(this, contextId, optionalActions); +} + +void BaseTextEditorFactory::setEditorActionHandlers(uint optionalActions) +{ + new TextEditorActionHandler(this, id(), optionalActions); +} + +BaseTextEditor *BaseTextEditorFactory::duplicateTextEditor(BaseTextEditor *other) +{ + return createEditorHelper(other->editorWidget()->textDocumentPtr()); +} + +IEditor *BaseTextEditorFactory::createEditor() +{ + BaseTextDocumentPtr doc(m_documentCreator()); + + if (m_indenterCreator) + doc->setIndenter(m_indenterCreator()); + + if (m_syntaxHighlighterCreator) { + SyntaxHighlighter *highlighter = m_syntaxHighlighterCreator(); + highlighter->setParent(doc.data()); + doc->setSyntaxHighlighter(highlighter); + } + + return createEditorHelper(doc); +} + +BaseTextEditor *BaseTextEditorFactory::createEditorHelper(const BaseTextDocumentPtr &document) +{ + BaseTextEditorWidget *widget = m_widgetCreator(); + BaseTextEditor *editor = m_editorCreator(); + editor->d->m_origin = this; + + widget->d->m_editor = editor; + editor->m_widget = widget; + widget->setTextDocument(document); + + widget->d->m_codeAssistant.configure(editor); + + return editor; +} + } // namespace TextEditor #include "basetexteditor.moc" |