Animation implementation based on spyder-ide/qtawesome.

Stil work in progress, we need to improve the interface for setting the animation.
This commit is contained in:
Rick Blommers 2015-12-08 07:06:46 +01:00
parent 9b3209925d
commit 9b354b3f04
6 changed files with 103 additions and 3 deletions

View File

@ -8,6 +8,7 @@
*/
#include "QtAwesome.h"
#include "QtAwesomeAnim.h"
#include <QDebug>
#include <QFile>
@ -27,6 +28,13 @@ public:
painter->save();
QObject* qanim = qvariant_cast<QObject*>(options.value("anim"));
QtAwesomeAnimation* anim = qobject_cast<QtAwesomeAnimation*>(qanim);
if( anim ) {
anim->setup( *painter, rect );
}
// set the correct color
QColor color = options.value("color").value<QColor>();
QString text = options.value("text").toString();

View File

@ -10,6 +10,8 @@
#ifndef QTAWESOME_H
#define QTAWESOME_H
#include "QtAwesomeAnim.h"
#include <QIcon>
#include <QIconEngine>
#include <QPainter>

View File

@ -1,9 +1,11 @@
INCLUDEPATH += $$PWD
SOURCES += $$PWD/QtAwesome.cpp
SOURCES += $$PWD/QtAwesome.cpp \
$$PWD/QtAwesomeAnim.cpp
HEADERS += $$PWD/QtAwesome.h
HEADERS += $$PWD/QtAwesome.h \
$$PWD/QtAwesomeAnim.h
RESOURCES += $$PWD/QtAwesome.qrc

View File

@ -0,0 +1,46 @@
#include "QtAwesomeAnim.h"
#include <cmath>
#include <QPainter>
#include <QRect>
#include <QTimer>
#include <QWidget>
QtAwesomeAnimation::QtAwesomeAnimation(QWidget *parentWidget, int interval, int step)
: parentWidgetRef_( parentWidget )
, timer_( 0 )
, interval_( interval )
, step_( step )
, angle_( 0.0f )
{
}
void QtAwesomeAnimation::setup( QPainter &painter, const QRect &rect)
{
// first time set the timer
if( !timer_ )
{
timer_ = new QTimer();
connect(timer_,SIGNAL(timeout()), this, SLOT(update()) );
timer_->start(interval_);
}
else
{
//timer, angle, self.step = self.info[self.parent_widget]
float x_center = rect.width() * 0.5;
float y_center = rect.height() * 0.5;
painter.translate(x_center, y_center);
painter.rotate(angle_);
painter.translate(-x_center, -y_center);
}
}
void QtAwesomeAnimation::update()
{
angle_ += step_;
angle_ = std::fmod( angle_, 360);
parentWidgetRef_->update();
}

36
QtAwesome/QtAwesomeAnim.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef QTAWESOMEANIMATION_H
#define QTAWESOMEANIMATION_H
#include <QObject>
class QPainter;
class QRect;
class QTimer;
class QWidget;
///
/// Basic Animation Support for QtAwesome (Inspired by https://github.com/spyder-ide/qtawesome)
///
class QtAwesomeAnimation : public QObject
{
Q_OBJECT
public:
QtAwesomeAnimation( QWidget* parentWidget, int interval=10, int step=1);
void setup( QPainter& painter, const QRect& rect );
public slots:
void update();
private:
QWidget* parentWidgetRef_;
QTimer* timer_;
int interval_;
int step_;
float angle_;
};
#endif // QTAWESOMEANIMATION_H

View File

@ -20,7 +20,13 @@ int main(int argc, char *argv[])
awesome->initFontAwesome();
// a simple beer button
QPushButton* beerButton = new QPushButton( awesome->icon( fa::beer ), "Cheers!" );
QPushButton* beerButton = new QPushButton( "Cheers!");
QVariantMap options;
options.insert("anim", qVariantFromValue( new QtAwesomeAnimation(beerButton) ) );
beerButton->setIcon( awesome->icon( fa::beer, options ) );
w.setCentralWidget( beerButton );
w.show();