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

Try out some fading

This commit is contained in:
Leo Franchi
2012-05-24 21:05:35 -04:00
parent 216e43a502
commit 052093f1ea
3 changed files with 97 additions and 38 deletions

View File

@@ -23,6 +23,7 @@
#include <QPainter>
#include <QAbstractItemView>
#include <QMouseEvent>
#include <QTimeLine>
#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 )
@@ -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();
@@ -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() );
}
@@ -325,8 +325,29 @@ 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 );
}
@@ -485,6 +506,38 @@ AlbumItemDelegate::onPlaybackStarted( const QPersistentModelIndex& index )
}
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 )
{

View File

@@ -21,9 +21,11 @@
#define ALBUMITEMDELEGATE_H
#include <QStyledItemDelegate>
#include <QTimeLine>
#include "DllMacro.h"
class QTimeLine;
namespace Tomahawk {
class PixmapDelegateFader;
}
@@ -60,7 +62,11 @@ private slots:
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

View File

@@ -29,8 +29,6 @@
using namespace Tomahawk;
#define COVER_FADEIN 1000
QWeakPointer< TomahawkUtils::SharedTimeLine > PixmapDelegateFader::s_stlInstance = QWeakPointer< TomahawkUtils::SharedTimeLine >();