From 3823774dca1c3ce1c3a36ad2456c8d14f58c789f Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Sun, 23 Dec 2012 08:12:06 +0100
Subject: [PATCH] * Change PlayableProxyModelPlaylistInterface's currentIndex
 when the underlying model's has changed.

---
 .../PlayableProxyModelPlaylistInterface.cpp   | 31 +++++++++++++------
 .../PlayableProxyModelPlaylistInterface.h     |  5 ++-
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/libtomahawk/playlist/PlayableProxyModelPlaylistInterface.cpp b/src/libtomahawk/playlist/PlayableProxyModelPlaylistInterface.cpp
index 827e02bc7..218e07089 100644
--- a/src/libtomahawk/playlist/PlayableProxyModelPlaylistInterface.cpp
+++ b/src/libtomahawk/playlist/PlayableProxyModelPlaylistInterface.cpp
@@ -39,6 +39,7 @@ PlayableProxyModelPlaylistInterface::PlayableProxyModelPlaylistInterface( Playab
     connect( proxyModel, SIGNAL( indexPlayable( QModelIndex ) ), SLOT( onItemsChanged() ) );
     connect( proxyModel, SIGNAL( filterChanged( QString ) ), SLOT( onItemsChanged() ) );
     connect( proxyModel, SIGNAL( itemCountChanged( unsigned int ) ), SLOT( onItemsChanged() ) );
+    connect( proxyModel, SIGNAL( currentIndexChanged() ), SLOT( onCurrentIndexChanged() ) );
 }
 
 
@@ -83,6 +84,16 @@ PlayableProxyModelPlaylistInterface::tracks() const
 }
 
 
+void
+PlayableProxyModelPlaylistInterface::onCurrentIndexChanged()
+{
+    if ( m_proxyModel.data()->currentIndex().isValid() )
+        setCurrentIndex( (qint64) m_proxyModel.data()->mapToSource( m_proxyModel.data()->currentIndex() ).internalPointer() );
+    else
+        setCurrentIndex( -1 );
+}
+
+
 void
 PlayableProxyModelPlaylistInterface::setCurrentIndex( qint64 index )
 {
@@ -90,12 +101,14 @@ PlayableProxyModelPlaylistInterface::setCurrentIndex( qint64 index )
     if ( m_proxyModel.isNull() )
         return;
 
-    PlayableItem* item = static_cast<PlayableItem*>( (void*)index );
-    if ( index < 0 || !item )
-    {
-        m_proxyModel.data()->setCurrentIndex( QModelIndex() );
-    }
-    else
+    if ( m_currentIndex == index )
+        return;
+    m_currentIndex = index; // we need to manually set m_currentIndex (protected member from PlaylistInterface) here
+                            // because calling m_proxyModel.data()->setCurrentIndex( ... ) will end up emitting a
+                            // signal which leads right back here and would cause an infinite loop.
+
+    PlayableItem* item = reinterpret_cast<PlayableItem*>( (void*)index );
+    if ( index >= 0 && item )
     {
         if ( m_shuffled && m_shuffleHistory.count() > 1 )
         {
@@ -195,7 +208,7 @@ PlayableProxyModelPlaylistInterface::siblingIndex( int itemsAway, qint64 rootInd
                 }
                 else
                 {
-                    PlayableItem* pitem = static_cast<PlayableItem*>( (void*)rootIndex );
+                    PlayableItem* pitem = reinterpret_cast<PlayableItem*>( (void*)rootIndex );
                     if ( !pitem || !pitem->index.isValid() )
                         return -1;
 
@@ -259,7 +272,7 @@ PlayableProxyModelPlaylistInterface::queryAt( qint64 index ) const
     if ( m_proxyModel.isNull() )
         return query_ptr();
 
-    PlayableItem* item = static_cast<PlayableItem*>( (void*)index );
+    PlayableItem* item = reinterpret_cast<PlayableItem*>( (void*)index );
     if ( item && item->query() )
         return item->query();
 
@@ -273,7 +286,7 @@ PlayableProxyModelPlaylistInterface::resultAt( qint64 index ) const
     if ( m_proxyModel.isNull() )
         return result_ptr();
 
-    PlayableItem* item = static_cast<PlayableItem*>( (void*)index );
+    PlayableItem* item = reinterpret_cast<PlayableItem*>( (void*)index );
     if ( item && item->result() )
         return item->result();
 
diff --git a/src/libtomahawk/playlist/PlayableProxyModelPlaylistInterface.h b/src/libtomahawk/playlist/PlayableProxyModelPlaylistInterface.h
index 7b6d50d11..f134d2293 100644
--- a/src/libtomahawk/playlist/PlayableProxyModelPlaylistInterface.h
+++ b/src/libtomahawk/playlist/PlayableProxyModelPlaylistInterface.h
@@ -61,9 +61,8 @@ public slots:
     virtual void setRepeatMode( Tomahawk::PlaylistModes::RepeatMode mode ) { m_repeatMode = mode; emit repeatModeChanged( mode ); }
     virtual void setShuffled( bool enabled ) { m_shuffled = enabled; emit shuffleModeChanged( enabled ); }
 
-signals:
-    void previousTrackAvailable();
-    void nextTrackAvailable();
+private slots:
+    void onCurrentIndexChanged();
 
 protected:
     QWeakPointer< PlayableProxyModel > m_proxyModel;