TextEditor: skip painting overlays with invalid background

Makes it possible to turn of specific overlays by unsetting the color.
Such color resulted in a black overlay until now, which is also kind of
unexpected.

Change-Id: I90732ae496af62b573b2e3b8d8c7fe56632ca8d9
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
David Schulz
2019-01-22 14:01:30 +01:00
parent 8d9ac8d94b
commit bcda567392
2 changed files with 34 additions and 32 deletions

View File

@@ -143,6 +143,14 @@ uint qHash(const TextStyle &textStyle)
return ::qHash(quint8(textStyle)); return ::qHash(quint8(textStyle));
} }
static bool isOverlayCategory(TextStyle category)
{
return category == C_OCCURRENCES
|| category == C_OCCURRENCES_RENAME
|| category == C_SEARCH_RESULT
|| category == C_PARENTHESES_MISMATCH;
}
/** /**
* Returns the QTextCharFormat of the given format category. * Returns the QTextCharFormat of the given format category.
*/ */
@@ -166,18 +174,18 @@ QTextCharFormat FontSettings::toTextCharFormat(TextStyle category) const
"Unused variable")); "Unused variable"));
} }
if (f.foreground().isValid() if (f.foreground().isValid() && !isOverlayCategory(category))
&& category != C_OCCURRENCES
&& category != C_OCCURRENCES_RENAME
&& category != C_SEARCH_RESULT
&& category != C_PARENTHESES_MISMATCH)
tf.setForeground(f.foreground()); tf.setForeground(f.foreground());
if (f.background().isValid() && (category == C_TEXT || f.background() != m_scheme.formatFor(C_TEXT).background())) if (f.background().isValid()) {
tf.setBackground(f.background()); if (category == C_TEXT || f.background() != m_scheme.formatFor(C_TEXT).background())
tf.setBackground(f.background());
// underline does not need to fill without having background color } else if (isOverlayCategory(category)) {
if (f.underlineStyle() != QTextCharFormat::NoUnderline && !f.background().isValid()) // overlays without a background schouldn't get painted
tf.setBackground(QBrush(Qt::BrushStyle::NoBrush)); tf.setBackground(QColor());
} else if (f.underlineStyle() != QTextCharFormat::NoUnderline) {
// underline does not need to fill without having background color
tf.setBackground(Qt::BrushStyle::NoBrush);
}
tf.setFontWeight(f.bold() ? QFont::Bold : QFont::Normal); tf.setFontWeight(f.bold() ? QFont::Bold : QFont::Normal);
tf.setFontItalic(f.italic()); tf.setFontItalic(f.italic());

View File

@@ -323,9 +323,7 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
const QColor &bg = selection.m_bg; const QColor &bg = selection.m_bg;
if (begin.isNull() if (begin.isNull() || end.isNull() || begin.position() > end.position() || !bg.isValid())
|| end.isNull()
|| begin.position() > end.position())
return; return;
QPainterPath path = createSelectionPath(begin, end, m_editor->viewport()->rect()); QPainterPath path = createSelectionPath(begin, end, m_editor->viewport()->rect());
@@ -339,25 +337,21 @@ void TextEditorOverlay::paintSelection(QPainter *painter,
QRectF pathRect = path.controlPointRect(); QRectF pathRect = path.controlPointRect();
if (bg.isValid()) { if (!m_alpha || begin.blockNumber() != end.blockNumber()) {
if (!m_alpha || begin.blockNumber() != end.blockNumber()) { // gradients are too slow for larger selections :(
// gradients are too slow for larger selections :( QColor col = bg;
QColor col = bg; if (m_alpha)
if (m_alpha) col.setAlpha(50);
col.setAlpha(50); painter->setBrush(col);
painter->setBrush(col);
} else {
QLinearGradient linearGrad(pathRect.topLeft(), pathRect.bottomLeft());
QColor col1 = fg.lighter(150);
col1.setAlpha(20);
QColor col2 = fg;
col2.setAlpha(80);
linearGrad.setColorAt(0, col1);
linearGrad.setColorAt(1, col2);
painter->setBrush(QBrush(linearGrad));
}
} else { } else {
painter->setBrush(QBrush()); QLinearGradient linearGrad(pathRect.topLeft(), pathRect.bottomLeft());
QColor col1 = fg.lighter(150);
col1.setAlpha(20);
QColor col2 = fg;
col2.setAlpha(80);
linearGrad.setColorAt(0, col1);
linearGrad.setColorAt(1, col2);
painter->setBrush(QBrush(linearGrad));
} }
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);