From 8246c5c2c5d35deaf7265a225f98ec769638a42a Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Tue, 22 Oct 2024 17:55:24 -0400 Subject: [PATCH] Custom Elide for Suggestion List Text Qt painting also doesn't have elide support. --- src/suggestionlistdelegate.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/suggestionlistdelegate.cpp b/src/suggestionlistdelegate.cpp index c77a8639..954f7782 100644 --- a/src/suggestionlistdelegate.cpp +++ b/src/suggestionlistdelegate.cpp @@ -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); }