Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2013
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. ///////////////////////////////////////
  2. /* FILE fading_highlight_animation.h */
  3.  
  4. #ifndef FADING_HIGHLIGHT_ANIMATION_H
  5. #define FADING_HIGHLIGHT_ANIMATION_H
  6.  
  7. #include <QVariantAnimation>
  8. #include <QPalette>
  9. #include <QColor>
  10. #include <QAbstractButton>
  11. #include <QLineEdit>
  12. #include <QComboBox>
  13. #include <QSlider>
  14. #include <QListView>
  15. #include <QGroupBox>
  16.  
  17. /* Class implements animated highlight effect for QWidget subclasses.
  18.  * Effect triggered by simply call highlight() when widget updated internally by application.
  19.  * Note: some widgets amy require tricky configuration to make it work.
  20.  * Note: tested only on Windows 7.
  21.  */
  22. class FadingHighlightAnimation : public QVariantAnimation
  23. {
  24.     Q_OBJECT
  25. public:
  26.     FadingHighlightAnimation(QAbstractButton *target, QColor color, QObject *parent = 0);
  27.     FadingHighlightAnimation(QLineEdit *target, QColor color, QObject *parent = 0);
  28.     FadingHighlightAnimation(QComboBox *target, QColor color, QObject *parent = 0);
  29.     FadingHighlightAnimation(QSlider *target, QColor color, QObject *parent = 0);
  30.     FadingHighlightAnimation(QListView *target, QColor color, QObject *parent = 0);
  31.     FadingHighlightAnimation(QGroupBox *target, QColor color, QObject *parent = 0);
  32.     void highlight();
  33.  
  34. protected:
  35.     virtual void updateCurrentValue(const QVariant &value);
  36.  
  37. private:
  38.     QWidget *target;
  39.     QColor color, default_color;
  40.     QPalette::ColorRole target_color_role;
  41.     enum {
  42.         tAbstractButton,
  43.         tLineEdit,
  44.         tComboBox,
  45.         tSlider,
  46.         tListView,
  47.         tGroupBox
  48.     } target_type;
  49. };
  50.  
  51. #endif // FADING_HIGHLIGHT_ANIMATION_H
  52.  
  53. ///////////////////////////////////////
  54. /* FILE fading_highlight_animation.cpp */
  55.  
  56. #include "fading_highlight_animation.h"
  57.  
  58. FadingHighlightAnimation::FadingHighlightAnimation(QAbstractButton *target, QColor color, QObject *parent) :
  59.     QVariantAnimation(parent),
  60.     target(target), color(color), default_color(target->palette().color(QPalette::Button)), target_type(tAbstractButton)
  61. {
  62. }
  63.  
  64. FadingHighlightAnimation::FadingHighlightAnimation(QLineEdit *target, QColor color, QObject *parent) :
  65.     QVariantAnimation(parent),
  66.     target(target), color(color), default_color(target->palette().color(QPalette::Base)), target_type(tLineEdit)
  67. {
  68. }
  69.  
  70. FadingHighlightAnimation::FadingHighlightAnimation(QComboBox *target, QColor color, QObject *parent) :
  71.     QVariantAnimation(parent),
  72.     target(target), color(color), default_color(target->palette().color(QPalette::Base)), target_type(tComboBox)
  73. {
  74.     target->setStyleSheet("background-color: palette(base);");
  75. }
  76.  
  77. FadingHighlightAnimation::FadingHighlightAnimation(QSlider *target, QColor color, QObject *parent) :
  78.     QVariantAnimation(parent),
  79.     target(target), color(color), default_color(target->palette().color(QPalette::Window)), target_type(tSlider)
  80. {
  81. }
  82.  
  83. FadingHighlightAnimation::FadingHighlightAnimation(QListView *target, QColor color, QObject *parent) :
  84.     QVariantAnimation(parent),
  85.     target(target), color(color), default_color(target->palette().color(QPalette::Base)), target_type(tListView)
  86. {
  87. }
  88.  
  89. FadingHighlightAnimation::FadingHighlightAnimation(QGroupBox *target, QColor color, QObject *parent) :
  90.     QVariantAnimation(parent),
  91.     target(target), color(color), default_color(target->palette().color(QPalette::Window)), target_type(tGroupBox)
  92. {
  93. }
  94.  
  95. void FadingHighlightAnimation::highlight()
  96. {
  97.     stop();
  98.     setStartValue(color);
  99.     setEndValue(default_color);
  100.     setDuration(800);
  101.     start();
  102. }
  103.  
  104. void FadingHighlightAnimation::updateCurrentValue(const QVariant &value)
  105. {
  106.     QColor color = value.value<QColor>();
  107.     target->setStyleSheet(QString("background-color: rgb(%1, %2, %3);").arg(color.red()).arg(color.green()).arg(color.blue()));
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement