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