From 052093f1eabc7217adb1128e6d17cd344e401e69 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Thu, 24 May 2012 21:05:35 -0400 Subject: [PATCH] Try out some fading --- .../playlist/AlbumItemDelegate.cpp | 119 +++++++++++++----- src/libtomahawk/playlist/AlbumItemDelegate.h | 12 +- src/libtomahawk/utils/PixmapDelegateFader.cpp | 4 +- 3 files changed, 97 insertions(+), 38 deletions(-) diff --git a/src/libtomahawk/playlist/AlbumItemDelegate.cpp b/src/libtomahawk/playlist/AlbumItemDelegate.cpp index 980454817..a01abac36 100644 --- a/src/libtomahawk/playlist/AlbumItemDelegate.cpp +++ b/src/libtomahawk/playlist/AlbumItemDelegate.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "Artist.h" #include "Query.h" @@ -42,6 +43,10 @@ #include "widgets/ImageButton.h" #include "utils/Logger.h" +namespace { + static const int FADE_DURATION = 90; +}; + AlbumItemDelegate::AlbumItemDelegate( QAbstractItemView* parent, AlbumProxyModel* proxy ) : QStyledItemDelegate( (QObject*)parent ) @@ -50,7 +55,7 @@ AlbumItemDelegate::AlbumItemDelegate( QAbstractItemView* parent, AlbumProxyModel { if ( m_view && m_view->metaObject()->indexOfSignal( "modelChanged()" ) > -1 ) connect( m_view, SIGNAL( modelChanged() ), this, SLOT( modelChanged() ) ); - + connect( m_view, SIGNAL( scrolledContents( int, int ) ), SLOT( onScrolled( int, int ) ) ); } @@ -121,33 +126,28 @@ AlbumItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QPixmap cover = fader->currentPixmap(); - if ( false && option.state & QStyle::State_Selected ) - { -#if defined(Q_WS_MAC) || defined(Q_WS_WIN) - painter->save(); - - QPainterPath border; - border.addRoundedRect( r.adjusted( -2, -2, 2, 2 ), 3, 3 ); - QPen borderPen( QColor( 86, 170, 243 ) ); - borderPen.setWidth( 5 ); - painter->setPen( borderPen ); - painter->drawPath( border ); - - painter->restore(); -#else - opt.palette.setColor( QPalette::Text, opt.palette.color( QPalette::HighlightedText ) ); -#endif - } - painter->drawPixmap( r, cover ); - if ( m_hoverIndex == index ) + + qreal opacity = -1.; + if ( m_hoverFaders.contains( index ) ) + { + const qreal pct = ( m_hoverFaders[ index ]->currentFrame() / 100. ); + opacity = 0.15 - pct * 0.15; + } + else if ( m_hoverIndex != index ) + { + opacity = 0.15; + } + + + if ( opacity > -1. ) { painter->save(); - painter->setPen( QColor( 33, 33, 33 ) ); - painter->setBrush( QColor( 33, 33, 33 ) ); - painter->setOpacity( 0.5 ); + painter->setPen( QColor( 240, 240, 240 ) ); + painter->setBrush( QColor( 240, 240, 240 ) ); + painter->setOpacity( opacity ); painter->drawRect( r ); painter->restore(); @@ -236,12 +236,12 @@ AlbumItemDelegate::onPlayClicked( const QPersistentModelIndex& index ) spinner->installEventFilter( this ); m_spinner[ index ] = spinner; - + PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) ); if ( item ) { _detail::Closure* closure; - + closure = NewClosure( AudioEngine::instance(), SIGNAL( loading( Tomahawk::result_ptr ) ), const_cast(this), SLOT( onPlaybackStarted( QPersistentModelIndex ) ), QPersistentModelIndex( index ) ); @@ -276,8 +276,8 @@ AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const bool hoveringArtist = false; if ( m_artistNameRects.contains( index ) ) { - QRect artistNameRect = m_artistNameRects[ index ]; - QMouseEvent* ev = static_cast< QMouseEvent* >( event ); + const QRect artistNameRect = m_artistNameRects[ index ]; + const QMouseEvent* ev = static_cast< QMouseEvent* >( event ); hoveringArtist = artistNameRect.contains( ev->pos() ); } @@ -288,7 +288,7 @@ AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const if ( index != idx ) m_playButton.take( idx )->deleteLater(); } - + if ( !m_playButton.contains( index ) && !m_spinner.contains( index ) && !m_pauseButton.contains( index ) ) { foreach ( ImageButton* button, m_playButton ) @@ -304,7 +304,7 @@ AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const button->setFocusPolicy( Qt::NoFocus ); button->installEventFilter( this ); button->show(); - + NewClosure( button, SIGNAL( clicked( bool ) ), const_cast(this), SLOT( onPlayClicked( QPersistentModelIndex ) ), QPersistentModelIndex( index ) ); @@ -325,11 +325,32 @@ AlbumItemDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const if ( m_hoverIndex != index ) { + if ( m_hoverIndex.isValid() ) + { + QTimeLine* fadeOut = createTimeline( QTimeLine::Backward ); + _detail::Closure* c = NewClosure( fadeOut, SIGNAL( frameChanged( int ) ), this, SLOT( fadingFrameChanged( QPersistentModelIndex ) ), QPersistentModelIndex( m_hoverIndex ) ); + c->setAutoDelete( false ); + c = NewClosure( fadeOut, SIGNAL( finished() ), this, SLOT( fadingFrameFinished( QPersistentModelIndex ) ), QPersistentModelIndex( m_hoverIndex ) ); + c->setAutoDelete( false ); + m_hoverFaders[ m_hoverIndex ] = fadeOut; + fadeOut->start(); + } + emit updateIndex( m_hoverIndex ); m_hoverIndex = index; + + QTimeLine* fadeIn = createTimeline( QTimeLine::Forward ); + _detail::Closure* c = NewClosure( fadeIn, SIGNAL( frameChanged( int ) ), this, SLOT( fadingFrameChanged( QPersistentModelIndex ) ), QPersistentModelIndex( index ) ); + c->setAutoDelete( false ); + c = NewClosure( fadeIn, SIGNAL( finished() ), this, SLOT( fadingFrameFinished( QPersistentModelIndex ) ), QPersistentModelIndex( index ) ); + c->setAutoDelete( false ); + + m_hoverFaders[ index ] = fadeIn; + fadeIn->start(); + emit updateIndex( index ); } - + event->accept(); return true; } @@ -443,7 +464,7 @@ AlbumItemDelegate::onPlaylistChanged( const QPersistentModelIndex& index ) if ( AudioEngine::instance()->currentTrackPlaylist() != item->artist()->playlistInterface( Tomahawk::Mixed ) ) finished = true; } - + if ( finished ) { if ( m_pauseButton.contains( index ) ) @@ -468,7 +489,7 @@ AlbumItemDelegate::onPlaybackStarted( const QPersistentModelIndex& index ) delete widget; } m_spinner.clear(); - + ImageButton* button = new ImageButton( m_view ); button->setPixmap( RESPATH "images/pause-rest.png" ); button->setPixmap( RESPATH "images/pause-pressed.png", QIcon::Off, QIcon::Active ); @@ -478,13 +499,45 @@ AlbumItemDelegate::onPlaybackStarted( const QPersistentModelIndex& index ) button->setFocusPolicy( Qt::NoFocus ); button->installEventFilter( this ); button->show(); - + connect( button, SIGNAL( clicked( bool ) ), AudioEngine::instance(), SLOT( playPause() ) ); m_pauseButton[ index ] = button; } +void +AlbumItemDelegate::fadingFrameChanged( const QPersistentModelIndex& idx ) +{ + emit updateIndex( idx ); +} + + +void +AlbumItemDelegate::fadingFrameFinished( const QPersistentModelIndex& idx ) +{ + if ( m_hoverFaders.contains( idx ) ) + { + m_hoverFaders.take( idx )->deleteLater(); + emit updateIndex( idx ); + } +} + + +QTimeLine* +AlbumItemDelegate::createTimeline( QTimeLine::Direction direction ) +{ + QTimeLine* timeline = new QTimeLine( FADE_DURATION, this ); + timeline->setDirection( direction ); + timeline->setCurveShape( QTimeLine::LinearCurve ); + timeline->setUpdateInterval( 30 ); + timeline->setStartFrame( 0 ); + timeline->setEndFrame( 100 ); + + return timeline; +} + + bool AlbumItemDelegate::eventFilter( QObject* obj, QEvent* event ) { diff --git a/src/libtomahawk/playlist/AlbumItemDelegate.h b/src/libtomahawk/playlist/AlbumItemDelegate.h index 0715e781d..1f38d76a9 100644 --- a/src/libtomahawk/playlist/AlbumItemDelegate.h +++ b/src/libtomahawk/playlist/AlbumItemDelegate.h @@ -21,9 +21,11 @@ #define ALBUMITEMDELEGATE_H #include +#include #include "DllMacro.h" +class QTimeLine; namespace Tomahawk { class PixmapDelegateFader; } @@ -52,15 +54,19 @@ signals: private slots: void modelChanged(); void doUpdateIndex( const QPersistentModelIndex& idx ); - + void onScrolled( int dx, int dy ); void onPlaybackStarted( const QPersistentModelIndex& index ); void onPlaybackFinished(); - + void onPlayClicked( const QPersistentModelIndex& index ); void onPlaylistChanged( const QPersistentModelIndex& index ); + void fadingFrameChanged( const QPersistentModelIndex& ); + void fadingFrameFinished( const QPersistentModelIndex& ); private: + QTimeLine* createTimeline( QTimeLine::Direction direction ); + QAbstractItemView* m_view; AlbumProxyModel* m_model; @@ -75,6 +81,8 @@ private: mutable QHash< QPersistentModelIndex, QWidget* > m_spinner; mutable QHash< QPersistentModelIndex, ImageButton* > m_playButton; mutable QHash< QPersistentModelIndex, ImageButton* > m_pauseButton; + + mutable QHash< QPersistentModelIndex, QTimeLine* > m_hoverFaders; }; #endif // ALBUMITEMDELEGATE_H diff --git a/src/libtomahawk/utils/PixmapDelegateFader.cpp b/src/libtomahawk/utils/PixmapDelegateFader.cpp index 7fe2f5dab..867a4d26a 100644 --- a/src/libtomahawk/utils/PixmapDelegateFader.cpp +++ b/src/libtomahawk/utils/PixmapDelegateFader.cpp @@ -29,8 +29,6 @@ using namespace Tomahawk; -#define COVER_FADEIN 1000 - QWeakPointer< TomahawkUtils::SharedTimeLine > PixmapDelegateFader::s_stlInstance = QWeakPointer< TomahawkUtils::SharedTimeLine >(); @@ -126,7 +124,7 @@ void PixmapDelegateFader::setSize( const QSize& size ) { m_size = size; - + if ( m_defaultImage ) { // No cover loaded yet, use default and don't fade in