PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 52ms app.codeStats 0ms

/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
20#include "QueueProxyModel.h"
21
22#include "audio/AudioEngine.h"
23#include "playlist/TrackView.h"
24#include "utils/Logger.h"
25
26#include "PlayableItem.h"
27#include "Result.h"
28#include "Source.h"
29#include "ViewManager.h"
30
31using namespace Tomahawk;
32
33
34QueueProxyModel::QueueProxyModel( TrackView* parent )
35    : PlayableProxyModel( parent )
36{
37    connect( this, SIGNAL( indexPlayable( QModelIndex ) ), SLOT( onIndexChanged( QModelIndex ) ) );
38    connect( this, SIGNAL( indexResolved( QModelIndex ) ), SLOT( onIndexChanged( QModelIndex ) ) );
39    connect( parent, SIGNAL( itemActivated( QModelIndex ) ), SLOT( onIndexActivated( QModelIndex ) ) );
40    connect( AudioEngine::instance(), SIGNAL( loading( Tomahawk::result_ptr ) ), SLOT( onPlaybackStarted( Tomahawk::result_ptr ) ) );
41}
42
43
44QueueProxyModel::~QueueProxyModel()
45{
46}
47
48
49void
50QueueProxyModel::onIndexChanged( const QModelIndex& index )
51{
52    PlayableItem* item = itemFromIndex( mapToSource( index ) );
53    if ( item && item->query() )
54    {
55        // tDebug() << Q_FUNC_INFO << item->query()->toString() << item->query()->resolvingFinished() << item->query()->playable();
56    }
57    if ( !item || !item->query() || ( item->query()->resolvingFinished() && !item->query()->playable() ) )
58    {
59        removeIndex( index );
60    }
61}
62
63
64void
65QueueProxyModel::onPlaybackStarted( const Tomahawk::result_ptr result )
66{
67    for ( int i = 0; i < rowCount(); i++ )
68    {
69        QModelIndex idx = index( i, 0 );
70        PlayableItem* item = itemFromIndex( mapToSource( idx ) );
71        if ( item && item->query() && ( item->query()->results().contains( result ) ||
72                                        item->query()->track()->equals( result->track() ) ) )
73        {
74            removeIndex( idx );
75        }
76    }
77}
78
79
80void
81QueueProxyModel::onIndexActivated( const QModelIndex& index )
82{
83    setCurrentIndex( QModelIndex() );
84    removeIndex( index );
85}