Files
qt-creator/src/plugins/coreplugin/progressmanager/futureprogress.cpp

228 lines
6.2 KiB
C++
Raw Normal View History

/**************************************************************************
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
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2008-12-02 12:01:29 +01:00
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** 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
**
**************************************************************************/
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>
#include <QtGui/QProgressBar>
#include <QtGui/QHBoxLayout>
2008-12-02 12:01:29 +01:00
using namespace Core;
/*!
\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()));
}
/*!
\fn FutureProgress::~FutureProgress()
\internal
*/
2008-12-02 12:01:29 +01:00
FutureProgress::~FutureProgress()
{
if (m_widget)
delete m_widget;
}
/*!
\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);
}
/*!
\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);
}
/*!
\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();
}
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());
}
void FutureProgress::setFinished()
{
updateToolTip(m_watcher.future().progressText());
2008-12-02 12:01:29 +01:00
if (m_watcher.future().isCanceled()) {
m_progress->setError(true);
} else {
m_progress->setError(false);
}
emit finished();
}
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)
{
updateToolTip(text);
2008-12-02 12:01:29 +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);
}
/*!
\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();
}
/*!
\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);
}
/*!
\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();
}