]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/icon_view.cpp
Typo in [1b9f9692f91dcf0659919b16551389e0dbf96412]
[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_W         100
38 #define RECT_SIZE_H         105
39 #define ART_SIZE            64
40 #define OFFSET              (RECT_SIZE_W-64)/2
41 #define ITEMS_SPACING       10
42 #define ART_RADIUS          5
43
44
45 static const QRect drawRect = QRect( 0, 0, RECT_SIZE_W, RECT_SIZE_H );
46 static const QRect artRect = drawRect.adjusted( OFFSET - 1, 2, - OFFSET, - OFFSET *2 );
47
48 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
49 {
50     PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
51     assert( currentItem );
52
53     char *meta = input_item_GetTitleFbName( currentItem->inputItem() );
54     QString title = qfu( meta );
55     free( meta );
56
57     meta = input_item_GetArtist( currentItem->inputItem() );
58     QString artist = qfu( meta );
59     free( meta );
60
61     /* Primary ArtUrl */
62     QString artUrl = InputManager::decodeArtURL( currentItem->inputItem() );
63
64     /* else look up through all children and use the first picture found */
65     if( artUrl.isEmpty() )
66     {
67         for( int i = 0; i < currentItem->childCount(); i++ )
68         {
69             artUrl = InputManager::decodeArtURL( currentItem->child( i )->inputItem() );
70             if( !artUrl.isEmpty() )
71                 break;
72         }
73     }
74
75     /*if( option.state & QStyle::State_Selected )
76          painter->fillRect(option.rect, option.palette.highlight());*/
77     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option,
78                                           painter );
79
80     /* Pixmap where all the rendering will happen and that will be cached */
81     QPixmap pix;
82
83     QString key = title + artist + artUrl
84                   + QString( index.data( PLModel::IsCurrentRole ).toBool() );
85     if(QPixmapCache::find( key, pix ))
86     {
87         // cool, we found it in the cache
88         painter->drawPixmap( option.rect, pix );
89         return;
90     }
91
92     /* Background decorations */
93     pix = QPixmap( RECT_SIZE_W, RECT_SIZE_H );
94     pix.fill( Qt::transparent );
95
96     QPainter *pixpainter = new QPainter( &pix );
97
98     pixpainter->setRenderHints(
99             QPainter::Antialiasing | QPainter::SmoothPixmapTransform |
100             QPainter::TextAntialiasing );
101
102     if( index.data( PLModel::IsCurrentRole ).toBool() )
103     {
104        pixpainter->save();
105        pixpainter->setOpacity( 0.2 );
106        pixpainter->setBrush( QBrush( Qt::gray ) );
107        pixpainter->drawRoundedRect( 0, 0, RECT_SIZE_W, RECT_SIZE_H, ART_RADIUS, ART_RADIUS );
108        pixpainter->restore();
109     }
110
111     // Draw the drop shadow
112     pixpainter->save();
113     pixpainter->setOpacity( 0.7 );
114     pixpainter->setBrush( QBrush( Qt::gray ) );
115     pixpainter->drawRoundedRect( artRect.adjusted( 2, 2, 2, 2 ), ART_RADIUS, ART_RADIUS );
116     pixpainter->restore();
117
118
119     // load album art in the pixmap
120     QPixmap artPix;
121     if( artUrl.isEmpty() || !artPix.load( artUrl ) )
122         artPix = QPixmap( ":/noart64" );
123     else
124         artPix = artPix.scaled( ART_SIZE, ART_SIZE,
125                 Qt::KeepAspectRatioByExpanding );
126
127     // Draw the art pixmap
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     /* */
135     QColor text = qApp->palette().text().color();
136
137     // Draw title
138     pixpainter->setPen( text );
139     QFont font;
140     font.setPointSize( 7 );
141     font.setItalic( true );
142     font.setBold( index.data( Qt::FontRole ).value<QFont>().bold() );
143     pixpainter->setFont( font );
144     QFontMetrics fm = pixpainter->fontMetrics();
145     QRect textRect = drawRect.adjusted( 1, ART_SIZE + 4, 0, -1 );
146     textRect.setHeight( fm.height() + 1 );
147
148     pixpainter->drawText( textRect,
149                       fm.elidedText( title, Qt::ElideRight, textRect.width() ),
150                       QTextOption( Qt::AlignCenter ) );
151
152     // Draw artist
153     pixpainter->setPen( text.lighter( 220 ) );
154     font.setItalic( false );
155     pixpainter->setFont( font );
156     fm = pixpainter->fontMetrics();
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_W, RECT_SIZE_H );
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_W, RECT_SIZE_H ) );
186     setUniformItemSizes( true );
187     setWrapping( true );
188     setSelectionMode( QAbstractItemView::ExtendedSelection );
189     setAcceptDrops( true );
190
191     PlListViewItemDelegate *pl = new PlListViewItemDelegate( this );
192     setItemDelegate( pl );
193 }
194