From 15805aa32ce1dd2653178e9089d7f59a24341f56 Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Mon, 11 Jul 2011 16:52:09 +0200
Subject: [PATCH] * Improved AlbumView and cover fetching in Artist /
 Album-Model.

---
 .../playlist/albumitemdelegate.cpp            | 38 +++++++++++++++----
 src/libtomahawk/playlist/albumitemdelegate.h  |  1 +
 src/libtomahawk/playlist/albummodel.cpp       | 10 +----
 src/libtomahawk/playlist/albummodel.h         |  1 -
 src/libtomahawk/playlist/albumview.cpp        | 16 +++++++-
 src/libtomahawk/playlist/artistview.cpp       |  2 +
 src/libtomahawk/playlist/treeitemdelegate.cpp |  6 ++-
 src/libtomahawk/playlist/treeitemdelegate.h   |  1 +
 src/libtomahawk/playlist/treemodel.cpp        | 12 +-----
 src/libtomahawk/playlist/treemodel.h          |  1 -
 10 files changed, 58 insertions(+), 30 deletions(-)

diff --git a/src/libtomahawk/playlist/albumitemdelegate.cpp b/src/libtomahawk/playlist/albumitemdelegate.cpp
index f8c8acd9a..7871a7bc9 100644
--- a/src/libtomahawk/playlist/albumitemdelegate.cpp
+++ b/src/libtomahawk/playlist/albumitemdelegate.cpp
@@ -38,6 +38,8 @@ AlbumItemDelegate::AlbumItemDelegate( QAbstractItemView* parent, AlbumProxyModel
     , m_model( proxy )
 {
     m_shadowPixmap = QPixmap( RESPATH "images/cover-shadow.png" );
+    m_defaultCover = QPixmap( RESPATH "images/no-album-art-placeholder.png" )
+                     .scaled( QSize( 120, 120 ), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
 }
 
 
@@ -66,22 +68,44 @@ AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
     }
 
     painter->save();
+
 //    painter->setRenderHint( QPainter::Antialiasing );
+//    painter->drawPixmap( option.rect.adjusted( 4, 4, -4, -38 ), m_shadowPixmap );
+
+    QPixmap cover = item->cover.isNull() ? m_defaultCover : item->cover;
+    painter->drawPixmap( option.rect.adjusted( 6, 4, -6, -41 ), cover );
     painter->setPen( opt.palette.color( QPalette::Text ) );
 
-//    painter->drawPixmap( option.rect.adjusted( 4, 4, -4, -38 ), m_shadowPixmap );
-    painter->drawPixmap( option.rect.adjusted( 6, 4, -6, -41 ), item->cover );
-
     QTextOption to;
-    to.setAlignment( Qt::AlignHCenter );
+    to.setWrapMode( QTextOption::NoWrap );
+
+    QString text;
     QFont font = opt.font;
     QFont boldFont = opt.font;
     boldFont.setBold( true );
 
-    painter->drawText( option.rect.adjusted( 0, option.rect.height() - 16, 0, -2 ), item->album()->artist()->name(), to );
+    QRect textRect = option.rect.adjusted( 0, option.rect.height() - 32, 0, -2 );
 
-    painter->setFont( boldFont );
-    painter->drawText( option.rect.adjusted( 0, option.rect.height() - 32, 0, -18 ), item->album()->name(), to );
+    bool oneLiner = ( textRect.height() / 2 < painter->fontMetrics().boundingRect( item->album()->name() ).height() ||
+                      textRect.height() / 2 < painter->fontMetrics().boundingRect( item->album()->artist()->name() ).height() );
+
+    if ( oneLiner )
+    {
+        to.setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
+        text = painter->fontMetrics().elidedText( item->album()->name(), Qt::ElideRight, textRect.width() - 3 );
+        painter->drawText( textRect, text, to );
+    }
+    else
+    {
+        to.setAlignment( Qt::AlignHCenter | Qt::AlignTop );
+        text = painter->fontMetrics().elidedText( item->album()->name(), Qt::ElideRight, textRect.width() - 3 );
+        painter->drawText( textRect, text, to );
+
+        painter->setFont( boldFont );
+        to.setAlignment( Qt::AlignHCenter | Qt::AlignBottom );
+        text = painter->fontMetrics().elidedText( item->album()->artist()->name(), Qt::ElideRight, textRect.width() - 3 );
+        painter->drawText( textRect, text, to );
+    }
 
     painter->restore();
 }
