PageRenderTime 31ms CodeModel.GetById 14ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libtomahawk/widgets/searchwidget.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 215 lines | 149 code | 48 blank | 18 comment | 5 complexity | 4f2f783611ecd0c3f0a37d8e3a66eac0 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2012       Leo Franchi            <lfranchi@kde.org>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#include "SearchWidget.h"
 21#include "ui_SearchWidget.h"
 22
 23#include <QPushButton>
 24#include <QDialogButtonBox>
 25
 26#include "SourceList.h"
 27#include "ViewManager.h"
 28#include "playlist/PlayableModel.h"
 29#include "playlist/PlaylistModel.h"
 30#include "utils/AnimatedSpinner.h"
 31
 32#include "utils/TomahawkUtilsGui.h"
 33#include "utils/Logger.h"
 34
 35
 36SearchWidget::SearchWidget( const QString& search, QWidget* parent )
 37    : QWidget( parent )
 38    , ui( new Ui::SearchWidget )
 39    , m_search( search )
 40{
 41    ui->setupUi( this );
 42
 43    ui->resultsView->setGuid( "searchwidget" );
 44    m_resultsModel = new PlaylistModel( ui->resultsView );
 45    ui->resultsView->setPlaylistModel( m_resultsModel );
 46    ui->resultsView->sortByColumn( PlaylistModel::Score, Qt::DescendingOrder );
 47
 48    m_albumsModel = new PlayableModel( ui->albumView );
 49    ui->albumView->setPlayableModel( m_albumsModel );
 50
 51    m_artistsModel = new PlayableModel( ui->artistView );
 52    ui->artistView->setPlayableModel( m_artistsModel );
 53
 54    ui->artistView->proxyModel()->sort( -1 );
 55    ui->albumView->proxyModel()->sort( -1 );
 56    ui->artistView->proxyModel()->setHideDupeItems( true );
 57    ui->albumView->proxyModel()->setHideDupeItems( true );
 58
 59    TomahawkUtils::unmarginLayout( ui->verticalLayout );
 60
 61    m_artistsModel->startLoading();
 62    m_albumsModel->startLoading();
 63    m_resultsModel->startLoading();
 64    m_queries << Tomahawk::Query::get( search, uuid() );
 65
 66    ui->splitter_2->setStretchFactor( 0, 0 );
 67    ui->splitter_2->setStretchFactor( 1, 1 );
 68
 69    foreach ( const Tomahawk::query_ptr& query, m_queries )
 70    {
 71        connect( query.data(), SIGNAL( artistsAdded( QList<Tomahawk::artist_ptr> ) ), SLOT( onArtistsFound( QList<Tomahawk::artist_ptr> ) ) );
 72        connect( query.data(), SIGNAL( albumsAdded( QList<Tomahawk::album_ptr> ) ), SLOT( onAlbumsFound( QList<Tomahawk::album_ptr> ) ) );
 73        connect( query.data(), SIGNAL( resultsAdded( QList<Tomahawk::result_ptr> ) ), SLOT( onResultsFound( QList<Tomahawk::result_ptr> ) ) );
 74        connect( query.data(), SIGNAL( resolvingFinished( bool ) ), SLOT( onQueryFinished() ) );
 75    }
 76}
 77
 78
 79SearchWidget::~SearchWidget()
 80{
 81    tDebug() << Q_FUNC_INFO;
 82    delete ui;
 83}
 84
 85
 86void
 87SearchWidget::changeEvent( QEvent* e )
 88{
 89    QWidget::changeEvent( e );
 90    switch ( e->type() )
 91    {
 92        case QEvent::LanguageChange:
 93            ui->retranslateUi( this );
 94            break;
 95
 96        default:
 97            break;
 98    }
 99}
100
101
102Tomahawk::playlistinterface_ptr
103SearchWidget::playlistInterface() const
104{
105    return ui->resultsView->playlistInterface();
106}
107
108
109bool
110SearchWidget::jumpToCurrentTrack()
111{
112    return ui->resultsView->jumpToCurrentTrack();
113}
114
115
116void
117SearchWidget::onResultsFound( const QList<Tomahawk::result_ptr>& results )
118{
119    foreach( const Tomahawk::result_ptr& result, results )
120    {
121        if ( !result->collection().isNull() && !result->isOnline() )
122            continue;
123
124        QList< Tomahawk::result_ptr > rl;
125        rl << result;
126
127        Tomahawk::query_ptr q = result->toQuery();
128        q->addResults( rl );
129
130        m_resultsModel->appendQuery( q );
131
132        m_artists << result->artist();
133        m_albums << result->album();
134    }
135}
136
137
138void
139SearchWidget::onAlbumsFound( const QList<Tomahawk::album_ptr>& albums )
140{
141    m_albums << albums;
142}
143
144
145void
146SearchWidget::onArtistsFound( const QList<Tomahawk::artist_ptr>& artists )
147{
148    m_artists << artists;
149}
150
151
152void
153SearchWidget::onQueryFinished()
154{
155    sortAlbums();
156    sortArtists();
157
158    m_artistsModel->finishLoading();
159    m_albumsModel->finishLoading();
160    m_resultsModel->finishLoading();
161}
162
163
164void
165SearchWidget::sortArtists()
166{
167    QMap< float, Tomahawk::artist_ptr > ars;
168    QList< Tomahawk::artist_ptr > sortedArtists;
169    foreach ( const Tomahawk::artist_ptr& artist, m_artists )
170    {
171        int distance = TomahawkUtils::levenshtein( m_search, artist->name() );
172        int maxlen = qMax( m_search.length(), artist->name().length() );
173        float score = (float)( maxlen - distance ) / maxlen;
174
175        ars.insert( score, artist );
176    }
177
178    QList< float > floats = ars.keys();
179    qSort( floats.begin(), floats.end() );
180
181    for ( int i = floats.count() - 1; i >= 0; i-- )
182    {
183        sortedArtists << ars.value( floats.at( i ) );
184    }
185
186    m_artistsModel->clear();
187    m_artistsModel->appendArtists( sortedArtists );
188}
189
190
191void
192SearchWidget::sortAlbums()
193{
194    QMap< float, Tomahawk::album_ptr > ars;
195    QList< Tomahawk::album_ptr > sortedAlbums;
196    foreach ( const Tomahawk::album_ptr& album, m_albums )
197    {
198        int distance = TomahawkUtils::levenshtein( m_search, album->name() );
199        int maxlen = qMax( m_search.length(), album->name().length() );
200        float score = (float)( maxlen - distance ) / maxlen;
201
202        ars.insert( score, album );
203    }
204
205    QList< float > floats = ars.keys();
206    qSort( floats.begin(), floats.end() );
207
208    for ( int i = floats.count() - 1; i >= 0; i-- )
209    {
210        sortedAlbums << ars.value( floats.at( i ) );
211    }
212
213    m_albumsModel->clear();
214    m_albumsModel->appendAlbums( sortedAlbums );
215}