From 18b9bdd41302a24f5122748c4036bfd71e04c4e9 Mon Sep 17 00:00:00 2001 From: mae Date: Tue, 1 Dec 2009 19:44:31 +0100 Subject: [PATCH] some tuning of the overlay search result selection --- src/plugins/texteditor/basetexteditor.cpp | 23 ++++++++--- src/plugins/texteditor/basetexteditor.h | 1 + src/plugins/texteditor/texteditoroverlay.cpp | 42 ++++++++++++-------- src/plugins/texteditor/texteditoroverlay.h | 11 +++-- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 438ee50e3c4..f2080a75b09 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -1871,6 +1871,9 @@ void BaseTextEditorPrivate::highlightSearchResults(const QTextBlock &block, if (m_searchExpr.isEmpty()) return; + int blockPosition = block.position(); + + QTextCursor cursor = q->textCursor(); QString text = block.text(); text.replace(QChar::Nbsp, QLatin1Char(' ')); int idx = -1; @@ -1885,8 +1888,8 @@ void BaseTextEditorPrivate::highlightSearchResults(const QTextBlock &block, continue; if (m_findScope.isNull() - || (block.position() + idx >= m_findScope.selectionStart() - && block.position() + idx + l <= m_findScope.selectionEnd())) { + || (blockPosition + idx >= m_findScope.selectionStart() + && blockPosition + idx + l <= m_findScope.selectionEnd())) { if (selections) { QTextLayout::FormatRange selection; selection.start = idx; @@ -1895,10 +1898,12 @@ void BaseTextEditorPrivate::highlightSearchResults(const QTextBlock &block, selections->append(selection); } - overlay->addOverlaySelection(block.position() + idx, - block.position() + idx + l, + overlay->addOverlaySelection(blockPosition + idx, + blockPosition + idx + l, m_searchResultFormat.background().color().darker(120), - QColor()); + QColor(), false, + (idx == cursor.selectionStart() - blockPosition + && idx + l == cursor.selectionEnd() - blockPosition)); } } @@ -2935,7 +2940,7 @@ void BaseTextEditor::slotCursorPositionChanged() saveCurrentCursorPositionForNavigation(); } - if (d->m_parenthesesMatchingEnabled) { + if (d->m_parenthesesMatchingEnabled && hasFocus()) { // Delay update when no matching is displayed yet, to avoid flicker if (extraSelections(ParenthesesMatchingSelection).isEmpty() && d->m_animator == 0) { @@ -4268,6 +4273,12 @@ void BaseTextEditor::changeEvent(QEvent *e) } } +void BaseTextEditor::focusInEvent(QFocusEvent *e) +{ + QPlainTextEdit::focusInEvent(e); + slotCursorPositionChanged(); +} + void BaseTextEditor::focusOutEvent(QFocusEvent *e) { QPlainTextEdit::focusOutEvent(e); diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index 6f5453f1f4c..cf12b3b7cc1 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -418,6 +418,7 @@ protected: void keyPressEvent(QKeyEvent *e); void wheelEvent(QWheelEvent *e); void changeEvent(QEvent *e); + void focusInEvent(QFocusEvent *e); void focusOutEvent(QFocusEvent *e); void showEvent(QShowEvent *); diff --git a/src/plugins/texteditor/texteditoroverlay.cpp b/src/plugins/texteditor/texteditoroverlay.cpp index 1e3065ca3e3..f40037c1750 100644 --- a/src/plugins/texteditor/texteditoroverlay.cpp +++ b/src/plugins/texteditor/texteditoroverlay.cpp @@ -37,7 +37,9 @@ void TextEditorOverlay::clear() } void TextEditorOverlay::addOverlaySelection(int begin, int end, - const QColor &fg, const QColor &bg, bool lockSize) + const QColor &fg, const QColor &bg, + bool lockSize, + bool dropShadow) { if (end < begin) return; @@ -57,6 +59,8 @@ void TextEditorOverlay::addOverlaySelection(int begin, int end, if (lockSize) selection.m_fixedLength = (end - begin); + selection.m_dropShadow = dropShadow; + m_selections += selection; update(); } @@ -232,9 +236,16 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co return path; } -void TextEditorOverlay::paintSelection(QPainter *painter, const QTextCursor &begin, const QTextCursor &end, - const QColor &fg, const QColor &bg) +void TextEditorOverlay::paintSelection(QPainter *painter, + const OverlaySelection &selection) { + + const QTextCursor &begin = selection.m_cursor_begin; + const QTextCursor &end= selection.m_cursor_end; + const QColor &fg = selection.m_fg; + const QColor &bg = selection.m_bg; + + if (begin.isNull() || end.isNull() || begin.position() > end.position()) return; @@ -268,9 +279,12 @@ void TextEditorOverlay::paintSelection(QPainter *painter, const QTextCursor &beg painter->restore(); } -void TextEditorOverlay::fillSelection(QPainter *painter, const QTextCursor &begin, const QTextCursor &end, - const QColor &color) +void TextEditorOverlay::fillSelection(QPainter *painter, + const OverlaySelection &selection, + const QColor &color) { + const QTextCursor &begin = selection.m_cursor_begin; + const QTextCursor &end= selection.m_cursor_end; if (begin.isNull() || end.isNull() || begin.position() > end.position()) return; @@ -279,6 +293,11 @@ void TextEditorOverlay::fillSelection(QPainter *painter, const QTextCursor &begi painter->save(); painter->translate(-.5, -.5); painter->setRenderHint(QPainter::Antialiasing); + if (selection.m_dropShadow) { + painter->translate(2, 2); + painter->fillPath(path, QColor(0, 0, 0, 100)); + painter->translate(-2, -2); + } painter->fillPath(path, color); painter->restore(); } @@ -293,12 +312,7 @@ void TextEditorOverlay::paint(QPainter *painter, const QRect &clip) != selection.m_fixedLength) continue; - paintSelection(painter, - selection.m_cursor_begin, - selection.m_cursor_end, - selection.m_fg, - selection.m_bg - ); + paintSelection(painter, selection); } } @@ -312,11 +326,7 @@ void TextEditorOverlay::fill(QPainter *painter, const QColor &color, const QRect != selection.m_fixedLength) continue; - fillSelection(painter, - selection.m_cursor_begin, - selection.m_cursor_end, - color - ); + fillSelection(painter, selection, color); } } diff --git a/src/plugins/texteditor/texteditoroverlay.h b/src/plugins/texteditor/texteditoroverlay.h index 79836776434..452f10bec23 100644 --- a/src/plugins/texteditor/texteditoroverlay.h +++ b/src/plugins/texteditor/texteditoroverlay.h @@ -8,12 +8,13 @@ namespace TextEditor { namespace Internal { struct TEXTEDITOR_EXPORT OverlaySelection { - OverlaySelection():m_fixedLength(-1){} + OverlaySelection():m_fixedLength(-1), m_dropShadow(false){} QTextCursor m_cursor_begin; QTextCursor m_cursor_end; QColor m_fg; QColor m_bg; int m_fixedLength; + bool m_dropShadow; }; class TEXTEDITOR_EXPORT TextEditorOverlay : public QObject @@ -47,16 +48,14 @@ public: void clear(); void addOverlaySelection(const QTextCursor &cursor, const QColor &fg, const QColor &bg, bool lockSize = false); - void addOverlaySelection(int begin, int end, const QColor &fg, const QColor &bg, bool lockSize = false); + void addOverlaySelection(int begin, int end, const QColor &fg, const QColor &bg, bool lockSize = false, bool dropShadow = false); inline bool isEmpty() const { return m_selections.isEmpty(); } private: QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end, const QRect& clip); - void paintSelection(QPainter *painter, const QTextCursor &begin, const QTextCursor &end, - const QColor &fg, const QColor &bg); - void fillSelection(QPainter *painter, const QTextCursor &begin, const QTextCursor &end, - const QColor &color); + void paintSelection(QPainter *painter, const OverlaySelection &selection); + void fillSelection(QPainter *painter, const OverlaySelection &selection, const QColor &color); };