/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. #include "SearchWidget.h"
  20. #include "ui_SearchWidget.h"
  21. #include <QPushButton>
  22. #include <QDialogButtonBox>
  23. #include "SourceList.h"
  24. #include "ViewManager.h"
  25. #include "playlist/PlayableModel.h"
  26. #include "playlist/PlaylistModel.h"
  27. #include "utils/AnimatedSpinner.h"
  28. #include "utils/TomahawkUtilsGui.h"
  29. #include "utils/Logger.h"
  30. SearchWidget::SearchWidget( const QString& search, QWidget* parent )
  31. : QWidget( parent )
  32. , ui( new Ui::SearchWidget )
  33. , m_search( search )
  34. {
  35. ui->setupUi( this );
  36. ui->resultsView->setGuid( "searchwidget" );
  37. m_resultsModel = new PlaylistModel( ui->resultsView );
  38. ui->resultsView->setPlaylistModel( m_resultsModel );
  39. ui->resultsView->sortByColumn( PlaylistModel::Score, Qt::DescendingOrder );
  40. m_albumsModel = new PlayableModel( ui->albumView );
  41. ui->albumView->setPlayableModel( m_albumsModel );
  42. m_artistsModel = new PlayableModel( ui->artistView );
  43. ui->artistView->setPlayableModel( m_artistsModel );
  44. ui->artistView->proxyModel()->sort( -1 );
  45. ui->albumView->proxyModel()->sort( -1 );
  46. ui->artistView->proxyModel()->setHideDupeItems( true );
  47. ui->albumView->proxyModel()->setHideDupeItems( true );
  48. TomahawkUtils::unmarginLayout( ui->verticalLayout );
  49. m_artistsModel->startLoading();
  50. m_albumsModel->startLoading();
  51. m_resultsModel->startLoading();
  52. m_queries << Tomahawk::Query::get( search, uuid() );
  53. ui->splitter_2->setStretchFactor( 0, 0 );
  54. ui->splitter_2->setStretchFactor( 1, 1 );
  55. foreach ( const Tomahawk::query_ptr& query, m_queries )
  56. {
  57. connect( query.data(), SIGNAL( artistsAdded( QList<Tomahawk::artist_ptr> ) ), SLOT( onArtistsFound( QList<Tomahawk::artist_ptr> ) ) );
  58. connect( query.data(), SIGNAL( albumsAdded( QList<Tomahawk::album_ptr> ) ), SLOT( onAlbumsFound( QList<Tomahawk::album_ptr> ) ) );
  59. connect( query.data(), SIGNAL( resultsAdded( QList<Tomahawk::result_ptr> ) ), SLOT( onResultsFound( QList<Tomahawk::result_ptr> ) ) );
  60. connect( query.data(), SIGNAL( resolvingFinished( bool ) ), SLOT( onQueryFinished() ) );
  61. }
  62. }
  63. SearchWidget::~SearchWidget()
  64. {
  65. tDebug() << Q_FUNC_INFO;
  66. delete ui;
  67. }
  68. void
  69. SearchWidget::changeEvent( QEvent* e )
  70. {
  71. QWidget::changeEvent( e );
  72. switch ( e->type() )
  73. {
  74. case QEvent::LanguageChange:
  75. ui->retranslateUi( this );
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. Tomahawk::playlistinterface_ptr
  82. SearchWidget::playlistInterface() const
  83. {
  84. return ui->resultsView->playlistInterface();
  85. }
  86. bool
  87. SearchWidget::jumpToCurrentTrack()
  88. {
  89. return ui->resultsView->jumpToCurrentTrack();
  90. }
  91. void
  92. SearchWidget::onResultsFound( const QList<Tomahawk::result_ptr>& results )
  93. {
  94. foreach( const Tomahawk::result_ptr& result, results )
  95. {
  96. if ( !result->collection().isNull() && !result->isOnline() )
  97. continue;
  98. QList< Tomahawk::result_ptr > rl;
  99. rl << result;
  100. Tomahawk::query_ptr q = result->toQuery();
  101. q->addResults( rl );
  102. m_resultsModel->appendQuery( q );
  103. m_artists << result->artist();
  104. m_albums << result->album();
  105. }
  106. }
  107. void
  108. SearchWidget::onAlbumsFound( const QList<Tomahawk::album_ptr>& albums )
  109. {
  110. m_albums << albums;
  111. }
  112. void
  113. SearchWidget::onArtistsFound( const QList<Tomahawk::artist_ptr>& artists )
  114. {
  115. m_artists << artists;
  116. }
  117. void
  118. SearchWidget::onQueryFinished()
  119. {
  120. sortAlbums();
  121. sortArtists();
  122. m_artistsModel->finishLoading();
  123. m_albumsModel->finishLoading();
  124. m_resultsModel->finishLoading();
  125. }
  126. void
  127. SearchWidget::sortArtists()
  128. {
  129. QMap< float, Tomahawk::artist_ptr > ars;
  130. QList< Tomahawk::artist_ptr > sortedArtists;
  131. foreach ( const Tomahawk::artist_ptr& artist, m_artists )
  132. {
  133. int distance = TomahawkUtils::levenshtein( m_search, artist->name() );
  134. int maxlen = qMax( m_search.length(), artist->name().length() );
  135. float score = (float)( maxlen - distance ) / maxlen;
  136. ars.insert( score, artist );
  137. }
  138. QList< float > floats = ars.keys();
  139. qSort( floats.begin(), floats.end() );
  140. for ( int i = floats.count() - 1; i >= 0; i-- )
  141. {
  142. sortedArtists << ars.value( floats.at( i ) );
  143. }
  144. m_artistsModel->clear();
  145. m_artistsModel->appendArtists( sortedArtists );
  146. }
  147. void
  148. SearchWidget::sortAlbums()
  149. {
  150. QMap< float, Tomahawk::album_ptr > ars;
  151. QList< Tomahawk::album_ptr > sortedAlbums;
  152. foreach ( const Tomahawk::album_ptr& album, m_albums )
  153. {
  154. int distance = TomahawkUtils::levenshtein( m_search, album->name() );
  155. int maxlen = qMax( m_search.length(), album->name().length() );
  156. float score = (float)( maxlen - distance ) / maxlen;
  157. ars.insert( score, album );
  158. }
  159. QList< float > floats = ars.keys();
  160. qSort( floats.begin(), floats.end() );
  161. for ( int i = floats.count() - 1; i >= 0; i-- )
  162. {
  163. sortedAlbums << ars.value( floats.at( i ) );
  164. }
  165. m_albumsModel->clear();
  166. m_albumsModel->appendAlbums( sortedAlbums );
  167. }