diff --git a/src/libtomahawk/playlist/PlayableProxyModel.cpp b/src/libtomahawk/playlist/PlayableProxyModel.cpp index a7a4364ac..58dd544d0 100644 --- a/src/libtomahawk/playlist/PlayableProxyModel.cpp +++ b/src/libtomahawk/playlist/PlayableProxyModel.cpp @@ -144,6 +144,68 @@ PlayableProxyModel::setSourcePlayableModel( PlayableModel* sourceModel ) bool PlayableProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const +{ + bool dupeFilter = true; + bool visibilityFilter = true; + + if ( m_hideDupeItems ) + dupeFilter = dupeFilterAcceptsRow( sourceRow, sourceParent ); + if ( m_maxVisibleItems > 0 ) + visibilityFilter = visibilityFilterAcceptsRow( sourceRow, sourceParent ); + + return ( dupeFilter && visibilityFilter && nameFilterAcceptsRow( sourceRow, sourceParent ) ); +} + + +bool +PlayableProxyModel::dupeFilterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const +{ + if ( !m_hideDupeItems ) + return true; + + PlayableItem* pi = itemFromIndex( sourceModel()->index( sourceRow, 0, sourceParent ) ); + if ( !pi ) + return false; + + for ( int i = 0; i < sourceRow; i++ ) + { + PlayableItem* di = itemFromIndex( sourceModel()->index( i, 0, sourceParent ) ); + if ( !di ) + continue; + + bool b = ( pi->query() && pi->query()->equals( di->query() ) ) || + ( pi->album() && pi->album() == di->album() ) || + ( pi->artist() && pi->artist()->name() == di->artist()->name() ); + + if ( b && filterAcceptsRow( i, sourceParent ) ) + return false; + } + + return true; +} + + +bool +PlayableProxyModel::visibilityFilterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const +{ + if ( m_maxVisibleItems <= 0 ) + return true; + + int items = 0; + for ( int i = 0; i < sourceRow; i++ ) + { + if ( dupeFilterAcceptsRow( i, sourceParent ) && nameFilterAcceptsRow( i, sourceParent ) ) + { + items++; + } + } + + return ( items < m_maxVisibleItems ); +} + + +bool +PlayableProxyModel::nameFilterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const { PlayableItem* pi = itemFromIndex( sourceModel()->index( sourceRow, 0, sourceParent ) ); if ( !pi ) @@ -157,26 +219,6 @@ PlayableProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourcePa } } - if ( m_maxVisibleItems > 0 && sourceRow > m_maxVisibleItems - 1 ) - return false; - - if ( m_hideDupeItems ) - { - for ( int i = 0; i < sourceRow; i++ ) - { - PlayableItem* di = itemFromIndex( sourceModel()->index( i, 0, sourceParent ) ); - if ( !di ) - continue; - - bool b = ( pi->query() && pi->query()->equals( di->query() ) ) || - ( pi->album() && pi->album() == di->album() ) || - ( pi->artist() && pi->artist()->name() == di->artist()->name() ); - - if ( b && filterAcceptsRow( i, sourceParent ) ) - return false; - } - } - if ( pi->query() ) { Tomahawk::result_ptr r; diff --git a/src/libtomahawk/playlist/PlayableProxyModel.h b/src/libtomahawk/playlist/PlayableProxyModel.h index fb38073b5..c8f57f6f5 100644 --- a/src/libtomahawk/playlist/PlayableProxyModel.h +++ b/src/libtomahawk/playlist/PlayableProxyModel.h @@ -117,7 +117,10 @@ private slots: void onCurrentIndexChanged( const QModelIndex& newIndex, const QModelIndex& oldIndex ); private: - virtual bool lessThan( int column, const Tomahawk::query_ptr& left, const Tomahawk::query_ptr& right ) const; + bool nameFilterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const; + bool dupeFilterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const; + bool visibilityFilterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const; + bool lessThan( int column, const Tomahawk::query_ptr& left, const Tomahawk::query_ptr& right ) const; PlayableModel* m_model;