]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/icon_view.cpp
QT4 Icon view: cache album art pixmap
[vlc] / modules / gui / qt4 / components / playlist / icon_view.cpp
1 /*****************************************************************************
2  * icon_view.cpp : Icon view for the Playlist
3  ****************************************************************************
4  * Copyright © 2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors:         Jean-Baptiste Kempf <jb@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "components/playlist/icon_view.hpp"
25 #include "components/playlist/playlist_model.hpp"
26 #include "input_manager.hpp"
27
28 #include <QApplication>
29 #include <QPainter>
30 #include <QRect>
31 #include <QStyleOptionViewItem>
32 #include <QFontMetrics>
33 #include <QPixmapCache>
34
35 #include "assert.h"
36
37 #define RECT_SIZE           100
38 #define ART_SIZE            64
39 #define OFFSET              (RECT_SIZE-64)/2
40 #define ITEMS_SPACING       10
41 #define ART_RADIUS          5
42
43 static QPixmap find_art_pixmap( const QString& url )
44 {
45     QPixmap pix;
46
47     if( QPixmapCache::find( url, pix ) )    /* great, we found it */
48         return pix;
49
50     if( url.isEmpty() || !pix.load( url ) )
51         pix = QPixmap( ":/noart64" );
52     else
53         pix = pix.scaled( ART_SIZE, ART_SIZE, Qt::KeepAspectRatioByExpanding );
54
55     QPixmapCache::insert( url, pix );       /* save it for next time */
56     return pix;
57 }
58
59 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
60 {
61     painter->setRenderHints( QPainter::Antialiasing | QPainter::SmoothPixmapTransform );
62
63     /*if( option.state & QStyle::State_Selected )
64          painter->fillRect(option.rect, option.palette.highlight());*/
65     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
66
67     PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
68     assert( currentItem );
69
70     QPixmap artpix;
71     QString url = InputManager::decodeArtURL( currentItem->inputItem() );
72
73     /* look up through all children and use the first picture found */
74     if( url.isEmpty() )
75     {
76         int children = currentItem->childCount();
77         for( int i = 0; i < children; i++ )
78         {
79             PLItem *child = currentItem->child( i );
80             url = InputManager::decodeArtURL( child->inputItem() );
81             if( !url.isEmpty() )
82                 break;
83         }
84     }
85
86     QRect artRect = option.rect.adjusted( OFFSET - 1, 0, - OFFSET, - OFFSET *2 );
87     artpix = find_art_pixmap( url ); /* look in the QPixmapCache for art */
88
89     QPainterPath artRectPath;
90     artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
91
92     // Draw the drop shadow
93     painter->save();
94     painter->setOpacity( 0.7 );
95     painter->setBrush( QBrush( Qt::gray ) );
96     painter->drawRoundedRect( artRect.adjusted( 2, 2, 2, 2 ), ART_RADIUS, ART_RADIUS );
97     painter->restore();
98
99     // Draw the art pixmap
100     painter->setClipPath( artRectPath );
101     painter->drawPixmap( artRect, artpix );
102     painter->setClipping( false );
103
104     QColor text = qApp->palette().text().color();
105     QString title = qfu( input_item_GetTitleFbName( currentItem->inputItem() ) );
106     QString artist = qfu( input_item_GetArtist( currentItem->inputItem() ) );
107
108     painter->setPen( text );
109     QFont font;
110     font.setPointSize( 7 );
111     font.setItalic(true);
112     font.setBold( index.data( Qt::FontRole ).value<QFont>().bold() );
113     painter->setFont( font );
114     QFontMetrics fm = painter->fontMetrics();
115     QRect titleRect = option.rect.adjusted( 1, ART_SIZE + 4, 0, -1 );
116     titleRect.setHeight( fm.height() + 2 );
117
118     painter->drawText( titleRect, fm.elidedText( title, Qt::ElideRight, titleRect.width() ),
119                        QTextOption( Qt::AlignCenter ) );
120
121     painter->setPen( text.lighter( 240 ) );
122     font.setItalic( false );
123     painter->setFont( font );
124     fm = painter->fontMetrics();
125     QRect artistRect = option.rect.adjusted( 1, ART_SIZE + 4 + titleRect.height(), -1, -1 );
126
127     painter->drawText( artistRect, fm.elidedText( artist, Qt::ElideRight, artistRect.width() ),
128                        QTextOption( Qt::AlignCenter ) );
129 }
130
131 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
132 {
133     return QSize( RECT_SIZE, RECT_SIZE);
134 }
135
136
137 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
138 {
139     setModel( model );
140     setViewMode( QListView::IconMode );
141     setMovement( QListView::Static );
142     setResizeMode( QListView::Adjust );
143     setGridSize( QSize( RECT_SIZE, RECT_SIZE ) );
144     setUniformItemSizes( true );
145     setSpacing( ITEMS_SPACING );
146     setWrapping( true );
147     setSelectionMode( QAbstractItemView::ExtendedSelection );
148     setAcceptDrops( true );
149
150     PlListViewItemDelegate *pl = new PlListViewItemDelegate();
151     setItemDelegate( pl );
152 }