Commit 18b9bdd4 authored by mae's avatar mae
Browse files

some tuning of the overlay search result selection

parent 43f5b466
......@@ -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);
......
......@@ -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 *);
......
......@@ -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);
}
}
......
......@@ -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);
};
......
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