1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 08:19:42 +01:00

* Updated Playable- & TreeModel to handle new PlaylistInterfaces.

This commit is contained in:
Christian Muehlhaeuser 2012-11-28 04:52:07 +01:00
parent f8917de6a3
commit ad7263477f
8 changed files with 160 additions and 30 deletions

View File

@ -56,6 +56,18 @@ PlayableModel::~PlayableModel()
}
QModelIndex
PlayableModel::createIndex( int row, int column, PlayableItem* item ) const
{
if ( item->query() )
{
connect( item->query().data(), SIGNAL( playableStateChanged( bool ) ), SLOT( onQueryBecamePlayable( bool ) ), Qt::UniqueConnection );
}
return QAbstractItemModel::createIndex( row, column, item );
}
QModelIndex
PlayableModel::index( int row, int column, const QModelIndex& parent ) const
{
@ -192,7 +204,7 @@ PlayableModel::queryData( const query_ptr& query, int column, int role ) const
if ( query->albumpos() != 0 )
{
tPos = QString::number( query->albumpos() );
if( query->discnumber() == 0 )
if ( query->discnumber() == 0 )
return tPos;
else
return QString( "%1.%2" ).arg( QString::number( query->discnumber() ) )
@ -542,7 +554,7 @@ PlayableModel::insertInternal( const QList< T >& items, int row )
int i = 0;
PlayableItem* plitem;
foreach( const T& item, items )
foreach ( const T& item, items )
{
plitem = new PlayableItem( item, m_rootItem, row + i );
plitem->index = createIndex( row + i, 0, plitem );
@ -846,9 +858,32 @@ PlayableModel::setIcon( const QPixmap& pixmap )
}
void
PlayableModel::onQueryBecamePlayable( bool playable )
{
Tomahawk::Query* q = qobject_cast< Query* >( sender() );
if ( !q )
{
// Track has been removed from the playlist by now
return;
}
Tomahawk::query_ptr query = q->weakRef().toStrongRef();
PlayableItem* item = itemFromQuery( query );
if ( item )
{
emit indexPlayable( item->index );
}
}
PlayableItem*
PlayableModel::itemFromQuery( const Tomahawk::query_ptr& query ) const
{
if ( !query )
return 0;
for ( int i = 0; i < rowCount( QModelIndex() ); i++ )
{
QModelIndex idx = index( i, 0, QModelIndex() );
@ -862,3 +897,24 @@ PlayableModel::itemFromQuery( const Tomahawk::query_ptr& query ) const
tDebug() << "Could not find item for query:" << query->toString();
return 0;
}
PlayableItem*
PlayableModel::itemFromResult( const Tomahawk::result_ptr& result ) const
{
if ( !result )
return 0;
for ( int i = 0; i < rowCount( QModelIndex() ); i++ )
{
QModelIndex idx = index( i, 0, QModelIndex() );
PlayableItem* item = itemFromIndex( idx );
if ( item && item->result() == result )
{
return item;
}
}
tDebug() << "Could not find item for result:" << result->toString();
return 0;
}

View File

@ -38,7 +38,8 @@ class DLLEXPORT PlayableModel : public QAbstractItemModel
Q_OBJECT
public:
enum Columns {
enum Columns
{
Artist = 0,
Track = 1,
Composer = 2,
@ -102,6 +103,7 @@ public:
PlayableItem* itemFromIndex( const QModelIndex& index ) const;
PlayableItem* itemFromQuery( const Tomahawk::query_ptr& query ) const;
PlayableItem* itemFromResult( const Tomahawk::result_ptr& result ) const;
/// Returns a flat list of all tracks in this model
QList< Tomahawk::query_ptr > queries() const;
@ -119,6 +121,7 @@ signals:
void loadingStarted();
void loadingFinished();
void indexPlayable( const QModelIndex& index );
void changed();
public slots:
@ -150,9 +153,11 @@ public slots:
protected:
PlayableItem* rootItem() const { return m_rootItem; }
QModelIndex createIndex( int row, int column, PlayableItem* item = 0 ) const;
private slots:
void onDataChanged();
void onQueryBecamePlayable( bool playable );
void onPlaybackStarted( const Tomahawk::result_ptr& result );
void onPlaybackStopped();

View File

@ -38,6 +38,8 @@ PlayableProxyModel::PlayableProxyModel( QObject* parent )
, m_maxVisibleItems( -1 )
, m_style( Detailed )
{
m_playlistInterface = Tomahawk::playlistinterface_ptr( new Tomahawk::PlayableProxyModelPlaylistInterface( this ) );
setFilterCaseSensitivity( Qt::CaseInsensitive );
setSortCaseSensitivity( Qt::CaseInsensitive );
setDynamicSortFilter( true );
@ -50,6 +52,20 @@ PlayableProxyModel::PlayableProxyModel( QObject* parent )
}
Tomahawk::playlistinterface_ptr
PlayableProxyModel::playlistInterface() const
{
return m_playlistInterface;
}
void
PlayableProxyModel::setPlaylistInterface( const Tomahawk::playlistinterface_ptr& playlistInterface )
{
m_playlistInterface = playlistInterface;
}
QString
PlayableProxyModel::guid() const
{
@ -90,6 +106,7 @@ PlayableProxyModel::setSourcePlayableModel( PlayableModel* sourceModel )
{
disconnect( m_model, SIGNAL( loadingStarted() ), this, SIGNAL( loadingStarted() ) );
disconnect( m_model, SIGNAL( loadingFinished() ), this, SIGNAL( loadingFinished() ) );
disconnect( m_model, SIGNAL( indexPlayable( QModelIndex ) ), this, SLOT( onIndexPlayable( QModelIndex ) ) );
}
m_model = sourceModel;
@ -98,6 +115,7 @@ PlayableProxyModel::setSourcePlayableModel( PlayableModel* sourceModel )
{
connect( m_model, SIGNAL( loadingStarted() ), SIGNAL( loadingStarted() ) );
connect( m_model, SIGNAL( loadingFinished() ), SIGNAL( loadingFinished() ) );
connect( m_model, SIGNAL( indexPlayable( QModelIndex ) ), SLOT( onIndexPlayable( QModelIndex ) ) );
}
QSortFilterProxyModel::setSourceModel( m_model );
@ -482,18 +500,6 @@ PlayableProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right
}
Tomahawk::playlistinterface_ptr
PlayableProxyModel::playlistInterface()
{
if ( m_playlistInterface.isNull() )
{
m_playlistInterface = Tomahawk::playlistinterface_ptr( new Tomahawk::PlayableProxyModelPlaylistInterface( this ) );
}
return m_playlistInterface;
}
int
PlayableProxyModel::columnCount( const QModelIndex& parent ) const
{
@ -616,3 +622,20 @@ PlayableProxyModel::setFilter( const QString& pattern )
emit filterChanged( pattern );
}
}
void
PlayableProxyModel::setCurrentIndex( const QModelIndex& index )
{
tDebug() << Q_FUNC_INFO << QThread::currentThread();
m_model->setCurrentItem( mapToSource( index ) );
emit currentIndexChanged();
tDebug() << Q_FUNC_INFO << QThread::currentThread() << "Finished";
}
void
PlayableProxyModel::onIndexPlayable( const QModelIndex& index )
{
emit indexPlayable( mapFromSource( index ) );
}

View File

@ -53,7 +53,7 @@ public:
void setStyle( PlayableProxyModel::PlayableItemStyle style ) { m_style = style; }
virtual QPersistentModelIndex currentIndex() const { return mapFromSource( m_model->currentItem() ); }
virtual void setCurrentIndex( const QModelIndex& index ) { m_model->setCurrentItem( mapToSource( index ) ); }
virtual void setCurrentIndex( const QModelIndex& index );
virtual void removeIndex( const QModelIndex& index );
virtual void removeIndexes( const QModelIndexList& indexes );
@ -70,8 +70,10 @@ public:
virtual PlayableItem* itemFromIndex( const QModelIndex& index ) const { return sourceModel()->itemFromIndex( index ); }
virtual PlayableItem* itemFromQuery( const Tomahawk::query_ptr& query ) const { return sourceModel()->itemFromQuery( query ); }
virtual PlayableItem* itemFromResult( const Tomahawk::result_ptr& result ) const { return sourceModel()->itemFromResult( result ); }
virtual Tomahawk::playlistinterface_ptr playlistInterface();
virtual Tomahawk::playlistinterface_ptr playlistInterface() const;
void setPlaylistInterface( const Tomahawk::playlistinterface_ptr& playlistInterface );
QList< double > columnWeights() const;
@ -91,12 +93,18 @@ signals:
void loadingStarted();
void loadingFinished();
void indexPlayable( const QModelIndex& index );
void currentIndexChanged();
protected:
virtual bool filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const;
virtual bool lessThan( const QModelIndex& left, const QModelIndex& right ) const;
Tomahawk::playlistinterface_ptr m_playlistInterface;
private slots:
void onIndexPlayable( const QModelIndex& index );
private:
virtual bool lessThan( int column, const Tomahawk::query_ptr& left, const Tomahawk::query_ptr& right ) const;

View File

@ -421,3 +421,32 @@ TreeModel::indexFromAlbum( const Tomahawk::album_ptr& album ) const
tDebug() << "Could not find item for album:" << album->name() << album->artist()->name();
return QModelIndex();
}
QModelIndex
TreeModel::indexFromResult( const Tomahawk::result_ptr& result ) const
{
QModelIndex artistIdx = indexFromArtist( result->artist() );
for ( int i = 0; i < rowCount( artistIdx ); i++ )
{
QModelIndex idx = index( i, 0, artistIdx );
PlayableItem* albumItem = itemFromIndex( idx );
if ( albumItem && albumItem->album() == result->album() )
{
for ( int j = 0; j < rowCount( idx ); j++ )
{
QModelIndex subidx = index( j, 0, idx );
PlayableItem* item = itemFromIndex( subidx );
if ( item && item->result() == result )
{
return subidx;
}
}
break;
}
}
tDebug() << "Could not find item for result:" << result->toString();
return QModelIndex();
}

View File

@ -63,6 +63,7 @@ public:
QModelIndex indexFromArtist( const Tomahawk::artist_ptr& artist ) const;
QModelIndex indexFromAlbum( const Tomahawk::album_ptr& album ) const;
QModelIndex indexFromResult( const Tomahawk::result_ptr& result ) const;
public slots:
void addAlbums( const QModelIndex& parent, const QList<Tomahawk::album_ptr>& albums );

View File

@ -36,6 +36,7 @@ TreeProxyModel::TreeProxyModel( QObject* parent )
, m_artistsFilterCmd( 0 )
, m_model( 0 )
{
setPlaylistInterface( Tomahawk::playlistinterface_ptr( new Tomahawk::TreeProxyModelPlaylistInterface( this ) ) );
}
@ -347,13 +348,22 @@ TreeProxyModel::textForItem( PlayableItem* item ) const
}
Tomahawk::playlistinterface_ptr
TreeProxyModel::playlistInterface()
QModelIndex
TreeProxyModel::indexFromArtist( const Tomahawk::artist_ptr& artist ) const
{
if ( m_playlistInterface.isNull() )
{
m_playlistInterface = Tomahawk::playlistinterface_ptr( new Tomahawk::TreeProxyModelPlaylistInterface( this ) );
}
return m_playlistInterface;
return mapFromSource( m_model->indexFromArtist( artist ) );
}
QModelIndex
TreeProxyModel::indexFromAlbum( const Tomahawk::album_ptr& album ) const
{
return mapFromSource( m_model->indexFromAlbum( album ) );
}
QModelIndex
TreeProxyModel::indexFromResult( const Tomahawk::result_ptr& result ) const
{
return mapFromSource( m_model->indexFromResult( result ) );
}

View File

@ -45,11 +45,11 @@ public:
// workaround overloaded-virtual warning
virtual void setSourcePlayableModel( PlayableModel* ) { Q_ASSERT( false ); }
virtual Tomahawk::playlistinterface_ptr playlistInterface();
virtual void setFilter( const QString& pattern );
signals:
QModelIndex indexFromArtist( const Tomahawk::artist_ptr& artist ) const;
QModelIndex indexFromAlbum( const Tomahawk::album_ptr& album ) const;
QModelIndex indexFromResult( const Tomahawk::result_ptr& result ) const;
protected:
bool filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const;
@ -78,8 +78,6 @@ private:
QString m_filter;
TreeModel* m_model;
Tomahawk::playlistinterface_ptr m_playlistInterface;
};
#endif // TREEPROXYMODEL_H