]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/components/playlist/icon_view.cpp
Qt: no need to go from bool to int
[vlc] / modules / gui / qt4 / components / playlist / icon_view.cpp
index 48d6db7b79fb21aa1582a65f8cc730e064d9edd7..4e1c7a9b84060900e7f1a4d2e2dd9511db1d5339 100644 (file)
 
 #include "components/playlist/icon_view.hpp"
 #include "components/playlist/playlist_model.hpp"
+#include "input_manager.hpp"
 
+#include <QApplication>
 #include <QPainter>
+#include <QRect>
+#include <QStyleOptionViewItem>
+#include <QFontMetrics>
+#include <QPixmapCache>
+
+#include "assert.h"
+
+#define RECT_SIZE           100
+#define ART_SIZE            64
+#define OFFSET              (RECT_SIZE-64)/2
+#define ITEMS_SPACING       10
+#define ART_RADIUS          5
+
+
+static const QRect drawRect = QRect( 0, 0, RECT_SIZE, RECT_SIZE );
+static const QRect artRect = drawRect.adjusted( OFFSET - 1, 0, - OFFSET, - OFFSET *2 );
 
 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
 {
+    PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
+    assert( currentItem );
+
+    char *meta;
+
+    meta = input_item_GetTitleFbName( currentItem->inputItem() );
+    QString title = qfu( meta );
+    free( meta );
+
+    meta = input_item_GetArtist( currentItem->inputItem() );
+    QString artist = qfu( meta );
+    free( meta );
+
+    QString artUrl = InputManager::decodeArtURL( currentItem->inputItem() );
+
+    // look up through all children and use the first picture found
+    if( artUrl.isEmpty() )
+    {
+        int children = currentItem->childCount();
+        for( int i = 0; i < children; i++ )
+        {
+            PLItem *child = currentItem->child( i );
+            artUrl = InputManager::decodeArtURL( child->inputItem() );
+            if( !artUrl.isEmpty() )
+                break;
+        }
+    }
+
+    /*if( option.state & QStyle::State_Selected )
+         painter->fillRect(option.rect, option.palette.highlight());*/
+    QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option,
+                                          painter );
+
+    // picture where all the rendering happens and which will be cached
+    QPixmap pix;
+
+    QString key = title + artist + artUrl
+                  + QString( index.data( PLModel::IsCurrentRole ).toBool() );
+    if(QPixmapCache::find( key, pix ))
+    {
+        // cool, we found it in the cache
+        painter->drawPixmap( option.rect, pix );
+        return;
+    }
+
+    // load album art
+    QPixmap artPix;
+    if( artUrl.isEmpty() || !artPix.load( artUrl ) )
+        artPix = QPixmap( ":/noart64" );
+    else
+        artPix = artPix.scaled( ART_SIZE, ART_SIZE,
+                Qt::KeepAspectRatioByExpanding );
+
+    pix = QPixmap( RECT_SIZE, RECT_SIZE );
+    pix.fill( Qt::transparent );
+
+    QPainter *pixpainter = new QPainter( &pix );
+
+    pixpainter->setRenderHints(
+            QPainter::Antialiasing | QPainter::SmoothPixmapTransform |
+            QPainter::TextAntialiasing );
+
+    if( index.data( PLModel::IsCurrentRole ).toBool() )
+    {
+       pixpainter->save();
+       pixpainter->setOpacity( 0.2 );
+       pixpainter->setBrush( QBrush( Qt::gray ) );
+       pixpainter->drawRoundedRect( 0, -1, RECT_SIZE, RECT_SIZE+1, ART_RADIUS, ART_RADIUS );
+       pixpainter->restore();
+    }
+
+    // Draw the drop shadow
+    pixpainter->save();
+    pixpainter->setOpacity( 0.7 );
+    pixpainter->setBrush( QBrush( Qt::gray ) );
+    pixpainter->drawRoundedRect( artRect.adjusted( 2, 2, 2, 2 ), ART_RADIUS, ART_RADIUS );
+    pixpainter->restore();
+
+    // Draw the art pix
+    QPainterPath artRectPath;
+    artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
+    pixpainter->setClipPath( artRectPath );
+    pixpainter->drawPixmap( artRect, artPix );
+    pixpainter->setClipping( false );
+
+    QColor text = qApp->palette().text().color();
+
+    // Draw title
+    pixpainter->setPen( text );
+    QFont font;
+    font.setPointSize( 7 );
+    font.setItalic(true);
+    font.setBold( index.data( Qt::FontRole ).value<QFont>().bold() );
+    pixpainter->setFont( font );
+    QFontMetrics fm = pixpainter->fontMetrics();
+    QRect textRect = drawRect.adjusted( 1, ART_SIZE + 4, 0, -1 );
+    textRect.setHeight( fm.height() + 2 );
+
+    pixpainter->drawText( textRect,
+                      fm.elidedText( title, Qt::ElideRight, textRect.width() ),
+                      QTextOption( Qt::AlignCenter ) );
+
+    // Draw artist
+    pixpainter->setPen( text.lighter( 240 ) );
+    font.setItalic( false );
+    pixpainter->setFont( font );
+    fm = pixpainter->fontMetrics();
+
+
+    textRect = textRect.adjusted( 0, textRect.height(),
+                                    0, textRect.height() );
+    pixpainter->drawText(  textRect,
+                    fm.elidedText( artist, Qt::ElideRight, textRect.width() ),
+                    QTextOption( Qt::AlignCenter ) );
+
+    delete pixpainter; // Ensure all paint operations have finished
+
+    // Here real drawing happens
+    painter->drawPixmap( option.rect, pix );
+
+    // Cache the rendering
+    QPixmapCache::insert( key, pix );
 }
 
 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
 {
-    return QSize(100, 100);
+    return QSize( RECT_SIZE, RECT_SIZE);
 }
 
 
@@ -40,9 +180,15 @@ PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
 {
     setModel( model );
     setViewMode( QListView::IconMode );
-    setMovement( QListView::Snap );
+    setMovement( QListView::Static );
+    setResizeMode( QListView::Adjust );
+    setGridSize( QSize( RECT_SIZE, RECT_SIZE ) );
+    setUniformItemSizes( true );
+    setSpacing( ITEMS_SPACING );
+    setWrapping( true );
+    setSelectionMode( QAbstractItemView::ExtendedSelection );
+    setAcceptDrops( true );
 
     PlListViewItemDelegate *pl = new PlListViewItemDelegate();
     setItemDelegate( pl );
 }
-