Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////
- /* FILE fading_highlight_animation.h */
- #ifndef FADING_HIGHLIGHT_ANIMATION_H
- #define FADING_HIGHLIGHT_ANIMATION_H
- #include <QVariantAnimation>
- #include <QPalette>
- #include <QColor>
- #include <QAbstractButton>
- #include <QLineEdit>
- #include <QComboBox>
- #include <QSlider>
- #include <QListView>
- #include <QGroupBox>
- /* Class implements animated highlight effect for QWidget subclasses.
- * Effect triggered by simply call highlight() when widget updated internally by application.
- * Note: some widgets amy require tricky configuration to make it work.
- * Note: tested only on Windows 7.
- */
- class FadingHighlightAnimation : public QVariantAnimation
- {
- Q_OBJECT
- public:
- FadingHighlightAnimation(QAbstractButton *target, QColor color, QObject *parent = 0);
- FadingHighlightAnimation(QLineEdit *target, QColor color, QObject *parent = 0);
- FadingHighlightAnimation(QComboBox *target, QColor color, QObject *parent = 0);
- FadingHighlightAnimation(QSlider *target, QColor color, QObject *parent = 0);
- FadingHighlightAnimation(QListView *target, QColor color, QObject *parent = 0);
- FadingHighlightAnimation(QGroupBox *target, QColor color, QObject *parent = 0);
- void highlight();
- protected:
- virtual void updateCurrentValue(const QVariant &value);
- private:
- QWidget *target;
- QColor color, default_color;
- QPalette::ColorRole target_color_role;
- enum {
- tAbstractButton,
- tLineEdit,
- tComboBox,
- tSlider,
- tListView,
- tGroupBox
- } target_type;
- };
- #endif // FADING_HIGHLIGHT_ANIMATION_H
- ///////////////////////////////////////
- /* FILE fading_highlight_animation.cpp */
- #include "fading_highlight_animation.h"
- FadingHighlightAnimation::FadingHighlightAnimation(QAbstractButton *target, QColor color, QObject *parent) :
- QVariantAnimation(parent),
- target(target), color(color), default_color(target->palette().color(QPalette::Button)), target_type(tAbstractButton)
- {
- }
- FadingHighlightAnimation::FadingHighlightAnimation(QLineEdit *target, QColor color, QObject *parent) :
- QVariantAnimation(parent),
- target(target), color(color), default_color(target->palette().color(QPalette::Base)), target_type(tLineEdit)
- {
- }
- FadingHighlightAnimation::FadingHighlightAnimation(QComboBox *target, QColor color, QObject *parent) :
- QVariantAnimation(parent),
- target(target), color(color), default_color(target->palette().color(QPalette::Base)), target_type(tComboBox)
- {
- target->setStyleSheet("background-color: palette(base);");
- }
- FadingHighlightAnimation::FadingHighlightAnimation(QSlider *target, QColor color, QObject *parent) :
- QVariantAnimation(parent),
- target(target), color(color), default_color(target->palette().color(QPalette::Window)), target_type(tSlider)
- {
- }
- FadingHighlightAnimation::FadingHighlightAnimation(QListView *target, QColor color, QObject *parent) :
- QVariantAnimation(parent),
- target(target), color(color), default_color(target->palette().color(QPalette::Base)), target_type(tListView)
- {
- }
- FadingHighlightAnimation::FadingHighlightAnimation(QGroupBox *target, QColor color, QObject *parent) :
- QVariantAnimation(parent),
- target(target), color(color), default_color(target->palette().color(QPalette::Window)), target_type(tGroupBox)
- {
- }
- void FadingHighlightAnimation::highlight()
- {
- stop();
- setStartValue(color);
- setEndValue(default_color);
- setDuration(800);
- start();
- }
- void FadingHighlightAnimation::updateCurrentValue(const QVariant &value)
- {
- QColor color = value.value<QColor>();
- target->setStyleSheet(QString("background-color: rgb(%1, %2, %3);").arg(color.red()).arg(color.green()).arg(color.blue()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement