From f24dd7ce81d896e41aaf29f43aa930c94938edee Mon Sep 17 00:00:00 2001 From: Flavio Tordini Date: Wed, 16 Sep 2015 17:38:42 +0200 Subject: [PATCH] Elided text --- src/playlistitemdelegate.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/playlistitemdelegate.cpp b/src/playlistitemdelegate.cpp index 4a83605..f83d951 100644 --- a/src/playlistitemdelegate.cpp +++ b/src/playlistitemdelegate.cpp @@ -31,6 +31,15 @@ const int PlaylistItemDelegate::THUMB_HEIGHT = 90; const int PlaylistItemDelegate::THUMB_WIDTH = 160; const int PlaylistItemDelegate::PADDING = 10; +namespace { + +void drawElidedText(QPainter *painter, const QRect &textBox, const int flags, const QString &text) { + QString elidedText = QFontMetrics(painter->font()).elidedText(text, Qt::ElideRight, textBox.width(), flags); + painter->drawText(textBox, 0, elidedText); +} + +} + PlaylistItemDelegate::PlaylistItemDelegate(QObject* parent, bool downloadInfo) : QStyledItemDelegate(parent), downloadInfo(downloadInfo), @@ -199,7 +208,8 @@ void PlaylistItemDelegate::paintBody( QPainter* painter, stringSize = QSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, authorString ) ); QRect authorTextBox(textLoc , stringSize); authorRects.insert(index.row(), authorTextBox); - painter->drawText(authorTextBox, Qt::AlignLeft | Qt::AlignTop, authorString); + if (authorTextBox.right() > line.width()) authorTextBox.setRight(line.width()); + drawElidedText(painter, authorTextBox, Qt::AlignLeft | Qt::AlignTop, authorString); painter->restore(); // view count @@ -209,7 +219,8 @@ void PlaylistItemDelegate::paintBody( QPainter* painter, textLoc.setX(textLoc.x() + stringSize.width() + PADDING); stringSize = QSize(QFontMetrics(painter->font()).size( Qt::TextSingleLine, viewCountString ) ); QRect viewCountTextBox(textLoc , stringSize); - painter->drawText(viewCountTextBox, Qt::AlignLeft | Qt::AlignBottom, viewCountString); + if (viewCountTextBox.right() > line.width()) viewCountTextBox.setRight(line.width()); + drawElidedText(painter, viewCountTextBox, Qt::AlignLeft | Qt::AlignBottom, viewCountString); } if (downloadInfo) { -- 2.39.5