]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/icon_view.cpp
dadd7d777cec0bbf7518935437213cc069b54093
[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
34 #include "assert.h"
35
36 #define RECT_SIZE           100
37 #define ART_SIZE            64
38 #define OFFSET              (RECT_SIZE-64)/2
39 #define ITEMS_SPACING       10
40 #define ART_RADIUS          5
41
42 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
43 {
44     painter->setRenderHints( QPainter::Antialiasing | QPainter::SmoothPixmapTransform );
45
46     /*if( option.state & QStyle::State_Selected )
47          painter->fillRect(option.rect, option.palette.highlight());*/
48     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
49
50     PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
51     assert( currentItem );
52
53     QPixmap pix;
54     QString url = InputManager::decodeArtURL( currentItem->inputItem() );
55
56     if( !url.isEmpty() && pix.load( url ) )
57     {
58         pix = pix.scaled( ART_SIZE, ART_SIZE, Qt::KeepAspectRatioByExpanding );
59     }
60     else
61     {
62         pix = QPixmap( ":/noart64" );
63     }
64
65     QRect artRect = option.rect.adjusted( OFFSET - 1, 0, - OFFSET, - OFFSET *2 );
66     QPainterPath artRectPath;
67     artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
68
69     // Draw the drop shadow
70     painter->save();
71     painter->setOpacity( 0.7 );
72     painter->setBrush( QBrush( Qt::gray ) );
73     painter->drawRoundedRect( artRect.adjusted( 2, 2, 2, 2 ), ART_RADIUS, ART_RADIUS );
74     painter->restore();
75
76     // Draw the art pixmap
77     painter->setClipPath( artRectPath );
78     painter->drawPixmap( artRect, pix );
79     painter->setClipping( false );
80
81     QColor text = qApp->palette().text().color();
82     QString title = qfu( input_item_GetTitleFbName( currentItem->inputItem() ) );
83     QString artist = qfu( input_item_GetArtist( currentItem->inputItem() ) );
84
85     painter->setPen( text );
86     QFont font;
87     font.setPointSize( 7 );
88     font.setItalic(true);
89     font.setBold( index.data( Qt::FontRole ).value<QFont>().bold() );
90     painter->setFont( font );
91     QFontMetrics fm = painter->fontMetrics();
92     QRect titleRect = option.rect.adjusted( 1, ART_SIZE + 4, 0, -1 );
93     titleRect.setHeight( fm.height() + 2 );
94
95     painter->drawText( titleRect, fm.elidedText( title, Qt::ElideRight, titleRect.width() ),
96                        QTextOption( Qt::AlignCenter ) );
97
98     painter->setPen( text.lighter( 240 ) );
99     font.setItalic( false );
100     painter->setFont( font );
101     fm = painter->fontMetrics();
102     QRect artistRect = option.rect.adjusted( 1, ART_SIZE + 4 + titleRect.height(), -1, -1 );
103
104     painter->drawText( artistRect, fm.elidedText( artist, Qt::ElideRight, artistRect.width() ),
105                        QTextOption( Qt::AlignCenter ) );
106 }
107
108 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
109 {
110     return QSize( RECT_SIZE, RECT_SIZE);
111 }
112
113
114 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
115 {
116     setModel( model );
117     setViewMode( QListView::IconMode );
118     setMovement( QListView::Static );
119     setResizeMode( QListView::Adjust );
120     setGridSize( QSize( RECT_SIZE, RECT_SIZE ) );
121     setUniformItemSizes( true );
122     setSpacing( ITEMS_SPACING );
123     setWrapping( true );
124     setSelectionMode( QAbstractItemView::ExtendedSelection );
125     setAcceptDrops( true );
126
127     PlListViewItemDelegate *pl = new PlListViewItemDelegate();
128     setItemDelegate( pl );
129 }