1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-19 23:39:42 +01:00

* Made StatsGauge animation smooth and use QProgressBar's own inverted state handling.

This commit is contained in:
Christian Muehlhaeuser 2013-06-07 12:17:58 +02:00
parent b93e5c90c0
commit e193524cd3
3 changed files with 34 additions and 16 deletions

View File

@ -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();
}

View File

@ -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

View File

@ -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 );