From 546abf4a6d32a8e8e9cba1285c0f0ce3cfa6ee02 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Sat, 7 Apr 2012 21:09:22 -0400 Subject: [PATCH] Wrote this SharedTimeLine thing, then converted the wrong class to use it, oops. Seems to work perfectly, though, so might as well use it. --- src/libtomahawk/utils/tomahawkutils.cpp | 41 +++++++++++++++++- src/libtomahawk/utils/tomahawkutils.h | 33 ++++++++++++++- src/libtomahawk/widgets/FadingPixmap.cpp | 54 ++++++++++++++++++------ src/libtomahawk/widgets/FadingPixmap.h | 14 +++++- 4 files changed, 125 insertions(+), 17 deletions(-) diff --git a/src/libtomahawk/utils/tomahawkutils.cpp b/src/libtomahawk/utils/tomahawkutils.cpp index 0ab5611ad..dbbac206d 100644 --- a/src/libtomahawk/utils/tomahawkutils.cpp +++ b/src/libtomahawk/utils/tomahawkutils.cpp @@ -609,7 +609,8 @@ removeDirectory( const QString& dir ) } -quint64 infosystemRequestId() +quint64 +infosystemRequestId() { QMutexLocker locker( &s_infosystemRequestIdMutex ); quint64 result = s_infosystemRequestId; @@ -633,4 +634,42 @@ crash() *a = 1; } + +SharedTimeLine::SharedTimeLine() + : QObject( 0 ) + , m_refcount( 0 ) +{ + m_timeline.setCurveShape( QTimeLine::LinearCurve ); + m_timeline.setFrameRange( 0, INT_MAX ); + m_timeline.setDuration( INT_MAX ); + m_timeline.setUpdateInterval( 40 ); + connect( &m_timeline, SIGNAL( frameChanged( int ) ), SIGNAL( frameChanged( int ) ) ); +} + +void +SharedTimeLine::connectNotify( const char* signal ) +{ + if ( signal == QMetaObject::normalizedSignature( SIGNAL( frameChanged( int ) ) ) ) { + m_refcount++; + if ( m_timeline.state() != QTimeLine::Running ) + m_timeline.start(); + } +} + + +void +SharedTimeLine::disconnectNotify( const char* signal ) +{ + if ( signal == QMetaObject::normalizedSignature( SIGNAL( frameChanged( int ) ) ) ) + { + m_refcount--; + if ( m_timeline.state() == QTimeLine::Running && m_refcount == 0 ) + { + m_timeline.stop(); + deleteLater(); + } + } +} + + } // ns diff --git a/src/libtomahawk/utils/tomahawkutils.h b/src/libtomahawk/utils/tomahawkutils.h index a1ce5f887..3b127a710 100644 --- a/src/libtomahawk/utils/tomahawkutils.h +++ b/src/libtomahawk/utils/tomahawkutils.h @@ -26,9 +26,9 @@ #include #include #include +#include #include - #define RESPATH ":/data/" @@ -61,6 +61,34 @@ namespace TomahawkUtils ScaledCover }; + + class DLLEXPORT SharedTimeLine : public QObject + { + Q_OBJECT + + public: + SharedTimeLine(); + + virtual ~SharedTimeLine() {} + + int currentFrame() { return m_timeline.currentFrame(); } + + void setUpdateInterval( int msec ) { if ( msec != m_timeline.updateInterval() ) m_timeline.setUpdateInterval( msec ); } + + signals: + void frameChanged( int ); + + protected slots: + virtual void connectNotify( const char *signal ); + + virtual void disconnectNotify( const char *signal ); + + private: + int m_refcount; + QTimeLine m_timeline; + }; + + class DLLEXPORT NetworkProxyFactory : public QNetworkProxyFactory { public: @@ -85,6 +113,7 @@ namespace TomahawkUtils QStringList m_noProxyHosts; QNetworkProxy m_proxy; }; + DLLEXPORT QString appFriendlyVersion(); @@ -107,7 +136,7 @@ namespace TomahawkUtils DLLEXPORT QString md5( const QByteArray& data ); DLLEXPORT bool removeDirectory( const QString& dir ); - + /** * This helper is designed to help "update" an existing playlist with a newer revision of itself. * To avoid re-loading the whole playlist and re-resolving tracks that are the same in the old playlist, diff --git a/src/libtomahawk/widgets/FadingPixmap.cpp b/src/libtomahawk/widgets/FadingPixmap.cpp index 29b7304c4..5434a5e7a 100644 --- a/src/libtomahawk/widgets/FadingPixmap.cpp +++ b/src/libtomahawk/widgets/FadingPixmap.cpp @@ -18,22 +18,33 @@ #include "FadingPixmap.h" +#include "utils/logger.h" + +#include +#include #include #define ANIMATION_TIME 1000 +QWeakPointer< TomahawkUtils::SharedTimeLine > FadingPixmap::s_stlInstance = QWeakPointer< TomahawkUtils::SharedTimeLine >(); + +QWeakPointer< TomahawkUtils::SharedTimeLine > +FadingPixmap::stlInstance() +{ + if ( s_stlInstance.isNull() ) + s_stlInstance = QWeakPointer< TomahawkUtils::SharedTimeLine> ( new TomahawkUtils::SharedTimeLine() ); + + return s_stlInstance; +} + + FadingPixmap::FadingPixmap( QWidget* parent ) : QLabel( parent ) + , m_oldPixmap( QPixmap() ) , m_fadePct( 100 ) + , m_startFrame( 0 ) { // setCursor( Qt::PointingHandCursor ); - - m_timeLine = new QTimeLine( ANIMATION_TIME, this ); - m_timeLine->setUpdateInterval( 20 ); - m_timeLine->setEasingCurve( QEasingCurve::Linear ); - - connect( m_timeLine, SIGNAL( frameChanged( int ) ), SLOT( onAnimationStep( int ) ) ); - connect( m_timeLine, SIGNAL( finished() ), SLOT( onAnimationFinished() ) ); } @@ -45,8 +56,14 @@ FadingPixmap::~FadingPixmap() void FadingPixmap::onAnimationStep( int frame ) { - m_fadePct = (float)frame / 10.0; + m_fadePct = (float)( frame - m_startFrame ) / 10.0; + if ( m_fadePct > 100.0 ) + m_fadePct = 100.0; + repaint(); + + if ( m_fadePct == 100.0 ) + QTimer::singleShot( 0, this, SLOT( onAnimationFinished() ) ); } @@ -60,13 +77,25 @@ FadingPixmap::onAnimationFinished() { setPixmap( m_pixmapQueue.takeFirst() ); } + + disconnect( stlInstance().data(), SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) ); } void FadingPixmap::setPixmap( const QPixmap& pixmap, bool clearQueue ) { - if ( m_timeLine->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_oldPixmap.isNull() ) { if ( clearQueue ) m_pixmapQueue.clear(); @@ -78,9 +107,10 @@ FadingPixmap::setPixmap( const QPixmap& pixmap, bool clearQueue ) m_oldPixmap = m_pixmap; m_pixmap = pixmap; - m_timeLine->setFrameRange( 0, 1000 ); - m_timeLine->setDirection( QTimeLine::Forward ); - m_timeLine->start(); + stlInstance().data()->setUpdateInterval( 20 ); + m_startFrame = stlInstance().data()->currentFrame(); + m_fadePct = 0; + connect( stlInstance().data(), SIGNAL( frameChanged( int ) ), this, SLOT( onAnimationStep( int ) ) ); } diff --git a/src/libtomahawk/widgets/FadingPixmap.h b/src/libtomahawk/widgets/FadingPixmap.h index e6bd7720f..3c26268ee 100644 --- a/src/libtomahawk/widgets/FadingPixmap.h +++ b/src/libtomahawk/widgets/FadingPixmap.h @@ -19,9 +19,12 @@ #ifndef FADINGPIXMAP_H #define FADINGPIXMAP_H +#include "utils/tomahawkutils.h" + #include #include #include +#include #include "dllmacro.h" @@ -33,12 +36,15 @@ class DLLEXPORT FadingPixmap : public QLabel { Q_OBJECT + static QWeakPointer< TomahawkUtils::SharedTimeLine > stlInstance(); + public: FadingPixmap( QWidget* parent = 0 ); virtual ~FadingPixmap(); public slots: virtual void setPixmap( const QPixmap& pixmap, bool clearQueue = true ); + void onAnimationStep( int frame ); signals: void clicked(); @@ -48,17 +54,21 @@ protected: void mouseReleaseEvent( QMouseEvent* event ); private slots: - void onAnimationStep( int frame ); void onAnimationFinished(); private: QPixmap m_pixmap; QPixmap m_oldPixmap; + + QString m_oldImageMd5; QList m_pixmapQueue; - QTimeLine* m_timeLine; int m_fadePct; + + int m_startFrame; + + static QWeakPointer< TomahawkUtils::SharedTimeLine > s_stlInstance; }; #endif