File indexing completed on 2024-05-19 04:49:50

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Ian Monroe <ian@monroe.nu>                                        *
0003  * Copyright (c) 2008 Soren Harward <stharward@gmail.com>                               *
0004  * Copyright (c) 2009 Téo Mrnjavac <teo@kde.org>                                        *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
0009  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0010  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0011  * version 3 of the license.                                                            *
0012  *                                                                                      *
0013  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0016  *                                                                                      *
0017  * You should have received a copy of the GNU General Public License along with         *
0018  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0019  ****************************************************************************************/
0020 
0021 #include "TrackNavigator.h"
0022 
0023 #include "amarokconfig.h"
0024 #include "core/meta/Meta.h"
0025 #include "core/support/Amarok.h"
0026 #include "core/support/Debug.h"
0027 #include "playlist/PlaylistModelStack.h"
0028 
0029 #include <QQueue>
0030 
0031 Playlist::TrackNavigator::TrackNavigator()
0032 {
0033     m_model = The::playlist();
0034 
0035     // Connect to the QAbstractItemModel signals of the source model.
0036     //   Ignore SIGNAL dataChanged: we don't need to know when a playlist item changes.
0037     //   Ignore SIGNAL layoutChanged: we don't need to know when rows are moved around.
0038     connect( m_model->qaim(), &QAbstractItemModel::modelReset, this, &TrackNavigator::slotModelReset );
0039     connect( Playlist::ModelStack::instance()->bottom(), &Playlist::Model::rowsAboutToBeRemoved,
0040              this, &TrackNavigator::slotRowsAboutToBeRemoved );
0041     //   Ignore SIGNAL rowsInserted.
0042 }
0043 
0044 void
0045 Playlist::TrackNavigator::queueIds( const QList<quint64> &ids )
0046 {
0047     foreach( quint64 id, ids )
0048     {
0049         if( !m_queue.contains( id ) )
0050             m_queue.enqueue( id );
0051     }
0052 }
0053 
0054 void
0055 Playlist::TrackNavigator::dequeueId( const quint64 id )
0056 {
0057     m_queue.removeAll( id );
0058 }
0059 
0060 bool
0061 Playlist::TrackNavigator::queueMoveUp( const quint64 id )
0062 {
0063     const int idx = m_queue.indexOf( id );
0064     if ( idx < 1 )
0065         return false;
0066     quint64 temp = m_queue[ idx - 1 ];
0067     m_queue[ idx - 1 ] = m_queue[ idx ];
0068     m_queue[ idx ] = temp;
0069     return true;
0070 }
0071 
0072 bool
0073 Playlist::TrackNavigator::queueMoveDown( const quint64 id )
0074 {
0075     const int idx = m_queue.indexOf( id );
0076     if ( idx == -1 || idx == m_queue.count() - 1 )
0077         return false;
0078     quint64 temp = m_queue[ idx + 1 ];
0079     m_queue[ idx + 1 ] = m_queue[ idx ];
0080     m_queue[ idx ] = temp;
0081     return true;
0082 }
0083 
0084 int
0085 Playlist::TrackNavigator::queuePosition( const quint64 id ) const
0086 {
0087     return m_queue.indexOf( id );
0088 }
0089 
0090 QQueue<quint64> Playlist::TrackNavigator::queue()
0091 {
0092     return m_queue;
0093 }
0094 
0095 void
0096 Playlist::TrackNavigator::slotModelReset()
0097 {
0098     DEBUG_BLOCK
0099     m_queue.clear();    // We should check 'm_model's new contents, but this is unlikely to bother anyone.
0100 }
0101 
0102 void
0103 Playlist::TrackNavigator::slotRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end)
0104 {
0105     Q_UNUSED( parent );
0106 
0107     for ( int row = start; row <= end; ++row )
0108     {
0109         const quint64 itemId = Playlist::ModelStack::instance()->bottom()->idAt( row );
0110         m_queue.removeAll( itemId );
0111     }
0112 }
0113 
0114 quint64
0115 Playlist::TrackNavigator::bestFallbackItem()
0116 {
0117     quint64 item = m_model->activeId();
0118 
0119     if ( !item )
0120         if ( m_model->qaim()->rowCount() > 0 )
0121             item = m_model->idAt( 0 );
0122 
0123     return item;
0124 }