Commit 1dd05a18 authored by hjk's avatar hjk
Browse files

TextEditor: Prepare new editor construction scheme



IEditorFactory knows how to construct an *Editor, the
Editor knows how to construct the Widget.

Change-Id: I2894e6ae622cb43ef6974aac82505c1f9912ff7b
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
parent 271c1bb8
...@@ -191,8 +191,9 @@ public: ...@@ -191,8 +191,9 @@ public:
BaseTextEditorPrivate() BaseTextEditorPrivate()
{} {}
// Note: This is always a copy of IContext::m_widget. BaseTextEditor::EditorCreator m_editorCreator;
BaseTextEditorWidget *m_editorWidget; BaseTextEditor::DocumentCreator m_documentCreator;
BaseTextEditor::WidgetCreator m_widgetCreator;
CommentDefinition m_commentDefinition; CommentDefinition m_commentDefinition;
std::function<CompletionAssistProvider *()> m_completionAssistProvider; std::function<CompletionAssistProvider *()> m_completionAssistProvider;
...@@ -878,7 +879,7 @@ BaseTextEditor *BaseTextEditorWidget::editor() const ...@@ -878,7 +879,7 @@ BaseTextEditor *BaseTextEditorWidget::editor() const
if (!d->m_editor) { if (!d->m_editor) {
auto that = const_cast<BaseTextEditorWidget *>(this); auto that = const_cast<BaseTextEditorWidget *>(this);
d->m_editor = that->createEditor(); d->m_editor = that->createEditor();
if (!d->m_editor->widget()) if (!d->m_editor->m_widget)
d->m_editor->setEditorWidget(that); d->m_editor->setEditorWidget(that);
d->m_codeAssistant->configure(d->m_editor); d->m_codeAssistant->configure(d->m_editor);
} }
...@@ -6556,33 +6557,55 @@ BaseTextEditor::BaseTextEditor() ...@@ -6556,33 +6557,55 @@ BaseTextEditor::BaseTextEditor()
: d(new BaseTextEditorPrivate) : d(new BaseTextEditorPrivate)
{ {
d->m_completionAssistProvider = [] () -> CompletionAssistProvider * { return 0; }; d->m_completionAssistProvider = [] () -> CompletionAssistProvider * { return 0; };
addContext(TextEditor::Constants::C_TEXTEDITOR);
} }
void BaseTextEditor::setEditorWidget(BaseTextEditorWidget *widget) void BaseTextEditor::setEditorWidget(BaseTextEditorWidget *widget)
{ {
widget->d->m_editor = this;
setWidget(widget); setWidget(widget);
d->m_editorWidget = widget;
} }
BaseTextEditor::~BaseTextEditor() BaseTextEditor::~BaseTextEditor()
{ {
delete d->m_editorWidget; delete m_widget;
delete d; delete d;
} }
void BaseTextEditor::setEditorCreator(const BaseTextEditor::EditorCreator &creator)
{
d->m_editorCreator = creator;
}
void BaseTextEditor::setDocumentCreator(const BaseTextEditor::DocumentCreator &creator)
{
d->m_documentCreator = creator;
}
void BaseTextEditor::setWidgetCreator(const BaseTextEditor::WidgetCreator &creator)
{
d->m_widgetCreator = creator;
}
BaseTextDocument *BaseTextEditor::textDocument() BaseTextDocument *BaseTextEditor::textDocument()
{ {
return d->m_editorWidget->textDocument(); ensureDocument();
return editorWidget()->textDocument();
}
void BaseTextEditor::addContext(Id id)
{
m_context.add(id);
} }
IDocument *BaseTextEditor::document() IDocument *BaseTextEditor::document()
{ {
return d->m_editorWidget->textDocument(); return textDocument();
} }
QWidget *BaseTextEditor::toolBar() QWidget *BaseTextEditor::toolBar()
{ {
return d->m_editorWidget->d->m_toolBar; return editorWidget()->d->m_toolBar;
} }
void BaseTextEditorWidget::insertExtraToolBarWidget(BaseTextEditorWidget::Side side, void BaseTextEditorWidget::insertExtraToolBarWidget(BaseTextEditorWidget::Side side,
...@@ -6602,88 +6625,88 @@ void BaseTextEditorWidget::insertExtraToolBarWidget(BaseTextEditorWidget::Side s ...@@ -6602,88 +6625,88 @@ void BaseTextEditorWidget::insertExtraToolBarWidget(BaseTextEditorWidget::Side s
int BaseTextEditor::currentLine() const int BaseTextEditor::currentLine() const
{ {
return d->m_editorWidget->textCursor().blockNumber() + 1; return editorWidget()->textCursor().blockNumber() + 1;
} }
int BaseTextEditor::currentColumn() const int BaseTextEditor::currentColumn() const
{ {
QTextCursor cursor = d->m_editorWidget->textCursor(); QTextCursor cursor = editorWidget()->textCursor();
return cursor.position() - cursor.block().position() + 1; return cursor.position() - cursor.block().position() + 1;
} }
void BaseTextEditor::gotoLine(int line, int column, bool centerLine) void BaseTextEditor::gotoLine(int line, int column, bool centerLine)
{ {
d->m_editorWidget->gotoLine(line, column, centerLine); editorWidget()->gotoLine(line, column, centerLine);
} }
int BaseTextEditor::columnCount() const int BaseTextEditor::columnCount() const
{ {
return d->m_editorWidget->columnCount(); return editorWidget()->columnCount();
} }
int BaseTextEditor::rowCount() const int BaseTextEditor::rowCount() const
{ {
return d->m_editorWidget->rowCount(); return editorWidget()->rowCount();
} }
int BaseTextEditor::position(BaseTextEditor::PositionOperation posOp, int at) const int BaseTextEditor::position(BaseTextEditor::PositionOperation posOp, int at) const
{ {
return d->m_editorWidget->position(posOp, at); return editorWidget()->position(posOp, at);
} }
void BaseTextEditor::convertPosition(int pos, int *line, int *column) const void BaseTextEditor::convertPosition(int pos, int *line, int *column) const
{ {
d->m_editorWidget->convertPosition(pos, line, column); editorWidget()->convertPosition(pos, line, column);
} }
QRect BaseTextEditor::cursorRect(int pos) const QRect BaseTextEditor::cursorRect(int pos) const
{ {
QTextCursor tc = d->m_editorWidget->textCursor(); QTextCursor tc = editorWidget()->textCursor();
if (pos >= 0) if (pos >= 0)
tc.setPosition(pos); tc.setPosition(pos);
QRect result = d->m_editorWidget->cursorRect(tc); QRect result = editorWidget()->cursorRect(tc);
result.moveTo(d->m_editorWidget->viewport()->mapToGlobal(result.topLeft())); result.moveTo(editorWidget()->viewport()->mapToGlobal(result.topLeft()));
return result; return result;
} }
QString BaseTextEditor::selectedText() const QString BaseTextEditor::selectedText() const
{ {
return d->m_editorWidget->selectedText(); return editorWidget()->selectedText();
} }
void BaseTextEditor::remove(int length) void BaseTextEditor::remove(int length)
{ {
QTextCursor tc = d->m_editorWidget->textCursor(); QTextCursor tc = editorWidget()->textCursor();
tc.setPosition(tc.position() + length, QTextCursor::KeepAnchor); tc.setPosition(tc.position() + length, QTextCursor::KeepAnchor);
tc.removeSelectedText(); tc.removeSelectedText();
} }
void BaseTextEditor::insert(const QString &string) void BaseTextEditor::insert(const QString &string)
{ {
d->m_editorWidget->insertPlainText(string); editorWidget()->insertPlainText(string);
} }
void BaseTextEditor::replace(int length, const QString &string) void BaseTextEditor::replace(int length, const QString &string)
{ {
QTextCursor tc = d->m_editorWidget->textCursor(); QTextCursor tc = editorWidget()->textCursor();
tc.setPosition(tc.position() + length, QTextCursor::KeepAnchor); tc.setPosition(tc.position() + length, QTextCursor::KeepAnchor);
tc.insertText(string); tc.insertText(string);
} }
void BaseTextEditor::setCursorPosition(int pos) void BaseTextEditor::setCursorPosition(int pos)
{ {
d->m_editorWidget->setBlockSelection(false); editorWidget()->setBlockSelection(false);
QTextCursor tc = d->m_editorWidget->textCursor(); QTextCursor tc = editorWidget()->textCursor();
tc.setPosition(pos); tc.setPosition(pos);
d->m_editorWidget->setTextCursor(tc); editorWidget()->setTextCursor(tc);
} }
void BaseTextEditor::select(int toPos) void BaseTextEditor::select(int toPos)
{ {
d->m_editorWidget->setBlockSelection(false); editorWidget()->setBlockSelection(false);
QTextCursor tc = d->m_editorWidget->textCursor(); QTextCursor tc = editorWidget()->textCursor();
tc.setPosition(toPos, QTextCursor::KeepAnchor); tc.setPosition(toPos, QTextCursor::KeepAnchor);
d->m_editorWidget->setTextCursor(tc); editorWidget()->setTextCursor(tc);
} }
CommentDefinition &BaseTextEditor::commentDefinition() const CommentDefinition &BaseTextEditor::commentDefinition() const
...@@ -6732,7 +6755,7 @@ QString BaseTextEditor::contextHelpId() const ...@@ -6732,7 +6755,7 @@ QString BaseTextEditor::contextHelpId() const
{ {
if (m_contextHelpId.isEmpty()) if (m_contextHelpId.isEmpty())
emit const_cast<BaseTextEditor*>(this)->contextHelpIdRequested(const_cast<BaseTextEditor*>(this), emit const_cast<BaseTextEditor*>(this)->contextHelpIdRequested(const_cast<BaseTextEditor*>(this),
d->m_editorWidget->textCursor().position()); editorWidget()->textCursor().position());
return m_contextHelpId; return m_contextHelpId;
} }
...@@ -7031,17 +7054,17 @@ QString BaseTextEditorWidget::foldReplacementText(const QTextBlock &) const ...@@ -7031,17 +7054,17 @@ QString BaseTextEditorWidget::foldReplacementText(const QTextBlock &) const
bool BaseTextEditor::open(QString *errorString, const QString &fileName, const QString &realFileName) bool BaseTextEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
{ {
return d->m_editorWidget->open(errorString, fileName, realFileName); return editorWidget()->open(errorString, fileName, realFileName);
} }
QByteArray BaseTextEditor::saveState() const QByteArray BaseTextEditor::saveState() const
{ {
return d->m_editorWidget->saveState(); return editorWidget()->saveState();
} }
bool BaseTextEditor::restoreState(const QByteArray &state) bool BaseTextEditor::restoreState(const QByteArray &state)
{ {
return d->m_editorWidget->restoreState(state); return editorWidget()->restoreState(state);
} }
BaseTextEditor *BaseTextEditor::currentTextEditor() BaseTextEditor *BaseTextEditor::currentTextEditor()
...@@ -7051,7 +7074,8 @@ BaseTextEditor *BaseTextEditor::currentTextEditor() ...@@ -7051,7 +7074,8 @@ BaseTextEditor *BaseTextEditor::currentTextEditor()
BaseTextEditorWidget *BaseTextEditor::editorWidget() const BaseTextEditorWidget *BaseTextEditor::editorWidget() const
{ {
return d->m_editorWidget; QTC_ASSERT(qobject_cast<BaseTextEditorWidget *>(m_widget.data()), return 0);
return static_cast<BaseTextEditorWidget *>(m_widget.data());
} }
void BaseTextEditorWidget::configureMimeType(const QString &mimeType) void BaseTextEditorWidget::configureMimeType(const QString &mimeType)
...@@ -7139,14 +7163,52 @@ void BaseTextEditorWidget::setupAsPlainEditor() ...@@ -7139,14 +7163,52 @@ void BaseTextEditorWidget::setupAsPlainEditor()
IEditor *BaseTextEditor::duplicate() IEditor *BaseTextEditor::duplicate()
{ {
// Use standard setup if that's available.
if (d->m_editorCreator) {
BaseTextEditor *editor = d->m_editorCreator();
BaseTextEditorWidget *widget = editor->ensureWidget();
widget->setTextDocument(editorWidget()->textDocumentPtr());
m_widget = widget;
return editor;
}
// That's a really plain text editor.
auto newWidget = new BaseTextEditorWidget; auto newWidget = new BaseTextEditorWidget;
newWidget->setTextDocument(editorWidget()->textDocumentPtr()); newWidget->setTextDocument(editorWidget()->textDocumentPtr());
newWidget->setupAsPlainEditor(); newWidget->setupAsPlainEditor();
auto editor = newWidget->editor(); auto editor = newWidget->editor();
editor->setContext(Core::Context(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID, editor->addContext(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID);
TextEditor::Constants::C_TEXTEDITOR));
editor->setDuplicateSupported(true); editor->setDuplicateSupported(true);
return editor; return editor;
// If neither is sufficient, you need to implement 'YourEditor::duplicate'.
}
QWidget *BaseTextEditor::widget() const
{
return ensureWidget();
}
BaseTextEditorWidget *BaseTextEditor::ensureWidget() const
{
if (m_widget.isNull()) {
QTC_ASSERT(d->m_widgetCreator, return 0);
BaseTextEditorWidget *widget = d->m_widgetCreator();
auto that = const_cast<BaseTextEditor *>(this);
widget->d->m_editor = that;
that->m_widget = widget;
}
return editorWidget();
}
BaseTextDocumentPtr BaseTextEditor::ensureDocument()
{
BaseTextEditorWidget *widget = ensureWidget();
if (widget->d->m_document.isNull()) {
QTC_ASSERT(d->m_documentCreator, return BaseTextDocumentPtr());
widget->setTextDocument(BaseTextDocumentPtr(d->m_documentCreator()));
}
return widget->textDocumentPtr();
} }
} // namespace TextEditor } // namespace TextEditor
......
...@@ -120,6 +120,15 @@ public: ...@@ -120,6 +120,15 @@ public:
BaseTextEditor(); BaseTextEditor();
~BaseTextEditor(); ~BaseTextEditor();
typedef std::function<BaseTextEditor *()> EditorCreator;
void setEditorCreator(const EditorCreator &creator);
typedef std::function<BaseTextDocument *()> DocumentCreator;
void setDocumentCreator(const DocumentCreator &creator);
typedef std::function<BaseTextEditorWidget *()> WidgetCreator;
void setWidgetCreator(const WidgetCreator &creator);
void setEditorWidget(BaseTextEditorWidget *editorWidget); void setEditorWidget(BaseTextEditorWidget *editorWidget);
enum MarkRequestKind { enum MarkRequestKind {
...@@ -130,14 +139,20 @@ public: ...@@ -130,14 +139,20 @@ public:
static BaseTextEditor *currentTextEditor(); static BaseTextEditor *currentTextEditor();
BaseTextEditorWidget *ensureWidget() const;
BaseTextDocumentPtr ensureDocument();
BaseTextEditorWidget *editorWidget() const; BaseTextEditorWidget *editorWidget() const;
BaseTextDocument *textDocument(); BaseTextDocument *textDocument();
void addContext(Core::Id id);
// IEditor // IEditor
Core::IDocument *document(); Core::IDocument *document();
bool open(QString *errorString, const QString &fileName, const QString &realFileName); bool open(QString *errorString, const QString &fileName, const QString &realFileName);
IEditor *duplicate(); IEditor *duplicate();
QWidget *widget() const;
QByteArray saveState() const; QByteArray saveState() const;
bool restoreState(const QByteArray &state); bool restoreState(const QByteArray &state);
......
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