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 "components/playlist/sorting.h"
27 #include "input_manager.hpp"
29 #include <QApplication>
32 #include <QStyleOptionViewItem>
33 #include <QFontMetrics>
34 #include <QPixmapCache>
36 #include <QDragMoveEvent>
40 #define ART_SIZE_W 110
45 QString AbstractPlViewItemDelegate::getMeta( const QModelIndex & index, int meta ) const
47 return index.model()->index( index.row(),
48 PLModel::columnFromMeta( meta ),
53 void AbstractPlViewItemDelegate::paintBackground(
54 QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
56 /* FIXME: This does not indicate item selection in all QStyles, so for the time being we
57 have to draw it ourselves, to ensure visible effect of selection on all platforms */
58 /* QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option,
62 QRect r = option.rect.adjusted( 0, 0, -1, -1 );
63 if( option.state & QStyle::State_Selected )
65 painter->setBrush( option.palette.color( QPalette::Highlight ) );
66 painter->setPen( option.palette.color( QPalette::Highlight ).darker( 150 ) );
67 painter->drawRect( r );
69 else if( index.data( PLModel::IsCurrentRole ).toBool() )
71 painter->setBrush( QBrush( Qt::lightGray ) );
72 painter->setPen( QColor( Qt::darkGray ) );
73 painter->drawRect( r );
75 if( option.state & QStyle::State_MouseOver )
77 painter->setOpacity( 0.5 );
78 painter->setPen( Qt::NoPen );
79 painter->setBrush( option.palette.color( QPalette::Highlight ).lighter( 150 ) );
80 painter->drawRect( option.rect );
85 QPixmap AbstractPlViewItemDelegate::getArtPixmap( const QModelIndex & index, const QSize & size ) const
87 PLItem *item = static_cast<PLItem*>( index.internalPointer() );
90 QString artUrl = InputManager::decodeArtURL( item->inputItem() );
92 if( artUrl.isEmpty() )
94 for( int i = 0; i < item->childCount(); i++ )
96 artUrl = InputManager::decodeArtURL( item->child( i )->inputItem() );
97 if( !artUrl.isEmpty() )
104 QString key = artUrl + QString("%1%2").arg(size.width()).arg(size.height());
106 if( !QPixmapCache::find( key, artPix ))
108 if( artUrl.isEmpty() || !artPix.load( artUrl ) )
110 key = QString("noart%1%2").arg(size.width()).arg(size.height());
111 if( !QPixmapCache::find( key, artPix ) )
113 artPix = QPixmap( ":/noart" ).scaled( size,
115 Qt::SmoothTransformation );
116 QPixmapCache::insert( key, artPix );
121 artPix = artPix.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
122 QPixmapCache::insert( key, artPix );
129 void PlIconViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
131 QString title = getMeta( index, COLUMN_TITLE );
132 QString artist = getMeta( index, COLUMN_ARTIST );
134 QPixmap artPix = getArtPixmap( index, QSize( ART_SIZE_W, ART_SIZE_H ) );
136 paintBackground( painter, option, index );
140 QRect artRect( option.rect.x() + 5 + ( ART_SIZE_W - artPix.width() ) / 2,
141 option.rect.y() + 5 + ( ART_SIZE_H - artPix.height() ) / 2,
142 artPix.width(), artPix.height() );
144 // Draw the drop shadow
146 painter->setOpacity( 0.7 );
147 painter->setBrush( QBrush( Qt::darkGray ) );
148 painter->setPen( Qt::NoPen );
149 painter->drawRoundedRect( artRect.adjusted( 0, 0, 2, 2 ), ART_RADIUS, ART_RADIUS );
152 // Draw the art pixmap
153 QPainterPath artRectPath;
154 artRectPath.addRoundedRect( artRect, ART_RADIUS, ART_RADIUS );
155 painter->setClipPath( artRectPath );
156 painter->drawPixmap( artRect, artPix );
157 painter->setClipping( false );
159 if( option.state & QStyle::State_Selected )
160 painter->setPen( option.palette.color( QPalette::HighlightedText ) );
162 QFont font( index.data( Qt::FontRole ).value<QFont>() );
163 font.setPointSize( 7 );
165 //Draw children indicator
166 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
168 painter->setOpacity( 0.75 );
169 QRect r( option.rect );
170 r.setSize( QSize( 25, 25 ) );
172 painter->fillRect( r, option.palette.color( QPalette::Mid ) );
173 painter->setOpacity( 1.0 );
174 QPixmap dirPix( ":/type/node" );
175 QRect r2( dirPix.rect() );
176 r2.moveCenter( r.center() );
177 painter->drawPixmap( r2, dirPix );
181 font.setItalic( true );
182 painter->setFont( font );
184 QFontMetrics fm = painter->fontMetrics();
185 QRect textRect = option.rect.adjusted( 1, ART_SIZE_H + 10, 0, -1 );
186 textRect.setHeight( fm.height() );
188 painter->drawText( textRect,
189 fm.elidedText( title, Qt::ElideRight, textRect.width() ),
190 QTextOption( Qt::AlignCenter ) );
193 painter->setPen( painter->pen().color().lighter( 150 ) );
194 font.setItalic( false );
195 painter->setFont( font );
196 fm = painter->fontMetrics();
198 textRect.moveTop( textRect.bottom() + 1 );
200 painter->drawText( textRect,
201 fm.elidedText( artist, Qt::ElideRight, textRect.width() ),
202 QTextOption( Qt::AlignCenter ) );
207 QSize PlIconViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
212 QFontMetrics fm( f );
213 int textHeight = fm.height();
214 QSize sz ( ART_SIZE_W + 2 * SPACER,
215 ART_SIZE_H + 3 * SPACER + 2 * textHeight + 1 );
220 #define LISTVIEW_ART_SIZE 45
222 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
224 QModelIndex parent = index.parent();
227 QString title = getMeta( index, COLUMN_TITLE );
228 QString duration = getMeta( index, COLUMN_DURATION );
229 if( !duration.isEmpty() ) title += QString(" [%1]").arg( duration );
231 QString artist = getMeta( index, COLUMN_ARTIST );
232 QString album = getMeta( index, COLUMN_ALBUM );
233 QString trackNum = getMeta( index, COLUMN_TRACK_NUMBER );
234 QString artistAlbum = artist;
235 if( !album.isEmpty() )
237 if( !artist.isEmpty() ) artistAlbum += ": ";
238 artistAlbum += album;
239 if( !trackNum.isEmpty() ) artistAlbum += QString( " [#%1]" ).arg( trackNum );
242 QPixmap artPix = getArtPixmap( index, QSize( LISTVIEW_ART_SIZE, LISTVIEW_ART_SIZE ) );
244 //Draw selection rectangle and current playing item indication
245 paintBackground( painter, option, index );
247 QRect artRect( artPix.rect() );
248 artRect.moveCenter( QPoint( artRect.center().x() + 3,
249 option.rect.center().y() ) );
251 painter->drawPixmap( artRect, artPix );
256 if( option.state & QStyle::State_Selected )
257 painter->setPen( option.palette.color( QPalette::HighlightedText ) );
259 QTextOption textOpt( Qt::AlignVCenter | Qt::AlignLeft );
260 textOpt.setWrapMode( QTextOption::NoWrap );
262 QFont f( index.data( Qt::FontRole ).value<QFont>() );
266 painter->setFont( f );
267 QFontMetrics fm( painter->fontMetrics() );
269 QRect textRect = option.rect.adjusted( LISTVIEW_ART_SIZE + 10, 0, -10, 0 );
270 if( !artistAlbum.isEmpty() )
272 textRect.setHeight( fm.height() );
273 textRect.moveBottom( option.rect.center().y() - 2 );
276 //Draw children indicator
277 if( !index.data( PLModel::IsLeafNodeRole ).toBool() )
279 QPixmap dirPix = QPixmap( ":/type/node" );
280 painter->drawPixmap( QPoint( textRect.x(), textRect.center().y() - dirPix.height() / 2 ),
282 textRect.setLeft( textRect.x() + dirPix.width() + 5 );
285 painter->drawText( textRect,
286 fm.elidedText( title, Qt::ElideRight, textRect.width() ),
289 // Draw artist and album info
290 if( !artistAlbum.isEmpty() )
292 f.setItalic( false );
293 painter->setFont( f );
294 fm = painter->fontMetrics();
296 textRect.moveTop( textRect.bottom() + 4 );
297 textRect.setLeft( textRect.x() + 20 );
299 painter->drawText( textRect,
300 fm.elidedText( artistAlbum, Qt::ElideRight, textRect.width() ),
307 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
311 QFontMetrics fm( f );
312 int height = qMax( LISTVIEW_ART_SIZE, 2 * fm.height() + 4 ) + 6;
313 return QSize( 0, height );
316 static void plViewStartDrag( QAbstractItemView *view, const Qt::DropActions & supportedActions )
318 QDrag *drag = new QDrag( view );
319 drag->setPixmap( QPixmap( ":/noart64" ) );
320 drag->setMimeData( view->model()->mimeData(
321 view->selectionModel()->selectedIndexes() ) );
322 drag->exec( supportedActions );
325 static void plViewDragMoveEvent( QAbstractItemView *view, QDragMoveEvent * event )
327 if( event->keyboardModifiers() & Qt::ControlModifier &&
328 event->possibleActions() & Qt::CopyAction )
329 event->setDropAction( Qt::CopyAction );
330 else event->acceptProposedAction();
333 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
335 PlIconViewItemDelegate *delegate = new PlIconViewItemDelegate( this );
338 setViewMode( QListView::IconMode );
339 setMovement( QListView::Static );
340 setResizeMode( QListView::Adjust );
341 setGridSize( delegate->sizeHint() );
343 setUniformItemSizes( true );
344 setSelectionMode( QAbstractItemView::ExtendedSelection );
345 setDragEnabled(true);
346 /* dropping in QListView::IconMode does not seem to work */
347 //setAcceptDrops( true );
348 //setDropIndicatorShown(true);
350 setItemDelegate( delegate );
353 void PlIconView::startDrag ( Qt::DropActions supportedActions )
355 plViewStartDrag( this, supportedActions );
358 void PlIconView::dragMoveEvent ( QDragMoveEvent * event )
360 plViewDragMoveEvent( this, event );
361 QAbstractItemView::dragMoveEvent( event );
364 PlListView::PlListView( PLModel *model, QWidget *parent ) : QListView( parent )
367 setViewMode( QListView::ListMode );
368 setUniformItemSizes( true );
369 setSelectionMode( QAbstractItemView::ExtendedSelection );
370 setAlternatingRowColors( true );
371 setDragEnabled(true);
372 setAcceptDrops( true );
373 setDropIndicatorShown(true);
375 PlListViewItemDelegate *delegate = new PlListViewItemDelegate( this );
376 setItemDelegate( delegate );
379 void PlListView::startDrag ( Qt::DropActions supportedActions )
381 plViewStartDrag( this, supportedActions );
384 void PlListView::dragMoveEvent ( QDragMoveEvent * event )
386 plViewDragMoveEvent( this, event );
387 QAbstractItemView::dragMoveEvent( event );
390 void PlTreeView::startDrag ( Qt::DropActions supportedActions )
392 plViewStartDrag( this, supportedActions );
395 void PlTreeView::dragMoveEvent ( QDragMoveEvent * event )
397 plViewDragMoveEvent( this, event );
398 QAbstractItemView::dragMoveEvent( event );