PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 23ms RepoModel.GetById 37ms app.codeStats 0ms

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