~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/playlist/TreeProxyModelPlaylistInterface.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
4
 *   Copyright 2010-2011, 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 "TreeProxyModelPlaylistInterface.h"
 
21
 
 
22
#include "TreeProxyModel.h"
 
23
 
 
24
#include "Source.h"
 
25
#include "Query.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"
 
31
 
 
32
using namespace Tomahawk;
 
33
 
 
34
 
 
35
TreeProxyModelPlaylistInterface::TreeProxyModelPlaylistInterface( TreeProxyModel* proxyModel )
 
36
    : PlaylistInterface()
 
37
    , m_proxyModel( proxyModel )
 
38
    , m_repeatMode( PlaylistModes::NoRepeat )
 
39
    , m_shuffled( false )
 
40
{
 
41
}
 
42
 
 
43
 
 
44
TreeProxyModelPlaylistInterface::~TreeProxyModelPlaylistInterface()
 
45
{
 
46
    m_proxyModel.clear();
 
47
}
 
48
 
 
49
 
 
50
QString
 
51
TreeProxyModelPlaylistInterface::filter() const
 
52
{
 
53
    if ( m_proxyModel.isNull() )
 
54
        return 0;
 
55
    TreeProxyModel* proxyModel = m_proxyModel.data();
 
56
    return proxyModel->filterRegExp().pattern();
 
57
}
 
58
 
 
59
 
 
60
int
 
61
TreeProxyModelPlaylistInterface::trackCount() const
 
62
{
 
63
    if ( m_proxyModel.isNull() )
 
64
        return 0;
 
65
    TreeProxyModel* proxyModel = m_proxyModel.data();
 
66
    return proxyModel->rowCount( QModelIndex() );
 
67
}
 
68
 
 
69
 
 
70
void
 
71
TreeProxyModelPlaylistInterface::setCurrentIndex( qint64 index )
 
72
{
 
73
    PlaylistInterface::setCurrentIndex( index );
 
74
 
 
75
    PlayableItem* item = static_cast<PlayableItem*>( (void*)index );
 
76
    if ( index < 0 || !item )
 
77
    {
 
78
        m_proxyModel.data()->setCurrentIndex( QModelIndex() );
 
79
    }
 
80
    else
 
81
    {
 
82
        m_proxyModel.data()->setCurrentIndex( m_proxyModel.data()->mapFromSource( item->index ) );
 
83
    }
 
84
}
 
85
 
 
86
 
 
87
qint64
 
88
TreeProxyModelPlaylistInterface::siblingIndex( int itemsAway, qint64 rootIndex ) const
 
89
{
 
90
    if ( m_proxyModel.isNull() )
 
91
        return -1;
 
92
 
 
93
    TreeProxyModel* proxyModel = m_proxyModel.data();
 
94
    QModelIndex idx;
 
95
 
 
96
    if ( rootIndex == -1 )
 
97
    {
 
98
        idx = proxyModel->currentIndex();
 
99
    }
 
100
    else
 
101
    {
 
102
        PlayableItem* pitem = static_cast<PlayableItem*>( (void*)rootIndex );
 
103
        if ( !pitem )
 
104
            return -1;
 
105
 
 
106
        idx = proxyModel->mapFromSource( pitem->index );
 
107
    }
 
108
    if ( !idx.isValid() )
 
109
        return -1;
 
110
 
 
111
    if ( m_shuffled )
 
112
    {
 
113
        idx = proxyModel->index( qrand() % proxyModel->rowCount( idx.parent() ), 0, idx.parent() );
 
114
    }
 
115
    else
 
116
    {
 
117
        if ( m_repeatMode != PlaylistModes::RepeatOne )
 
118
            idx = proxyModel->index( idx.row() + ( itemsAway > 0 ? 1 : -1 ), 0, idx.parent() );
 
119
    }
 
120
 
 
121
    if ( !idx.isValid() && m_repeatMode == PlaylistModes::RepeatAll )
 
122
    {
 
123
        if ( itemsAway > 0 )
 
124
        {
 
125
            // reset to first item
 
126
            idx = proxyModel->index( 0, 0, proxyModel->currentIndex().parent() );
 
127
        }
 
128
        else
 
129
        {
 
130
            // reset to last item
 
131
            idx = proxyModel->index( proxyModel->rowCount( proxyModel->currentIndex().parent() ) - 1, 0, proxyModel->currentIndex().parent() );
 
132
        }
 
133
    }
 
134
 
 
135
    // Try to find the next available PlaylistItem (with results)
 
136
    while ( idx.isValid() )
 
137
    {
 
138
        PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( idx ) );
 
139
        if ( item )
 
140
        {
 
141
            return (qint64)( item->index.internalPointer() );
 
142
        }
 
143
 
 
144
        idx = proxyModel->index( idx.row() + ( itemsAway > 0 ? 1 : -1 ), 0, idx.parent() );
 
145
    }
 
