2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Nokia.
|
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
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Alternatively, 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
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
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
|
|
|
|
|
|
|
#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 <QtCore/QTimer>
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
#include <QtCore/QPropertyAnimation>
|
|
|
|
#include <utils/stylehelper.h>
|
2009-10-01 16:38:08 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace Core;
|
|
|
|
|
2010-03-12 17:24:20 +01:00
|
|
|
const int notificationTimeout = 8000;
|
|
|
|
|
2010-03-12 16:26:42 +01:00
|
|
|
void FadeWidgetHack::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
if (m_opacity == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QPainter p(this);
|
|
|
|
p.setOpacity(m_opacity);
|
|
|
|
Utils::StyleHelper::verticalGradient(&p, rect(), rect());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2008-12-02 12:01:29 +01:00
|
|
|
FutureProgress::FutureProgress(QWidget *parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
m_progress(new ProgressBar),
|
|
|
|
m_widget(0),
|
|
|
|
m_widgetLayout(new QHBoxLayout)
|
|
|
|
{
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout;
|
|
|
|
setLayout(layout);
|
|
|
|
layout->addWidget(m_progress);
|
|
|
|
layout->setMargin(0);
|
|
|
|
layout->setSpacing(0);
|
|
|
|
layout->addLayout(m_widgetLayout);
|
2010-02-25 18:53:42 +01:00
|
|
|
m_widgetLayout->setContentsMargins(7, 0, 7, 2);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_widgetLayout->setSpacing(0);
|
|
|
|
|
|
|
|
connect(&m_watcher, SIGNAL(started()), this, SLOT(setStarted()));
|
|
|
|
connect(&m_watcher, SIGNAL(finished()), this, SLOT(setFinished()));
|
|
|
|
connect(&m_watcher, SIGNAL(progressRangeChanged(int,int)), this, SLOT(setProgressRange(int,int)));
|
|
|
|
connect(&m_watcher, SIGNAL(progressValueChanged(int)), this, SLOT(setProgressValue(int)));
|
|
|
|
connect(&m_watcher, SIGNAL(progressTextChanged(const QString&)),
|
|
|
|
this, SLOT(setProgressText(const QString&)));
|
|
|
|
connect(m_progress, SIGNAL(clicked()), this, SLOT(cancel()));
|
2010-03-12 16:26:42 +01:00
|
|
|
|
|
|
|
m_keep = false;
|
|
|
|
m_waitingForUserInteraction = false;
|
|
|
|
m_faderWidget = new FadeWidgetHack(this);
|
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()
|
|
|
|
{
|
|
|
|
if (m_widget)
|
|
|
|
delete m_widget;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (m_widget)
|
|
|
|
delete m_widget;
|
|
|
|
QSizePolicy sp = widget->sizePolicy();
|
|
|
|
sp.setHorizontalPolicy(QSizePolicy::Ignored);
|
|
|
|
widget->setSizePolicy(sp);
|
|
|
|
m_widget = widget;
|
|
|
|
if (m_widget)
|
|
|
|
m_widgetLayout->addWidget(m_widget);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
m_progress->setTitle(title);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return m_progress->title();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FutureProgress::cancel()
|
|
|
|
{
|
|
|
|
m_watcher.future().cancel();
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
m_progress->reset();
|
|
|
|
m_progress->setError(false);
|
|
|
|
m_progress->setRange(m_watcher.progressMinimum(), m_watcher.progressMaximum());
|
|
|
|
m_progress->setValue(m_watcher.progressValue());
|
|
|
|
}
|
|
|
|
|
2010-03-12 16:26:42 +01:00
|
|
|
|
|
|
|
bool FutureProgress::eventFilter(QObject *, QEvent *e)
|
|
|
|
{
|
|
|
|
if (m_waitingForUserInteraction
|
|
|
|
&& (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-03-09 19:31:06 +01:00
|
|
|
updateToolTip(m_watcher.future().progressText());
|
2010-03-22 14:58:41 +01:00
|
|
|
|
|
|
|
// Special case for concurrent jobs that don't use QFutureInterface to report progress
|
|
|
|
if (m_watcher.progressMinimum() == 0 && m_watcher.progressMaximum() == 0) {
|
|
|
|
m_progress->setRange(0, 1);
|
|
|
|
m_progress->setValue(1);
|
|
|
|
}
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (m_watcher.future().isCanceled()) {
|
|
|
|
m_progress->setError(true);
|
|
|
|
} else {
|
|
|
|
m_progress->setError(false);
|
|
|
|
}
|
|
|
|
emit finished();
|
2010-03-12 16:26:42 +01:00
|
|
|
if (m_keep) {
|
|
|
|
m_waitingForUserInteraction = true;
|
|
|
|
qApp->installEventFilter(this);
|
|
|
|
} else {
|
2010-03-12 17:24:20 +01:00
|
|
|
QTimer::singleShot(notificationTimeout, this, SLOT(fadeAway()));
|
2010-03-12 16:26:42 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FutureProgress::setProgressRange(int min, int max)
|
|
|
|
{
|
|
|
|
m_progress->setRange(min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FutureProgress::setProgressValue(int val)
|
|
|
|
{
|
|
|
|
m_progress->setValue(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
m_watcher.setFuture(future);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return m_watcher.future();
|
|
|
|
}
|
|
|
|
|
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 *)
|
|
|
|
{
|
|
|
|
m_faderWidget->setGeometry(rect());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return m_progress->hasError();
|
|
|
|
}
|
2010-03-12 16:26:42 +01:00
|
|
|
|
|
|
|
void FutureProgress::fadeAway()
|
|
|
|
{
|
|
|
|
m_faderWidget->raise();
|
|
|
|
QPropertyAnimation *animation = new QPropertyAnimation(m_faderWidget, "opacity");
|
|
|
|
animation->setDuration(600);
|
|
|
|
animation->setEndValue(1.0);
|
|
|
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
|
|
|
connect(animation, SIGNAL(finished()), this, SIGNAL(removeMe()));
|
|
|
|
}
|