/src/libtomahawk/playlist/trackmodelitem.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 124 lines · 81 code · 23 blank · 20 comment · 8 complexity · aecec6ff711661f61798e29ab30ae344 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. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "trackmodelitem.h"
  19. #include "playlist.h"
  20. #include "query.h"
  21. #include "utils/tomahawkutils.h"
  22. #include "utils/logger.h"
  23. using namespace Tomahawk;
  24. TrackModelItem::~TrackModelItem()
  25. {
  26. // Don't use qDeleteAll here! The children will remove themselves
  27. // from the list when they get deleted and the qDeleteAll iterator
  28. // will fail badly!
  29. if ( parent && index.isValid() )
  30. {
  31. parent->children.remove( index.row() );
  32. }
  33. for ( int i = children.count() - 1; i >= 0; i-- )
  34. delete children.at( i );
  35. }
  36. TrackModelItem::TrackModelItem( TrackModelItem* parent, QAbstractItemModel* model )
  37. {
  38. this->parent = parent;
  39. this->model = model;
  40. childCount = 0;
  41. toberemoved = false;
  42. m_isPlaying = false;
  43. if ( parent )
  44. {
  45. parent->children.append( this );
  46. }
  47. }
  48. TrackModelItem::TrackModelItem( const Tomahawk::query_ptr& query, TrackModelItem* parent, int row )
  49. : QObject( parent )
  50. {
  51. setupItem( query, parent, row );
  52. }
  53. TrackModelItem::TrackModelItem( const Tomahawk::plentry_ptr& entry, TrackModelItem* parent, int row )
  54. : QObject( parent )
  55. , m_entry( entry )
  56. {
  57. setupItem( entry->query(), parent, row );
  58. }
  59. const Tomahawk::plentry_ptr&
  60. TrackModelItem::entry() const
  61. {
  62. return m_entry;
  63. }
  64. const Tomahawk::query_ptr&
  65. TrackModelItem::query() const
  66. {
  67. if ( !m_entry.isNull() )
  68. return m_entry->query();
  69. else
  70. return m_query;
  71. }
  72. void
  73. TrackModelItem::setupItem( const Tomahawk::query_ptr& query, TrackModelItem* parent, int row )
  74. {
  75. this->parent = parent;
  76. if ( parent )
  77. {
  78. if ( row < 0 )
  79. {
  80. parent->children.append( this );
  81. row = parent->children.count() - 1;
  82. }
  83. else
  84. {
  85. parent->children.insert( row, this );
  86. }
  87. this->model = parent->model;
  88. }
  89. m_isPlaying = false;
  90. toberemoved = false;
  91. m_query = query;
  92. if ( !query->numResults() )
  93. {
  94. connect( query.data(), SIGNAL( resultsAdded( QList<Tomahawk::result_ptr> ) ),
  95. SIGNAL( dataChanged() ) );
  96. connect( query.data(), SIGNAL( resultsRemoved( Tomahawk::result_ptr ) ),
  97. SIGNAL( dataChanged() ) );
  98. connect( query.data(), SIGNAL( resultsChanged() ),
  99. SIGNAL( dataChanged() ) );
  100. }
  101. }