]> 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 da4bf85094be0c2e8e4128b57bf6784c4d811b0a..4e1c7a9b84060900e7f1a4d2e2dd9511db1d5339 100644 (file)
 #include "components/playlist/playlist_model.hpp"
 #include "input_manager.hpp"
 
+#include <QApplication>
 #include <QPainter>
 #include <QRect>
 #include <QStyleOptionViewItem>
-#include <QApplication>
+#include <QFontMetrics>
+#include <QPixmapCache>
 
 #include "assert.h"
 
 #define RECT_SIZE           100
 #define ART_SIZE            64
-#define OFFSET              (100-64)/2
+#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
 {
-    painter->setRenderHints( QPainter::Antialiasing | QPainter::SmoothPixmapTransform );
+    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 );
-
-    PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
-    assert( currentItem );
+    QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option,
+                                          painter );
 
+    // picture where all the rendering happens and which will be cached
     QPixmap pix;
-    QString url = InputManager::decodeArtURL( currentItem->inputItem() );
 
-    if( !url.isEmpty() && pix.load( url ) )
+    QString key = title + artist + artUrl
+                  + QString( index.data( PLModel::IsCurrentRole ).toBool() );
+    if(QPixmapCache::find( key, pix ))
     {
-        pix = pix.scaled( ART_SIZE, ART_SIZE, Qt::KeepAspectRatioByExpanding );
+        // 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
-    {
-        pix = QPixmap( ":/noart64" );
-    }
+        artPix = artPix.scaled( ART_SIZE, ART_SIZE,
+                Qt::KeepAspectRatioByExpanding );
 
-    QRect artRect = option.rect.adjusted( OFFSET - 1, 0, - OFFSET, - OFFSET *2 );
-    QPainterPath artRectPath;
-    artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
+    pix = QPixmap( RECT_SIZE, RECT_SIZE );
+    pix.fill( Qt::transparent );
 
-    // Draw the drop shadow
-    painter->save();
-    painter->setOpacity( 0.7 );
-    painter->setBrush( QBrush( Qt::gray ) );
-    painter->drawRoundedRect( artRect.adjusted( 2, 2, 2, 2 ), ART_RADIUS, ART_RADIUS );
-    painter->restore();
+    QPainter *pixpainter = new QPainter( &pix );
 
-    // Draw the art pixmap
-    painter->setClipPath( artRectPath );
-    painter->drawPixmap( artRect, pix );
-    painter->setClipping( false );
+    pixpainter->setRenderHints(
+            QPainter::Antialiasing | QPainter::SmoothPixmapTransform |
+            QPainter::TextAntialiasing );
 
-    painter->setFont( QFont( "Verdana", 7 ) );
+    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();
+    }
 
-    QRect textRect = option.rect.adjusted( 1, ART_SIZE + 2, -1, -1 );
-    painter->drawText( textRect, qfu( input_item_GetTitleFbName( currentItem->inputItem() ) ),
-                       QTextOption( Qt::AlignCenter ) );
+    // 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
@@ -106,18 +191,4 @@ PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
 
     PlListViewItemDelegate *pl = new PlListViewItemDelegate();
     setItemDelegate( pl );
-
-    CONNECT( this, activated( const QModelIndex & ), this, activate( const QModelIndex & ) );
-}
-
-void PlIconView::activate( const QModelIndex & index )
-{
-    if( model()->hasChildren( index ) )
-        setRootIndex( index );
-    else
-    {
-        PLModel *plModel = qobject_cast<PLModel*>( model() );
-        if( !plModel ) return;
-        plModel->activateItem( index );
-    }
 }