diff --git a/QtAwesome/QtAwesome.cpp b/QtAwesome/QtAwesome.cpp index e586d69..23b0ac5 100644 --- a/QtAwesome/QtAwesome.cpp +++ b/QtAwesome/QtAwesome.cpp @@ -8,6 +8,7 @@ */ #include "QtAwesome.h" +#include "QtAwesomeAnim.h" #include #include @@ -27,6 +28,13 @@ public: painter->save(); + QObject* qanim = qvariant_cast(options.value("anim")); + QtAwesomeAnimation* anim = qobject_cast(qanim); + if( anim ) { + anim->setup( *painter, rect ); + } + + // set the correct color QColor color = options.value("color").value(); QString text = options.value("text").toString(); diff --git a/QtAwesome/QtAwesome.h b/QtAwesome/QtAwesome.h index 0aea574..eff6735 100644 --- a/QtAwesome/QtAwesome.h +++ b/QtAwesome/QtAwesome.h @@ -10,6 +10,8 @@ #ifndef QTAWESOME_H #define QTAWESOME_H +#include "QtAwesomeAnim.h" + #include #include #include diff --git a/QtAwesome/QtAwesome.pri b/QtAwesome/QtAwesome.pri index 5e4002d..caef09b 100644 --- a/QtAwesome/QtAwesome.pri +++ b/QtAwesome/QtAwesome.pri @@ -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 diff --git a/QtAwesome/QtAwesomeAnim.cpp b/QtAwesome/QtAwesomeAnim.cpp new file mode 100644 index 0000000..8ad44b6 --- /dev/null +++ b/QtAwesome/QtAwesomeAnim.cpp @@ -0,0 +1,46 @@ +#include "QtAwesomeAnim.h" + +#include +#include +#include +#include +#include + + +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(); +} diff --git a/QtAwesome/QtAwesomeAnim.h b/QtAwesome/QtAwesomeAnim.h new file mode 100644 index 0000000..5e3d11d --- /dev/null +++ b/QtAwesome/QtAwesomeAnim.h @@ -0,0 +1,36 @@ +#ifndef QTAWESOMEANIMATION_H +#define QTAWESOMEANIMATION_H + +#include + +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 diff --git a/QtAwesomeSample/main.cpp b/QtAwesomeSample/main.cpp index 390470e..f8a1c73 100644 --- a/QtAwesomeSample/main.cpp +++ b/QtAwesomeSample/main.cpp @@ -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();