/src/libtomahawk/playlist/QueueProxyModel.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 85 lines · 52 code · 14 blank · 19 comment · 10 complexity · 82eb7508a9177e783b87f1941f7f3969 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2014, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.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 "QueueProxyModel.h"
  20. #include "audio/AudioEngine.h"
  21. #include "playlist/TrackView.h"
  22. #include "utils/Logger.h"
  23. #include "PlayableItem.h"
  24. #include "Result.h"
  25. #include "Source.h"
  26. #include "ViewManager.h"
  27. using namespace Tomahawk;
  28. QueueProxyModel::QueueProxyModel( TrackView* parent )
  29. : PlayableProxyModel( parent )
  30. {
  31. connect( this, SIGNAL( indexPlayable( QModelIndex ) ), SLOT( onIndexChanged( QModelIndex ) ) );
  32. connect( this, SIGNAL( indexResolved( QModelIndex ) ), SLOT( onIndexChanged( QModelIndex ) ) );
  33. connect( parent, SIGNAL( itemActivated( QModelIndex ) ), SLOT( onIndexActivated( QModelIndex ) ) );
  34. connect( AudioEngine::instance(), SIGNAL( loading( Tomahawk::result_ptr ) ), SLOT( onPlaybackStarted( Tomahawk::result_ptr ) ) );
  35. }
  36. QueueProxyModel::~QueueProxyModel()
  37. {
  38. }
  39. void
  40. QueueProxyModel::onIndexChanged( const QModelIndex& index )
  41. {
  42. PlayableItem* item = itemFromIndex( mapToSource( index ) );
  43. if ( item && item->query() )
  44. {
  45. // tDebug() << Q_FUNC_INFO << item->query()->toString() << item->query()->resolvingFinished() << item->query()->playable();
  46. }
  47. if ( !item || !item->query() || ( item->query()->resolvingFinished() && !item->query()->playable() ) )
  48. {
  49. removeIndex( index );
  50. }
  51. }
  52. void
  53. QueueProxyModel::onPlaybackStarted( const Tomahawk::result_ptr result )
  54. {
  55. for ( int i = 0; i < rowCount(); i++ )
  56. {
  57. QModelIndex idx = index( i, 0 );
  58. PlayableItem* item = itemFromIndex( mapToSource( idx ) );
  59. if ( item && item->query() && ( item->query()->results().contains( result ) ||
  60. item->query()->track()->equals( result->track() ) ) )
  61. {
  62. removeIndex( idx );
  63. }
  64. }
  65. }
  66. void
  67. QueueProxyModel::onIndexActivated( const QModelIndex& index )
  68. {
  69. setCurrentIndex( QModelIndex() );
  70. removeIndex( index );
  71. }