2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
**
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "futureprogress.h"
|
2010-02-25 18:53:42 +01:00
|
|
|
#include "progressbar.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-09-16 12:26:28 +02:00
|
|
|
#include <QtCore/QFutureWatcher>
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
#include <QtCore/QPropertyAnimation>
|
|
|
|
#include <QtCore/QSequentialAnimationGroup>
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QColor>
|
|
|
|
#include <QtGui/QVBoxLayout>
|
|
|
|
#include <QtGui/QMenu>
|
2009-10-01 16:38:08 +02:00
|
|
|
#include <QtGui/QProgressBar>
|
|
|
|
#include <QtGui/QHBoxLayout>
|
2010-03-12 16:26:42 +01:00
|
|
|
#include <QtGui/QPainter>
|
2010-03-18 10:59:06 +01:00
|
|
|
#include <QtGui/QMouseEvent>
|
2010-03-12 16:26:42 +01:00
|
|
|
#include <utils/stylehelper.h>
|
2009-10-01 16:38:08 +02:00
|
|
|
|
2010-03-12 17:24:20 +01:00
|
|
|
const int notificationTimeout = 8000;
|
2010-03-29 13:43:36 +02:00
|
|
|
const int shortNotificationTimeout = 1000;
|
2010-03-12 17:24:20 +01:00
|
|
|
|
2010-03-22 15:02:16 +01:00
|
|
|
namespace Core {
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
class FadeWidgetHack : public QWidget
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2010-03-12 16:26:42 +01:00
|
|
|
void FadeWidgetHack::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
if (m_opacity == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QPainter p(this);
|
|
|
|
p.setOpacity(m_opacity);
|
2010-03-24 11:15:56 +01:00
|
|
|
if (m_opacity > 0)
|
|
|
|
Utils::StyleHelper::verticalGradient(&p, rect(), rect());
|
2010-03-12 16:26:42 +01:00
|
|
|
}
|
|
|
|
|
2010-09-16 12:26:28 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
struct FutureProgressPrivate {
|
|
|
|
explicit FutureProgressPrivate(FutureProgress *q);
|
|
|
|
|
2010-10-15 17:22:45 +02:00
|
|
|
void tryToFadeAway();
|
|
|
|
|
2010-09-16 12:26:28 +02:00
|
|
|
QFutureWatcher<void> m_watcher;
|
|
|
|
Internal::ProgressBar *m_progress;
|
|
|
|
QWidget *m_widget;
|
|
|
|
QHBoxLayout *m_widgetLayout;
|
|
|
|
QString m_type;
|
2010-10-15 17:22:45 +02:00
|
|
|
FutureProgress::KeepOnFinishType m_keep;
|
2010-09-16 12:26:28 +02:00
|
|
|
bool m_waitingForUserInteraction;
|
|
|
|
Internal::FadeWidgetHack *m_faderWidget;
|
2010-10-15 17:22:45 +02:00
|
|
|
FutureProgress *m_q;
|
|
|
|
bool m_isFading;
|
2010-09-16 12:26:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
FutureProgressPrivate::FutureProgressPrivate(FutureProgress *q) :
|
|
|
|
m_progress(new Internal::ProgressBar), m_widget(0), m_widgetLayout(new QHBoxLayout),
|
2011-06-24 16:00:47 +02:00
|
|
|
m_keep(FutureProgress::HideOnFinish), m_waitingForUserInteraction(false),
|
2010-10-15 17:22:45 +02:00
|
|
|
m_faderWidget(new Internal::FadeWidgetHack(q)), m_q(q), m_isFading(false)
|
2010-09-16 12:26:28 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-12-21 17:11:47 +01:00
|
|
|
/*!
|
|
|
|
\mainclass
|
|
|
|
\class Core::FutureProgress
|
|
|
|
\brief The FutureProgress class is used to adapt the appearance of
|
|
|
|
progress indicators that were created through the ProgressManager class.
|
|
|
|
|
|
|
|
Use the instance of this class that was returned by
|
|
|
|
ProgressManager::addTask() to define a widget that
|
|
|
|
should be shown below the progress bar, or to change the
|
|
|
|
progress title.
|
|
|
|
Also use it to react on the event that the user clicks on
|
|
|
|
the progress indicator (which can be used to e.g. open a more detailed
|
|
|
|
view, or the results of the task).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn void FutureProgress::clicked()
|
|
|
|
Connect to this signal to get informed when the user clicks on the
|
|
|
|
progress indicator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn void FutureProgress::finished()
|
|
|
|
Another way to get informed when the task has finished.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn QWidget FutureProgress::widget() const
|
|
|
|
Returns the custom widget that is shown below the progress indicator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn FutureProgress::FutureProgress(QWidget *parent)
|
|
|
|
\internal
|
|
|
|
*/
|
2010-09-16 12:26:28 +02:00
|
|
|
FutureProgress::FutureProgress(QWidget *parent) :
|
|
|
|
QWidget(parent), d(new FutureProgressPrivate(this))
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout;
|
|
|
|
setLayout(layout);
|
2010-09-16 12:26:28 +02:00
|
|
|
layout->addWidget(d->m_progress);
|
2008-12-02 12:01:29 +01:00
|
|
|
layout->setMargin(0);
|
|
|
|
layout->setSpacing(0);
|
2010-09-16 12:26:28 +02:00
|
|
|
layout->addLayout(d->m_widgetLayout);
|
|
|
|
d->m_widgetLayout->setContentsMargins(7, 0, 7, 2);
|
|
|
|
d->m_widgetLayout->setSpacing(0);
|
|
|
|
|
|
|
|
connect(&d->m_watcher, SIGNAL(started()), this, SLOT(setStarted()));
|
|
|
|
connect(&d->m_watcher, SIGNAL(finished()), this, SLOT(setFinished()));
|
|
|
|
connect(&d->m_watcher, SIGNAL(progressRangeChanged(int,int)), this, SLOT(setProgressRange(int,int)));
|
|
|
|
connect(&d->m_watcher, SIGNAL(progressValueChanged(int)), this, SLOT(setProgressValue(int)));
|
2011-09-16 15:00:41 +02:00
|
|
|
connect(&d->m_watcher, SIGNAL(progressTextChanged(QString)),
|
|
|
|
this, SLOT(setProgressText(QString)));
|
2010-09-16 12:26:28 +02:00
|
|
|
connect(d->m_progress, SIGNAL(clicked()), this, SLOT(cancel()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 17:11:47 +01:00
|
|
|
/*!
|
|
|
|
\fn FutureProgress::~FutureProgress()
|
|
|
|
\internal
|
|
|
|
*/
|
2008-12-02 12:01:29 +01:00
|
|
|
FutureProgress::~FutureProgress()
|
|
|
|
{
|
2011-09-16 15:00:41 +02:00
|
|
|
delete d->m_widget;
|
|
|
|
delete d;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 17:11:47 +01:00
|
|
|
/*!
|
|
|
|
\fn void FutureProgress::setWidget(QWidget *widget)
|
|
|
|
Sets the \a widget to show below the progress bar.
|
|
|
|
This will be destroyed when the progress indicator is destroyed.
|
|
|
|
Default is to show no widget below the progress indicator.
|
|
|
|
*/
|
2008-12-02 12:01:29 +01:00
|
|
|
void FutureProgress::setWidget(QWidget *widget)
|
|
|
|
{
|
2011-09-16 15:00:41 +02:00
|
|
|
delete d->m_widget;
|
2008-12-02 12:01:29 +01:00
|
|
|
QSizePolicy sp = widget->sizePolicy();
|
|
|
|
sp.setHorizontalPolicy(QSizePolicy::Ignored);
|
|
|
|
widget->setSizePolicy(sp);
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_widget = widget;
|
|
|
|
if (d->m_widget)
|
|
|
|
d->m_widgetLayout->addWidget(d->m_widget);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 17:11:47 +01:00
|
|
|
/*!
|
|
|
|
\fn void FutureProgress::setTitle(const QString &title)
|
|
|
|
Changes the \a title of the progress indicator.
|
|
|
|
*/
|
2008-12-02 12:01:29 +01:00
|
|
|
void FutureProgress::setTitle(const QString &title)
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_progress->setTitle(title);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 17:11:47 +01:00
|
|
|
/*!
|
|
|
|
\fn QString FutureProgress::title() const
|
|
|
|
Returns the title of the progress indicator.
|
|
|
|
*/
|
2008-12-02 12:01:29 +01:00
|
|
|
QString FutureProgress::title() const
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
return d->m_progress->title();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FutureProgress::cancel()
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_watcher.future().cancel();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2010-03-09 19:31:06 +01:00
|
|
|
void FutureProgress::updateToolTip(const QString &text)
|
|
|
|
{
|
|
|
|
setToolTip("<b>" + title() + "</b><br>" + text);
|
|
|
|
}
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void FutureProgress::setStarted()
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_progress->reset();
|
|
|
|
d->m_progress->setError(false);
|
|
|
|
d->m_progress->setRange(d->m_watcher.progressMinimum(), d->m_watcher.progressMaximum());
|
|
|
|
d->m_progress->setValue(d->m_watcher.progressValue());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2010-03-12 16:26:42 +01:00
|
|
|
|
|
|
|
bool FutureProgress::eventFilter(QObject *, QEvent *e)
|
|
|
|
{
|
2010-10-15 17:22:45 +02:00
|
|
|
if (d->m_keep != KeepOnFinish && d->m_waitingForUserInteraction
|
2010-03-12 16:26:42 +01:00
|
|
|
&& (e->type() == QEvent::MouseMove || e->type() == QEvent::KeyPress)) {
|
|
|
|
qApp->removeEventFilter(this);
|
2010-03-12 17:24:20 +01:00
|
|
|
QTimer::singleShot(notificationTimeout, this, SLOT(fadeAway()));
|
2010-03-12 16:26:42 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void FutureProgress::setFinished()
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
updateToolTip(d->m_watcher.future().progressText());
|
2010-03-22 14:58:41 +01:00
|
|
|
|
2011-05-12 13:21:41 +02:00
|
|
|
d->m_progress->setFinished(true);
|
2010-03-22 14:58:41 +01:00
|
|
|
|
2010-09-16 12:26:28 +02:00
|
|
|
if (d->m_watcher.future().isCanceled()) {
|
|
|
|
d->m_progress->setError(true);
|
2008-12-02 12:01:29 +01:00
|
|
|
} else {
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_progress->setError(false);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
emit finished();
|
2010-10-15 17:22:45 +02:00
|
|
|
d->tryToFadeAway();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FutureProgressPrivate::tryToFadeAway()
|
|
|
|
{
|
|
|
|
if (m_isFading)
|
|
|
|
return;
|
2011-06-24 15:47:48 +02:00
|
|
|
if (m_keep == FutureProgress::KeepOnFinishTillUserInteraction
|
2011-06-24 16:00:47 +02:00
|
|
|
|| (m_keep == FutureProgress::HideOnFinish && m_progress->hasError())) {
|
2010-10-15 17:22:45 +02:00
|
|
|
m_waitingForUserInteraction = true;
|
|
|
|
//eventfilter is needed to get user interaction
|
|
|
|
//events to start QTimer::singleShot later
|
|
|
|
qApp->installEventFilter(m_q);
|
|
|
|
m_isFading = true;
|
2011-06-24 16:00:47 +02:00
|
|
|
} else if (m_keep == FutureProgress::HideOnFinish) {
|
2010-10-15 17:22:45 +02:00
|
|
|
QTimer::singleShot(shortNotificationTimeout, m_q, SLOT(fadeAway()));
|
|
|
|
m_isFading = true;
|
2010-03-12 16:26:42 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2010-10-15 17:22:45 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void FutureProgress::setProgressRange(int min, int max)
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_progress->setRange(min, max);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FutureProgress::setProgressValue(int val)
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_progress->setValue(val);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FutureProgress::setProgressText(const QString &text)
|
|
|
|
{
|
2010-03-09 19:31:06 +01:00
|
|
|
updateToolTip(text);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 17:11:47 +01:00
|
|
|
/*!
|
|
|
|
\fn void FutureProgress::setFuture(const QFuture<void> &future)
|
|
|
|
\internal
|
|
|
|
*/
|
2008-12-02 12:01:29 +01:00
|
|
|
void FutureProgress::setFuture(const QFuture<void> &future)
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_watcher.setFuture(future);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 17:11:47 +01:00
|
|
|
/*!
|
|
|
|
\fn QFuture<void> FutureProgress::future() const
|
|
|
|
Returns a QFuture object that represents this running task.
|
|
|
|
*/
|
2008-12-02 12:01:29 +01:00
|
|
|
QFuture<void> FutureProgress::future() const
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
return d->m_watcher.future();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 17:11:47 +01:00
|
|
|
/*!
|
|
|
|
\fn void FutureProgress::mousePressEvent(QMouseEvent *event)
|
|
|
|
\internal
|
|
|
|
*/
|
2008-12-02 12:01:29 +01:00
|
|
|
void FutureProgress::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
emit clicked();
|
|
|
|
QWidget::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
2010-03-12 16:26:42 +01:00
|
|
|
void FutureProgress::resizeEvent(QResizeEvent *)
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_faderWidget->setGeometry(rect());
|
2010-03-12 16:26:42 +01:00
|
|
|
}
|
|
|
|
|
2009-12-21 17:11:47 +01:00
|
|
|
/*!
|
|
|
|
\fn bool FutureProgress::hasError() const
|
|
|
|
Returns the error state of this progress indicator.
|
|
|
|
*/
|
2008-12-02 12:01:29 +01:00
|
|
|
bool FutureProgress::hasError() const
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
return d->m_progress->hasError();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-03-12 16:26:42 +01:00
|
|
|
|
|
|
|
void FutureProgress::fadeAway()
|
|
|
|
{
|
2010-09-16 12:26:28 +02:00
|
|
|
d->m_faderWidget->raise();
|
2011-01-12 10:04:55 +01:00
|
|
|
QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this);
|
2010-09-16 12:26:28 +02:00
|
|
|
QPropertyAnimation *animation = new QPropertyAnimation(d->m_faderWidget, "opacity");
|
2010-03-12 16:26:42 +01:00
|
|
|
animation->setDuration(600);
|
|
|
|
animation->setEndValue(1.0);
|
2010-03-24 11:15:56 +01:00
|
|
|
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()));
|
2010-03-12 16:26:42 +01:00
|
|
|
}
|
2010-03-22 15:02:16 +01:00
|
|
|
|
2010-09-16 12:26:28 +02:00
|
|
|
void FutureProgress::setType(const QString &type)
|
|
|
|
{
|
|
|
|
d->m_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FutureProgress::type() const
|
|
|
|
{
|
|
|
|
return d->m_type;
|
|
|
|
}
|
|
|
|
|
2010-10-15 17:22:45 +02:00
|
|
|
void FutureProgress::setKeepOnFinish(KeepOnFinishType keepType)
|
2010-09-16 12:26:28 +02:00
|
|
|
{
|
2010-10-15 17:22:45 +02:00
|
|
|
if (d->m_keep == keepType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
d->m_keep = keepType;
|
|
|
|
|
|
|
|
//if it is not finished tryToFadeAway is called by setFinished at the end
|
|
|
|
if (d->m_watcher.isFinished()) {
|
|
|
|
d->tryToFadeAway();
|
|
|
|
}
|
2010-09-16 12:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FutureProgress::keepOnFinish() const
|
|
|
|
{
|
|
|
|
return d->m_keep;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *FutureProgress::widget() const
|
|
|
|
{
|
|
|
|
return d->m_widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Core
|
|
|
|
|
2010-03-22 15:02:16 +01:00
|
|
|
#include "futureprogress.moc"
|