From 4a97e057be224b0cf9f64ea27d642623fc992db1 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 27 Jun 2012 05:22:01 +0200 Subject: [PATCH] * Properly sort artists & albums in search widget. --- src/libtomahawk/widgets/SearchWidget.cpp | 71 ++++++++++++++++++++---- src/libtomahawk/widgets/SearchWidget.h | 6 ++ 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/libtomahawk/widgets/SearchWidget.cpp b/src/libtomahawk/widgets/SearchWidget.cpp index a38b1fb43..29af944c7 100644 --- a/src/libtomahawk/widgets/SearchWidget.cpp +++ b/src/libtomahawk/widgets/SearchWidget.cpp @@ -113,9 +113,6 @@ SearchWidget::jumpToCurrentTrack() void SearchWidget::onResultsFound( const QList& results ) { - QList artists; - QList albums; - foreach( const Tomahawk::result_ptr& result, results ) { if ( !result->collection().isNull() && !result->isOnline() ) @@ -125,36 +122,88 @@ SearchWidget::onResultsFound( const QList& results ) rl << result; Tomahawk::query_ptr q = result->toQuery(); - q->setResolveFinished( true ); q->addResults( rl ); m_resultsModel->append( q ); - artists << result->artist(); - albums << result->album(); + m_artists << result->artist(); + m_albums << result->album(); } - - m_artistsModel->append( artists ); - m_albumsModel->append( albums ); } void SearchWidget::onAlbumsFound( const QList& albums ) { - m_albumsModel->append( albums ); + m_albums << albums; } void SearchWidget::onArtistsFound( const QList& artists ) { - m_artistsModel->append( artists ); + m_artists << artists; } void SearchWidget::onQueryFinished() { + sortAlbums(); + sortArtists(); m_resultsModel->finishLoading(); } + + +void +SearchWidget::sortArtists() +{ + QMap< float, Tomahawk::artist_ptr > ars; + QList< Tomahawk::artist_ptr > sortedArtists; + foreach ( const Tomahawk::artist_ptr& artist, m_artists ) + { + int distance = TomahawkUtils::levenshtein( m_search, artist->name() ); + int maxlen = qMax( m_search.length(), artist->name().length() ); + float score = (float)( maxlen - distance ) / maxlen; + + ars.insert( score, artist ); + } + + QList< float > floats = ars.keys(); + qSort( floats.begin(), floats.end() ); + + for ( int i = floats.count() - 1; i >= 0; i-- ) + { + sortedArtists << ars.value( floats.at( i ) ); + } + + m_artistsModel->clear(); + m_artistsModel->append( sortedArtists ); +} + + +void +SearchWidget::sortAlbums() +{ + QMap< float, Tomahawk::album_ptr > ars; + QList< Tomahawk::album_ptr > sortedAlbums; + foreach ( const Tomahawk::album_ptr& album, m_albums ) + { + int distance = TomahawkUtils::levenshtein( m_search, album->name() ); + int maxlen = qMax( m_search.length(), album->name().length() ); + float score = (float)( maxlen - distance ) / maxlen; + + ars.insert( score, album ); + } + + QList< float > floats = ars.keys(); + qSort( floats.begin(), floats.end() ); + + for ( int i = floats.count() - 1; i >= 0; i-- ) + { + sortedAlbums << ars.value( floats.at( i ) ); + } + + m_albumsModel->clear(); + m_albumsModel->append( sortedAlbums ); +} diff --git a/src/libtomahawk/widgets/SearchWidget.h b/src/libtomahawk/widgets/SearchWidget.h index 70f942bfe..3a1784427 100644 --- a/src/libtomahawk/widgets/SearchWidget.h +++ b/src/libtomahawk/widgets/SearchWidget.h @@ -73,6 +73,9 @@ private slots: void onQueryFinished(); private: + void sortArtists(); + void sortAlbums(); + Ui::SearchWidget *ui; QString m_search; @@ -80,7 +83,10 @@ private: PlayableModel* m_artistsModel; PlayableModel* m_albumsModel; PlaylistModel* m_resultsModel; + QList< Tomahawk::query_ptr > m_queries; + QList< Tomahawk::artist_ptr > m_artists; + QList< Tomahawk::album_ptr > m_albums; }; #endif // NEWPLAYLISTWIDGET_H