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
44 static const QRect drawRect = QRect( 0, 0, RECT_SIZE, RECT_SIZE );
45 static const QRect artRect = drawRect.adjusted( OFFSET - 1, 0, - OFFSET, - OFFSET *2 );
47 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
49 PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
50 assert( currentItem );
54 meta = input_item_GetTitleFbName( currentItem->inputItem() );
55 QString title = qfu( meta );
58 meta = input_item_GetArtist( currentItem->inputItem() );
59 QString artist = qfu( meta );
62 QString artUrl = InputManager::decodeArtURL( currentItem->inputItem() );
64 // look up through all children and use the first picture found
65 if( artUrl.isEmpty() )
67 int children = currentItem->childCount();
68 for( int i = 0; i < children; i++ )
70 PLItem *child = currentItem->child( i );
71 artUrl = InputManager::decodeArtURL( child->inputItem() );
72 if( !artUrl.isEmpty() )
77 /*if( option.state & QStyle::State_Selected )
78 painter->fillRect(option.rect, option.palette.highlight());*/
79 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option,
82 // picture where all the rendering happens and which will be cached
85 QString key = title + artist + artUrl;
86 if(QPixmapCache::find( key, pix ))
88 // cool, we found it in the cache
89 painter->drawPixmap( option.rect, pix );
95 if( artUrl.isEmpty() || !artPix.load( artUrl ) )
96 artPix = QPixmap( ":/noart64" );
98 artPix = artPix.scaled( ART_SIZE, ART_SIZE,
99 Qt::KeepAspectRatioByExpanding );
101 pix = QPixmap( RECT_SIZE, RECT_SIZE );
102 pix.fill( Qt::transparent );
104 QPainter *pixpainter = new QPainter( &pix );
106 pixpainter->setRenderHints(
107 QPainter::Antialiasing | QPainter::SmoothPixmapTransform );
109 // Draw the drop shadow
111 pixpainter->setOpacity( 0.7 );
112 pixpainter->setBrush( QBrush( Qt::gray ) );
113 pixpainter->drawRoundedRect( artRect.adjusted( 2, 2, 2, 2 ), ART_RADIUS, ART_RADIUS );
114 pixpainter->restore();
117 QPainterPath artRectPath;
118 artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
119 pixpainter->setClipPath( artRectPath );
120 pixpainter->drawPixmap( artRect, artPix );
121 pixpainter->setClipping( false );
123 QColor text = qApp->palette().text().color();
126 pixpainter->setPen( text );
128 font.setPointSize( 7 );
129 font.setItalic(true);
130 font.setBold( index.data( Qt::FontRole ).value<QFont>().bold() );
131 pixpainter->setFont( font );
132 QFontMetrics fm = pixpainter->fontMetrics();
133 QRect textRect = drawRect.adjusted( 1, ART_SIZE + 4, 0, -1 );
134 textRect.setHeight( fm.height() + 2 );
136 pixpainter->drawText( textRect,
137 fm.elidedText( title, Qt::ElideRight, textRect.width() ),
138 QTextOption( Qt::AlignCenter ) );
141 pixpainter->setPen( text.lighter( 240 ) );
142 font.setItalic( false );
143 pixpainter->setFont( font );
144 fm = pixpainter->fontMetrics();
147 textRect = textRect.adjusted( 0, textRect.height(),
148 0, textRect.height() );
149 pixpainter->drawText( textRect,
150 fm.elidedText( artist, Qt::ElideRight, textRect.width() ),
151 QTextOption( Qt::AlignCenter ) );
153 delete pixpainter; // Ensure all paint operations have finished
155 // Here real drawing happens
156 painter->drawPixmap( option.rect, pix );
158 // Cache the rendering
159 QPixmapCache::insert( key, pix );
162 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
164 return QSize( RECT_SIZE, RECT_SIZE);
168 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
171 setViewMode( QListView::IconMode );
172 setMovement( QListView::Static );
173 setResizeMode( QListView::Adjust );
174 setGridSize( QSize( RECT_SIZE, RECT_SIZE ) );
175 setUniformItemSizes( true );
176 setSpacing( ITEMS_SPACING );
178 setSelectionMode( QAbstractItemView::ExtendedSelection );
179 setAcceptDrops( true );
181 PlListViewItemDelegate *pl = new PlListViewItemDelegate();
182 setItemDelegate( pl );