]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/icon_view.cpp
Qt: more details on the icon view.
[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     painter->setFont( QFont( "Verdana", 7, QFont::Bold ) );
87     QFontMetrics fm = painter->fontMetrics();
88     QRect titleRect = option.rect.adjusted( 1, ART_SIZE + 4, 0, -1 );
89     titleRect.setHeight( fm.height() + 2 );
90
91     painter->drawText( titleRect, fm.elidedText( title, Qt::ElideRight, titleRect.width() ),
92                        QTextOption( Qt::AlignCenter ) );
93
94     painter->setPen( text.lighter( 240 ) );
95     painter->setFont( QFont( "Verdana", 7 ) );
96     fm = painter->fontMetrics();
97     QRect artistRect = option.rect.adjusted( 1, ART_SIZE + 4 + titleRect.height(), -1, -1 );
98
99     painter->drawText( artistRect, fm.elidedText( artist, Qt::ElideRight, artistRect.width() ),
100                        QTextOption( Qt::AlignCenter ) );
101 }
102
103 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
104 {
105     return QSize( RECT_SIZE, RECT_SIZE);
106 }
107
108
109 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
110 {
111     setModel( model );
112     setViewMode( QListView::IconMode );
113     setMovement( QListView::Static );
114     setResizeMode( QListView::Adjust );
115     setGridSize( QSize( RECT_SIZE, RECT_SIZE ) );
116     setUniformItemSizes( true );
117     setSpacing( ITEMS_SPACING );
118     setWrapping( true );
119     setSelectionMode( QAbstractItemView::ExtendedSelection );
120     setAcceptDrops( true );
121
122     PlListViewItemDelegate *pl = new PlListViewItemDelegate();
123     setItemDelegate( pl );
124
125     CONNECT( this, activated( const QModelIndex & ), this, activate( const QModelIndex & ) );
126 }
127
128 void PlIconView::activate( const QModelIndex & index )
129 {
130     if( model()->hasChildren( index ) )
131         setRootIndex( index );
132     else
133     {
134         PLModel *plModel = qobject_cast<PLModel*>( model() );
135         if( !plModel ) return;
136         plModel->activateItem( index );
137     }
138 }