Skip to content

Commit

Permalink
Custom Elide for Suggestion List Text
Browse files Browse the repository at this point in the history
Qt painting also doesn't have elide support.
  • Loading branch information
ShaopengLin committed Oct 25, 2024
1 parent 134be31 commit c3f4a1e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/suggestionlistdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,23 @@ void SuggestionListDelegate::paintText(QPainter *p,

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

/* Custom text elide. */
const QFontMetrics metrics = opt.fontMetrics;
const int elideMarkerLength = metrics.tightBoundingRect("(...)").width();
const 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 */
const bool textDirFlipped = KiwixApp::isRightToLeft() != text.isRightToLeft();
elidedText = textDirFlipped ? "(...)" + elidedText.trimmed()
: elidedText.trimmed() + "(...)";
p->drawText(textRect, flag, elidedText);
}
else
p->drawText(textRect, flag, text);
}

0 comments on commit c3f4a1e

Please sign in to comment.