diff --git a/src/libtomahawk/playlist/albumitemdelegate.h b/src/libtomahawk/playlist/albumitemdelegate.h
index 7ef608c69..caaef3a84 100644
--- a/src/libtomahawk/playlist/albumitemdelegate.h
+++ b/src/libtomahawk/playlist/albumitemdelegate.h
@@ -43,6 +43,7 @@ private:
     AlbumProxyModel* m_model;
 
     QPixmap m_shadowPixmap;
+    QPixmap m_defaultCover;
 };
 
 #endif // ALBUMITEMDELEGATE_H
diff --git a/src/libtomahawk/playlist/albummodel.cpp b/src/libtomahawk/playlist/albummodel.cpp
index 81f559765..562834bf2 100644
--- a/src/libtomahawk/playlist/albummodel.cpp
+++ b/src/libtomahawk/playlist/albummodel.cpp
@@ -27,8 +27,6 @@
 
 #include "utils/tomahawkutils.h"
 
-#define LASTFM_DEFAULT_COVER "http://cdn.last.fm/flatness/catalogue/noimage"
-
 static QString s_tmInfoIdentifier = QString( "ALBUMMODEL" );
 
 using namespace Tomahawk;
@@ -40,9 +38,6 @@ AlbumModel::AlbumModel( QObject* parent )
 {
     qDebug() << Q_FUNC_INFO;
 
-    m_defaultCover = QPixmap( RESPATH "images/no-album-art-placeholder.png" )
-                     .scaled( QSize( 120, 120 ), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
-
     connect( Tomahawk::InfoSystem::InfoSystem::instance(),
              SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ),
                SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ) );
@@ -299,7 +294,6 @@ AlbumModel::onAlbumsAdded( const QList<Tomahawk::album_ptr>& albums )
     foreach( const album_ptr& album, albums )
     {
         albumitem = new AlbumItem( album, m_rootItem );
-        albumitem->cover = m_defaultCover;
         albumitem->index = createIndex( m_rootItem->children.count() - 1, 0, albumitem );
 
         connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
@@ -340,9 +334,7 @@ AlbumModel::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, Q
         qlonglong p = pptr["pptr"].toLongLong( &ok );
         AlbumItem* ai = reinterpret_cast<AlbumItem*>(p);
 
-        if ( pm.isNull() )
-            ai->cover = m_defaultCover;
-        else
+        if ( !pm.isNull() )
             ai->cover = pm;
 
         emit dataChanged( ai->index, ai->index.sibling( ai->index.row(), columnCount( QModelIndex() ) - 1 ) );
diff --git a/src/libtomahawk/playlist/albummodel.h b/src/libtomahawk/playlist/albummodel.h
index 87e1d88f9..9265440e5 100644
--- a/src/libtomahawk/playlist/albummodel.h
+++ b/src/libtomahawk/playlist/albummodel.h
@@ -103,7 +103,6 @@ private slots:
 private:
     QPersistentModelIndex m_currentIndex;
     AlbumItem* m_rootItem;
-    QPixmap m_defaultCover;
 
     QString m_title;
     QString m_description;
diff --git a/src/libtomahawk/playlist/albumview.cpp b/src/libtomahawk/playlist/albumview.cpp
index 31d686985..71a522733 100644
--- a/src/libtomahawk/playlist/albumview.cpp
+++ b/src/libtomahawk/playlist/albumview.cpp
@@ -145,18 +145,32 @@ AlbumView::onScrollTimeout()
     int rowHeight = m_proxyModel->data( QModelIndex(), Qt::SizeHintRole ).toSize().height();
     viewRect.adjust( 0, -rowHeight, 0, rowHeight );
 
+    bool started = false;
+    bool done = false;
     for ( int i = 0; i < m_proxyModel->rowCount(); i++ )
     {
+        if ( started && done )
+            break;
+
         for ( int j = 0; j < m_proxyModel->columnCount(); j++ )
         {
             QModelIndex idx = m_proxyModel->index( i, j );
             if ( !viewRect.contains( visualRect( idx ) ) )
+            {
+                done = true;
                 break;
+            }
+
+            started = true;
+            done = false;
 
             AlbumItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( idx ) );
             if ( !item )
                 break;
+            if ( !item->cover.isNull() )
+                break;
 
+            qDebug() << "Need cover for:" << item->album()->artist()->name() << item->album()->name();
             Tomahawk::InfoSystem::InfoCriteriaHash trackInfo;
             trackInfo["artist"] = item->album()->artist()->name();
             trackInfo["album"] = item->album()->name();
@@ -167,7 +181,7 @@ AlbumView::onScrollTimeout()
             requestData.type = Tomahawk::InfoSystem::InfoAlbumCoverArt;
             requestData.input = QVariant::fromValue< Tomahawk::InfoSystem::InfoCriteriaHash >( trackInfo );
             requestData.customData = QVariantMap();
-            
+
             Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData );
         }
     }
