/src/libtomahawk/playlist/treemodelitem.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 261 lines · 192 code · 49 blank · 20 comment · 30 complexity · 0a3b72c90dba296485838b72b6b51238 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 "treemodelitem.h"
  19. #include "utils/tomahawkutils.h"
  20. #include "utils/logger.h"
  21. #include "artist.h"
  22. #include "album.h"
  23. #include "query.h"
  24. using namespace Tomahawk;
  25. TreeModelItem::~TreeModelItem()
  26. {
  27. // Don't use qDeleteAll here! The children will remove themselves
  28. // from the list when they get deleted and the qDeleteAll iterator
  29. // will fail badly!
  30. for ( int i = children.count() - 1; i >= 0; i-- )
  31. delete children.at( i );
  32. if ( parent && index.isValid() )
  33. {
  34. parent->children.removeAt( index.row() );
  35. }
  36. }
  37. TreeModelItem::TreeModelItem( TreeModelItem* parent, QAbstractItemModel* model )
  38. {
  39. this->parent = parent;
  40. this->model = model;
  41. childCount = 0;
  42. toberemoved = false;
  43. fetchingMore = false;
  44. m_isPlaying = false;
  45. if ( parent )
  46. {
  47. parent->children.append( this );
  48. }
  49. }
  50. TreeModelItem::TreeModelItem( const Tomahawk::album_ptr& album, TreeModelItem* parent, int row )
  51. : QObject( parent )
  52. , m_album( album )
  53. {
  54. this->parent = parent;
  55. fetchingMore = false;
  56. m_isPlaying = false;
  57. if ( parent )
  58. {
  59. if ( row < 0 )
  60. {
  61. parent->children.append( this );
  62. row = parent->children.count() - 1;
  63. }
  64. else
  65. {
  66. parent->children.insert( row, this );
  67. }
  68. this->model = parent->model;
  69. }
  70. toberemoved = false;
  71. connect( album.data(), SIGNAL( updated() ), SIGNAL( dataChanged() ) );
  72. }
  73. TreeModelItem::TreeModelItem( const Tomahawk::artist_ptr& artist, TreeModelItem* parent, int row )
  74. : QObject( parent )
  75. , m_artist( artist )
  76. {
  77. this->parent = parent;
  78. fetchingMore = false;
  79. m_isPlaying = false;
  80. if ( parent )
  81. {
  82. if ( row < 0 )
  83. {
  84. parent->children.append( this );
  85. row = parent->children.count() - 1;
  86. }
  87. else
  88. {
  89. parent->children.insert( row, this );
  90. }
  91. this->model = parent->model;
  92. }
  93. toberemoved = false;
  94. connect( artist.data(), SIGNAL( updated() ), SIGNAL( dataChanged() ) );
  95. }
  96. TreeModelItem::TreeModelItem( const Tomahawk::result_ptr& result, TreeModelItem* parent, int row )
  97. : QObject( parent )
  98. , m_result( result )
  99. {
  100. this->parent = parent;
  101. fetchingMore = false;
  102. m_isPlaying = false;
  103. if ( parent )
  104. {
  105. if ( row < 0 )
  106. {
  107. parent->children.append( this );
  108. row = parent->children.count() - 1;
  109. }
  110. else
  111. {
  112. parent->children.insert( row, this );
  113. }
  114. this->model = parent->model;
  115. }
  116. toberemoved = false;
  117. }
  118. TreeModelItem::TreeModelItem( const Tomahawk::query_ptr& query, TreeModelItem* parent, int row )
  119. : QObject( parent )
  120. , m_query( query )
  121. {
  122. this->parent = parent;
  123. fetchingMore = false;
  124. m_isPlaying = false;
  125. if ( parent )
  126. {
  127. if ( row < 0 )
  128. {
  129. parent->children.append( this );
  130. row = parent->children.count() - 1;
  131. }
  132. else
  133. {
  134. parent->children.insert( row, this );
  135. }
  136. this->model = parent->model;
  137. }
  138. toberemoved = false;
  139. connect( query.data(), SIGNAL( resultsAdded( QList<Tomahawk::result_ptr> ) ),
  140. SLOT( onResultsChanged() ) );
  141. connect( query.data(), SIGNAL( resultsRemoved( Tomahawk::result_ptr ) ),
  142. SLOT( onResultsChanged() ) );
  143. connect( query.data(), SIGNAL( resultsChanged() ),
  144. SLOT( onResultsChanged() ) );
  145. }
  146. void
  147. TreeModelItem::onResultsChanged()
  148. {
  149. if ( m_query->numResults() )
  150. m_result = m_query->results().first();
  151. else
  152. m_result = result_ptr();
  153. emit dataChanged();
  154. }
  155. QString
  156. TreeModelItem::name() const
  157. {
  158. if ( !m_artist.isNull() )
  159. {
  160. return m_artist->name();
  161. }
  162. else if ( !m_album.isNull() )
  163. {
  164. return m_album->name();
  165. }
  166. else if ( !m_result.isNull() )
  167. {
  168. return m_result->track();
  169. }
  170. else if ( !m_query.isNull() )
  171. {
  172. return m_query->track();
  173. }
  174. Q_ASSERT( false );
  175. return QString();
  176. }
  177. QString
  178. TreeModelItem::artistName() const
  179. {
  180. if ( !m_result.isNull() )
  181. {
  182. return m_result->artist()->name();
  183. }
  184. else if ( !m_query.isNull() )
  185. {
  186. return m_query->artist();
  187. }
  188. return QString();
  189. }
  190. QString
  191. TreeModelItem::albumName() const
  192. {
  193. if ( !m_result.isNull() && !m_result->album().isNull() )
  194. {
  195. return m_result->album()->name();
  196. }
  197. else if ( !m_query.isNull() )
  198. {
  199. return m_query->album();
  200. }
  201. return QString();
  202. }
  203. const Tomahawk::result_ptr&
  204. TreeModelItem::result() const
  205. {
  206. if ( m_result.isNull() && !m_query.isNull() )
  207. {
  208. if ( m_query->results().count() )
  209. return m_query->results().first();
  210. }
  211. return m_result;
  212. }