PageRenderTime 114ms CodeModel.GetById 30ms app.highlight 43ms RepoModel.GetById 38ms app.codeStats 0ms

/src/libtomahawk/playlist/trackproxymodel.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 299 lines | 227 code | 54 blank | 18 comment | 61 complexity | a96984eaa47a6945754e043b02204e9c MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "trackproxymodel.h"
 20
 21#include <QTreeView>
 22
 23#include "trackproxymodelplaylistinterface.h"
 24#include "artist.h"
 25#include "album.h"
 26#include "query.h"
 27#include "utils/logger.h"
 28
 29
 30TrackProxyModel::TrackProxyModel( QObject* parent )
 31    : QSortFilterProxyModel( parent )
 32    , m_model( 0 )
 33    , m_showOfflineResults( true )
 34{
 35    setFilterCaseSensitivity( Qt::CaseInsensitive );
 36    setSortCaseSensitivity( Qt::CaseInsensitive );
 37    setDynamicSortFilter( true );
 38
 39    setSourceTrackModel( 0 );
 40}
 41
 42
 43void
 44TrackProxyModel::setSourceModel( QAbstractItemModel* model )
 45{
 46    Q_UNUSED( model );
 47    qDebug() << "Explicitly use setSourceTrackModel instead";
 48    Q_ASSERT( false );
 49}
 50
 51
 52void
 53TrackProxyModel::setSourceTrackModel( TrackModel* sourceModel )
 54{
 55    m_model = sourceModel;
 56
 57    if ( m_model && m_model->metaObject()->indexOfSignal( "trackCountChanged(uint)" ) > -1 )
 58        connect( m_model, SIGNAL( trackCountChanged( unsigned int ) ), playlistInterface().data(), SIGNAL( sourceTrackCountChanged( unsigned int ) ) );
 59
 60    QSortFilterProxyModel::setSourceModel( m_model );
 61}
 62
 63
 64bool
 65TrackProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
 66{
 67    TrackModelItem* pi = itemFromIndex( sourceModel()->index( sourceRow, 0, sourceParent ) );
 68    if ( !pi )
 69        return false;
 70
 71    const Tomahawk::query_ptr& q = pi->query();
 72    if( q.isNull() ) // uh oh? filter out invalid queries i guess
 73        return false;
 74
 75    Tomahawk::result_ptr r;
 76    if ( q->numResults() )
 77        r = q->results().first();
 78
 79    if ( !m_showOfflineResults && !r.isNull() && !r->isOnline() )
 80        return false;
 81
 82    if ( filterRegExp().isEmpty() )
 83        return true;
 84
 85    QStringList sl = filterRegExp().pattern().split( " ", QString::SkipEmptyParts );
 86    foreach( QString s, sl )
 87    {
 88        s = s.toLower();
 89        if ( !r.isNull() )
 90        {
 91            if ( !r->artist()->name().toLower().contains( s ) &&
 92                 !r->album()->name().toLower().contains( s ) &&
 93                 !r->track().toLower().contains( s ) )
 94            {
 95                return false;
 96            }
 97        }
 98        else
 99        {
100            if ( !q->artist().toLower().contains( s ) &&
101                 !q->album().toLower().contains( s ) &&
102                 !q->track().toLower().contains( s ) )
103            {
104                return false;
105            }
106        }
107    }
108
109    return true;
110}
111
112
113void
114TrackProxyModel::remove( const QModelIndex& index )
115{
116    if ( !sourceModel() )
117        return;
118    if ( !index.isValid() )
119        return;
120
121    sourceModel()->remove( mapToSource( index ) );
122}
123
124
125void
126TrackProxyModel::remove( const QModelIndexList& indexes )
127{
128    if ( !sourceModel() )
129        return;
130
131    QList<QPersistentModelIndex> pil;
132    foreach ( const QModelIndex& idx, indexes )
133    {
134        if ( idx.isValid() )
135            pil << mapToSource( idx );
136    }
137
138    sourceModel()->remove( pil );
139}
140
141
142void
143TrackProxyModel::remove( const QList< QPersistentModelIndex >& indexes )
144{
145    if ( !sourceModel() )
146        return;
147
148    QList<QPersistentModelIndex> pil;
149    foreach ( const QPersistentModelIndex& idx, indexes )
150    {
151        if ( idx.isValid() )
152            pil << mapToSource( idx );
153    }
154
155    sourceModel()->remove( pil );
156}
157
158
159bool
160TrackProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right ) const
161{
162    TrackModelItem* p1 = itemFromIndex( left );
163    TrackModelItem* p2 = itemFromIndex( right );
164
165    if ( !p1 )
166        return true;
167    if ( !p2 )
168        return false;
169
170    const Tomahawk::query_ptr& q1 = p1->query();
171    const Tomahawk::query_ptr& q2 = p2->query();
172
173    QString artist1 = q1->artistSortname();
174    QString artist2 = q2->artistSortname();
175    QString album1 = q1->album();
176    QString album2 = q2->album();
177    QString track1 = q1->track();
178    QString track2 = q2->track();
179    unsigned int albumpos1 = 0, albumpos2 = 0;
180    unsigned int discnumber1 = 0, discnumber2 = 0;
181    unsigned int bitrate1 = 0, bitrate2 = 0;
182    unsigned int mtime1 = 0, mtime2 = 0;
183    unsigned int size1 = 0, size2 = 0;
184    qint64 id1 = 0, id2 = 0;
185
186    if ( q1->numResults() )
187    {
188        const Tomahawk::result_ptr& r = q1->results().at( 0 );
189        artist1 = r->artist()->sortname();
190        album1 = r->album()->name();
191        track1 = r->track();
192        albumpos1 = r->albumpos();
193        discnumber1 = r->discnumber();
194        bitrate1 = r->bitrate();
195        mtime1 = r->modificationTime();
196        id1 = r->trackId();
197        size1 = r->size();
198    }
199    if ( q2->numResults() )
200    {
201        const Tomahawk::result_ptr& r = q2->results().at( 0 );
202        artist2 = r->artist()->sortname();
203        album2 = r->album()->name();
204        track2 = r->track();
205        albumpos2 = r->albumpos();
206        discnumber2 = r->discnumber();
207        bitrate2 = r->bitrate();
208        mtime2 = r->modificationTime();
209        id2 = r->trackId();
210        size2 = r->size();
211    }
212
213    // This makes it a stable sorter and prevents items from randomly jumping about.
214    if ( id1 == id2 )
215    {
216        id1 = (qint64)&q1;
217        id2 = (qint64)&q2;
218    }
219
220    if ( left.column() == TrackModel::Artist ) // sort by artist
221    {
222        if ( artist1 == artist2 )
223        {
224            if ( album1 == album2 )
225            {
226                if( discnumber1 == discnumber2 )
227                {
228                    if ( albumpos1 == albumpos2 )
229                        return id1 < id2;
230
231                    return albumpos1 < albumpos2;
232                }
233
234                return discnumber1 < discnumber2;
235            }
236
237            return QString::localeAwareCompare( album1, album2 ) < 0;
238        }
239
240        return QString::localeAwareCompare( artist1, artist2 ) < 0;
241    }
242    else if ( left.column() == TrackModel::Album ) // sort by album
243    {
244        if ( album1 == album2 )
245        {
246            if( discnumber1 == discnumber2 )
247            {
248                if ( albumpos1 == albumpos2 )
249                    return id1 < id2;
250
251                return albumpos1 < albumpos2;
252            }
253
254            return discnumber1 < discnumber2;
255        }
256
257        return QString::localeAwareCompare( album1, album2 ) < 0;
258    }
259    else if ( left.column() == TrackModel::Bitrate ) // sort by bitrate
260    {
261        if ( bitrate1 == bitrate2 )
262            return id1 < id2;
263
264        return bitrate1 < bitrate2;
265    }
266    else if ( left.column() == TrackModel::Age ) // sort by mtime
267    {
268        if ( mtime1 == mtime2 )
269            return id1 < id2;
270
271        return mtime1 < mtime2;
272    }
273    else if ( left.column() == TrackModel::Filesize ) // sort by file size
274    {
275        if ( size1 == size2 )
276            return id1 < id2;
277
278        return size1 < size2;
279    }
280
281    const QString& lefts = sourceModel()->data( left ).toString();
282    const QString& rights = sourceModel()->data( right ).toString();
283    if ( lefts == rights )
284        return id1 < id2;
285
286    return QString::localeAwareCompare( lefts, rights ) < 0;
287}
288
289
290Tomahawk::playlistinterface_ptr
291TrackProxyModel::playlistInterface()
292{
293    if ( m_playlistInterface.isNull() )
294    {
295        m_playlistInterface = Tomahawk::playlistinterface_ptr( new Tomahawk::TrackProxyModelPlaylistInterface( this ) );
296    }
297
298    return m_playlistInterface;
299}