From 9ef58946857451b801f1d51a7a2e483cd24c7839 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Mon, 5 Sep 2011 06:32:40 +0200 Subject: [PATCH] * Move TreeModel filtering to TreeProxyModel. --- .../database/databasecommand_allartists.cpp | 3 +- src/libtomahawk/playlist/treemodel.cpp | 18 +----------- src/libtomahawk/playlist/treemodel.h | 8 ++--- src/libtomahawk/playlist/treeproxymodel.cpp | 29 ++++++++++++++++--- src/libtomahawk/playlist/treeproxymodel.h | 6 ++++ 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/libtomahawk/database/databasecommand_allartists.cpp b/src/libtomahawk/database/databasecommand_allartists.cpp index 7be9d0138..1e12574bc 100644 --- a/src/libtomahawk/database/databasecommand_allartists.cpp +++ b/src/libtomahawk/database/databasecommand_allartists.cpp @@ -82,7 +82,6 @@ DatabaseCommand_AllArtists::exec( DatabaseImpl* dbi ) al << artist; } - if ( al.count() ) - emit artists( al ); + emit artists( al ); emit done(); } diff --git a/src/libtomahawk/playlist/treemodel.cpp b/src/libtomahawk/playlist/treemodel.cpp index 045065daf..357df0edc 100644 --- a/src/libtomahawk/playlist/treemodel.cpp +++ b/src/libtomahawk/playlist/treemodel.cpp @@ -528,7 +528,6 @@ TreeModel::addAllCollections() emit loadingStarted(); DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists(); - cmd->setFilter( m_filter ); connect( cmd, SIGNAL( artists( QList ) ), SLOT( onArtistsAdded( QList ) ) ); @@ -579,7 +578,6 @@ TreeModel::addTracks( const album_ptr& album, const QModelIndex& parent ) emit loadingStarted(); DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_collection ); cmd->setAlbum( album.data() ); -// cmd->setArtist( album->artist().data() ); QList< QVariant > rows; rows << parent.row(); @@ -602,7 +600,6 @@ TreeModel::addCollection( const collection_ptr& collection ) m_collection = collection; DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists( collection ); - cmd->setFilter( m_filter ); connect( cmd, SIGNAL( artists( QList ) ), SLOT( onArtistsAdded( QList ) ) ); @@ -644,6 +641,7 @@ TreeModel::addFilteredCollection( const collection_ptr& collection, unsigned int void TreeModel::onArtistsAdded( const QList& artists ) { + emit loadingFinished(); if ( !artists.count() ) return; @@ -663,7 +661,6 @@ TreeModel::onArtistsAdded( const QList& artists ) } emit endInsertRows(); - emit loadingFinished(); } @@ -785,19 +782,6 @@ TreeModel::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QV } -void -TreeModel::setFilter( const QString& pattern ) -{ - m_filter = pattern; - clear(); - - if ( !m_collection.isNull() ) - addCollection( m_collection ); - else - addAllCollections(); -} - - void TreeModel::infoSystemFinished( QString target ) { diff --git a/src/libtomahawk/playlist/treemodel.h b/src/libtomahawk/playlist/treemodel.h index 1f08a7bdc..53205e858 100644 --- a/src/libtomahawk/playlist/treemodel.h +++ b/src/libtomahawk/playlist/treemodel.h @@ -81,6 +81,8 @@ public: virtual QPersistentModelIndex currentItem() { return m_currentIndex; } + Tomahawk::collection_ptr collection() const { return m_collection; } + void addAllCollections(); void addCollection( const Tomahawk::collection_ptr& collection ); void addFilteredCollection( const Tomahawk::collection_ptr& collection, unsigned int amount, DatabaseCommand_AllArtists::SortOrder order ); @@ -99,9 +101,6 @@ public: virtual void setTitle( const QString& title ) { m_title = title; } virtual void setDescription( const QString& description ) { m_description = description; } - virtual QString filter() const { return m_filter; } - virtual void setFilter( const QString& pattern ); - TreeModelItem* itemFromIndex( const QModelIndex& index ) const { if ( index.isValid() ) @@ -154,9 +153,10 @@ private: QString m_description; ColumnStyle m_columnStyle; + QList m_artistsFilter; + Tomahawk::collection_ptr m_collection; QHash m_coverHash; - QString m_filter; }; #endif // ALBUMMODEL_H diff --git a/src/libtomahawk/playlist/treeproxymodel.cpp b/src/libtomahawk/playlist/treeproxymodel.cpp index dd9da31da..5f3b739be 100644 --- a/src/libtomahawk/playlist/treeproxymodel.cpp +++ b/src/libtomahawk/playlist/treeproxymodel.cpp @@ -21,6 +21,7 @@ #include #include "query.h" +#include "database/database.h" #include "utils/logger.h" @@ -63,11 +64,27 @@ TreeProxyModel::setSourceTreeModel( TreeModel* sourceModel ) void TreeProxyModel::setFilter( const QString& pattern ) { - PlaylistInterface::setFilter( pattern ); - setFilterRegExp( pattern ); - m_model->setFilter( pattern ); + m_filter = pattern; - emit filterChanged( pattern ); + DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists( m_model->collection() ); + cmd->setFilter( pattern ); + + connect( cmd, SIGNAL( artists( QList ) ), + SLOT( onFilterArtists( QList ) ) ); + + Database::instance()->enqueue( QSharedPointer( cmd ) ); +} + + +void +TreeProxyModel::onFilterArtists( const QList& artists ) +{ + m_artistsFilter = artists; + + PlaylistInterface::setFilter( m_filter ); + setFilterRegExp( m_filter ); + + emit filterChanged( m_filter ); emit trackCountChanged( trackCount() ); } @@ -112,6 +129,10 @@ TreeProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent m_cache.insertMulti( sourceParent, pi->result() ); } + if ( !filterRegExp().isEmpty() && !pi->artist().isNull() ) + { + return m_artistsFilter.contains( pi->artist() ); + } return true; QStringList sl = filterRegExp().pattern().split( " ", QString::SkipEmptyParts ); diff --git a/src/libtomahawk/playlist/treeproxymodel.h b/src/libtomahawk/playlist/treeproxymodel.h index b50ae6268..eba5ae200 100644 --- a/src/libtomahawk/playlist/treeproxymodel.h +++ b/src/libtomahawk/playlist/treeproxymodel.h @@ -82,11 +82,17 @@ protected: bool filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const; bool lessThan( const QModelIndex& left, const QModelIndex& right ) const; +private slots: + void onFilterArtists( const QList& artists ); + private: QString textForItem( TreeModelItem* item ) const; mutable QMap< QPersistentModelIndex, Tomahawk::result_ptr > m_cache; + QList m_artistsFilter; + QString m_filter; + TreeModel* m_model; RepeatMode m_repeatMode; bool m_shuffled;