PageRenderTime 90ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 20ms app.codeStats 0ms

/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
 19#include "trackmodelitem.h"
 20
 21#include "playlist.h"
 22#include "query.h"
 23#include "utils/tomahawkutils.h"
 24#include "utils/logger.h"
 25
 26using namespace Tomahawk;
 27
 28
 29TrackModelItem::~TrackModelItem()
 30{
 31    // Don't use qDeleteAll here! The children will remove themselves
 32    // from the list when they get deleted and the qDeleteAll iterator
 33    // will fail badly!
 34    if ( parent && index.isValid() )
 35    {
 36        parent->children.remove( index.row() );
 37    }
 38
 39    for ( int i = children.count() - 1; i >= 0; i-- )
 40        delete children.at( i );
 41}
 42
 43
 44TrackModelItem::TrackModelItem( TrackModelItem* parent, QAbstractItemModel* model )
 45{
 46    this->parent = parent;
 47    this->model = model;
 48    childCount = 0;
 49    toberemoved = false;
 50    m_isPlaying = false;
 51
 52    if ( parent )
 53    {
 54        parent->children.append( this );
 55    }
 56}
 57
 58
 59TrackModelItem::TrackModelItem( const Tomahawk::query_ptr& query, TrackModelItem* parent, int row )
 60    : QObject( parent )
 61{
 62    setupItem( query, parent, row );
 63}
 64
 65
 66TrackModelItem::TrackModelItem( const Tomahawk::plentry_ptr& entry, TrackModelItem* parent, int row )
 67    : QObject( parent )
 68    , m_entry( entry )
 69{
 70    setupItem( entry->query(), parent, row );
 71}
 72
 73
 74const Tomahawk::plentry_ptr&
 75TrackModelItem::entry() const
 76{
 77    return m_entry;
 78}
 79
 80
 81const Tomahawk::query_ptr&
 82TrackModelItem::query() const
 83{
 84    if ( !m_entry.isNull() )
 85        return m_entry->query();
 86    else
 87        return m_query;
 88}
 89
 90
 91void
 92TrackModelItem::setupItem( const Tomahawk::query_ptr& query, TrackModelItem* parent, int row )
 93{
 94    this->parent = parent;
 95    if ( parent )
 96    {
 97        if ( row < 0 )
 98        {
 99            parent->children.append( this );
100            row = parent->children.count() - 1;
101        }
102        else
103        {
104            parent->children.insert( row, this );
105        }
106
107        this->model = parent->model;
108    }
109
110    m_isPlaying = false;
111    toberemoved = false;
112    m_query = query;
113    if ( !query->numResults() )
114    {
115        connect( query.data(), SIGNAL( resultsAdded( QList<Tomahawk::result_ptr> ) ),
116                               SIGNAL( dataChanged() ) );
117
118        connect( query.data(), SIGNAL( resultsRemoved( Tomahawk::result_ptr ) ),
119                               SIGNAL( dataChanged() ) );
120
121        connect( query.data(), SIGNAL( resultsChanged() ),
122                               SIGNAL( dataChanged() ) );
123    }
124}