146
 
 
147
    return -1;
 
148
}
 
149
 
 
150
 
 
151
Tomahawk::result_ptr
 
152
TreeProxyModelPlaylistInterface::currentItem() const
 
153
{
 
154
    if ( m_proxyModel.isNull() )
 
155
        return Tomahawk::result_ptr();
 
156
    TreeProxyModel* proxyModel = m_proxyModel.data();
 
157
 
 
158
    PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( proxyModel->currentIndex() ) );
 
159
    if ( item && !item->result().isNull() && item->result()->isOnline() )
 
160
        return item->result();
 
161
 
 
162
    return Tomahawk::result_ptr();
 
163
}
 
164
 
 
165
 
 
166
QList< Tomahawk::query_ptr >
 
167
TreeProxyModelPlaylistInterface::tracks() const
 
168
{
 
169
    Q_ASSERT( false );
 
170
    QList< Tomahawk::query_ptr > queries;
 
171
    return queries;
 
172
}
 
173
 
 
174
 
 
175
qint64
 
176
TreeProxyModelPlaylistInterface::indexOfResult( const result_ptr& result ) const
 
177
{
 
178
    if ( m_proxyModel.isNull() )
 
179
        return -1;
 
180
 
 
181
    PlayableItem* item = m_proxyModel.data()->itemFromResult( result );
 
182
    if ( item )
 
183
    {
 
184
        return (qint64)( item->index.internalPointer() );
 
185
    }
 
186
 
 
187
    return -1;
 
188
}
 
189
 
 
190
 
 
191
qint64
 
192
TreeProxyModelPlaylistInterface::indexOfQuery( const query_ptr& query ) const
 
193
{
 
194
    if ( m_proxyModel.isNull() )
 
195
        return -1;
 
196
 
 
197
    PlayableItem* item = m_proxyModel.data()->itemFromQuery( query );
 
198
    if ( item )
 
199
    {
 
200
        return (qint64)( item->index.internalPointer() );
 
201
    }
 
202
 
 
203
    return -1;
 
204
}
 
205
 
 
206
 
 
207
Tomahawk::query_ptr
 
208
TreeProxyModelPlaylistInterface::queryAt( qint64 index ) const
 
209
{
 
210
    if ( m_proxyModel.isNull() )
 
211
        return query_ptr();
 
212
 
 
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();
 
218
 
 
219
    return query_ptr();
 
220
}
 
221
 
 
222
 
 
223
Tomahawk::result_ptr
 
224
TreeProxyModelPlaylistInterface::resultAt( qint64 index ) const
 
225
{
 
226
    if ( m_proxyModel.isNull() )
 
227
        return result_ptr();
 
228
 
 
229
    PlayableItem* item = static_cast<PlayableItem*>( (void*)index );
 
230
    if ( item && item->result() )
 
231
        return item->result();
 
232
 
 
233
    return result_ptr();
 
234
}