Debian Codegraph @

src/playlist/navigators/TrackNavigator.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2007 Ian Monroe <[email protected]> *
3 * Copyright (c) 2008 Soren Harward <[email protected]> *
4 * Copyright (c) 2009 Téo Mrnjavac <[email protected]> *
5 * *
6 * This program is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License as published by the Free Software *
8 * Foundation; either version 2 of the License, or (at your option) version 3 or *
9 * any later version accepted by the membership of KDE e.V. (or its successor approved *
10 * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of *
11 * version 3 of the license. *
12 * *
13 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
15 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License along with *
18 * this program. If not, see <http://www.gnu.org/licenses/>. *
19 ****************************************************************************************/
20
21 #include "TrackNavigator.h"
22
23 #include "amarokconfig.h"
24 #include "core/meta/Meta.h"
25 #include "core/support/Amarok.h"
26 #include "core/support/Debug.h"
27 #include "playlist/PlaylistModelStack.h"
28
29 #include <QQueue>
30
31 Playlist::TrackNavigator::TrackNavigator()
32 {
33 m_model = The::playlist();
34
35 // Connect to the QAbstractItemModel signals of the source model.
36 // Ignore SIGNAL dataChanged: we don't need to know when a playlist item changes.
37 // Ignore SIGNAL layoutChanged: we don't need to know when rows are moved around.
38 connect( m_model->qaim(), &QAbstractItemModel::modelReset, this, &TrackNavigator::slotModelReset );
39 connect( Playlist::ModelStack::instance()->bottom(), &Playlist::Model::rowsAboutToBeRemoved,
40 this, &TrackNavigator::slotRowsAboutToBeRemoved );
41 // Ignore SIGNAL rowsInserted.
42 }
43
44 void
45 Playlist::TrackNavigator::queueIds( const QList<quint64> &ids )
46 {
47 for( quint64 id : ids )
48 {
49 if( !m_queue.contains( id ) )
50 m_queue.enqueue( id );
51 }
52 }
53
54 void
55 Playlist::TrackNavigator::dequeueId( const quint64 id )
56 {
57 m_queue.removeAll( id );
58 }
59
60 bool
61 Playlist::TrackNavigator::queueMoveUp( const quint64 id )
62 {
63 return queueMoveTo( id, m_queue.indexOf( id ) - 1 );
64 }
65
66 bool
67 Playlist::TrackNavigator::queueMoveDown( const quint64 id )
68 {
69 return queueMoveTo( id, m_queue.indexOf( id ) + 1 );
70 }
71
72 bool
73 Playlist::TrackNavigator::queueMoveTo( const quint64 id, const int pos )
74 {
75 const int idx = m_queue.indexOf( id );
76 if( idx == -1 || idx == pos || pos < 0 || pos > m_queue.count() - 1 )
77 return false;
78 m_queue.move( idx, pos );
79 return true;
80 }
81
82 int
83 Playlist::TrackNavigator::queuePosition( const quint64 id ) const
84 {
85 return m_queue.indexOf( id );
86 }
87
88 QQueue<quint64> Playlist::TrackNavigator::queue()
89 {
90 return m_queue;
91 }
92
93 void
94 Playlist::TrackNavigator::slotModelReset()
95 {
96 DEBUG_BLOCK
97 m_queue.clear(); // We should check 'm_model's new contents, but this is unlikely to bother anyone.
98 }
99
100 void
101 Playlist::TrackNavigator::slotRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end)
102 {
103 Q_UNUSED( parent );
104
105 for ( int row = start; row <= end; ++row )
106 {
107 const quint64 itemId = Playlist::ModelStack::instance()->bottom()->idAt( row );
108 m_queue.removeAll( itemId );
109 }
110 }
111
112 quint64
113 Playlist::TrackNavigator::bestFallbackItem()
114 {
115 quint64 item = m_model->activeId();
116
117 if ( !item )
118 if ( m_model->qaim()->rowCount() > 0 )
119 item = m_model->idAt( 0 );
120
121 return item;
122 }