Skip to content

Commit

Permalink
Proper Elide for Suggestion List Text
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaopengLin committed Sep 28, 2024
1 parent 64b30c3 commit b19ea81
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/suggestionlistdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ void SuggestionListDelegate::paintText(QPainter *p,

int flag = {Qt::AlignVCenter | Qt::AlignLeading};
QString text = index.data(Qt::DisplayRole).toString();
p->drawText(textRect, flag, text);

QFontMetrics metrics = opt.fontMetrics;
int elideMarkerLength = metrics.tightBoundingRect("(...)").width();
int textLength = textRect.width() - elideMarkerLength;
QString elidedText = metrics.elidedText(text, Qt::ElideRight, textLength);
if (elidedText != text)
{
/* Remove built-in elide marker */
elidedText.chop(1);

/* drawText's Align direction determines text direction */
bool textDirectionFlipped = KiwixApp::isRightToLeft() != text.isRightToLeft();
elidedText = textDirectionFlipped ? "(...)" + elidedText.trimmed()
: elidedText.trimmed() + "(...)";
p->drawText(textRect, flag, elidedText);
}
else
p->drawText(textRect, flag, text);
return;
}

0 comments on commit b19ea81

Please sign in to comment.