From 36a421e778c5c78dcebabb6712ec215e94e466cd Mon Sep 17 00:00:00 2001
From: Eike Ziller <eike.ziller@digia.com>
Date: Wed, 20 Mar 2013 16:02:53 +0100
Subject: [PATCH] ProgressManager: Use QGraphicsOpacityEffect instead of hack

Change-Id: Ic3bd8e7d8d33a14be0e49e2d4a22ea30384aebc5
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
---
 .../progressmanager/futureprogress.cpp        | 94 +++++++------------
 .../progressmanager/futureprogress.h          |  4 +-
 2 files changed, 34 insertions(+), 64 deletions(-)

diff --git a/src/plugins/coreplugin/progressmanager/futureprogress.cpp b/src/plugins/coreplugin/progressmanager/futureprogress.cpp
index 9db8c768f2d..d0e78fece90 100644
--- a/src/plugins/coreplugin/progressmanager/futureprogress.cpp
+++ b/src/plugins/coreplugin/progressmanager/futureprogress.cpp
@@ -30,55 +30,30 @@
 #include "futureprogress.h"
 #include "progressbar.h"
 
-#include <QFutureWatcher>
-#include <QTimer>
 #include <QCoreApplication>
+#include <QFutureWatcher>
+#include <QGraphicsOpacityEffect>
 #include <QPropertyAnimation>
 #include <QSequentialAnimationGroup>
+#include <QTimer>
 
 #include <QVBoxLayout>
 #include <QHBoxLayout>
-#include <QPainter>
 #include <QMouseEvent>
-#include <utils/stylehelper.h>
 
 const int notificationTimeout = 8000;
 const int shortNotificationTimeout = 1000;
 
 namespace Core {
-namespace Internal {
 
-class FadeWidgetHack : public QWidget
+class FutureProgressPrivate : public QObject
 {
     Q_OBJECT
-    Q_PROPERTY(float opacity READ opacity WRITE setOpacity)
-public:
-    FadeWidgetHack(QWidget *parent):QWidget(parent), m_opacity(0){
-        setAttribute(Qt::WA_TransparentForMouseEvents);
-    }
-    void paintEvent(QPaintEvent *);
-
-    void setOpacity(float o) { m_opacity = o; update(); }
-    float opacity() const { return m_opacity; }
-
-private:
-    float m_opacity;
-};
-
-void FadeWidgetHack::paintEvent(QPaintEvent *)
-{
-    if (m_opacity == 0)
-        return;
-
-    QPainter p(this);
-    p.setOpacity(m_opacity);
-    if (m_opacity > 0)
-        Utils::StyleHelper::verticalGradient(&p, rect(), rect());
-}
 
-} // namespace Internal
+public slots:
+    void fadeAway();
 
-struct FutureProgressPrivate {
+public:
     explicit FutureProgressPrivate(FutureProgress *q);
 
     void tryToFadeAway();
@@ -90,7 +65,6 @@ struct FutureProgressPrivate {
     QString m_type;
     FutureProgress::KeepOnFinishType m_keep;
     bool m_waitingForUserInteraction;
-    Internal::FadeWidgetHack *m_faderWidget;
     FutureProgress *m_q;
     bool m_isFading;
 };
@@ -98,7 +72,7 @@ struct FutureProgressPrivate {
 FutureProgressPrivate::FutureProgressPrivate(FutureProgress *q) :
     m_progress(new Internal::ProgressBar), m_widget(0), m_widgetLayout(new QHBoxLayout),
     m_keep(FutureProgress::HideOnFinish), m_waitingForUserInteraction(false),
-    m_faderWidget(new Internal::FadeWidgetHack(q)), m_q(q), m_isFading(false)
+    m_q(q), m_isFading(false)
 {
 }
 
@@ -231,9 +205,9 @@ void FutureProgress::setStarted()
 bool FutureProgress::eventFilter(QObject *, QEvent *e)
 {
     if (d->m_keep != KeepOnFinish && d->m_waitingForUserInteraction
-        && (e->type() == QEvent::MouseMove || e->type() == QEvent::KeyPress)) {
+            && (e->type() == QEvent::MouseMove || e->type() == QEvent::KeyPress)) {
         qApp->removeEventFilter(this);
-        QTimer::singleShot(notificationTimeout, this, SLOT(fadeAway()));
+        QTimer::singleShot(notificationTimeout, d, SLOT(fadeAway()));
     }
     return false;
 }
@@ -264,7 +238,7 @@ void FutureProgressPrivate::tryToFadeAway()
         qApp->installEventFilter(m_q);
         m_isFading = true;
     } else if (m_keep == FutureProgress::HideOnFinish) {
-        QTimer::singleShot(shortNotificationTimeout, m_q, SLOT(fadeAway()));
+        QTimer::singleShot(shortNotificationTimeout, this, SLOT(fadeAway()));
         m_isFading = true;
     }
 }
@@ -314,11 +288,6 @@ void FutureProgress::mousePressEvent(QMouseEvent *event)
     QWidget::mousePressEvent(event);
 }
 
