1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
4
* Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
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.
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.
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/>.
20
#include "TreeProxyModelPlaylistInterface.h"
22
#include "TreeProxyModel.h"
26
#include "database/Database.h"
27
#include "database/DatabaseImpl.h"
28
#include "database/DatabaseCommand_AllAlbums.h"
29
#include "PlayableItem.h"
30
#include "utils/Logger.h"
32
using namespace Tomahawk;
35
TreeProxyModelPlaylistInterface::TreeProxyModelPlaylistInterface( TreeProxyModel* proxyModel )
37
, m_proxyModel( proxyModel )
38
, m_repeatMode( PlaylistModes::NoRepeat )
44
TreeProxyModelPlaylistInterface::~TreeProxyModelPlaylistInterface()
51
TreeProxyModelPlaylistInterface::filter() const
53
if ( m_proxyModel.isNull() )
55
TreeProxyModel* proxyModel = m_proxyModel.data();
56
return proxyModel->filterRegExp().pattern();
61
TreeProxyModelPlaylistInterface::trackCount() const
63
if ( m_proxyModel.isNull() )
65
TreeProxyModel* proxyModel = m_proxyModel.data();
66
return proxyModel->rowCount( QModelIndex() );
71
TreeProxyModelPlaylistInterface::setCurrentIndex( qint64 index )
73
PlaylistInterface::setCurrentIndex( index );
75
PlayableItem* item = static_cast<PlayableItem*>( (void*)index );
76
if ( index < 0 || !item )
78
m_proxyModel.data()->setCurrentIndex( QModelIndex() );
82
m_proxyModel.data()->setCurrentIndex( m_proxyModel.data()->mapFromSource( item->index ) );
88
TreeProxyModelPlaylistInterface::siblingIndex( int itemsAway, qint64 rootIndex ) const
90
if ( m_proxyModel.isNull() )
93
TreeProxyModel* proxyModel = m_proxyModel.data();
96
if ( rootIndex == -1 )
98
idx = proxyModel->currentIndex();
102
PlayableItem* pitem = static_cast<PlayableItem*>( (void*)rootIndex );
106
idx = proxyModel->mapFromSource( pitem->index );
108
if ( !idx.isValid() )
113
idx = proxyModel->index( qrand() % proxyModel->rowCount( idx.parent() ), 0, idx.parent() );
117
if ( m_repeatMode != PlaylistModes::RepeatOne )
118
idx = proxyModel->index( idx.row() + ( itemsAway > 0 ? 1 : -1 ), 0, idx.parent() );
121
if ( !idx.isValid() && m_repeatMode == PlaylistModes::RepeatAll )
125
// reset to first item
126
idx = proxyModel->index( 0, 0, proxyModel->currentIndex().parent() );
130
// reset to last item
131
idx = proxyModel->index( proxyModel->rowCount( proxyModel->currentIndex().parent() ) - 1, 0, proxyModel->currentIndex().parent() );
135
// Try to find the next available PlaylistItem (with results)
136
while ( idx.isValid() )
138
PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( idx ) );
141
return (qint64)( item->index.internalPointer() );
144
idx = proxyModel->index( idx.row() + ( itemsAway > 0 ? 1 : -1 ), 0, idx.parent() );
152
TreeProxyModelPlaylistInterface::currentItem() const
154
if ( m_proxyModel.isNull() )
155
return Tomahawk::result_ptr();
156
TreeProxyModel* proxyModel = m_proxyModel.data();
158
PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( proxyModel->currentIndex() ) );
159
if ( item && !item->result().isNull() && item->result()->isOnline() )
160
return item->result();
162
return Tomahawk::result_ptr();
166
QList< Tomahawk::query_ptr >
167
TreeProxyModelPlaylistInterface::tracks() const
170
QList< Tomahawk::query_ptr > queries;
176
TreeProxyModelPlaylistInterface::indexOfResult( const result_ptr& result ) const
178
if ( m_proxyModel.isNull() )
181
PlayableItem* item = m_proxyModel.data()->itemFromResult( result );
184
return (qint64)( item->index.internalPointer() );
192
TreeProxyModelPlaylistInterface::indexOfQuery( const query_ptr& query ) const
194
if ( m_proxyModel.isNull() )
197
PlayableItem* item = m_proxyModel.data()->itemFromQuery( query );
200
return (qint64)( item->index.internalPointer() );
208
TreeProxyModelPlaylistInterface::queryAt( qint64 index ) const
210
if ( m_proxyModel.isNull() )
213
PlayableItem* item = static_cast<PlayableItem*>( (void*)index );
214
if ( item && item->query() )
215
return item->query();
216
if ( item && item->result() )
217
return item->result()->toQuery();
224
TreeProxyModelPlaylistInterface::resultAt( qint64 index ) const
226
if ( m_proxyModel.isNull() )
229
PlayableItem* item = static_cast<PlayableItem*>( (void*)index );
230
if ( item && item->result() )
231
return item->result();