1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-04 05:07:27 +02:00

Use shared timeline for pixmapdelegatefader.

This commit is contained in:
Jeff Mitchell
2012-04-08 09:49:39 -04:00
parent 546abf4a6d
commit ff3770a464
4 changed files with 77 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2010-2012, Leo Franchi <lfranchi@kde.org> * Copyright 2010-2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2012, Jeff Mitchell <jeffe@tomahawk-player.org>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -20,16 +21,33 @@
#include "tomahawkutilsgui.h" #include "tomahawkutilsgui.h"
#include <QPainter> #include <QPainter>
#include <QBuffer>
#include <QPaintEngine> #include <QPaintEngine>
#include <QTimer>
using namespace Tomahawk; using namespace Tomahawk;
#define COVER_FADEIN 1000 #define COVER_FADEIN 1000
QWeakPointer< TomahawkUtils::SharedTimeLine > PixmapDelegateFader::s_stlInstance = QWeakPointer< TomahawkUtils::SharedTimeLine >();
QWeakPointer< TomahawkUtils::SharedTimeLine >
PixmapDelegateFader::stlInstance()
{
if ( s_stlInstance.isNull() )
s_stlInstance = QWeakPointer< TomahawkUtils::SharedTimeLine> ( new TomahawkUtils::SharedTimeLine() );
return s_stlInstance;
}
PixmapDelegateFader::PixmapDelegateFader( const artist_ptr& artist, const QSize& size, TomahawkUtils::ImageMode mode, bool forceLoad ) PixmapDelegateFader::PixmapDelegateFader( const artist_ptr& artist, const QSize& size, TomahawkUtils::ImageMode mode, bool forceLoad )
: m_artist( artist ) : m_artist( artist )
, m_size( size ) , m_size( size )
, m_mode( mode ) , m_mode( mode )
, m_startFrame( 0 )
, m_connectedToStl( false )
, m_fadePct( 100 )
{ {
if ( !m_artist.isNull() ) if ( !m_artist.isNull() )
{ {
@@ -44,6 +62,9 @@ PixmapDelegateFader::PixmapDelegateFader( const album_ptr& album, const QSize& s
: m_album( album ) : m_album( album )
, m_size( size ) , m_size( size )
, m_mode( mode ) , m_mode( mode )
, m_startFrame( 0 )
, m_connectedToStl( false )
, m_fadePct( 100 )
{ {
if ( !m_album.isNull() ) if ( !m_album.isNull() )
{ {
@@ -59,6 +80,9 @@ PixmapDelegateFader::PixmapDelegateFader( const query_ptr& track, const QSize& s
: m_track( track ) : m_track( track )
, m_size( size ) , m_size( size )
, m_mode( mode ) , m_mode( mode )
, m_startFrame( 0 )
, m_connectedToStl( false )
, m_fadePct( 100 )
{ {
if ( !m_track.isNull() ) if ( !m_track.isNull() )
{ {
@@ -82,12 +106,7 @@ PixmapDelegateFader::init()
m_current = QPixmap( m_size ); m_current = QPixmap( m_size );
m_current.fill( Qt::transparent ); m_current.fill( Qt::transparent );
m_crossfadeTimeline.setDuration( COVER_FADEIN ); stlInstance().data()->setUpdateInterval( 20 );
m_crossfadeTimeline.setUpdateInterval( 20 );
m_crossfadeTimeline.setFrameRange( 0, 1000 );
m_crossfadeTimeline.setDirection( QTimeLine::Forward );
connect( &m_crossfadeTimeline, SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) );
connect( &m_crossfadeTimeline, SIGNAL( finished() ), this, SLOT( onAnimationFinished() ) );
if ( m_currentReference.isNull() ) if ( m_currentReference.isNull() )
{ {
@@ -102,7 +121,11 @@ PixmapDelegateFader::init()
return; return;
} }
m_crossfadeTimeline.start(); stlInstance().data()->setUpdateInterval( 20 );
m_startFrame = stlInstance().data()->currentFrame();
m_connectedToStl = true;
m_fadePct = 0;
connect( stlInstance().data(), SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) );
} }
@@ -141,7 +164,17 @@ PixmapDelegateFader::setPixmap( const QPixmap& pixmap )
if ( pixmap.isNull() ) if ( pixmap.isNull() )
return; return;
if ( m_crossfadeTimeline.state() == QTimeLine::Running ) QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
pixmap.save( &buffer, "PNG" );
QString newImageMd5 = TomahawkUtils::md5( buffer.data() );
if ( m_oldImageMd5 == newImageMd5 )
return;
m_oldImageMd5 = newImageMd5;
if ( m_connectedToStl )
{ {
m_pixmapQueue.enqueue( pixmap ); m_pixmapQueue.enqueue( pixmap );
return; return;
@@ -150,15 +183,25 @@ PixmapDelegateFader::setPixmap( const QPixmap& pixmap )
m_oldReference = m_currentReference; m_oldReference = m_currentReference;
m_currentReference = pixmap; m_currentReference = pixmap;
m_crossfadeTimeline.start(); m_startFrame = stlInstance().data()->currentFrame();
m_connectedToStl = true;
m_fadePct = 0;
connect( stlInstance().data(), SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) );
} }
void void
PixmapDelegateFader::onAnimationStep( int step ) PixmapDelegateFader::onAnimationStep( int step )
{ {
const qreal opacity = ((qreal)step / 1000.); m_fadePct = (float)( step - m_startFrame ) / 10.0;
const qreal oldOpacity = ( 1000. - step ) / 1000. ; if ( m_fadePct > 100.0 )
m_fadePct = 100.0;
if ( m_fadePct == 100.0 )
QTimer::singleShot( 0, this, SLOT( onAnimationFinished() ) );
const qreal opacity = m_fadePct / 100.0;
const qreal oldOpacity = ( 100.0 - m_fadePct ) / 100.0;
m_current.fill( Qt::transparent ); m_current.fill( Qt::transparent );
// Update our pixmap with the new opacity // Update our pixmap with the new opacity
@@ -227,12 +270,12 @@ void
PixmapDelegateFader::onAnimationFinished() PixmapDelegateFader::onAnimationFinished()
{ {
m_oldReference = QPixmap(); m_oldReference = QPixmap();
onAnimationStep( 1000 ); onAnimationStep( INT_MAX );
disconnect( stlInstance().data(), SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) );
if ( !m_pixmapQueue.isEmpty() ) if ( !m_pixmapQueue.isEmpty() )
{
setPixmap( m_pixmapQueue.dequeue() ); setPixmap( m_pixmapQueue.dequeue() );
}
} }

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2011-2012, Leo Franchi <lfranchi@kde.org> * Copyright 2011-2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2012, Jeff Mitchell <jeffe@tomahawk-player.org>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -26,6 +27,7 @@
#include <QObject> #include <QObject>
#include <QTimeLine> #include <QTimeLine>
#include <QQueue> #include <QQueue>
#include <QWeakPointer>
namespace Tomahawk namespace Tomahawk
{ {
@@ -39,6 +41,9 @@ namespace Tomahawk
class PixmapDelegateFader : public QObject class PixmapDelegateFader : public QObject
{ {
Q_OBJECT Q_OBJECT
static QWeakPointer< TomahawkUtils::SharedTimeLine > stlInstance();
public: public:
PixmapDelegateFader( const artist_ptr& artist, const QSize& size, TomahawkUtils::ImageMode mode = TomahawkUtils::Original, bool forceLoad = true ); PixmapDelegateFader( const artist_ptr& artist, const QSize& size, TomahawkUtils::ImageMode mode = TomahawkUtils::Original, bool forceLoad = true );
PixmapDelegateFader( const album_ptr& album, const QSize& size, TomahawkUtils::ImageMode mode = TomahawkUtils::Original, bool forceLoad = true ); PixmapDelegateFader( const album_ptr& album, const QSize& size, TomahawkUtils::ImageMode mode = TomahawkUtils::Original, bool forceLoad = true );
@@ -68,10 +73,16 @@ private:
query_ptr m_track; query_ptr m_track;
QSize m_size; QSize m_size;
TomahawkUtils::ImageMode m_mode; TomahawkUtils::ImageMode m_mode;
int m_startFrame;
bool m_connectedToStl;
float m_fadePct;
QString m_oldImageMd5;
QQueue<QPixmap> m_pixmapQueue; QQueue<QPixmap> m_pixmapQueue;
QTimeLine m_crossfadeTimeline;
QPixmap m_currentReference, m_oldReference, m_current; QPixmap m_currentReference, m_oldReference, m_current;
static QWeakPointer< TomahawkUtils::SharedTimeLine > s_stlInstance;
}; };
} }

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2011 - 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org> * Copyright 2011 - 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2012, Jeff Mitchell <jeffe@tomahawk-player.org>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -73,12 +74,10 @@ FadingPixmap::onAnimationFinished()
m_oldPixmap = QPixmap(); m_oldPixmap = QPixmap();
repaint(); repaint();
if ( m_pixmapQueue.count() )
{
setPixmap( m_pixmapQueue.takeFirst() );
}
disconnect( stlInstance().data(), SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) ); disconnect( stlInstance().data(), SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) );
if ( m_pixmapQueue.count() )
setPixmap( m_pixmapQueue.takeFirst() );
} }

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2011 - 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org> * Copyright 2011 - 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2012, Jeff Mitchell <jeffe@tomahawk-player.org>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by