]> git.sur5r.net Git - minitube/blobdiff - src/playlistitemdelegate.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / playlistitemdelegate.cpp
index c7795d5b572d77e600c5c4507371f1ebd20a1e59..33d431e9bb9e594ffcd978e38a019012205c682e 100644 (file)
@@ -25,6 +25,7 @@ $END_LICENSE */
 #include "iconutils.h"
 #include "playlistmodel.h"
 #include "playlistview.h"
+#include "variantpromise.h"
 #include "video.h"
 #include "videodefinition.h"
 
@@ -40,10 +41,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 +66,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 +100,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,31 +127,41 @@ 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();
+    Video *video = index.data(VideoRole).value<VideoPointer>().data();
 
     // 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());
+    qreal pixelRatio = painter->device()->devicePixelRatioF();
+    QByteArray thumbKey = ("t" + QString::number(pixelRatio)).toUtf8();
+    const QPixmap &thumb = video->property(thumbKey).value<QPixmap>();
+    if (!thumb.isNull()) {
+        painter->drawPixmap(0, 0, thumb);
+        if (video->getDuration() > 0) drawTime(painter, video->getFormattedDuration(), line);
+    } else
+        video->loadThumb({thumbWidth, thumbHeight}, pixelRatio)
+                .then([pixelRatio, thumbKey, video](auto variant) {
+                    QPixmap pixmap;
+                    pixmap.loadFromData(variant.toByteArray());
+                    pixmap.setDevicePixelRatio(pixelRatio);
+                    const int thumbWidth = PlaylistItemDelegate::thumbWidth * pixelRatio;
+                    if (pixmap.width() > thumbWidth)
+                        pixmap = pixmap.scaledToWidth(thumbWidth, Qt::SmoothTransformation);
+                    video->setProperty(thumbKey, pixmap);
+                    video->changed();
+                })
+                .onFailed([](auto msg) { qDebug() << msg; });
 
     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 +174,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 +218,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 +229,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);
             }
@@ -300,6 +310,7 @@ void PlaylistItemDelegate::drawTime(QPainter *painter,
 
     painter->save();
     painter->setPen(Qt::white);
+    painter->setFont(smallerFont);
     painter->drawText(textBox, Qt::AlignCenter, time);
     painter->restore();
 }