]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/icon_view.cpp
Qt4: adjust iconviews art position littlebit
[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
44 static const QRect drawRect = QRect( 0, 0, RECT_SIZE, RECT_SIZE );
45 static const QRect artRect = drawRect.adjusted( OFFSET - 1, 2, - OFFSET, - OFFSET *2 );
46
47 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
48 {
49     PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
50     assert( currentItem );
51
52     char *meta;
53
54     meta = input_item_GetTitleFbName( currentItem->inputItem() );
55     QString title = qfu( meta );
56     free( meta );
57
58     meta = input_item_GetArtist( currentItem->inputItem() );
59     QString artist = qfu( meta );
60     free( meta );
61
62     QString artUrl = InputManager::decodeArtURL( currentItem->inputItem() );
63
64     // look up through all children and use the first picture found
65     if( artUrl.isEmpty() )
66     {
67         int children = currentItem->childCount();
68         for( int i = 0; i < children; i++ )
69         {
70             PLItem *child = currentItem->child( i );
71             artUrl = InputManager::decodeArtURL( child->inputItem() );
72             if( !artUrl.isEmpty() )
73                 break;
74         }
75     }
76
77     /*if( option.state & QStyle::State_Selected )
78          painter->fillRect(option.rect, option.palette.highlight());*/
79     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option,
80                                           painter );
81
82     // picture where all the rendering happens and which will be cached
83     QPixmap pix;
84
85     QString key = title + artist + artUrl
86                   + QString( index.data( PLModel::IsCurrentRole ).toBool() );
87     if(QPixmapCache::find( key, pix ))
88     {
89         // cool, we found it in the cache
90         painter->drawPixmap( option.rect, pix );
91         return;
92     }
93
94     // load album art
95     QPixmap artPix;
96     if( artUrl.isEmpty() || !artPix.load( artUrl ) )
97         artPix = QPixmap( ":/noart64" );
98     else
99         artPix = artPix.scaled( ART_SIZE, ART_SIZE,
100                 Qt::KeepAspectRatioByExpanding );
101
102     pix = QPixmap( RECT_SIZE, RECT_SIZE );
103     pix.fill( Qt::transparent );
104
105     QPainter *pixpainter = new QPainter( &pix );
106
107     pixpainter->setRenderHints(
108             QPainter::Antialiasing | QPainter::SmoothPixmapTransform |
109             QPainter::TextAntialiasing );
110
111     if( index.data( PLModel::IsCurrentRole ).toBool() )
112     {
113        pixpainter->save();
114        pixpainter->setOpacity( 0.2 );
115        pixpainter->setBrush( QBrush( Qt::gray ) );
116        pixpainter->drawRoundedRect( 0, 0, RECT_SIZE, RECT_SIZE, ART_RADIUS, ART_RADIUS );
117        pixpainter->restore();
118     }
119
120     // Draw the drop shadow
121     pixpainter->save();
122     pixpainter->setOpacity( 0.7 );
123     pixpainter->setBrush( QBrush( Qt::gray ) );
124     pixpainter->drawRoundedRect( artRect.adjusted( 2, 2, 2, 2 ), ART_RADIUS, ART_RADIUS );
125     pixpainter->restore();
126
127     // Draw the art pix
128     QPainterPath artRectPath;
129     artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
130     pixpainter->setClipPath( artRectPath );
131     pixpainter->drawPixmap( artRect, artPix );
132     pixpainter->setClipping( false );
133
134     QColor text = qApp->palette().text().color();
135
136     // Draw title
137     pixpainter->setPen( text );
138     QFont font;
139     font.setPointSize( 7 );
140     font.setItalic(true);
141     font.setBold( index.data( Qt::FontRole ).value<QFont>().bold() );
142     pixpainter->setFont( font );
143     QFontMetrics fm = pixpainter->fontMetrics();
144     QRect textRect = drawRect.adjusted( 1, ART_SIZE + 4, 0, -1 );
145     textRect.setHeight( fm.height() + 2 );
146
147     pixpainter->drawText( textRect,
148                       fm.elidedText( title, Qt::ElideRight, textRect.width() ),
149                       QTextOption( Qt::AlignCenter ) );
150
151     // Draw artist
152     pixpainter->setPen( text.lighter( 240 ) );
153     font.setItalic( false );
154     pixpainter->setFont( font );
155     fm = pixpainter->fontMetrics();
156
157
158     textRect = textRect.adjusted( 0, textRect.height(),
159                                     0, textRect.height() );
160     pixpainter->drawText(  textRect,
161                     fm.elidedText( artist, Qt::ElideRight, textRect.width() ),
162                     QTextOption( Qt::AlignCenter ) );
163
164     delete pixpainter; // Ensure all paint operations have finished
165
166     // Here real drawing happens
167     painter->drawPixmap( option.rect, pix );
168
169     // Cache the rendering
170     QPixmapCache::insert( key, pix );
171 }
172
173 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
174 {
175     return QSize( RECT_SIZE, RECT_SIZE);
176 }
177
178
179 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
180 {
181     setModel( model );
182     setViewMode( QListView::IconMode );
183     setMovement( QListView::Static );
184     setResizeMode( QListView::Adjust );
185     setGridSize( QSize( RECT_SIZE, RECT_SIZE ) );
186     setUniformItemSizes( true );
187     setSpacing( ITEMS_SPACING );
188     setWrapping( true );
189     setSelectionMode( QAbstractItemView::ExtendedSelection );
190     setAcceptDrops( true );
191
192     PlListViewItemDelegate *pl = new PlListViewItemDelegate( this );
193     setItemDelegate( pl );
194 }