1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-24 04:36:34 +01:00

LocationCompleter: Painting underline manually looks better than by Qt

- QFont::Underline is ugly with graphicssystem raster
- also fixed issue when not all parts of text was highlighted as they
  should be
This commit is contained in:
nowrep 2012-09-01 15:05:16 +02:00
parent 9f376ac14c
commit 6d52ef3c77

View File

@ -131,6 +131,9 @@ void LocationCompleterDelegate::drawHighlightedTextLine(const QRect &rect, QStri
} }
} }
// We need to sort delimiters to properly paint all parts that user typed
qSort(delimiters);
// If we don't find any match, just paint it without any highlight // If we don't find any match, just paint it without any highlight
if (delimiters.isEmpty() || delimiters.count() % 2) { if (delimiters.isEmpty() || delimiters.count() % 2) {
drawTextLine(rect, text, painter, style, option, role); drawTextLine(rect, text, painter, style, option, role);
@ -140,7 +143,6 @@ void LocationCompleterDelegate::drawHighlightedTextLine(const QRect &rect, QStri
QFont normalFont = painter->font(); QFont normalFont = painter->font();
QFont boldFont = normalFont; QFont boldFont = normalFont;
boldFont.setBold(true); boldFont.setBold(true);
boldFont.setUnderline(true);
QFontMetrics normalMetrics(normalFont); QFontMetrics normalMetrics(normalFont);
QFontMetrics boldMetrics(boldFont); QFontMetrics boldMetrics(boldFont);
@ -159,22 +161,30 @@ void LocationCompleterDelegate::drawHighlightedTextLine(const QRect &rect, QStri
if (!normalPart.isEmpty()) { if (!normalPart.isEmpty()) {
int width = normalMetrics.width(normalPart); int width = normalMetrics.width(normalPart);
QRect nRect(lastRectPos, rect.top(), width, rect.height()); const QRect &nRect = adjustRect(rect, QRect(lastRectPos, rect.top(), width, rect.height()));
if (nRect.width() > 0) {
painter->setFont(normalFont); painter->setFont(normalFont);
drawTextLine(adjustRect(rect, nRect), normalPart, painter, style, option, role); drawTextLine(nRect, normalPart, painter, style, option, role);
lastRectPos += width; lastRectPos += nRect.width();
}
} }
if (!boldPart.isEmpty()) { if (!boldPart.isEmpty()) {
int width = boldMetrics.width(boldPart); int width = boldMetrics.width(boldPart);
QRect bRect(lastRectPos, rect.top(), width, rect.height()); const QRect &bRect = adjustRect(rect, QRect(lastRectPos, rect.top(), width, rect.height()));
if (bRect.width() > 0) {
painter->setFont(boldFont); painter->setFont(boldFont);
drawTextLine(adjustRect(rect, bRect), boldPart, painter, style, option, role); drawTextLine(bRect, boldPart, painter, style, option, role);
lastRectPos += width; lastRectPos += bRect.width();
// Paint manually line under text instead of using QFont::underline
QRect underlineRect(bRect.left(), bRect.top() + boldMetrics.ascent() + 1, bRect.width(), 2);
painter->fillRect(underlineRect, option.palette.color(role));
}
} }
if (delimiters.isEmpty() && lastEndPos != text.size()) { if (delimiters.isEmpty() && lastEndPos != text.size()) {
@ -212,12 +222,9 @@ void LocationCompleterDelegate::drawTextLine(const QRect &rect, QString text, QP
QRect LocationCompleterDelegate::adjustRect(const QRect &original, const QRect &created) const QRect LocationCompleterDelegate::adjustRect(const QRect &original, const QRect &created) const
{ {
int crLeft = created.left() + created.width(); if (created.left() + created.width() >= original.right()) {
int orLeft = original.left() + original.width();
if (crLeft > orLeft) {
QRect nRect = created; QRect nRect = created;
nRect.setWidth(orLeft - created.left()); nRect.setWidth(original.right() - created.left());
return nRect; return nRect;
} }