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-2012, 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 "PlayableProxyModel.h"
24
#include "PlayableProxyModelPlaylistInterface.h"
29
#include "PlayableItem.h"
30
#include "utils/Logger.h"
33
PlayableProxyModel::PlayableProxyModel( QObject* parent )
34
: QSortFilterProxyModel( parent )
36
, m_showOfflineResults( true )
37
, m_hideDupeItems( false )
38
, m_maxVisibleItems( -1 )
41
m_playlistInterface = Tomahawk::playlistinterface_ptr( new Tomahawk::PlayableProxyModelPlaylistInterface( this ) );
43
setFilterCaseSensitivity( Qt::CaseInsensitive );
44
setSortCaseSensitivity( Qt::CaseInsensitive );
45
setDynamicSortFilter( true );
47
setSourcePlayableModel( 0 );
49
m_headerStyle[ Large ] << PlayableModel::Name;
50
m_headerStyle[ Detailed ] << PlayableModel::Artist << PlayableModel::Track << PlayableModel::Composer << PlayableModel::Album << PlayableModel::AlbumPos << PlayableModel::Duration << PlayableModel::Bitrate << PlayableModel::Age << PlayableModel::Year << PlayableModel::Filesize << PlayableModel::Origin << PlayableModel::Score;
51
m_headerStyle[ Collection ] << PlayableModel::Name << PlayableModel::Composer << PlayableModel::Duration << PlayableModel::Bitrate << PlayableModel::Age << PlayableModel::Year << PlayableModel::Filesize << PlayableModel::Origin;
55
Tomahawk::playlistinterface_ptr
56
PlayableProxyModel::playlistInterface() const
58
return m_playlistInterface;
63
PlayableProxyModel::setPlaylistInterface( const Tomahawk::playlistinterface_ptr& playlistInterface )
65
m_playlistInterface = playlistInterface;
70
PlayableProxyModel::guid() const
74
return m_model->guid();
82
PlayableProxyModel::isLoading() const
86
return m_model->isLoading();
94
PlayableProxyModel::setSourceModel( QAbstractItemModel* model )
97
qDebug() << "Explicitly use setSourcePlayableModel instead";
103
PlayableProxyModel::setSourcePlayableModel( PlayableModel* sourceModel )
107
disconnect( m_model, SIGNAL( loadingStarted() ), this, SIGNAL( loadingStarted() ) );
108
disconnect( m_model, SIGNAL( loadingFinished() ), this, SIGNAL( loadingFinished() ) );
109
disconnect( m_model, SIGNAL( itemCountChanged( unsigned int ) ), this, SIGNAL( itemCountChanged( unsigned int ) ) );
110
disconnect( m_model, SIGNAL( indexPlayable( QModelIndex ) ), this, SLOT( onIndexPlayable( QModelIndex ) ) );
111
disconnect( m_model, SIGNAL( indexResolved( QModelIndex ) ), this, SLOT( onIndexResolved( QModelIndex ) ) );
112
disconnect( m_model, SIGNAL( currentIndexChanged() ), this, SIGNAL( currentIndexChanged() ) );
115
m_model = sourceModel;
119
connect( m_model, SIGNAL( loadingStarted() ), SIGNAL( loadingStarted() ) );
120
connect( m_model, SIGNAL( loadingFinished() ), SIGNAL( loadingFinished() ) );
121
connect( m_model, SIGNAL( itemCountChanged( unsigned int ) ), SIGNAL( itemCountChanged( unsigned int ) ) );
122
connect( m_model, SIGNAL( indexPlayable( QModelIndex ) ), SLOT( onIndexPlayable( QModelIndex ) ) );
123
connect( m_model, SIGNAL( indexResolved( QModelIndex ) ), SLOT( onIndexResolved( QModelIndex ) ) );
124
connect( m_model, SIGNAL( currentIndexChanged() ), SIGNAL( currentIndexChanged() ) );
127
QSortFilterProxyModel::setSourceModel( m_model );
132
PlayableProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
134
PlayableItem* pi = itemFromIndex( sourceModel()->index( sourceRow, 0, sourceParent ) );
138
if ( m_maxVisibleItems > 0 && sourceRow > m_maxVisibleItems - 1 )
141
if ( m_hideDupeItems )
143
for ( int i = 0; i < sourceRow; i++ )
145
PlayableItem* di = itemFromIndex( sourceModel()->index( i, 0, sourceParent ) );
149
bool b = ( pi->query() && pi->query()->equals( di->query() ) ) ||
150
( pi->album() && pi->album() == di->album() ) ||
151
( pi->artist() && pi->artist()->name() == di->artist()->name() );
153
if ( b && filterAcceptsRow( i, sourceParent ) )
160
const Tomahawk::query_ptr& q = pi->query()->displayQuery();
161
if ( q.isNull() ) // uh oh? filter out invalid queries i guess
164
Tomahawk::result_ptr r;
165
if ( q->numResults() )
166
r = q->results().first();
168
if ( !m_showOfflineResults && ( r.isNull() || !r->isOnline() ) )
171
if ( filterRegExp().isEmpty() )
174
QStringList sl = filterRegExp().pattern().split( " ", QString::SkipEmptyParts );
175
foreach( QString s, sl )
178
if ( !q->artist().toLower().contains( s ) &&
179
!q->album().toLower().contains( s ) &&
180
!q->track().toLower().contains( s ) )
187
const Tomahawk::album_ptr& al = pi->album();
190
QStringList sl = filterRegExp().pattern().split( " ", QString::SkipEmptyParts );
193
foreach( const QString& s, sl )
195
if ( !al->name().contains( s, Qt::CaseInsensitive ) && !al->artist()->name().contains( s, Qt::CaseInsensitive ) )
204
const Tomahawk::album_ptr& ar = pi->album();
207
QStringList sl = filterRegExp().pattern().split( " ", QString::SkipEmptyParts );
210
foreach( const QString& s, sl )
212
if ( !ar->name().contains( s, Qt::CaseInsensitive ) && !ar->artist()->name().contains( s, Qt::CaseInsensitive ) )
226
PlayableProxyModel::removeIndex( const QModelIndex& index )
228
if ( !sourceModel() )
230
if ( !index.isValid() )
233
sourceModel()->removeIndex( mapToSource( index ) );
238
PlayableProxyModel::removeIndexes( const QModelIndexList& indexes )
240
if ( !sourceModel() )
243
QList<QPersistentModelIndex> pil;
244
foreach ( const QModelIndex& idx, indexes )
247
pil << mapToSource( idx );
250
sourceModel()->removeIndexes( pil );
255
PlayableProxyModel::removeIndexes( const QList< QPersistentModelIndex >& indexes )
257
if ( !sourceModel() )
260
QList<QPersistentModelIndex> pil;
261
foreach ( const QPersistentModelIndex& idx, indexes )
264
pil << mapToSource( idx );
267
sourceModel()->removeIndexes( pil );
272
PlayableProxyModel::setShowOfflineResults( bool b )
274
m_showOfflineResults = b;
280
PlayableProxyModel::setHideDupeItems( bool b )
288
PlayableProxyModel::setMaxVisibleItems( int items )
290
if ( m_maxVisibleItems == items )
293
m_maxVisibleItems = items;
299
PlayableProxyModel::lessThan( int column, const Tomahawk::query_ptr& q1, const Tomahawk::query_ptr& q2 ) const
301
const QString artist1 = q1->artistSortname();
302
const QString artist2 = q2->artistSortname();
303
const QString album1 = q1->albumSortname();
304
const QString album2 = q2->albumSortname();
305
const QString track1 = q1->trackSortname();
306
const QString track2 = q2->trackSortname();
307
const QString composer1 = q1->composerSortname();
308
const QString composer2 = q2->composerSortname();
309
const unsigned int albumpos1 = q1->albumpos();
310
const unsigned int albumpos2 = q2->albumpos();
311
const unsigned int discnumber1 = q1->discnumber();
312
const unsigned int discnumber2 = q2->discnumber();
313
unsigned int duration1 = q1->duration(), duration2 = q2->duration();
314
unsigned int bitrate1 = 0, bitrate2 = 0;
315
unsigned int mtime1 = 0, mtime2 = 0;
316
unsigned int size1 = 0, size2 = 0;
317
unsigned int year1 = 0, year2 = 0;
318
float score1 = 0, score2 = 0;
321
qint64 id1 = 0, id2 = 0;
323
if ( q1->numResults() )
325
const Tomahawk::result_ptr& r = q1->results().at( 0 );
326
bitrate1 = r->bitrate();
327
duration1 = r->duration();
328
mtime1 = r->modificationTime();
332
origin1 = r->friendlySource().toLower();
335
if ( q2->numResults() )
337
const Tomahawk::result_ptr& r = q2->results().at( 0 );
338
bitrate2 = r->bitrate();
339
duration2 = r->duration();
340
mtime2 = r->modificationTime();
344
origin2 = r->friendlySource().toLower();
348
// This makes it a stable sorter and prevents items from randomly jumping about.
355
if ( column == PlayableModel::Artist ) // sort by artist
357
if ( artist1 == artist2 )
359
if ( album1 == album2 )
361
if ( discnumber1 == discnumber2 )
363
if ( albumpos1 == albumpos2 )
366
return albumpos1 < albumpos2;
369
return discnumber1 < discnumber2;
372
return QString::localeAwareCompare( album1, album2 ) < 0;
375
return QString::localeAwareCompare( artist1, artist2 ) < 0;
377
else if ( column == PlayableModel::Composer ) // sort by composer
379
if ( composer1 == composer2 )
381
if ( album1 == album2 )
383
if ( discnumber1 == discnumber2 )
385
if ( albumpos1 == albumpos2 )
388
return albumpos1 < albumpos2;
391
return discnumber1 < discnumber2;
394
return QString::localeAwareCompare( album1, album2 ) < 0;
397
return QString::localeAwareCompare( composer1, composer2 ) < 0;
399
else if ( column == PlayableModel::Album ) // sort by album
401
if ( album1 == album2 )
403
if ( discnumber1 == discnumber2 )
405
if ( albumpos1 == albumpos2 )
408
return albumpos1 < albumpos2;
411
return discnumber1 < discnumber2;
414
return QString::localeAwareCompare( album1, album2 ) < 0;
416
else if ( column == PlayableModel::Bitrate ) // sort by bitrate
418
if ( bitrate1 == bitrate2 )
421
return bitrate1 < bitrate2;
423
else if ( column == PlayableModel::Duration ) // sort by duration
425
if ( duration1 == duration2 )
428
return duration1 < duration2;
430
else if ( column == PlayableModel::Age ) // sort by mtime
432
if ( mtime1 == mtime2 )
435
return mtime1 < mtime2;
437
else if ( column == PlayableModel::Year ) // sort by release year
439
if ( year1 == year2 )
442
return year1 < year2;
444
else if ( column == PlayableModel::Filesize ) // sort by file size
446
if ( size1 == size2 )
449
return size1 < size2;
451
else if ( column == PlayableModel::Score ) // sort by file score
453
if ( score1 == score2 )
456
return score1 < score2;
458
else if ( column == PlayableModel::Origin ) // sort by file origin
460
if ( origin1 == origin2 )
463
return origin1 < origin2;
465
else if ( column == PlayableModel::AlbumPos ) // sort by album pos
467
if ( discnumber1 != discnumber2 )
469
return discnumber1 < discnumber2;
473
if ( albumpos1 != albumpos2 )
474
return albumpos1 < albumpos2;
478
const QString& lefts = q1->track();
479
const QString& rights = q2->track();
480
if ( lefts == rights )
483
return QString::localeAwareCompare( lefts, rights ) < 0;
488
PlayableProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right ) const
490
PlayableItem* p1 = itemFromIndex( left );
491
PlayableItem* p2 = itemFromIndex( right );
498
if ( p1->query() && p2->query() )
500
const Tomahawk::query_ptr& q1 = p1->query()->displayQuery();
501
const Tomahawk::query_ptr& q2 = p2->query()->displayQuery();
502
return lessThan( left.column(), q1, q2 );
505
return QString::localeAwareCompare( sourceModel()->data( left ).toString(), sourceModel()->data( right ).toString() ) < 0;
510
PlayableProxyModel::columnCount( const QModelIndex& parent ) const
517
case ShortWithAvatars:
535
PlayableProxyModel::data( const QModelIndex& index, int role ) const
537
if ( role == StyleRole )
542
if ( !sourceModel() )
544
if ( !m_headerStyle.contains( m_style ) )
546
if ( index.column() < 0 )
549
PlayableModel::Columns col = m_headerStyle[ m_style ].at( index.column() );
550
QModelIndex sourceIdx = mapToSource( index );
551
QModelIndex idx = sourceModel()->index( sourceIdx.row(), (int)col, sourceIdx.parent() );
553
return idx.data( role );
558
PlayableProxyModel::headerData( int section, Qt::Orientation orientation, int role ) const
560
if ( !sourceModel() )
562
if ( !m_headerStyle.contains( m_style ) )
565
if ( section < m_headerStyle[ m_style ].count() )
567
PlayableModel::Columns col = m_headerStyle[ m_style ].at( section );
568
return sourceModel()->headerData( (int)col, orientation, role );
571
return sourceModel()->headerData( 255, orientation, role );
576
PlayableProxyModel::columnWeights() const
583
case ShortWithAvatars:
589
w << 0.42 << 0.12 << 0.07 << 0.07 << 0.07 << 0.07 << 0.07; // << 0.11;
594
w << 0.15 << 0.15 << 0.12 << 0.12 << 0.05 << 0.05 << 0.05 << 0.05 << 0.05 << 0.05 << 0.09; // << 0.03;
603
PlayableProxyModel::updateDetailedInfo( const QModelIndex& index )
605
if ( style() != PlayableProxyModel::Short && style() != PlayableProxyModel::Large )
608
PlayableItem* item = itemFromIndex( mapToSource( index ) );
609
if ( item->query().isNull() )
612
if ( style() == PlayableProxyModel::Short || style() == PlayableProxyModel::Large )
614
item->query()->displayQuery()->cover( QSize( 0, 0 ) );
617
if ( style() == PlayableProxyModel::Large )
619
item->query()->loadSocialActions();
625
PlayableProxyModel::setFilter( const QString& pattern )
627
if ( pattern != filterRegExp().pattern() )
629
setFilterRegExp( pattern );
630
emit filterChanged( pattern );
636
PlayableProxyModel::setCurrentIndex( const QModelIndex& index )
638
tDebug() << Q_FUNC_INFO;
639
m_model->setCurrentIndex( mapToSource( index ) );
644
PlayableProxyModel::onIndexPlayable( const QModelIndex& index )
646
emit indexPlayable( mapFromSource( index ) );
651
PlayableProxyModel::onIndexResolved( const QModelIndex& index )
653
emit indexResolved( mapFromSource( index ) );