From e193524cd3e2063af16764376611992f9431a6c3 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 7 Jun 2013 12:17:58 +0200 Subject: [PATCH] * Made StatsGauge animation smooth and use QProgressBar's own inverted state handling. --- src/libtomahawk/widgets/StatsGauge.cpp | 41 +++++++++++++------ src/libtomahawk/widgets/StatsGauge.h | 7 +++- .../widgets/infowidgets/ArtistInfoWidget.cpp | 2 +- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/libtomahawk/widgets/StatsGauge.cpp b/src/libtomahawk/widgets/StatsGauge.cpp index 3bdc409f7..a1d91d1a4 100644 --- a/src/libtomahawk/widgets/StatsGauge.cpp +++ b/src/libtomahawk/widgets/StatsGauge.cpp @@ -33,7 +33,8 @@ StatsGauge::StatsGauge( QWidget* parent ) : QProgressBar( parent ) - , m_inverted( false ) + , m_percentage( 0 ) + , m_targetValue( 0 ) { QProgressBar::setValue( 0 ); QProgressBar::setMaximum( 0 ); @@ -58,9 +59,8 @@ StatsGauge::paintEvent( QPaintEvent* event ) p.setPen( pen ); int fullCircle = 16 * 360; - float pct = m_inverted ? ( 1.0 - (float)value() / (float)maximum() ) : (float)value() / (float)maximum(); - - p.drawArc( QRect( 12, 12, gaugeSize.width() - 24, gaugeSize.height() - 24 ), 4*360, (int)( -1.0 * (float)fullCircle * pct ) ); + p.drawArc( QRect( 12, 12, gaugeSize.width() - 24, gaugeSize.height() - 24 ), + 4*360, (int)( -1.0 * (float)fullCircle * ( invertedAppearance() ? ( 1.0 - m_percentage ) : m_percentage ) ) ); pen = QPen( TomahawkStyle::NOW_PLAYING_ITEM.darker() ); pen.setWidth( 6 ); @@ -116,15 +116,30 @@ StatsGauge::setValue( int v ) { if ( maximum() == 0 || v == 0 ) return; + if ( v == m_targetValue ) + return; - QPropertyAnimation* a = new QPropertyAnimation( (QProgressBar*)this, "value" ); - a->setEasingCurve( QEasingCurve( QEasingCurve::OutQuad ) ); - a->setStartValue( value() > 0 ? value() : 1 ); - a->setEndValue( v ); - a->setDuration( 2000 ); + m_targetValue = v; + { + QPropertyAnimation* a = new QPropertyAnimation( (QProgressBar*)this, "value" ); + a->setEasingCurve( QEasingCurve( QEasingCurve::OutQuad ) ); + a->setStartValue( value() > 0 ? value() : 1 ); + a->setEndValue( v ); + a->setDuration( 2000 ); - connect( a, SIGNAL( finished() ), a, SLOT( deleteLater() ) ); - a->start(); + connect( a, SIGNAL( finished() ), a, SLOT( deleteLater() ) ); + a->start(); + } + { + QPropertyAnimation* a = new QPropertyAnimation( (QProgressBar*)this, "percentage" ); + a->setEasingCurve( QEasingCurve( QEasingCurve::OutQuad ) ); + a->setStartValue( (float)0 ); + a->setEndValue( (float)v / (float)maximum() ); + a->setDuration( 2000 ); + + connect( a, SIGNAL( finished() ), a, SLOT( deleteLater() ) ); + a->start(); + } } @@ -137,8 +152,8 @@ StatsGauge::setText( const QString& text ) void -StatsGauge::setInvertedGauge( bool inverted ) +StatsGauge::setPercentage( float percentage ) { - m_inverted = inverted; + m_percentage = percentage; repaint(); } diff --git a/src/libtomahawk/widgets/StatsGauge.h b/src/libtomahawk/widgets/StatsGauge.h index 9e27e68d8..828c1fc4d 100644 --- a/src/libtomahawk/widgets/StatsGauge.h +++ b/src/libtomahawk/widgets/StatsGauge.h @@ -26,6 +26,7 @@ class DLLEXPORT StatsGauge : public QProgressBar { Q_OBJECT +Q_PROPERTY( float percentage READ percentage WRITE setPercentage ) public: /** this pixmap becomes the rest state pixmap and defines the size of the eventual widget */ @@ -34,11 +35,12 @@ public: virtual QSize sizeHint() const { return m_sizeHint; } QString text() const { return m_text; } - void setInvertedGauge( bool inverted ); + float percentage() const { return m_percentage; } public slots: void setValue( int value ); void setText( const QString& text ); + void setPercentage( float percentage ); protected: virtual void paintEvent( QPaintEvent* event ); @@ -46,7 +48,8 @@ protected: private: QSize m_sizeHint; QString m_text; - bool m_inverted; + float m_percentage; + int m_targetValue; }; #endif //STATS_GAUGE_H diff --git a/src/libtomahawk/widgets/infowidgets/ArtistInfoWidget.cpp b/src/libtomahawk/widgets/infowidgets/ArtistInfoWidget.cpp index 9376cfa51..0c359d33a 100644 --- a/src/libtomahawk/widgets/infowidgets/ArtistInfoWidget.cpp +++ b/src/libtomahawk/widgets/infowidgets/ArtistInfoWidget.cpp @@ -108,7 +108,7 @@ ArtistInfoWidget::ArtistInfoWidget( const Tomahawk::artist_ptr& artist, QWidget* QHBoxLayout* l = new QHBoxLayout( ui->statsWidget ); m_playStatsGauge = new StatsGauge( ui->statsWidget ); m_playStatsGauge->setText( tr( "CHART #" ) ); - m_playStatsGauge->setInvertedGauge( true ); + m_playStatsGauge->setInvertedAppearance( true ); l->addSpacerItem( new QSpacerItem( 0, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding ) ); l->addWidget( m_playStatsGauge );