From 04e813238411e469065ba651a00fd40efc3aeffb Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Wed, 9 Jan 2013 09:06:04 +0100
Subject: [PATCH] * Speed up item detection & size-hint calculation for huge
 collections.

---
 src/libtomahawk/Typedefs.h                    |  8 +++++
 src/libtomahawk/playlist/PlayableModel.cpp    | 31 +++++++++++++++----
 src/libtomahawk/playlist/PlayableProxyModel.h |  2 +-
 src/libtomahawk/playlist/TreeItemDelegate.cpp | 16 +++++++---
 4 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/src/libtomahawk/Typedefs.h b/src/libtomahawk/Typedefs.h
index 3d5f22785..fc0dc5c6b 100644
--- a/src/libtomahawk/Typedefs.h
+++ b/src/libtomahawk/Typedefs.h
@@ -78,6 +78,14 @@ namespace Tomahawk
         InfoSystemMode,
     };
 
+    enum ModelTypes
+    {
+        TypeArtist = 0,
+        TypeAlbum,
+        TypeQuery,
+        TypeResult
+    };
+
     class ExternalResolver;
     typedef boost::function<Tomahawk::ExternalResolver*(QString)> ResolverFactoryFunc;
 
diff --git a/src/libtomahawk/playlist/PlayableModel.cpp b/src/libtomahawk/playlist/PlayableModel.cpp
index 65e82ce24..4ac5cbcb2 100644
--- a/src/libtomahawk/playlist/PlayableModel.cpp
+++ b/src/libtomahawk/playlist/PlayableModel.cpp
@@ -24,14 +24,15 @@
 #include <QMimeData>
 #include <QTreeView>
 
-#include "audio/AudioEngine.h"
-#include "utils/TomahawkUtils.h"
-#include "Source.h"
-
 #include "Artist.h"
 #include "Album.h"
 #include "Pipeline.h"
 #include "PlayableItem.h"
+#include "PlayableProxyModel.h"
+#include "Source.h"
+#include "Typedefs.h"
+#include "audio/AudioEngine.h"
+#include "utils/TomahawkUtils.h"
 #include "utils/Logger.h"
 
 using namespace Tomahawk;
@@ -274,11 +275,29 @@ PlayableModel::data( const QModelIndex& index, int role ) const
     {
         return QVariant();
     }
-
-    if ( role == Qt::TextAlignmentRole )
+    else if ( role == Qt::TextAlignmentRole )
     {
         return QVariant( columnAlignment( index.column() ) );
     }
+    else if ( role == PlayableProxyModel::TypeRole )
+    {
+        if ( entry->result() )
+        {
+            return Tomahawk::TypeResult;
+        }
+        else if ( entry->query() )
+        {
+            return Tomahawk::TypeQuery;
+        }
+        else if ( entry->artist() )
+        {
+            return Tomahawk::TypeArtist;
+        }
+        else if ( entry->album() )
+        {
+            return Tomahawk::TypeAlbum;
+        }
+    }
 
     if ( !entry->query().isNull() )
     {
diff --git a/src/libtomahawk/playlist/PlayableProxyModel.h b/src/libtomahawk/playlist/PlayableProxyModel.h
index bb8b8924a..c3a0d7be0 100644
--- a/src/libtomahawk/playlist/PlayableProxyModel.h
+++ b/src/libtomahawk/playlist/PlayableProxyModel.h
@@ -36,7 +36,7 @@ public:
     { Detailed = 0, Short = 1, ShortWithAvatars = 2, Large = 3, Collection = 4 };
 
     enum PlayableProxyModelRole
-    { StyleRole = Qt::UserRole + 1 };
+    { StyleRole = Qt::UserRole + 1, TypeRole };
 
     explicit PlayableProxyModel ( QObject* parent = 0 );
     virtual ~PlayableProxyModel() {}
diff --git a/src/libtomahawk/playlist/TreeItemDelegate.cpp b/src/libtomahawk/playlist/TreeItemDelegate.cpp
index 38db070b9..7b66cf664 100644
--- a/src/libtomahawk/playlist/TreeItemDelegate.cpp
+++ b/src/libtomahawk/playlist/TreeItemDelegate.cpp
@@ -35,6 +35,7 @@
 #include "PlayableItem.h"
 #include "TreeProxyModel.h"
 #include "TreeView.h"
+#include "Typedefs.h"
 
 
 TreeItemDelegate::TreeItemDelegate( TreeView* parent, TreeProxyModel* proxy )
@@ -48,23 +49,28 @@ TreeItemDelegate::TreeItemDelegate( TreeView* parent, TreeProxyModel* proxy )
 QSize
 TreeItemDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
 {
-    QSize size = QStyledItemDelegate::sizeHint( option, index );
+    QSize size;
 
     if ( index.isValid() )
     {
-        PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
-        if ( item )
+        Tomahawk::ModelTypes type = (Tomahawk::ModelTypes)index.data( PlayableProxyModel::TypeRole ).toInt();
+        switch ( type )
         {
-            if ( item->album() )
+            case Tomahawk::TypeAlbum:
             {
                 size.setHeight( option.fontMetrics.height() * 3 );
                 return size;
             }
-            else if ( item->query() || item->result() )
+
+            case Tomahawk::TypeQuery:
+            case Tomahawk::TypeResult:
             {
                 size.setHeight( option.fontMetrics.height() * 1.6 );
                 return size;
             }
+
+            default:
+                break;
         }
     }