1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-20 07:49:42 +01:00

* Add filter capabilities to TreeModel / -View.

This commit is contained in:
Christian Muehlhaeuser 2011-09-05 04:50:22 +02:00
parent b32ac0cf04
commit c4a450e5de
10 changed files with 56 additions and 13 deletions

@ -29,7 +29,7 @@ DatabaseCommand_AllArtists::exec( DatabaseImpl* dbi )
{
TomahawkSqlQuery query = dbi->newquery();
QList<Tomahawk::artist_ptr> al;
QString orderToken, sourceToken;
QString orderToken, sourceToken, filterToken, tables;
switch ( m_sortOrder )
{
@ -41,20 +41,33 @@ DatabaseCommand_AllArtists::exec( DatabaseImpl* dbi )
}
if ( !m_collection.isNull() )
sourceToken = QString( "AND file.source %1 " ).arg( m_collection->source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) );
sourceToken = QString( "AND file.source %1" ).arg( m_collection->source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) );
if ( !m_filter.isEmpty() )
{
filterToken = QString( "AND file_join.track = track.id AND ( artist.name LIKE :filter OR track.name LIKE :filter )" );
tables = "artist, track, file, file_join";
}
else
tables = "artist, file, file_join";
QString sql = QString(
"SELECT DISTINCT artist.id, artist.name "
"FROM artist, file, file_join "
"FROM %1 "
"WHERE file.id = file_join.file "
"AND file_join.artist = artist.id "
"%1 %2 %3 %4"
).arg( sourceToken )
"%2 %3 %4 %5 %6"
).arg( tables )
.arg( sourceToken )
.arg( filterToken )
.arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
.arg( m_sortDescending ? "DESC" : QString() )
.arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
query.prepare( sql );
if ( !m_filter.isEmpty() )
query.bindValue( ":filter", QString( "%%1%" ).arg( m_filter ) );
query.exec();
while( query.next() )

@ -54,6 +54,7 @@ public:
void setLimit( unsigned int amount ) { m_amount = amount; }
void setSortOrder( DatabaseCommand_AllArtists::SortOrder order ) { m_sortOrder = order; }
void setSortDescending( bool descending ) { m_sortDescending = descending; }
void setFilter( const QString& filter ) { m_filter = filter; }
signals:
void artists( const QList<Tomahawk::artist_ptr>& );
@ -64,6 +65,7 @@ private:
unsigned int m_amount;
DatabaseCommand_AllArtists::SortOrder m_sortOrder;
bool m_sortDescending;
QString m_filter;
};
#endif // DATABASECOMMAND_ALLARTISTS_H

@ -203,6 +203,15 @@ ArtistView::onFilterChanged( const QString& )
{
if ( selectedIndexes().count() )
scrollTo( selectedIndexes().at( 0 ), QAbstractItemView::PositionAtCenter );
if ( !proxyModel()->filter().isEmpty() && !proxyModel()->trackCount() && model()->trackCount() )
{
/* m_overlay->setText( tr( "Sorry, your filter '%1' did not match any results." ).arg( proxyModel()->filter() ) );
m_overlay->show();*/
}
/* else
if ( model()->trackCount() )
m_overlay->hide();*/
}

@ -60,7 +60,7 @@ public:
virtual QPixmap pixmap() const { return QPixmap( RESPATH "images/music-icon.png" ); }
virtual bool showStatsBar() const { return false; }
virtual bool showFilter() const { return false; }
virtual bool showFilter() const { return true; }
virtual void setShowModes( bool b ) { m_showModes = b; }
virtual bool showModes() const { return m_showModes; }

@ -65,7 +65,6 @@ TrackProxyModel::setSourceTrackModel( TrackModel* sourceModel )
void
TrackProxyModel::setFilter( const QString& pattern )
{
qDebug() << Q_FUNC_INFO;
PlaylistInterface::setFilter( pattern );
setFilterRegExp( pattern );

@ -364,8 +364,7 @@ TrackView::onFilterChanged( const QString& )
if ( selectedIndexes().count() )
scrollTo( selectedIndexes().at( 0 ), QAbstractItemView::PositionAtCenter );
if ( !proxyModel()->filter().isEmpty() && !proxyModel()->trackCount() &&
model()->trackCount() )
if ( !proxyModel()->filter().isEmpty() && !proxyModel()->trackCount() && model()->trackCount() )
{
m_overlay->setText( tr( "Sorry, your filter '%1' did not match any results." ).arg( proxyModel()->filter() ) );
m_overlay->show();

@ -528,6 +528,7 @@ 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> ) ) );
@ -601,6 +602,7 @@ 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> ) ) );
@ -783,6 +785,19 @@ 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 )
{

@ -99,6 +99,9 @@ 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() )
@ -153,6 +156,7 @@ private:
Tomahawk::collection_ptr m_collection;
QHash<qlonglong, QPersistentModelIndex> m_coverHash;
QString m_filter;
};
#endif // ALBUMMODEL_H

@ -63,10 +63,12 @@ TreeProxyModel::setSourceTreeModel( TreeModel* sourceModel )
void
TreeProxyModel::setFilter( const QString& pattern )
{
qDebug() << Q_FUNC_INFO;
PlaylistInterface::setFilter( pattern );
setFilterRegExp( pattern );
m_model->setFilter( pattern );
emit filterChanged( pattern );
emit trackCountChanged( trackCount() );
}
@ -110,8 +112,7 @@ TreeProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent
m_cache.insertMulti( sourceParent, pi->result() );
}
if ( filterRegExp().isEmpty() )
return true;
return true;
QStringList sl = filterRegExp().pattern().split( " ", QString::SkipEmptyParts );
bool found = true;
@ -241,7 +242,7 @@ TreeProxyModel::textForItem( TreeModelItem* item ) const
{
if ( !item )
return QString();
if ( !item->artist().isNull() )
{
return item->artist()->name();

@ -54,6 +54,7 @@ public:
virtual Tomahawk::result_ptr siblingItem( int direction );
virtual Tomahawk::result_ptr siblingItem( int direction, bool readOnly );
virtual QString filter() const { return filterRegExp().pattern(); }
virtual void setFilter( const QString& pattern );
virtual PlaylistInterface::RepeatMode repeatMode() const { return m_repeatMode; }