Qt: more details on the icon view.
[vlc/solaris.git] / modules / gui / qt4 / components / playlist / icon_view.cpp
blobf07b25f60aea94de97d6c7c581964a9972455a9c
1 /*****************************************************************************
2 * icon_view.cpp : Icon view for the Playlist
3 ****************************************************************************
4 * Copyright © 2010 the VideoLAN team
5 * $Id$
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>
29 #include <QPainter>
30 #include <QRect>
31 #include <QStyleOptionViewItem>
32 #include <QFontMetrics>
34 #include "assert.h"
36 #define RECT_SIZE 100
37 #define ART_SIZE 64
38 #define OFFSET (RECT_SIZE-64)/2
39 #define ITEMS_SPACING 10
40 #define ART_RADIUS 5
42 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
44 painter->setRenderHints( QPainter::Antialiasing | QPainter::SmoothPixmapTransform );
46 /*if( option.state & QStyle::State_Selected )
47 painter->fillRect(option.rect, option.palette.highlight());*/
48 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
50 PLItem *currentItem = static_cast<PLItem*>( index.internalPointer() );
51 assert( currentItem );
53 QPixmap pix;
54 QString url = InputManager::decodeArtURL( currentItem->inputItem() );
56 if( !url.isEmpty() && pix.load( url ) )
58 pix = pix.scaled( ART_SIZE, ART_SIZE, Qt::KeepAspectRatioByExpanding );
60 else
62 pix = QPixmap( ":/noart64" );
65 QRect artRect = option.rect.adjusted( OFFSET - 1, 0, - OFFSET, - OFFSET *2 );
66 QPainterPath artRectPath;
67 artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
69 // Draw the drop shadow
70 painter->save();
71 painter->setOpacity( 0.7 );
72 painter->setBrush( QBrush( Qt::gray ) );
73 painter->drawRoundedRect( artRect.adjusted( 2, 2, 2, 2 ), ART_RADIUS, ART_RADIUS );
74 painter->restore();
76 // Draw the art pixmap
77 painter->setClipPath( artRectPath );
78 painter->drawPixmap( artRect, pix );
79 painter->setClipping( false );
81 QColor text = qApp->palette().text().color();
82 QString title = qfu( input_item_GetTitleFbName( currentItem->inputItem() ) );
83 QString artist = qfu( input_item_GetArtist( currentItem->inputItem() ) );
85 painter->setPen( text );
86 painter->setFont( QFont( "Verdana", 7, QFont::Bold ) );
87 QFontMetrics fm = painter->fontMetrics();
88 QRect titleRect = option.rect.adjusted( 1, ART_SIZE + 4, 0, -1 );
89 titleRect.setHeight( fm.height() + 2 );
91 painter->drawText( titleRect, fm.elidedText( title, Qt::ElideRight, titleRect.width() ),
92 QTextOption( Qt::AlignCenter ) );
94 painter->setPen( text.lighter( 240 ) );
95 painter->setFont( QFont( "Verdana", 7 ) );
96 fm = painter->fontMetrics();
97 QRect artistRect = option.rect.adjusted( 1, ART_SIZE + 4 + titleRect.height(), -1, -1 );
99 painter->drawText( artistRect, fm.elidedText( artist, Qt::ElideRight, artistRect.width() ),
100 QTextOption( Qt::AlignCenter ) );
103 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
105 return QSize( RECT_SIZE, RECT_SIZE);
109 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
111 setModel( model );
112 setViewMode( QListView::IconMode );
113 setMovement( QListView::Static );
114 setResizeMode( QListView::Adjust );
115 setGridSize( QSize( RECT_SIZE, RECT_SIZE ) );
116 setUniformItemSizes( true );
117 setSpacing( ITEMS_SPACING );
118 setWrapping( true );
119 setSelectionMode( QAbstractItemView::ExtendedSelection );
120 setAcceptDrops( true );
122 PlListViewItemDelegate *pl = new PlListViewItemDelegate();
123 setItemDelegate( pl );
125 CONNECT( this, activated( const QModelIndex & ), this, activate( const QModelIndex & ) );
128 void PlIconView::activate( const QModelIndex & index )
130 if( model()->hasChildren( index ) )
131 setRootIndex( index );
132 else
134 PLModel *plModel = qobject_cast<PLModel*>( model() );
135 if( !plModel ) return;
136 plModel->activateItem( index );