ProgressManager: Use QGraphicsOpacityEffect instead of hack

Change-Id: Ic3bd8e7d8d33a14be0e49e2d4a22ea30384aebc5
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
This commit is contained in:
Eike Ziller
2013-03-20 16:02:53 +01:00
parent 37e0b91450
commit 36a421e778
2 changed files with 35 additions and 65 deletions

View File

@@ -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 slots:
void fadeAway();
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
struct FutureProgressPrivate {
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"