From e3a7b8945a40fbbefcb1e55010e46fc0c4a2bca7 Mon Sep 17 00:00:00 2001
From: Ming Rui Zhang <mingrui.zhang@savoirfairelinux.com>
Date: Wed, 6 Nov 2019 15:30:14 -0500
Subject: [PATCH] widgethelpers: add blinkable widget

Change-Id: If5b7bcd9ebac2082ab13399497843b72f11be09a
---
 widgethelpers.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++
 widgethelpers.h   | 29 +++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git a/widgethelpers.cpp b/widgethelpers.cpp
index 552bb2f..2e2afb4 100644
--- a/widgethelpers.cpp
+++ b/widgethelpers.cpp
@@ -75,6 +75,50 @@ FadeOutable::slotTimeout()
     Q_EMIT willFadeOut();
 }
 
+Blinkable::Blinkable(QWidget* parent)
+    : QWidget(parent)
+{
+    setAttribute(Qt::WA_NoSystemBackground);
+    fadeAnimation_ = new FadeAnimation(this, blinkTime_);
+}
+
+Blinkable::~Blinkable()
+{}
+
+void
+Blinkable::slotAnimationFinished()
+{
+    fadeAnimation_->setDirection(fadeAnimation_->direction() == QAbstractAnimation::Forward
+                                 ? QAbstractAnimation::Backward
+                                 : QAbstractAnimation::Forward);
+    fadeAnimation_->start();
+}
+
+void
+Blinkable::start()
+{
+    connection_ = connect(fadeAnimation_, &QPropertyAnimation::finished, this, &Blinkable::slotAnimationFinished);
+    fadeAnimation_->setDirection(QAbstractAnimation::Forward);
+    fadeAnimation_->start();
+}
+
+void
+Blinkable::stop()
+{
+    disconnect(connection_);
+    fadeAnimation_->stop();
+}
+
+void
+Blinkable::paintEvent(QPaintEvent *event)
+{
+    QWidget::paintEvent(event);
+    if(!pixmap_)
+        return;
+    QPainter painter(this);
+    painter.drawPixmap(rect(), *pixmap_);
+}
+
 VignetteWidget::VignetteWidget(QWidget* parent)
     : QWidget(parent)
 {
diff --git a/widgethelpers.h b/widgethelpers.h
index f0fe5a5..c5eeb6d 100644
--- a/widgethelpers.h
+++ b/widgethelpers.h
@@ -24,6 +24,7 @@
 #include <QTimer>
 #include <QMouseEvent>
 #include <QPainter>
+#include <QPixmap>
 
 class FadeOutable : public QWidget
 {
@@ -60,6 +61,34 @@ private:
 
 };
 
+class Blinkable : public QWidget
+{
+    Q_OBJECT
+public:
+    explicit Blinkable(QWidget* parent = nullptr);
+    ~Blinkable();
+
+    void start();
+    void stop();
+
+    void setBlinkTime(const quint64 ms) { blinkTime_ = ms; }
+    void setPixmap(QPixmap* pixmap) { pixmap_ = pixmap; }
+    void unsetPixmap() { pixmap_ = nullptr; }
+
+protected:
+    void paintEvent(QPaintEvent *event);
+
+private slots:
+    void slotAnimationFinished();
+
+private:
+    FadeAnimation* fadeAnimation_;
+
+    quint64 blinkTime_{ 1000 };
+    QPixmap* pixmap_{ nullptr };
+    QMetaObject::Connection connection_;
+};
+
 class VignetteWidget : public QWidget
 {
     Q_OBJECT
-- 
GitLab