1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-10 16:14:40 +02:00

* Update GridItemDelegate's buttons when current index has changed.

This commit is contained in:
Christian Muehlhaeuser
2012-12-09 09:11:13 +01:00
parent 5e5a95716c
commit 1fd8221181
2 changed files with 43 additions and 85 deletions

View File

@@ -58,6 +58,8 @@ GridItemDelegate::GridItemDelegate( QAbstractItemView* parent, PlayableProxyMode
connect( proxy, SIGNAL( rowsAboutToBeInserted( QModelIndex, int, int ) ), SLOT( modelChanged() ) ); connect( proxy, SIGNAL( rowsAboutToBeInserted( QModelIndex, int, int ) ), SLOT( modelChanged() ) );
connect( proxy, SIGNAL( rowsAboutToBeRemoved( QModelIndex, int, int ) ), SLOT( modelChanged() ) ); connect( proxy, SIGNAL( rowsAboutToBeRemoved( QModelIndex, int, int ) ), SLOT( modelChanged() ) );
connect( proxy->playlistInterface().data(), SIGNAL( currentIndexChanged() ), SLOT( onCurrentIndexChanged() ), Qt::UniqueConnection );
connect( m_view, SIGNAL( scrolledContents( int, int ) ), SLOT( onViewChanged() ) ); connect( m_view, SIGNAL( scrolledContents( int, int ) ), SLOT( onViewChanged() ) );
connect( m_view, SIGNAL( resized() ), SLOT( onViewChanged() ) ); connect( m_view, SIGNAL( resized() ), SLOT( onViewChanged() ) );
} }
@@ -239,9 +241,7 @@ void
GridItemDelegate::onPlayClicked( const QPersistentModelIndex& index ) GridItemDelegate::onPlayClicked( const QPersistentModelIndex& index )
{ {
QPoint pos = m_playButton[ index ]->pos(); QPoint pos = m_playButton[ index ]->pos();
foreach ( ImageButton* button, m_playButton ) clearButtons();
button->deleteLater();
m_playButton.clear();
AnimatedSpinner* spinner = new AnimatedSpinner( m_view ); AnimatedSpinner* spinner = new AnimatedSpinner( m_view );
spinner->setAutoCenter( false ); spinner->setAutoCenter( false );
@@ -255,23 +255,8 @@ GridItemDelegate::onPlayClicked( const QPersistentModelIndex& index )
PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
if ( item ) if ( item )
{ {
NewClosure( AudioEngine::instance(), SIGNAL( loading( Tomahawk::result_ptr ) ),
const_cast<GridItemDelegate*>(this), SLOT( onPlaybackStarted( QPersistentModelIndex ) ), QPersistentModelIndex( index ) );
m_closures.remove( index );
m_closures.insertMulti( index, NewClosure( AudioEngine::instance(), SIGNAL( started( Tomahawk::result_ptr ) ),
const_cast<GridItemDelegate*>(this), SLOT( onPlaylistChanged( QPersistentModelIndex ) ), QPersistentModelIndex( index ) ) );
m_closures.insertMulti( index, NewClosure( AudioEngine::instance(), SIGNAL( stopped() ),
const_cast<GridItemDelegate*>(this), SLOT( onPlaylistChanged( QPersistentModelIndex ) ), QPersistentModelIndex( index ) ) );
foreach ( _detail::Closure* closure, m_closures.values( index ) )
closure->setAutoDelete( false );
connect( AudioEngine::instance(), SIGNAL( stopped() ), SLOT( onPlaybackFinished() ) );
if ( !item->query().isNull() ) if ( !item->query().isNull() )
AudioEngine::instance()->playItem( Tomahawk::playlistinterface_ptr(), item->query() ); AudioEngine::instance()->playItem( m_model->playlistInterface(), item->query() );
else if ( !item->album().isNull() ) else if ( !item->album().isNull() )
AudioEngine::instance()->playItem( item->album() ); AudioEngine::instance()->playItem( item->album() );
else if ( !item->artist().isNull() ) else if ( !item->artist().isNull() )
@@ -418,18 +403,12 @@ GridItemDelegate::modelChanged()
m_hoveringOver = QPersistentModelIndex(); m_hoveringOver = QPersistentModelIndex();
m_hoverIndex = QPersistentModelIndex(); m_hoverIndex = QPersistentModelIndex();
foreach ( ImageButton* button, m_playButton ) clearButtons();
button->deleteLater();
m_playButton.clear();
foreach ( ImageButton* button, m_pauseButton )
button->deleteLater();
m_pauseButton.clear();
foreach ( QWidget* widget, m_spinner )
widget->deleteLater();
m_spinner.clear();
if ( GridView* view = qobject_cast< GridView* >( m_view ) ) if ( GridView* view = qobject_cast< GridView* >( m_view ) )
m_model = view->proxyModel(); m_model = view->proxyModel();
connect( m_model->playlistInterface().data(), SIGNAL( currentIndexChanged() ), SLOT( onCurrentIndexChanged() ), Qt::UniqueConnection );
} }
@@ -466,78 +445,58 @@ GridItemDelegate::onViewChanged()
void void
GridItemDelegate::onPlaybackFinished() GridItemDelegate::onPlaybackFinished()
{ {
foreach ( ImageButton* button, m_pauseButton ) clearButtons();
button->deleteLater();
m_pauseButton.clear();
emit stoppedPlaying( QModelIndex() ); emit stoppedPlaying( QModelIndex() );
} }
void void
GridItemDelegate::onPlaylistChanged( const QPersistentModelIndex& index ) GridItemDelegate::onPlaybackStarted( const QPersistentModelIndex& index )
{ {
PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); clearButtons();
if ( item ) createPauseButton( index );
{
bool finished = false;
if ( !item->query().isNull() ) emit startedPlaying( index );
{
if ( !item->query()->numResults() || AudioEngine::instance()->currentTrack() != item->query()->results().first() )
finished = true;
}
else if ( !item->album().isNull() )
{
if ( AudioEngine::instance()->currentTrackPlaylist() != item->album()->playlistInterface( Tomahawk::Mixed ) )
finished = true;
}
else if ( !item->artist().isNull() )
{
if ( AudioEngine::instance()->currentTrackPlaylist() != item->artist()->playlistInterface( Tomahawk::Mixed ) )
finished = true;
}
if ( finished )
{
foreach ( _detail::Closure* closure, m_closures.values( index ) )
closure->deleteLater();
if ( m_pauseButton.contains( index ) )
{
m_pauseButton[ index ]->deleteLater();
m_pauseButton.remove( index );
}
if ( m_spinner.contains( index ) )
{
m_spinner[ index ]->deleteLater();
m_spinner.remove( index );
}
emit stoppedPlaying( index );
}
}
} }
void void
GridItemDelegate::onPlaybackStarted( const QPersistentModelIndex& index ) GridItemDelegate::clearButtons()
{ {
if ( !m_spinner.contains( index ) ) foreach ( ImageButton* button, m_playButton )
return; button->deleteLater();
m_playButton.clear();
QPoint pos = m_spinner[ index ]->pos(); foreach ( ImageButton* button, m_pauseButton )
foreach ( QWidget* widget, m_spinner.values() ) button->deleteLater();
{ m_pauseButton.clear();
delete widget; foreach ( QWidget* widget, m_spinner )
} widget->deleteLater();
m_spinner.clear(); m_spinner.clear();
}
void
GridItemDelegate::onCurrentIndexChanged()
{
tDebug() << Q_FUNC_INFO << m_model-> currentIndex();
if ( m_model->currentIndex().isValid() )
{
onPlaybackStarted( m_model->currentIndex() );
}
else
onPlaybackFinished();
}
void
GridItemDelegate::createPauseButton( const QPersistentModelIndex& index )
{
ImageButton* button = new ImageButton( m_view ); ImageButton* button = new ImageButton( m_view );
button->setPixmap( TomahawkUtils::defaultPixmap( TomahawkUtils::PauseButton, TomahawkUtils::Original, QSize( 48, 48 ) ) ); button->setPixmap( TomahawkUtils::defaultPixmap( TomahawkUtils::PauseButton, TomahawkUtils::Original, QSize( 48, 48 ) ) );
button->setPixmap( TomahawkUtils::defaultPixmap( TomahawkUtils::PauseButtonPressed, TomahawkUtils::Original, QSize( 48, 48 ) ), QIcon::Off, QIcon::Active ); button->setPixmap( TomahawkUtils::defaultPixmap( TomahawkUtils::PauseButtonPressed, TomahawkUtils::Original, QSize( 48, 48 ) ), QIcon::Off, QIcon::Active );
button->setFixedSize( 48, 48 ); button->setFixedSize( 48, 48 );
button->move( pos ); button->move( m_view->visualRect( index ).center() - QPoint( 23, 23 ) );
button->setContentsMargins( 0, 0, 0, 0 ); button->setContentsMargins( 0, 0, 0, 0 );
button->setFocusPolicy( Qt::NoFocus ); button->setFocusPolicy( Qt::NoFocus );
button->installEventFilter( this ); button->installEventFilter( this );
@@ -546,8 +505,6 @@ GridItemDelegate::onPlaybackStarted( const QPersistentModelIndex& index )
connect( button, SIGNAL( clicked( bool ) ), AudioEngine::instance(), SLOT( playPause() ) ); connect( button, SIGNAL( clicked( bool ) ), AudioEngine::instance(), SLOT( playPause() ) );
m_pauseButton[ index ] = button; m_pauseButton[ index ] = button;
emit startedPlaying( index );
} }

View File

@@ -64,19 +64,21 @@ signals:
private slots: private slots:
void modelChanged(); void modelChanged();
void doUpdateIndex( const QPersistentModelIndex& idx ); void doUpdateIndex( const QPersistentModelIndex& idx );
void onCurrentIndexChanged();
void onViewChanged(); void onViewChanged();
void onPlaybackStarted( const QPersistentModelIndex& index ); void onPlaybackStarted( const QPersistentModelIndex& index );
void onPlaybackFinished(); void onPlaybackFinished();
void onPlayClicked( const QPersistentModelIndex& index ); void onPlayClicked( const QPersistentModelIndex& index );
void onPlaylistChanged( const QPersistentModelIndex& index );
void fadingFrameChanged( const QPersistentModelIndex& ); void fadingFrameChanged( const QPersistentModelIndex& );
void fadingFrameFinished( const QPersistentModelIndex& ); void fadingFrameFinished( const QPersistentModelIndex& );
private: private:
QTimeLine* createTimeline( QTimeLine::Direction direction ); QTimeLine* createTimeline( QTimeLine::Direction direction );
void createPauseButton( const QPersistentModelIndex& index );
void clearButtons();
QAbstractItemView* m_view; QAbstractItemView* m_view;
PlayableProxyModel* m_model; PlayableProxyModel* m_model;
@@ -94,7 +96,6 @@ private:
mutable QHash< QPersistentModelIndex, ImageButton* > m_playButton; mutable QHash< QPersistentModelIndex, ImageButton* > m_playButton;
mutable QHash< QPersistentModelIndex, ImageButton* > m_pauseButton; mutable QHash< QPersistentModelIndex, ImageButton* > m_pauseButton;
mutable QHash< QPersistentModelIndex, QTimeLine* > m_hoverFaders; mutable QHash< QPersistentModelIndex, QTimeLine* > m_hoverFaders;
mutable QHash< QPersistentModelIndex, _detail::Closure* > m_closures;
}; };
#endif // GRIDITEMDELEGATE_H #endif // GRIDITEMDELEGATE_H