PageRenderTime 49ms CodeModel.GetById 34ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sourcetree/sourcesproxymodel.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 130 lines | 81 code | 28 blank | 21 comment | 22 complexity | bb33d178c66a7b01e8813337197e6529 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  5 *   Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
  6 *
  7 *   Tomahawk is free software: you can redistribute it and/or modify
  8 *   it under the terms of the GNU General Public License as published by
  9 *   the Free Software Foundation, either version 3 of the License, or
 10 *   (at your option) any later version.
 11 *
 12 *   Tomahawk is distributed in the hope that it will be useful,
 13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15 *   GNU General Public License for more details.
 16 *
 17 *   You should have received a copy of the GNU General Public License
 18 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 19 */
 20
 21#include "SourcesProxyModel.h"
 22
 23#include "SourceList.h"
 24#include "SourcesModel.h"
 25#include "sourcetree/items/SourceItem.h"
 26
 27#include "utils/Logger.h"
 28
 29#include <QTreeView>
 30
 31SourcesProxyModel::SourcesProxyModel( SourcesModel* model, QObject* parent )
 32    : QSortFilterProxyModel( parent )
 33    , m_model( model )
 34    , m_filtered( false )
 35{
 36    setDynamicSortFilter( true );
 37    setSortRole( SourcesModel::SortRole );
 38
 39    setSourceModel( model );
 40
 41    connect( model, SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( onModelChanged() ) );
 42    connect( model, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), SLOT( onModelChanged() ) );
 43
 44    if ( model && model->metaObject()->indexOfSignal( "toggleExpandRequest(QPersistentModelIndex)" ) > -1 )
 45        connect( model, SIGNAL( toggleExpandRequest( QPersistentModelIndex ) ), this, SLOT( toggleExpandRequested( QPersistentModelIndex ) ), Qt::QueuedConnection );
 46    if ( model && model->metaObject()->indexOfSignal( "expandRequest(QPersistentModelIndex)" ) > -1 )
 47        connect( model, SIGNAL( expandRequest( QPersistentModelIndex ) ), this, SLOT( expandRequested( QPersistentModelIndex ) ), Qt::QueuedConnection );
 48    if ( model && model->metaObject()->indexOfSignal( "selectRequest(QPersistentModelIndex)" ) > -1 )
 49        connect( model, SIGNAL( selectRequest( QPersistentModelIndex ) ), this, SLOT( selectRequested( QPersistentModelIndex ) ), Qt::QueuedConnection );
 50}
 51
 52
 53void
 54SourcesProxyModel::showOfflineSources( bool offlineSourcesShown )
 55{
 56    m_filtered = !offlineSourcesShown;
 57    invalidateFilter();
 58}
 59
 60
 61bool
 62SourcesProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
 63{
 64    // always filter empty top-level groups
 65    SourceTreeItem* item = m_model->data( sourceModel()->index( sourceRow, 0, sourceParent ), SourcesModel::SourceTreeItemRole ).value< SourceTreeItem* >();
 66
 67    if ( item && item->type() != SourcesModel::Divider && item->parent()->parent() == 0 && !item->children().count() )
 68        return false;
 69
 70    if ( !m_filtered )
 71        return true;
 72
 73    SourceItem* sti = qobject_cast< SourceItem* >( item );
 74    if ( sti )
 75    {
 76        if ( sti->source().isNull() || sti->source()->isOnline() )
 77            return true;
 78        else if ( m_model->sourcesWithViewPage().contains( sti->source() ) )
 79            return true;
 80        else
 81            return false;
 82    }
 83
 84    // accept rows that aren't sources
 85    return true;
 86}
 87
 88
 89void
 90SourcesProxyModel::selectRequested( const QPersistentModelIndex& idx )
 91{
 92    emit selectRequest( QPersistentModelIndex( mapFromSource( idx ) ) );
 93}
 94
 95
 96void
 97SourcesProxyModel::expandRequested( const QPersistentModelIndex& idx )
 98{
 99    emit expandRequest( QPersistentModelIndex( mapFromSource( idx ) ) );
100}
101
102
103void
104SourcesProxyModel::toggleExpandRequested( const QPersistentModelIndex& idx )
105{
106    emit toggleExpandRequest( QPersistentModelIndex( mapFromSource( idx ) ) );
107}
108
109
110bool
111SourcesProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right ) const
112{
113    if ( m_model->data( left, SourcesModel::SortRole ) != m_model->data( right, SourcesModel::SortRole ) )
114        return ( m_model->data( left, SourcesModel::SortRole ).toInt() < m_model->data( right, SourcesModel::SortRole ).toInt() );
115
116    const QString& lefts = left.data().toString().toLower();
117    const QString& rights = right.data().toString().toLower();
118
119    if ( lefts == rights )
120        return ( m_model->data( left, SourcesModel::IDRole ).toInt() < m_model->data( right, SourcesModel::IDRole ).toInt() );
121    else
122        return QString::localeAwareCompare( lefts, rights ) < 0;
123}
124
125
126void
127SourcesProxyModel::onModelChanged()
128{
129    invalidateFilter();
130}