diff --git a/src/libtomahawk/playlist/artistview.cpp b/src/libtomahawk/playlist/artistview.cpp
index 806756dc0..a7cb548c3 100644
--- a/src/libtomahawk/playlist/artistview.cpp
+++ b/src/libtomahawk/playlist/artistview.cpp
@@ -244,6 +244,8 @@ ArtistView::onScrollTimeout()
         TreeModelItem* item = m_model->itemFromIndex( m_proxyModel->mapToSource( m_proxyModel->index( i, 0 ) ) );
         if ( item->artist().isNull() )
             continue;
+        if ( !item->cover.isNull() )
+            continue;
 
         Tomahawk::InfoSystem::InfoCriteriaHash trackInfo;
         trackInfo["artist"] = item->artist()->name();
diff --git a/src/libtomahawk/playlist/treeitemdelegate.cpp b/src/libtomahawk/playlist/treeitemdelegate.cpp
index c42b51ee6..554f83c61 100644
--- a/src/libtomahawk/playlist/treeitemdelegate.cpp
+++ b/src/libtomahawk/playlist/treeitemdelegate.cpp
@@ -40,6 +40,8 @@ TreeItemDelegate::TreeItemDelegate( ArtistView* parent, TreeProxyModel* proxy )
     , m_model( proxy )
 {
     m_nowPlayingIcon = QPixmap( RESPATH "images/now-playing-speaker.png" );
+    m_defaultCover = QPixmap( RESPATH "images/no-album-art-placeholder.png" )
+                     .scaled( QSize( 120, 120 ), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
 }
 
 
@@ -131,7 +133,9 @@ TreeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option,
 
     QRect r = option.rect.adjusted( 4, 4, -option.rect.width() + option.rect.height() - 4, -4 );
 //    painter->drawPixmap( r, QPixmap( RESPATH "images/cover-shadow.png" ) );
-    painter->drawPixmap( r, item->cover );
+
+    QPixmap cover = item->cover.isNull() ? m_defaultCover : item->cover;
+    painter->drawPixmap( r, cover );
 
     QTextOption to;
     to.setAlignment( Qt::AlignVCenter );
diff --git a/src/libtomahawk/playlist/treeitemdelegate.h b/src/libtomahawk/playlist/treeitemdelegate.h
index 5074305e0..d90d20a74 100644
--- a/src/libtomahawk/playlist/treeitemdelegate.h
+++ b/src/libtomahawk/playlist/treeitemdelegate.h
@@ -44,6 +44,7 @@ private:
     TreeProxyModel* m_model;
 
     QPixmap m_nowPlayingIcon;
+    QPixmap m_defaultCover;
 };
 
 #endif // TREEITEMDELEGATE_H
