From 6d52ef3c77d428ddf6f82661b409bb3e3bd31102 Mon Sep 17 00:00:00 2001 From: nowrep Date: Sat, 1 Sep 2012 15:05:16 +0200 Subject: [PATCH] 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 --- .../completer/locationcompleterdelegate.cpp | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/lib/navigation/completer/locationcompleterdelegate.cpp b/src/lib/navigation/completer/locationcompleterdelegate.cpp index d352131bc..f1289c944 100644 --- a/src/lib/navigation/completer/locationcompleterdelegate.cpp +++ b/src/lib/navigation/completer/locationcompleterdelegate.cpp @@ -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 (delimiters.isEmpty() || delimiters.count() % 2) { drawTextLine(rect, text, painter, style, option, role); @@ -140,7 +143,6 @@ void LocationCompleterDelegate::drawHighlightedTextLine(const QRect &rect, QStri QFont normalFont = painter->font(); QFont boldFont = normalFont; boldFont.setBold(true); - boldFont.setUnderline(true); QFontMetrics normalMetrics(normalFont); QFontMetrics boldMetrics(boldFont); @@ -159,22 +161,30 @@ void LocationCompleterDelegate::drawHighlightedTextLine(const QRect &rect, QStri if (!normalPart.isEmpty()) { 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())); - painter->setFont(normalFont); - drawTextLine(adjustRect(rect, nRect), normalPart, painter, style, option, role); + if (nRect.width() > 0) { + painter->setFont(normalFont); + drawTextLine(nRect, normalPart, painter, style, option, role); - lastRectPos += width; + lastRectPos += nRect.width(); + } } if (!boldPart.isEmpty()) { 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())); - painter->setFont(boldFont); - drawTextLine(adjustRect(rect, bRect), boldPart, painter, style, option, role); + if (bRect.width() > 0) { + painter->setFont(boldFont); + 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()) { @@ -212,12 +222,9 @@ void LocationCompleterDelegate::drawTextLine(const QRect &rect, QString text, QP QRect LocationCompleterDelegate::adjustRect(const QRect &original, const QRect &created) const { - int crLeft = created.left() + created.width(); - int orLeft = original.left() + original.width(); - - if (crLeft > orLeft) { + if (created.left() + created.width() >= original.right()) { QRect nRect = created; - nRect.setWidth(orLeft - created.left()); + nRect.setWidth(original.right() - created.left()); return nRect; }