Skip to content

Commit

Permalink
* Move TreeModel filtering to TreeProxyModel.
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Sep 5, 2011
1 parent 71117d3 commit 9ef5894
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/libtomahawk/database/databasecommand_allartists.cpp
Expand Up @@ -82,7 +82,6 @@ DatabaseCommand_AllArtists::exec( DatabaseImpl* dbi )
al << artist;
}

if ( al.count() )
emit artists( al );
emit artists( al );
emit done();
}
18 changes: 1 addition & 17 deletions src/libtomahawk/playlist/treemodel.cpp
Expand Up @@ -528,7 +528,6 @@ TreeModel::addAllCollections()

emit loadingStarted();
DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists();
cmd->setFilter( m_filter );

connect( cmd, SIGNAL( artists( QList<Tomahawk::artist_ptr> ) ),
SLOT( onArtistsAdded( QList<Tomahawk::artist_ptr> ) ) );
Expand Down Expand Up @@ -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();
Expand All @@ -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<Tomahawk::artist_ptr> ) ),
SLOT( onArtistsAdded( QList<Tomahawk::artist_ptr> ) ) );
Expand Down Expand Up @@ -644,6 +641,7 @@ TreeModel::addFilteredCollection( const collection_ptr& collection, unsigned int
void
TreeModel::onArtistsAdded( const QList<Tomahawk::artist_ptr>& artists )
{
emit loadingFinished();
if ( !artists.count() )
return;

Expand All @@ -663,7 +661,6 @@ TreeModel::onArtistsAdded( const QList<Tomahawk::artist_ptr>& artists )
}

emit endInsertRows();
emit loadingFinished();
}


Expand Down Expand Up @@ -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 )
{
Expand Down
8 changes: 4 additions & 4 deletions src/libtomahawk/playlist/treemodel.h
Expand Up @@ -81,6 +81,8 @@ Q_OBJECT

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 );
Expand All @@ -99,9 +101,6 @@ Q_OBJECT
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() )
Expand Down Expand Up @@ -154,9 +153,10 @@ private slots:
QString m_description;
ColumnStyle m_columnStyle;

QList<Tomahawk::artist_ptr> m_artistsFilter;

Tomahawk::collection_ptr m_collection;
QHash<qlonglong, QPersistentModelIndex> m_coverHash;
QString m_filter;
};

#endif // ALBUMMODEL_H
29 changes: 25 additions & 4 deletions src/libtomahawk/playlist/treeproxymodel.cpp
Expand Up @@ -21,6 +21,7 @@
#include <QListView>

#include "query.h"
#include "database/database.h"
#include "utils/logger.h"


Expand Down Expand Up @@ -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<Tomahawk::artist_ptr> ) ),
SLOT( onFilterArtists( QList<Tomahawk::artist_ptr> ) ) );

Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
}


void
TreeProxyModel::onFilterArtists( const QList<Tomahawk::artist_ptr>& artists )
{
m_artistsFilter = artists;

PlaylistInterface::setFilter( m_filter );
setFilterRegExp( m_filter );

emit filterChanged( m_filter );
emit trackCountChanged( trackCount() );
}

Expand Down Expand Up @@ -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 );
Expand Down
6 changes: 6 additions & 0 deletions src/libtomahawk/playlist/treeproxymodel.h
Expand Up @@ -82,11 +82,17 @@ public slots:
bool filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const;
bool lessThan( const QModelIndex& left, const QModelIndex& right ) const;

private slots:
void onFilterArtists( const QList<Tomahawk::artist_ptr>& artists );

private:
QString textForItem( TreeModelItem* item ) const;

mutable QMap< QPersistentModelIndex, Tomahawk::result_ptr > m_cache;

QList<Tomahawk::artist_ptr> m_artistsFilter;
QString m_filter;

TreeModel* m_model;
RepeatMode m_repeatMode;
bool m_shuffled;
Expand Down

0 comments on commit 9ef5894

Please sign in to comment.