/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. #include "SourcesProxyModel.h"
  21. #include "SourceList.h"
  22. #include "SourcesModel.h"
  23. #include "sourcetree/items/SourceItem.h"
  24. #include "utils/Logger.h"
  25. #include <QTreeView>
  26. SourcesProxyModel::SourcesProxyModel( SourcesModel* model, QObject* parent )
  27. : QSortFilterProxyModel( parent )
  28. , m_model( model )
  29. , m_filtered( false )
  30. {
  31. setDynamicSortFilter( true );
  32. setSortRole( SourcesModel::SortRole );
  33. setSourceModel( model );
  34. connect( model, SIGNAL( rowsInserted( QModelIndex, int, int ) ), SLOT( onModelChanged() ) );
  35. connect( model, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), SLOT( onModelChanged() ) );
  36. if ( model && model->metaObject()->indexOfSignal( "toggleExpandRequest(QPersistentModelIndex)" ) > -1 )
  37. connect( model, SIGNAL( toggleExpandRequest( QPersistentModelIndex ) ), this, SLOT( toggleExpandRequested( QPersistentModelIndex ) ), Qt::QueuedConnection );
  38. if ( model && model->metaObject()->indexOfSignal( "expandRequest(QPersistentModelIndex)" ) > -1 )
  39. connect( model, SIGNAL( expandRequest( QPersistentModelIndex ) ), this, SLOT( expandRequested( QPersistentModelIndex ) ), Qt::QueuedConnection );
  40. if ( model && model->metaObject()->indexOfSignal( "selectRequest(QPersistentModelIndex)" ) > -1 )
  41. connect( model, SIGNAL( selectRequest( QPersistentModelIndex ) ), this, SLOT( selectRequested( QPersistentModelIndex ) ), Qt::QueuedConnection );
  42. }
  43. void
  44. SourcesProxyModel::showOfflineSources( bool offlineSourcesShown )
  45. {
  46. m_filtered = !offlineSourcesShown;
  47. invalidateFilter();
  48. }
  49. bool
  50. SourcesProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
  51. {
  52. // always filter empty top-level groups
  53. SourceTreeItem* item = m_model->data( sourceModel()->index( sourceRow, 0, sourceParent ), SourcesModel::SourceTreeItemRole ).value< SourceTreeItem* >();
  54. if ( item && item->type() != SourcesModel::Divider && item->parent()->parent() == 0 && !item->children().count() )
  55. return false;
  56. if ( !m_filtered )
  57. return true;
  58. SourceItem* sti = qobject_cast< SourceItem* >( item );
  59. if ( sti )
  60. {
  61. if ( sti->source().isNull() || sti->source()->isOnline() )
  62. return true;
  63. else if ( m_model->sourcesWithViewPage().contains( sti->source() ) )
  64. return true;
  65. else
  66. return false;
  67. }
  68. // accept rows that aren't sources
  69. return true;
  70. }
  71. void
  72. SourcesProxyModel::selectRequested( const QPersistentModelIndex& idx )
  73. {
  74. emit selectRequest( QPersistentModelIndex( mapFromSource( idx ) ) );
  75. }
  76. void
  77. SourcesProxyModel::expandRequested( const QPersistentModelIndex& idx )
  78. {
  79. emit expandRequest( QPersistentModelIndex( mapFromSource( idx ) ) );
  80. }
  81. void
  82. SourcesProxyModel::toggleExpandRequested( const QPersistentModelIndex& idx )
  83. {
  84. emit toggleExpandRequest( QPersistentModelIndex( mapFromSource( idx ) ) );
  85. }
  86. bool
  87. SourcesProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right ) const
  88. {
  89. if ( m_model->data( left, SourcesModel::SortRole ) != m_model->data( right, SourcesModel::SortRole ) )
  90. return ( m_model->data( left, SourcesModel::SortRole ).toInt() < m_model->data( right, SourcesModel::SortRole ).toInt() );
  91. const QString& lefts = left.data().toString().toLower();
  92. const QString& rights = right.data().toString().toLower();
  93. if ( lefts == rights )
  94. return ( m_model->data( left, SourcesModel::IDRole ).toInt() < m_model->data( right, SourcesModel::IDRole ).toInt() );
  95. else
  96. return QString::localeAwareCompare( lefts, rights ) < 0;
  97. }
  98. void
  99. SourcesProxyModel::onModelChanged()
  100. {
  101. invalidateFilter();
  102. }