1 /*****************************************************************************
2 * icon_view.cpp : Icon view for the Playlist
3 ****************************************************************************
4 * Copyright © 2010 the VideoLAN team
7 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include "components/playlist/icon_view.hpp"
25 #include "components/playlist/playlist_model.hpp"
26 #include "input_manager.hpp"
28 #include <QApplication>
31 #include <QStyleOptionViewItem>
32 #include <QFontMetrics>
33 #include <QPixmapCache>
39 #define OFFSET (RECT_SIZE-64)/2
40 #define ITEMS_SPACING 10
43 static QPixmap find_art_pixmap( const QString& url )
47 if( QPixmapCache::find( url, pix ) ) /* great, we found it */
50 if( url.isEmpty() || !pix.load( url ) )
51 pix = QPixmap( ":/noart64" );
53 pix = pix.scaled( ART_SIZE, ART_SIZE, Qt::KeepAspectRatioByExpanding );
55 QPixmapCache::insert( url, pix ); /* save it for next time */
59 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
61 painter->setRenderHints( QPainter::Antialiasing | QPainter::SmoothPixmapTransform );
63 /*if( option.state & QStyle::State_Selected )
64 painter->fillRect(option.rect, option.palette.highlight());*/
65 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
67 PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
68 assert( currentItem );
71 QString url = InputManager::decodeArtURL( currentItem->inputItem() );
73 /* look up through all children and use the first picture found */
76 int children = currentItem->childCount();
77 for( int i = 0; i < children; i++ )
79 PLItem *child = currentItem->child( i );
80 url = InputManager::decodeArtURL( child->inputItem() );
86 QRect artRect = option.rect.adjusted( OFFSET - 1, 0, - OFFSET, - OFFSET *2 );
87 artpix = find_art_pixmap( url ); /* look in the QPixmapCache for art */
89 QPainterPath artRectPath;
90 artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
92 // Draw the drop shadow
94 painter->setOpacity( 0.7 );
95 painter->setBrush( QBrush( Qt::gray ) );
96 painter->drawRoundedRect( artRect.adjusted( 2, 2, 2, 2 ), ART_RADIUS, ART_RADIUS );
99 // Draw the art pixmap
100 painter->setClipPath( artRectPath );
101 painter->drawPixmap( artRect, artpix );
102 painter->setClipping( false );
104 QColor text = qApp->palette().text().color();
105 QString title = qfu( input_item_GetTitleFbName( currentItem->inputItem() ) );
106 QString artist = qfu( input_item_GetArtist( currentItem->inputItem() ) );
108 painter->setPen( text );
110 font.setPointSize( 7 );
111 font.setItalic(true);
112 font.setBold( index.data( Qt::FontRole ).value<QFont>().bold() );
113 painter->setFont( font );
114 QFontMetrics fm = painter->fontMetrics();
115 QRect titleRect = option.rect.adjusted( 1, ART_SIZE + 4, 0, -1 );
116 titleRect.setHeight( fm.height() + 2 );
118 painter->drawText( titleRect, fm.elidedText( title, Qt::ElideRight, titleRect.width() ),
119 QTextOption( Qt::AlignCenter ) );
121 painter->setPen( text.lighter( 240 ) );
122 font.setItalic( false );
123 painter->setFont( font );
124 fm = painter->fontMetrics();
125 QRect artistRect = option.rect.adjusted( 1, ART_SIZE + 4 + titleRect.height(), -1, -1 );
127 painter->drawText( artistRect, fm.elidedText( artist, Qt::ElideRight, artistRect.width() ),
128 QTextOption( Qt::AlignCenter ) );
131 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
133 return QSize( RECT_SIZE, RECT_SIZE);
137 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
140 setViewMode( QListView::IconMode );
141 setMovement( QListView::Static );
142 setResizeMode( QListView::Adjust );
143 setGridSize( QSize( RECT_SIZE, RECT_SIZE ) );
144 setUniformItemSizes( true );
145 setSpacing( ITEMS_SPACING );
147 setSelectionMode( QAbstractItemView::ExtendedSelection );
148 setAcceptDrops( true );
150 PlListViewItemDelegate *pl = new PlListViewItemDelegate();
151 setItemDelegate( pl );