]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/playlist/icon_view.cpp
Qt: ensure that item selection has visible effect in playlist views
[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 "components/playlist/sorting.h"
27 #include "input_manager.hpp"
28
29 #include <QApplication>
30 #include <QPainter>
31 #include <QRect>
32 #include <QStyleOptionViewItem>
33 #include <QFontMetrics>
34 #include <QPixmapCache>
35
36 #include "assert.h"
37
38 #define ART_SIZE_W          110
39 #define ART_SIZE_H          80
40 #define ART_RADIUS          5
41 #define SPACER              5
42
43 QString AbstractPlViewItemDelegate::getMeta( const QModelIndex & index, int meta ) const
44 {
45     return index.model()->index( index.row(),
46                                   PLModel::columnFromMeta( meta ),
47                                   index.parent() )
48                                 .data().toString();
49 }
50
51 void AbstractPlViewItemDelegate::paintBackground(
52     QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
53 {
54     /* FIXME: This does not indicate item selection in all QStyles, so for the time being we
55        have to draw it ourselves, to ensure visible effect of selection on all platforms */
56     /* QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option,
57                                             painter ); */
58
59     painter->save();
60     QRect r = option.rect.adjusted( 0, 0, -1, -1 );
61     if( option.state & QStyle::State_Selected )
62     {
63         painter->setBrush( option.palette.color( QPalette::Highlight ) );
64         painter->setPen( option.palette.color( QPalette::Highlight ).darker( 150 ) );
65         painter->drawRect( r );
66     }
67     else if( index.data( PLModel::IsCurrentRole ).toBool() )
68     {
69         painter->setBrush( QBrush( Qt::lightGray ) );
70         painter->setPen( QColor( Qt::darkGray ) );
71         painter->drawRect( r );
72     }
73     if( option.state & QStyle::State_MouseOver )
74     {
75         painter->setOpacity( 0.5 );
76         painter->setPen( Qt::NoPen );
77         painter->setBrush( option.palette.color( QPalette::Highlight ).lighter( 150 ) );
78         painter->drawRect( option.rect );
79     }
80     painter->restore();
81 }
82
83 QPixmap AbstractPlViewItemDelegate::getArtPixmap( const QModelIndex & index, const QSize & size ) const
84 {
85     PLItem *item = static_cast<PLItem*>( index.internalPointer() );
86     assert( item );
87
88     QString artUrl = InputManager::decodeArtURL( item->inputItem() );
89
90     if( artUrl.isEmpty() )
91     {
92         for( int i = 0; i < item->childCount(); i++ )
93         {
94             artUrl = InputManager::decodeArtURL( item->child( i )->inputItem() );
95             if( !artUrl.isEmpty() )
96                 break;
97         }
98     }
99
100     QPixmap artPix;
101
102     QString key = artUrl + QString("%1%2").arg(size.width()).arg(size.height());
103
104     if( !QPixmapCache::find( key, artPix ))
105     {
106         if( artUrl.isEmpty() || !artPix.load( artUrl ) )
107         {
108             key = QString("noart%1%2").arg(size.width()).arg(size.height());
109             if( !QPixmapCache::find( key, artPix ) )
110             {
111                 artPix = QPixmap( ":/noart" ).scaled( size,
112                                                       Qt::KeepAspectRatio,
113                                                       Qt::SmoothTransformation );
114                 QPixmapCache::insert( key, artPix );
115             }
116         }
117         else
118         {
119             artPix = artPix.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
120             QPixmapCache::insert( key, artPix );
121         }
122     }
123
124     return artPix;
125 }
126
127 void PlIconViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
128 {
129     QString title = getMeta( index, COLUMN_TITLE );
130     QString artist = getMeta( index, COLUMN_ARTIST );
131
132     QPixmap artPix = getArtPixmap( index, QSize( ART_SIZE_W, ART_SIZE_H ) );
133
134     paintBackground( painter, option, index );
135
136     painter->save();
137
138     QRect artRect( option.rect.x() + 5 + ( ART_SIZE_W - artPix.width() ) / 2,
139                    option.rect.y() + 5 + ( ART_SIZE_H - artPix.height() ) / 2,
140                    artPix.width(), artPix.height() );
141
142     // Draw the drop shadow
143     painter->save();
144     painter->setOpacity( 0.7 );
145     painter->setBrush( QBrush( Qt::darkGray ) );
146     painter->setPen( Qt::NoPen );
147     painter->drawRoundedRect( artRect.adjusted( 0, 0, 2, 2 ), ART_RADIUS, ART_RADIUS );
148     painter->restore();
149
150     // Draw the art pixmap
151     QPainterPath artRectPath;
152     artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
153     painter->setClipPath( artRectPath );
154     painter->drawPixmap( artRect, artPix );
155     painter->setClipping( false );
156
157     if( option.state & QStyle::State_Selected )
158         painter->setPen( option.palette.color( QPalette::HighlightedText ) );
159
160     QFont font( index.data( Qt::FontRole ).value<QFont>() );
161     font.setPointSize( 7 );
162
163     // Draw title
164     font.setItalic( true );
165     painter->setFont( font );
166
167     QFontMetrics fm = painter->fontMetrics();
168     QRect textRect = option.rect.adjusted( 1, ART_SIZE_H + 10, 0, -1 );
169     textRect.setHeight( fm.height() );
170
171     painter->drawText( textRect,
172                       fm.elidedText( title, Qt::ElideRight, textRect.width() ),
173                       QTextOption( Qt::AlignCenter ) );
174
175     // Draw artist
176     painter->setPen( painter->pen().color().lighter( 150 ) );
177     font.setItalic( false );
178     painter->setFont( font );
179     fm = painter->fontMetrics();
180
181     textRect.moveTop( textRect.bottom() + 1 );
182
183     painter->drawText(  textRect,
184                         fm.elidedText( artist, Qt::ElideRight, textRect.width() ),
185                         QTextOption( Qt::AlignCenter ) );
186
187     painter->restore();
188 }
189
190 QSize PlIconViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
191 {
192     QFont f;
193     f.setPointSize( 7 );
194     f.setBold( true );
195     QFontMetrics fm( f );
196     int textHeight = fm.height();
197     QSize sz ( ART_SIZE_W + 2 * SPACER,
198                ART_SIZE_H + 3 * SPACER + 2 * textHeight + 1 );
199     return sz;
200 }
201
202
203 #define LISTVIEW_ART_SIZE 45
204
205 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
206 {
207     QModelIndex parent = index.parent();
208     QModelIndex i;
209
210     QString title = getMeta( index, COLUMN_TITLE );
211     QString duration = getMeta( index, COLUMN_DURATION );
212     if( !duration.isEmpty() ) title += QString(" [%1]").arg( duration );
213
214     QString artist = getMeta( index, COLUMN_ARTIST );
215     QString album = getMeta( index, COLUMN_ALBUM );
216     QString trackNum = getMeta( index, COLUMN_TRACK_NUMBER );
217     QString artistAlbum = artist
218                           + ( artist.isEmpty() ? QString() : QString( ": " ) )
219                           + album
220                           + ( album.isEmpty() || trackNum.isEmpty() ?
221                               QString() : QString( " [#%1]" ).arg( trackNum ) );
222
223     QPixmap artPix = getArtPixmap( index, QSize( LISTVIEW_ART_SIZE, LISTVIEW_ART_SIZE ) );
224
225     //Draw selection rectangle and current playing item indication
226     paintBackground( painter, option, index );
227
228     QRect artRect( artPix.rect() );
229     artRect.moveCenter( QPoint( artRect.center().x() + 3,
230                                 option.rect.center().y() ) );
231     //Draw album art
232     painter->drawPixmap( artRect, artPix );
233
234     //Start drawing text
235     painter->save();
236
237     if( option.state & QStyle::State_Selected )
238         painter->setPen( option.palette.color( QPalette::HighlightedText ) );
239
240     QTextOption textOpt( Qt::AlignVCenter | Qt::AlignLeft );
241     textOpt.setWrapMode( QTextOption::NoWrap );
242
243     QFont f( index.data( Qt::FontRole ).value<QFont>() );
244
245     //Draw title info
246     f.setItalic( true );
247     painter->setFont( f );
248     QFontMetrics fm( painter->fontMetrics() );
249
250     QRect textRect = option.rect.adjusted( LISTVIEW_ART_SIZE + 10, 0, -10, 0 );
251     if( !artistAlbum.isEmpty() )
252     {
253         textRect.setHeight( fm.height() );
254         textRect.moveBottom( option.rect.center().y() - 1 );
255     }
256
257     painter->drawText( textRect,
258                        fm.elidedText( title, Qt::ElideRight, textRect.width() ),
259                        textOpt );
260
261     // Draw artist and album info
262     if( !artistAlbum.isEmpty() )
263     {
264         f.setItalic( false );
265         painter->setFont( f );
266         fm = painter->fontMetrics();
267
268         textRect.moveTop( textRect.bottom() + 2 );
269
270         painter->drawText( textRect,
271                            fm.elidedText( artistAlbum, Qt::ElideRight, textRect.width() ),
272                            textOpt );
273     }
274
275     painter->restore();
276 }
277
278 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
279 {
280   QFont f;
281   f.setBold( true );
282   QFontMetrics fm( f );
283   int height = qMax( LISTVIEW_ART_SIZE, 2 * fm.height() + 2 ) + 6;
284   return QSize( 0, height );
285 }
286
287 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
288 {
289     PlIconViewItemDelegate *delegate = new PlIconViewItemDelegate( this );
290
291     setModel( model );
292     setViewMode( QListView::IconMode );
293     setMovement( QListView::Static );
294     setResizeMode( QListView::Adjust );
295     setGridSize( delegate->sizeHint() );
296     setWrapping( true );
297     setUniformItemSizes( true );
298     setSelectionMode( QAbstractItemView::ExtendedSelection );
299     setDragEnabled(true);
300     /* dropping in QListView::IconMode does not seem to work */
301     //setAcceptDrops( true );
302     //setDropIndicatorShown(true);
303
304     setItemDelegate( delegate );
305 }
306
307 PlListView::PlListView( PLModel *model, QWidget *parent ) : QListView( parent )
308 {
309     setModel( model );
310     setViewMode( QListView::ListMode );
311     setUniformItemSizes( true );
312     setSelectionMode( QAbstractItemView::ExtendedSelection );
313     setAlternatingRowColors( true );
314     setDragEnabled(true);
315     setAcceptDrops( true );
316     setDropIndicatorShown(true);
317
318     PlListViewItemDelegate *delegate = new PlListViewItemDelegate( this );
319     setItemDelegate( delegate );
320 }