From b3c2120dff93c7898809be20bf1fcbc43dbe54db Mon Sep 17 00:00:00 2001 From: David Schulz Date: Fri, 16 Apr 2021 09:35:46 +0200 Subject: [PATCH] Editor: add specialized snippet overlay Split out the snippet functionality into a new derived overlay implementation. Change-Id: I2d7fffabe16ce6348ed067eb52ff221420a23285 Reviewed-by: Christian Stenger --- src/plugins/texteditor/texteditor.cpp | 4 +-- src/plugins/texteditor/texteditoroverlay.cpp | 35 +++++++++++--------- src/plugins/texteditor/texteditoroverlay.h | 30 ++++++++++++----- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 0fcfbf02b67..c5016e35a1a 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -665,7 +665,7 @@ public: int extraAreaPreviousMarkTooltipRequestedLine = -1; TextEditorOverlay *m_overlay = nullptr; - TextEditorOverlay *m_snippetOverlay = nullptr; + SnippetOverlay *m_snippetOverlay = nullptr; TextEditorOverlay *m_searchResultOverlay = nullptr; bool snippetCheckCursor(const QTextCursor &cursor); void snippetTabOrBacktab(bool forward); @@ -1004,7 +1004,7 @@ void TextEditorWidgetPrivate::ctor(const QSharedPointer &doc) q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_overlay = new TextEditorOverlay(q); - m_snippetOverlay = new TextEditorOverlay(q); + m_snippetOverlay = new SnippetOverlay(q); m_searchResultOverlay = new TextEditorOverlay(q); m_refactorOverlay = new RefactorOverlay(q); diff --git a/src/plugins/texteditor/texteditoroverlay.cpp b/src/plugins/texteditor/texteditoroverlay.cpp index 863db175c9a..5522a7ef333 100644 --- a/src/plugins/texteditor/texteditoroverlay.cpp +++ b/src/plugins/texteditor/texteditoroverlay.cpp @@ -72,8 +72,6 @@ void TextEditorOverlay::clear() return; m_selections.clear(); m_firstSelectionOriginalBegin = -1; - m_equivalentSelections.clear(); - m_manglers.clear(); update(); } @@ -481,13 +479,27 @@ QTextCursor TextEditorOverlay::assembleCursorForSelection(int selectionIndex) co return cursor; } -void TextEditorOverlay::mapEquivalentSelections() +bool TextEditorOverlay::hasFirstSelectionBeginMoved() const +{ + if (m_firstSelectionOriginalBegin == -1 || m_selections.isEmpty()) + return false; + return m_selections.at(0).m_cursor_begin.position() != m_firstSelectionOriginalBegin; +} + +void SnippetOverlay::clear() +{ + TextEditorOverlay::clear(); + m_equivalentSelections.clear(); + m_manglers.clear(); +} + +void SnippetOverlay::mapEquivalentSelections() { m_equivalentSelections.clear(); - m_equivalentSelections.resize(m_selections.size()); + m_equivalentSelections.resize(selections().size()); QMultiMap all; - for (int i = 0; i < m_selections.size(); ++i) + for (int i = 0; i < selections().size(); ++i) all.insert(selectionText(i).toLower(), i); const QList &uniqueKeys = all.uniqueKeys(); @@ -506,7 +518,7 @@ void TextEditorOverlay::mapEquivalentSelections() } } -void TextEditorOverlay::updateEquivalentSelections(const QTextCursor &cursor) +void SnippetOverlay::updateEquivalentSelections(const QTextCursor &cursor) { int selectionIndex = selectionIndexForCursor(cursor); if (selectionIndex == -1) @@ -528,12 +540,12 @@ void TextEditorOverlay::updateEquivalentSelections(const QTextCursor &cursor) } } -void TextEditorOverlay::setNameMangler(const QList &manglers) +void SnippetOverlay::setNameMangler(const QList &manglers) { m_manglers = manglers; } -void TextEditorOverlay::mangle() +void SnippetOverlay::mangle() { for (int i = 0; i < m_manglers.count(); ++i) { if (!m_manglers.at(i)) @@ -550,10 +562,3 @@ void TextEditorOverlay::mangle() } } } - -bool TextEditorOverlay::hasFirstSelectionBeginMoved() const -{ - if (m_firstSelectionOriginalBegin == -1 || m_selections.isEmpty()) - return false; - return m_selections.at(0).m_cursor_begin.position() != m_firstSelectionOriginalBegin; -} diff --git a/src/plugins/texteditor/texteditoroverlay.h b/src/plugins/texteditor/texteditoroverlay.h index 374bc0ea1e7..a07252c0b22 100644 --- a/src/plugins/texteditor/texteditoroverlay.h +++ b/src/plugins/texteditor/texteditoroverlay.h @@ -74,7 +74,7 @@ public: void setAlpha(bool enabled) { m_alpha = enabled; } - void clear(); + virtual void clear(); enum OverlaySelectionFlags { LockSize = 1, @@ -95,20 +95,17 @@ public: bool hasCursorInSelection(const QTextCursor &cursor) const; - void mapEquivalentSelections(); - void updateEquivalentSelections(const QTextCursor &cursor); - void setNameMangler(const QList &manglers); - void mangle(); - bool hasFirstSelectionBeginMoved() const; +protected: + int selectionIndexForCursor(const QTextCursor &cursor) const; + QString selectionText(int selectionIndex) const; + QTextCursor assembleCursorForSelection(int selectionIndex) const; + private: QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end, const QRect& clip); void paintSelection(QPainter *painter, const OverlaySelection &selection); void fillSelection(QPainter *painter, const OverlaySelection &selection, const QColor &color); - int selectionIndexForCursor(const QTextCursor &cursor) const; - QString selectionText(int selectionIndex) const; - QTextCursor assembleCursorForSelection(int selectionIndex) const; bool m_visible; bool m_alpha; @@ -118,6 +115,21 @@ private: TextEditorWidget *m_editor; QWidget *m_viewport; QList m_selections; +}; + +class SnippetOverlay : public TextEditorOverlay +{ +public: + using TextEditorOverlay::TextEditorOverlay; + + void clear() override; + + void mapEquivalentSelections(); + void updateEquivalentSelections(const QTextCursor &cursor); + void setNameMangler(const QList &manglers); + void mangle(); + +private: QVector > m_equivalentSelections; QList m_manglers; };