]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/components/playlist/icon_view.cpp
Qt: respect font sizes
[vlc] / modules / gui / qt4 / components / playlist / icon_view.cpp
index 702288e363bcfe9285ee0359c833db7141a731c8..9b8d1f183322df734d558044429b0d1af233729b 100644 (file)
 
 #include "assert.h"
 
-#define RECT_SIZE_W         100
-#define RECT_SIZE_H         105
-#define ART_SIZE            64
-#define OFFSET              (RECT_SIZE_W-64)/2
-#define ITEMS_SPACING       10
+#define ART_SIZE_W          110
+#define ART_SIZE_H          80
 #define ART_RADIUS          5
+#define SPACER              5
 
 QString AbstractPlViewItemDelegate::getMeta( const QModelIndex & index, int meta ) const
 {
@@ -76,29 +74,21 @@ QPixmap AbstractPlViewItemDelegate::getArtPixmap( const QModelIndex & index, con
         }
     }
 
-    QPixmap artPix( size );
+    QPixmap artPix;
 
     QString key = artUrl + QString("%1%2").arg(size.width()).arg(size.height());
 
     if( !QPixmapCache::find( key, artPix ))
     {
-        QPixmap tmp;
-        bool cache = false;
-        if( artUrl.isEmpty() || !tmp.load( artUrl ) )
+        if( artUrl.isEmpty() || !artPix.load( artUrl ) )
         {
-            tmp = QPixmap( ":/noart64" ).scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
+            artPix = QPixmap( ":/noart" ).scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
         }
         else
         {
-            tmp = tmp.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
-            cache = true;
+            artPix = artPix.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
+            QPixmapCache::insert( key, artPix );
         }
-        artPix.fill( Qt::black );
-        QPainter p( &artPix );
-        p.drawPixmap( (size.width() - tmp.width()) / 2,
-                      (size.height() - tmp.height()) / 2,
-                      tmp );
-        if( cache ) QPixmapCache::insert( key, artPix );
     }
 
     return artPix;
@@ -109,7 +99,7 @@ void PlIconViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
     QString title = getMeta( index, COLUMN_TITLE );
     QString artist = getMeta( index, COLUMN_ARTIST );
 
-    QPixmap artPix = getArtPixmap( index, QSize( ART_SIZE, ART_SIZE ) );
+    QPixmap artPix = getArtPixmap( index, QSize( ART_SIZE_W, ART_SIZE_H ) );
 
     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option,
                                           painter );
@@ -125,7 +115,9 @@ void PlIconViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
        painter->restore();
     }
 
-    QRect artRect = option.rect.adjusted( OFFSET - 1, 2, - OFFSET, - OFFSET *2 );
+    QRect artRect( option.rect.x() + 5 + ( ART_SIZE_W - artPix.width() ) / 2,
+                   option.rect.y() + 5 + ( ART_SIZE_H - artPix.height() ) / 2,
+                   artPix.width(), artPix.height() );
 
     // Draw the drop shadow
     painter->save();
@@ -145,17 +137,16 @@ void PlIconViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
     if( option.state & QStyle::State_Selected )
         painter->setPen( option.palette.color( QPalette::HighlightedText ) );
 
-    QFont font;
+    QFont font( index.data( Qt::FontRole ).value<QFont>() );
     font.setPointSize( 7 );
-    font.setBold( index.data( Qt::FontRole ).value<QFont>().bold() );
 
     // Draw title
     font.setItalic( true );
     painter->setFont( font );
 
     QFontMetrics fm = painter->fontMetrics();
