[vlc-commits] qt: generate genre covers in 4x2 grid with blur and dark effects

Prince Gupta git at videolan.org
Tue Aug 18 17:14:46 CEST 2020


vlc | branch: master | Prince Gupta <guptaprince8832 at gmail.com> | Fri Jul 31 00:45:47 2020 +0530| [d5de61cfa945d3bd733277690b8a72730c5c5d40] | committer: Pierre Lamot

qt: generate genre covers in 4x2 grid with blur and dark effects

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=d5de61cfa945d3bd733277690b8a72730c5c5d40
---

 modules/gui/qt/medialibrary/mlgenre.cpp | 88 +++++++++++++++++++--------------
 1 file changed, 52 insertions(+), 36 deletions(-)

diff --git a/modules/gui/qt/medialibrary/mlgenre.cpp b/modules/gui/qt/medialibrary/mlgenre.cpp
index aee536fd41..34cb9263d5 100644
--- a/modules/gui/qt/medialibrary/mlgenre.cpp
+++ b/modules/gui/qt/medialibrary/mlgenre.cpp
@@ -23,13 +23,34 @@
 #include <QMutex>
 #include <QWaitCondition>
 #include <QDir>
+#include <QGradient>
+#include <QGraphicsScene>
+#include <QGraphicsPixmapItem>
+#include <QGraphicsBlurEffect>
+#include <algorithm>
 #include "mlgenre.hpp"
 #include "qt.hpp"
 
 namespace  {
 
-#define THUMBNAIL_WIDTH 512
-#define THUMBNAIL_HEIGHT 512
+#define THUMBNAIL_WIDTH 260
+#define THUMBNAIL_HEIGHT 130
+
+QImage blurImage(const QImage& src)
+{
+    QGraphicsScene scene;
+    QGraphicsPixmapItem item;
+    item.setPixmap(QPixmap::fromImage(src));
+    QGraphicsBlurEffect blurEffect;
+    blurEffect.setBlurRadius(4);
+    blurEffect.setBlurHints(QGraphicsBlurEffect::QualityHint);
+    item.setGraphicsEffect(&blurEffect);
+    scene.addItem(&item);
+    QImage res(src.size(), QImage::Format_ARGB32);
+    QPainter ptr(&res);
+    scene.render(&ptr);
+    return res;
+}
 
 class GenerateCoverTask : public QRunnable
 {
@@ -97,21 +118,28 @@ public:
         memset(&queryParams, 0, sizeof(vlc_ml_query_params_t));
         album_list.reset( vlc_ml_list_genre_albums(m_ml, &queryParams, genreId) );
 
-        QStringList thumnails;
-        int count = 0;
+        QStringList thumbnails;
+        thumbnails.reserve(8);
         for( const vlc_ml_album_t& media: ml_range_iterate<vlc_ml_album_t>( album_list ) ) {
             if (media.thumbnails[VLC_ML_THUMBNAIL_SMALL].b_generated) {
                 QUrl mediaURL( media.thumbnails[VLC_ML_THUMBNAIL_SMALL].psz_mrl );
                 //QImage only accept local file
                 if (mediaURL.isValid() && mediaURL.isLocalFile()) {
-                    thumnails.append(mediaURL.path());
-                    count++;
-                    if (count >= 4)
+                    thumbnails.append(mediaURL.path());
+                    if (thumbnails.size() == 8)
                         break;
                 }
             }
         }
 
+        if (thumbnails.empty()) {
+            thumbnails.append(":/noart_album.svg");
+        }
+
+        assert(thumbnails.size() <= 8);
+        std::copy(thumbnails.begin(), ( thumbnails.begin() + ( 8 - thumbnails.size() ) ), std::back_inserter(thumbnails));
+        assert(thumbnails.size() == 8);
+
         {
             QMutexLocker lock(&m_taskLock);
             if (m_canceled) {
@@ -122,41 +150,29 @@ public:
         }
 
         QImage image(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, QImage::Format_RGB32);
+        image.fill(Qt::white);
 
         QPainter painter;
         painter.begin(&image);
-        switch (count) {
-            case 0:
-                break;
-
-            case 1:
-                drawRegion(painter, thumnails[0], QRect(0,                 0,                  THUMBNAIL_WIDTH,   THUMBNAIL_HEIGHT   ));
-                break;
-
-            case 2:
-                drawRegion(painter, thumnails[0], QRect(0,                 0,                  THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT   ));
-                drawRegion(painter, thumnails[1], QRect(THUMBNAIL_WIDTH/2, 0,                  THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT   ));
-                break;
-
-            case 3:
-                drawRegion(painter, thumnails[0], QRect(0,                 0,                  THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT   ));
-                drawRegion(painter, thumnails[1], QRect(THUMBNAIL_WIDTH/2, 0,                  THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT/2));
-                drawRegion(painter, thumnails[2], QRect(THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT/2, THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT/2));
-                break;
-
-            case 4:
-                drawRegion(painter, thumnails[0], QRect(0,                 0,                  THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT/2));
-                drawRegion(painter, thumnails[1], QRect(THUMBNAIL_WIDTH/2, 0,                  THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT/2));
-                drawRegion(painter, thumnails[2], QRect(THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT/2, THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT/2));
-                drawRegion(painter, thumnails[3], QRect(0,                 THUMBNAIL_HEIGHT/2, THUMBNAIL_WIDTH/2, THUMBNAIL_HEIGHT/2));
-                break;
+        for (int i = 0; i < 2; i++) {
+            for (int j = 0; j < 4; j++) {
+                drawRegion(painter, thumbnails[2*j+1], QRect( ( THUMBNAIL_WIDTH / 4 ) * j, ( THUMBNAIL_HEIGHT / 2 ) * i, THUMBNAIL_WIDTH / 4, THUMBNAIL_HEIGHT / 2 ));
+            }
         }
         painter.end();
 
-        if (count > 0) {
-            if (image.save(m_filepath, "jpg"))
-                m_genre->setCover(QUrl::fromLocalFile(m_filepath).toString());
-        }
+        image = blurImage(image);
+
+        QLinearGradient gradient;
+        gradient.setColorAt(0, QColor(0, 0, 0, 255*.3));
+        gradient.setColorAt(1, QColor(0, 0, 0, 255*.7));
+        painter.begin(&image);
+        painter.setOpacity(.7);
+        painter.fillRect(image.rect(), gradient);
+        painter.end();
+
+        if (image.save(m_filepath, "jpg"))
+            m_genre->setCover(QUrl::fromLocalFile(m_filepath).toString());
 
         {
             QMutexLocker lock(&m_taskLock);



More information about the vlc-commits mailing list