PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 15ms RepoModel.GetById 25ms app.codeStats 0ms

/src/libtomahawk/playlist/albumproxymodel.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 152 lines | 99 code | 36 blank | 17 comment | 18 complexity | 55e8764d60c81520f386d532f9305652 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 "albumproxymodel.h"
 20
 21#include <QListView>
 22
 23#include "albumproxymodelplaylistinterface.h"
 24#include "artist.h"
 25#include "albumitem.h"
 26#include "query.h"
 27#include "utils/logger.h"
 28
 29
 30AlbumProxyModel::AlbumProxyModel( QObject* parent )
 31    : QSortFilterProxyModel( parent )
 32    , m_model( 0 )
 33{
 34    setFilterCaseSensitivity( Qt::CaseInsensitive );
 35    setSortCaseSensitivity( Qt::CaseInsensitive );
 36    setDynamicSortFilter( true );
 37
 38    setSourceAlbumModel( 0 );
 39}
 40
 41
 42void
 43AlbumProxyModel::setSourceModel( QAbstractItemModel* sourceModel )
 44{
 45    Q_UNUSED( sourceModel );
 46    qDebug() << "Explicitly use setSourceAlbumModel instead";
 47    Q_ASSERT( false );
 48}
 49
 50
 51void
 52AlbumProxyModel::setSourceAlbumModel( AlbumModel* sourceModel )
 53{
 54    m_model = sourceModel;
 55
 56    if ( m_model && m_model->metaObject()->indexOfSignal( "trackCountChanged(uint)" ) > -1 )
 57        connect( m_model, SIGNAL( trackCountChanged( unsigned int ) ), SIGNAL( sourceTrackCountChanged( unsigned int ) ) );
 58
 59    QSortFilterProxyModel::setSourceModel( sourceModel );
 60}
 61
 62
 63bool
 64AlbumProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
 65{
 66    if ( filterRegExp().isEmpty() )
 67        return true;
 68
 69    AlbumItem* pi = sourceModel()->itemFromIndex( sourceModel()->index( sourceRow, 0, sourceParent ) );
 70    if ( !pi )
 71        return false;
 72
 73    const Tomahawk::album_ptr& q = pi->album();
 74
 75    QStringList sl = filterRegExp().pattern().split( " ", QString::SkipEmptyParts );
 76
 77    bool found = true;
 78    foreach( const QString& s, sl )
 79    {
 80        if ( !q->name().contains( s, Qt::CaseInsensitive ) && !q->artist()->name().contains( s, Qt::CaseInsensitive ) )
 81        {
 82            found = false;
 83        }
 84    }
 85
 86    return found;
 87}
 88
 89
 90bool
 91AlbumProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right ) const
 92{
 93    AlbumItem* p1 = sourceModel()->itemFromIndex( left );
 94    AlbumItem* p2 = sourceModel()->itemFromIndex( right );
 95
 96    if ( !p1 )
 97        return true;
 98    if ( !p2 )
 99        return false;
100
101    if ( p1->album().isNull() || p1->album()->artist().isNull() )
102        return true;
103
104    if ( p2->album().isNull() || p2->album()->artist().isNull() )
105        return false;
106
107    if ( p1->album()->artist()->name() == p2->album()->artist()->name() )
108    {
109        return QString::localeAwareCompare( p1->album()->name(), p2->album()->name() ) < 0;
110    }
111
112    return QString::localeAwareCompare( p1->album()->artist()->name(), p2->album()->artist()->name() ) < 0;
113}
114
115
116void
117AlbumProxyModel::removeIndex( const QModelIndex& index )
118{
119    qDebug() << Q_FUNC_INFO;
120
121    if ( !sourceModel() )
122        return;
123    if ( index.column() > 0 )
124        return;
125
126    sourceModel()->removeIndex( mapToSource( index ) );
127}
128
129
130void
131AlbumProxyModel::removeIndexes( const QList<QModelIndex>& indexes )
132{
133    if ( !sourceModel() )
134        return;
135
136    foreach( const QModelIndex& idx, indexes )
137    {
138        removeIndex( idx );
139    }
140}
141
142
143Tomahawk::playlistinterface_ptr
144AlbumProxyModel::playlistInterface()
145{
146    if ( m_playlistInterface.isNull() )
147    {
148        m_playlistInterface = Tomahawk::playlistinterface_ptr( new Tomahawk::AlbumProxyModelPlaylistInterface( this ) );
149    }
150
151    return m_playlistInterface;
152}