-void FutureProgress::resizeEvent(QResizeEvent *)
-{
-    d->m_faderWidget->setGeometry(rect());
-}
-
 /*!
     \fn bool FutureProgress::hasError() const
     Returns the error state of this progress indicator.
@@ -328,25 +297,6 @@ bool FutureProgress::hasError() const
     return d->m_progress->hasError();
 }
 
-void FutureProgress::fadeAway()
-{
-    d->m_faderWidget->raise();
-    QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this);
-    QPropertyAnimation *animation = new QPropertyAnimation(d->m_faderWidget, "opacity");
-    animation->setDuration(600);
-    animation->setEndValue(1.0);
-    group->addAnimation(animation);
-    animation = new QPropertyAnimation(this, "maximumHeight");
-    animation->setDuration(120);
-    animation->setEasingCurve(QEasingCurve::InCurve);
-    animation->setStartValue(sizeHint().height());
-    animation->setEndValue(0.0);
-    group->addAnimation(animation);
-    group->start(QAbstractAnimation::DeleteWhenStopped);
-
-    connect(group, SIGNAL(finished()), this, SIGNAL(removeMe()));
-}
-
 void FutureProgress::setType(const QString &type)
 {
     d->m_type = type;
@@ -378,6 +328,28 @@ QWidget *FutureProgress::widget() const
     return d->m_widget;
 }
 
+void FutureProgressPrivate::fadeAway()
+{
+    QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect;
+    opacityEffect->setOpacity(1.);
+    m_q->setGraphicsEffect(opacityEffect);
+
+    QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this);
+    QPropertyAnimation *animation = new QPropertyAnimation(opacityEffect, "opacity");
+    animation->setDuration(600);
+    animation->setEndValue(0.);
+    group->addAnimation(animation);
+    animation = new QPropertyAnimation(m_q, "maximumHeight");
+    animation->setDuration(120);
+    animation->setEasingCurve(QEasingCurve::InCurve);
+    animation->setStartValue(m_q->sizeHint().height());
+    animation->setEndValue(0.0);
+    group->addAnimation(animation);
+    group->start(QAbstractAnimation::DeleteWhenStopped);
+
+    connect(group, SIGNAL(finished()), m_q, SIGNAL(removeMe()));
+}
+
 } // namespace Core
 
 #include "futureprogress.moc"
diff --git a/src/plugins/coreplugin/progressmanager/futureprogress.h b/src/plugins/coreplugin/progressmanager/futureprogress.h
index f4cbcc17895..b8cfec78ca2 100644
--- a/src/plugins/coreplugin/progressmanager/futureprogress.h
+++ b/src/plugins/coreplugin/progressmanager/futureprogress.h
@@ -37,7 +37,7 @@
 #include <QWidget>
 
 namespace Core {
-struct FutureProgressPrivate;
+class FutureProgressPrivate;
 
 class CORE_EXPORT FutureProgress : public QWidget
 {
@@ -79,7 +79,6 @@ signals:
 
 protected:
     void mousePressEvent(QMouseEvent *event);
-    void resizeEvent(QResizeEvent *);
 
 private slots:
     void updateToolTip(const QString &);
@@ -89,7 +88,6 @@ private slots:
     void setProgressRange(int min, int max);
     void setProgressValue(int val);
     void setProgressText(const QString &text);
-    void fadeAway();
 
 private:
     FutureProgressPrivate *d;
-- 
GitLab