Editor: limit painter path calculation for overlay selections

Only calculate a painter path if it is inside the paint event rect.

Task-number: QTCREATORBUG-26812
Change-Id: I9e7026b70251347fe8c38fc5b9278de943786956
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2022-03-17 07:09:51 +01:00
parent b822a9462e
commit 2d3d66c115
2 changed files with 12 additions and 10 deletions

View File

@@ -312,7 +312,8 @@ QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, co
} }
void TextEditorOverlay::paintSelection(QPainter *painter, void TextEditorOverlay::paintSelection(QPainter *painter,
const OverlaySelection &selection) const OverlaySelection &selection,
const QRect &clip)
{ {
QTextCursor begin = selection.m_cursor_begin; QTextCursor begin = selection.m_cursor_begin;
@@ -325,7 +326,7 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
if (begin.isNull() || end.isNull() || begin.position() > end.position() || !bg.isValid()) if (begin.isNull() || end.isNull() || begin.position() > end.position() || !bg.isValid())
return; return;
QPainterPath path = createSelectionPath(begin, end, m_editor->viewport()->rect()); QPainterPath path = createSelectionPath(begin, end, clip);
painter->save(); painter->save();
QColor penColor = fg; QColor penColor = fg;
@@ -374,14 +375,15 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
void TextEditorOverlay::fillSelection(QPainter *painter, void TextEditorOverlay::fillSelection(QPainter *painter,
const OverlaySelection &selection, const OverlaySelection &selection,
const QColor &color) const QColor &color,
const QRect &clip)
{ {
const QTextCursor &begin = selection.m_cursor_begin; const QTextCursor &begin = selection.m_cursor_begin;
const QTextCursor &end= selection.m_cursor_end; const QTextCursor &end= selection.m_cursor_end;
if (begin.isNull() || end.isNull() || begin.position() > end.position()) if (begin.isNull() || end.isNull() || begin.position() > end.position())
return; return;
QPainterPath path = createSelectionPath(begin, end, m_editor->viewport()->rect()); QPainterPath path = createSelectionPath(begin, end, clip);
painter->save(); painter->save();
painter->translate(-.5, -.5); painter->translate(-.5, -.5);
@@ -402,7 +404,7 @@ void TextEditorOverlay::paint(QPainter *painter, const QRect &clip)
!= selection.m_fixedLength) != selection.m_fixedLength)
continue; continue;
paintSelection(painter, selection); paintSelection(painter, selection, clip);
} }
for (int i = m_selections.size()-1; i >= 0; --i) { for (int i = m_selections.size()-1; i >= 0; --i) {
const OverlaySelection &selection = m_selections.at(i); const OverlaySelection &selection = m_selections.at(i);
@@ -413,7 +415,7 @@ void TextEditorOverlay::paint(QPainter *painter, const QRect &clip)
!= selection.m_fixedLength) != selection.m_fixedLength)
continue; continue;
paintSelection(painter, selection); paintSelection(painter, selection, clip);
} }
} }
@@ -444,7 +446,7 @@ void TextEditorOverlay::fill(QPainter *painter, const QColor &color, const QRect
!= selection.m_fixedLength) != selection.m_fixedLength)
continue; continue;
fillSelection(painter, selection, color); fillSelection(painter, selection, color, clip);
} }
for (int i = m_selections.size()-1; i >= 0; --i) { for (int i = m_selections.size()-1; i >= 0; --i) {
const OverlaySelection &selection = m_selections.at(i); const OverlaySelection &selection = m_selections.at(i);
@@ -455,7 +457,7 @@ void TextEditorOverlay::fill(QPainter *painter, const QColor &color, const QRect
!= selection.m_fixedLength) != selection.m_fixedLength)
continue; continue;
fillSelection(painter, selection, color); fillSelection(painter, selection, color, clip);
} }
} }

View File

@@ -100,8 +100,8 @@ protected:
private: private:
QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end, const QRect& clip); QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end, const QRect& clip);
void paintSelection(QPainter *painter, const OverlaySelection &selection); void paintSelection(QPainter *painter, const OverlaySelection &selection, const QRect &clip);
void fillSelection(QPainter *painter, const OverlaySelection &selection, const QColor &color); void fillSelection(QPainter *painter, const OverlaySelection &selection, const QColor &color, const QRect &clip);
bool m_visible; bool m_visible;
bool m_alpha; bool m_alpha;