diff --git a/src/libtomahawk/playlist/treemodel.cpp b/src/libtomahawk/playlist/treemodel.cpp
index a0418be5f..1a7ef9e62 100644
--- a/src/libtomahawk/playlist/treemodel.cpp
+++ b/src/libtomahawk/playlist/treemodel.cpp
@@ -40,9 +40,6 @@ TreeModel::TreeModel( QObject* parent )
 {
     qDebug() << Q_FUNC_INFO;
 
-    m_defaultCover = QPixmap( RESPATH "images/no-album-art-placeholder.png" )
-                     .scaled( QSize( 120, 120 ), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
-
     connect( AudioEngine::instance(), SIGNAL( finished( Tomahawk::result_ptr ) ), SLOT( onPlaybackFinished( Tomahawk::result_ptr ) ), Qt::DirectConnection );
     connect( AudioEngine::instance(), SIGNAL( stopped() ), SLOT( onPlaybackStopped() ), Qt::DirectConnection );
 
@@ -496,7 +493,6 @@ TreeModel::onArtistsAdded( const QList<Tomahawk::artist_ptr>& artists )
     foreach( const artist_ptr& artist, artists )
     {
         artistitem = new TreeModelItem( artist, m_rootItem );
-        artistitem->cover = m_defaultCover;
         artistitem->index = createIndex( m_rootItem->children.count() - 1, 0, artistitem );
         connect( artistitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
     }
@@ -533,7 +529,6 @@ TreeModel::onAlbumsAdded( const QList<Tomahawk::album_ptr>& albums, const QVaria
     foreach( const album_ptr& album, albums )
     {
         albumitem = new TreeModelItem( album, parentItem );
-        albumitem->cover = m_defaultCover;
         albumitem->index = createIndex( parentItem->children.count() - 1, 0, albumitem );
         connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
 
@@ -547,7 +542,7 @@ TreeModel::onAlbumsAdded( const QList<Tomahawk::album_ptr>& albums, const QVaria
         requestData.type = Tomahawk::InfoSystem::InfoAlbumCoverArt;
         requestData.input = QVariant::fromValue< Tomahawk::InfoSystem::InfoCriteriaHash >( trackInfo );
         requestData.customData = QVariantMap();
-        
+
         Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData );
     }
 
@@ -587,7 +582,6 @@ TreeModel::onTracksAdded( const QList<Tomahawk::query_ptr>& tracks, const QVaria
     {
         qDebug() << query->toString();
         item = new TreeModelItem( query->results().first(), parentItem );
-        item->cover = m_defaultCover;
         item->index = createIndex( parentItem->children.count() - 1, 0, item );
 
         connect( item, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
@@ -633,9 +627,7 @@ TreeModel::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QV
         qlonglong p = pptr["pptr"].toLongLong( &ok );
         TreeModelItem* ai = reinterpret_cast<TreeModelItem*>(p);
 
-        if ( pm.isNull() )
-            ai->cover = m_defaultCover;
-        else
+        if ( !pm.isNull() )
             ai->cover = pm;
 
         emit dataChanged( ai->index, ai->index.sibling( ai->index.row(), columnCount( QModelIndex() ) - 1 ) );
diff --git a/src/libtomahawk/playlist/treemodel.h b/src/libtomahawk/playlist/treemodel.h
index 8ade79568..13c903572 100644
--- a/src/libtomahawk/playlist/treemodel.h
+++ b/src/libtomahawk/playlist/treemodel.h
@@ -136,7 +136,6 @@ private slots:
 private:
     QPersistentModelIndex m_currentIndex;
     TreeModelItem* m_rootItem;
-    QPixmap m_defaultCover;
 
     QString m_title;
     QString m_description;