]> git.sur5r.net Git - minitube/blobdiff - src/playlistitemdelegate.cpp
New upstream version 3.1
[minitube] / src / playlistitemdelegate.cpp
index c7795d5b572d77e600c5c4507371f1ebd20a1e59..c111113c886ae32d204deba90ecb1409fec0aa9f 100644 (file)
@@ -40,10 +40,10 @@ bool drawElidedText(QPainter *painter, const QRect &textBox, const int flags, co
     painter->drawText(textBox, 0, elidedText);
     return elidedText.length() < text.length();
 }
-}
+} // namespace
 
 PlaylistItemDelegate::PlaylistItemDelegate(QObject *parent, bool downloadInfo)
-    : QStyledItemDelegate(parent), downloadInfo(downloadInfo), progressBar(0) {
+    : QStyledItemDelegate(parent), downloadInfo(downloadInfo), progressBar(nullptr) {
     listView = qobject_cast<PlaylistView *>(parent);
 
     smallerBoldFont = FontUtils::smallBold();
@@ -65,7 +65,7 @@ PlaylistItemDelegate::~PlaylistItemDelegate() {
 }
 
 void PlaylistItemDelegate::createPlayIcon() {
-    qreal maxRatio = IconUtils::maxSupportedPixelRatio();
+    qreal maxRatio = 2.0;
     playIcon = QPixmap(thumbWidth * maxRatio, thumbHeight * maxRatio);
     playIcon.setDevicePixelRatio(maxRatio);
     playIcon.fill(Qt::transparent);
@@ -99,26 +99,26 @@ void PlaylistItemDelegate::createPlayIcon() {
 
 QSize PlaylistItemDelegate::sizeHint(const QStyleOptionViewItem & /*option*/,
                                      const QModelIndex & /*index*/) const {
-    return QSize(thumbWidth, thumbHeight + 1);
+    return QSize(thumbWidth, thumbHeight);
 }
 
 void PlaylistItemDelegate::paint(QPainter *painter,
                                  const QStyleOptionViewItem &option,
                                  const QModelIndex &index) const {
     int itemType = index.data(ItemTypeRole).toInt();
-    if (itemType == ItemTypeVideo) {
-        QStyleOptionViewItem opt = QStyleOptionViewItem(option);
-        initStyleOption(&opt, index);
-        opt.text.clear();
-        opt.widget->style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
-        paintBody(painter, opt, index);
-    } else
+    if (itemType == ItemTypeVideo)
+        paintBody(painter, option, index);
+    else
         QStyledItemDelegate::paint(painter, option, index);
 }
 
 void PlaylistItemDelegate::paintBody(QPainter *painter,
                                      const QStyleOptionViewItem &option,
                                      const QModelIndex &index) const {
+    const bool isSelected = option.state & QStyle::State_Selected;
+    if (isSelected)
+        QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
+
     painter->save();
     painter->translate(option.rect.topLeft());
 
@@ -126,7 +126,6 @@ void PlaylistItemDelegate::paintBody(QPainter *painter,
     if (downloadInfo) line.setWidth(line.width() / 2);
 
     const bool isActive = index.data(ActiveTrackRole).toBool();
-    const bool isSelected = option.state & QStyle::State_Selected;
 
     // get the video metadata
     const Video *video = index.data(VideoRole).value<VideoPointer>().data();
@@ -134,23 +133,20 @@ void PlaylistItemDelegate::paintBody(QPainter *painter,
     // draw the "current track" highlight underneath the text
     if (isActive && !isSelected) paintActiveOverlay(painter, option, line);
 
-    // separator
-    painter->setPen(option.palette.color(QPalette::Midlight));
-    painter->drawLine(thumbWidth, thumbHeight, option.rect.width(), thumbHeight);
-    if (!video->getThumbnail().isNull()) painter->setPen(Qt::black);
-    painter->drawLine(0, thumbHeight, thumbWidth - 1, thumbHeight);
-
     // thumb
-    painter->drawPixmap(0, 0, video->getThumbnail());
+    const QPixmap &thumb = video->getThumbnail();
+    if (!thumb.isNull()) {
+        painter->drawPixmap(0, 0, thumb);
+        if (video->getDuration() > 0) drawTime(painter, video->getFormattedDuration(), line);
+    }
 
     const bool thumbsOnly = line.width() <= thumbWidth + 60;
     const bool isHovered = index.data(HoveredItemRole).toBool();
 
     // play icon overlayed on the thumb
-    if (isActive && (!isHovered && thumbsOnly)) painter->drawPixmap(0, 0, playIcon);
-
-    // time
-    if (video->getDuration() > 0) drawTime(painter, video->getFormattedDuration(), line);
+    bool needPlayIcon = isActive;
+    if (thumbsOnly) needPlayIcon = needPlayIcon && !isHovered;
+    if (needPlayIcon) painter->drawPixmap(0, 0, playIcon);
 
     if (!thumbsOnly) {
         // text color
@@ -163,7 +159,7 @@ void PlaylistItemDelegate::paintBody(QPainter *painter,
         QStringRef title(&video->getTitle());
         QString elidedTitle = video->getTitle();
         static const int titleFlags = Qt::AlignTop | Qt::TextWordWrap;
-        QRect textBox = line.adjusted(padding + thumbWidth, padding, 0, 0);
+        QRect textBox = line.adjusted(padding + thumbWidth, padding, -padding, 0);
         textBox = painter->boundingRect(textBox, titleFlags, elidedTitle);
         while (textBox.height() > 55 && elidedTitle.length() > 10) {
 #if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
@@ -207,7 +203,7 @@ void PlaylistItemDelegate::paintBody(QPainter *painter,
             textSize = QSize(painter->fontMetrics().size(Qt::TextSingleLine, author));
             textBox = QRect(textPoint, textSize);
             authorRects.insert(index.row(), textBox);
-            if (textBox.right() > line.width()) {
+            if (textBox.right() > line.width() - padding) {
                 textBox.setRight(line.width());
                 elided = drawElidedText(painter, textBox, flags, author);
             } else {
@@ -218,11 +214,10 @@ void PlaylistItemDelegate::paintBody(QPainter *painter,
 
         // view count
         if (video->getViewCount() > 0) {
-            QLocale locale;
-            const QString viewCount = tr("%1 views").arg(locale.toString(video->getViewCount()));
+            const QString &viewCount = video->getFormattedViewCount();
             textPoint.setX(textBox.right() + padding);
             textSize = QSize(fontMetrics.size(Qt::TextSingleLine, viewCount));
-            if (elided || textPoint.x() + textSize.width() > line.width()) {
+            if (elided || textPoint.x() + textSize.width() > line.width() - padding) {
                 textPoint.setX(thumbWidth + padding);
                 textPoint.setY(textPoint.y() + textSize.height() + padding);
             }