-    QRect textRect = option.rect.adjusted( 1, ART_SIZE + 8, 0, -1 );
-    textRect.setHeight( fm.height() + 1 );
+    QRect textRect = option.rect.adjusted( 1, ART_SIZE_H + 10, 0, -1 );
+    textRect.setHeight( fm.height() );
 
     painter->drawText( textRect,
                       fm.elidedText( title, Qt::ElideRight, textRect.width() ),
@@ -167,8 +158,8 @@ void PlIconViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
     painter->setFont( font );
     fm = painter->fontMetrics();
 
-    textRect = textRect.adjusted( 0, textRect.height(),
-                                    0, textRect.height() );
+    textRect.moveTop( textRect.bottom() + 1 );
+
     painter->drawText(  textRect,
                         fm.elidedText( artist, Qt::ElideRight, textRect.width() ),
                         QTextOption( Qt::AlignCenter ) );
@@ -178,9 +169,17 @@ void PlIconViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
 
 QSize PlIconViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
 {
-    return QSize( RECT_SIZE_W, RECT_SIZE_H );
+    QFont f;
+    f.setPointSize( 7 );
+    f.setBold( true );
+    QFontMetrics fm( f );
+    int textHeight = fm.height();
+    QSize sz ( ART_SIZE_W + 2 * SPACER,
+               ART_SIZE_H + 3 * SPACER + 2 * textHeight + 1 );
+    return sz;
 }
 
+
 #define LISTVIEW_ART_SIZE 45
 
 void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
@@ -203,22 +202,20 @@ void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
 
     QPixmap artPix = getArtPixmap( index, QSize( LISTVIEW_ART_SIZE, LISTVIEW_ART_SIZE ) );
 
+    //Draw selection rectangle
     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
 
+    //Paint background if item is playing
     if( index.data( PLModel::IsCurrentRole ).toBool() )
         paintPlayingItemBg( painter, option );
 
-    painter->drawPixmap( option.rect.topLeft() + QPoint(3,3), artPix );
-
-
-    int textH = option.fontMetrics.height() + 2;
-    int margin = ( option.rect.height() / 2 ) - textH;
-
-    QRect textRect = option.rect.adjusted( LISTVIEW_ART_SIZE + 10,
-                                           margin,
-                                           -10,
-                                           margin * -1 - ( artistAlbum.isEmpty() ? 0 : textH ) );
+    QRect artRect( artPix.rect() );
+    artRect.moveCenter( QPoint( artRect.center().x() + 3,
+                                option.rect.center().y() ) );
+    //Draw album art
+    painter->drawPixmap( artRect, artPix );
 
+    //Start drawing text
     painter->save();
 
     if( option.state & QStyle::State_Selected )
@@ -227,41 +224,67 @@ void PlListViewItemDelegate::paint( QPainter * painter, const QStyleOptionViewIt
     QTextOption textOpt( Qt::AlignVCenter | Qt::AlignLeft );
     textOpt.setWrapMode( QTextOption::NoWrap );
 
-    QFont f( option.font );
-    if( index.data( PLModel::IsCurrentRole ).toBool() ) f.setBold( true );
+    QFont f( index.data( Qt::FontRole ).value<QFont>() );
 
+    //Draw title info
     f.setItalic( true );
     painter->setFont( f );
+    QFontMetrics fm( painter->fontMetrics() );
 
-    painter->drawText( textRect, title, textOpt );
+    QRect textRect = option.rect.adjusted( LISTVIEW_ART_SIZE + 10, 0, -10, 0 );
+    if( !artistAlbum.isEmpty() )
+    {
+        textRect.setHeight( fm.height() );
+        textRect.moveBottom( option.rect.center().y() - 1 );
+    }
 
-    f.setItalic( false );
-    painter->setFont( f );
-    textRect.moveTop( textRect.top() + textH );
+    painter->drawText( textRect,
+                       fm.elidedText( title, Qt::ElideRight, textRect.width() ),
+                       textOpt );
 
-    painter->drawText( textRect, artistAlbum, textOpt );
+    // Draw artist and album info
+    if( !artistAlbum.isEmpty() )
+    {
+        f.setItalic( false );
+        painter->setFont( f );
+        fm = painter->fontMetrics();
+
+        textRect.moveTop( textRect.bottom() + 2 );
+
+        painter->drawText( textRect,
+                           fm.elidedText( artistAlbum, Qt::ElideRight, textRect.width() ),
+                           textOpt );
+    }
 
     painter->restore();
 }
 
 QSize PlListViewItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
 {
-  return QSize( LISTVIEW_ART_SIZE + 6, LISTVIEW_ART_SIZE + 6 );
+  QFont f;
+  f.setBold( true );
+  QFontMetrics fm( f );
+  int height = qMax( LISTVIEW_ART_SIZE, 2 * fm.height() + 2 ) + 6;
+  return QSize( 0, height );
 }
 
 PlIconView::PlIconView( PLModel *model, QWidget *parent ) : QListView( parent )
 {
+    PlIconViewItemDelegate *delegate = new PlIconViewItemDelegate( this );
+
     setModel( model );
     setViewMode( QListView::IconMode );
     setMovement( QListView::Static );
     setResizeMode( QListView::Adjust );
-    setGridSize( QSize( RECT_SIZE_W, RECT_SIZE_H ) );
+    setGridSize( delegate->sizeHint() );
     setWrapping( true );
     setUniformItemSizes( true );
     setSelectionMode( QAbstractItemView::ExtendedSelection );
-    setAcceptDrops( true );
+    setDragEnabled(true);
+    /* dropping in QListView::IconMode does not seem to work */
+    //setAcceptDrops( true );
+    //setDropIndicatorShown(true);
 
-    PlIconViewItemDelegate *delegate = new PlIconViewItemDelegate( this );
     setItemDelegate( delegate );
 }
 
@@ -271,8 +294,10 @@ PlListView::PlListView( PLModel *model, QWidget *parent ) : QListView( parent )
     setViewMode( QListView::ListMode );
     setUniformItemSizes( true );
     setSelectionMode( QAbstractItemView::ExtendedSelection );
-    setAcceptDrops( true );
     setAlternatingRowColors( true );
+    setDragEnabled(true);
+    setAcceptDrops( true );
+    setDropIndicatorShown(true);
 
     PlListViewItemDelegate *delegate = new PlListViewItemDelegate( this );
     setItemDelegate( delegate );