2010-03-10 22:36:47 +00:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file multitask.h
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
|
2013-05-19 17:37:30 +03:00
|
|
|
* @brief
|
2010-03-10 22:36:47 +00:00
|
|
|
* @see The GNU Public License (GPL) Version 3
|
2013-05-19 17:37:30 +03:00
|
|
|
* @defgroup
|
2010-03-10 22:36:47 +00:00
|
|
|
* @{
|
2013-05-19 17:37:30 +03:00
|
|
|
*
|
2010-03-10 22:36:47 +00:00
|
|
|
*****************************************************************************/
|
2013-05-19 17:37:30 +03:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-03-10 22:36:47 +00:00
|
|
|
* (at your option) any later version.
|
2013-05-19 17:37:30 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2010-03-10 22:36:47 +00:00
|
|
|
* for more details.
|
2013-05-19 17:37:30 +03:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-03-10 22:36:47 +00:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2010-02-01 14:10:06 +00:00
|
|
|
|
|
|
|
#ifndef MULTITASK_H
|
|
|
|
#define MULTITASK_H
|
|
|
|
|
|
|
|
#include "qtconcurrent_global.h"
|
|
|
|
#include "runextensions.h"
|
|
|
|
|
|
|
|
#include <QtCore/QObject>
|
|
|
|
#include <QtCore/QList>
|
|
|
|
#include <QtCore/QEventLoop>
|
|
|
|
#include <QtCore/QFutureWatcher>
|
2013-09-15 23:01:56 +02:00
|
|
|
#include <QtConcurrent/QtConcurrentRun>
|
2010-02-01 14:10:06 +00:00
|
|
|
#include <QtCore/QThreadPool>
|
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
namespace QtConcurrent {
|
2013-05-19 17:37:30 +03:00
|
|
|
class QTCONCURRENT_EXPORT MultiTaskBase : public QObject, public QRunnable {
|
2010-02-01 14:10:06 +00:00
|
|
|
Q_OBJECT
|
|
|
|
protected slots:
|
2013-05-19 17:37:30 +03:00
|
|
|
virtual void cancelSelf() = 0;
|
2010-02-01 14:10:06 +00:00
|
|
|
virtual void setFinished() = 0;
|
|
|
|
virtual void setProgressRange(int min, int max) = 0;
|
|
|
|
virtual void setProgressValue(int value) = 0;
|
2013-05-19 17:37:30 +03:00
|
|
|
virtual void setProgressText(QString value) = 0;
|
2010-02-01 14:10:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Class, typename R>
|
2013-05-19 17:37:30 +03:00
|
|
|
class MultiTask : public MultiTaskBase {
|
2010-02-01 14:10:06 +00:00
|
|
|
public:
|
2013-05-19 17:37:30 +03:00
|
|
|
MultiTask(void(Class::*fn)(QFutureInterface<R> &), const QList<Class *> &objects)
|
2010-02-01 14:10:06 +00:00
|
|
|
: fn(fn),
|
|
|
|
objects(objects)
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
maxProgress = 100 * objects.size();
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QFuture<R> future()
|
|
|
|
{
|
|
|
|
futureInterface.reportStarted();
|
|
|
|
return futureInterface.future();
|
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
QThreadPool::globalInstance()->releaseThread();
|
2013-05-19 17:37:30 +03:00
|
|
|
|
2010-02-01 14:10:06 +00:00
|
|
|
futureInterface.setProgressRange(0, maxProgress);
|
2013-05-19 17:37:30 +03:00
|
|
|
foreach(Class * object, objects) {
|
2010-02-01 14:10:06 +00:00
|
|
|
QFutureWatcher<R> *watcher = new QFutureWatcher<R>();
|
|
|
|
watchers.insert(object, watcher);
|
|
|
|
finished.insert(watcher, false);
|
|
|
|
connect(watcher, SIGNAL(finished()), this, SLOT(setFinished()));
|
2013-05-19 17:37:30 +03:00
|
|
|
connect(watcher, SIGNAL(progressRangeChanged(int, int)), this, SLOT(setProgressRange(int, int)));
|
2010-02-01 14:10:06 +00:00
|
|
|
connect(watcher, SIGNAL(progressValueChanged(int)), this, SLOT(setProgressValue(int)));
|
|
|
|
connect(watcher, SIGNAL(progressTextChanged(QString)), this, SLOT(setProgressText(QString)));
|
|
|
|
watcher->setFuture(QtConcurrent::run(fn, object));
|
|
|
|
}
|
|
|
|
selfWatcher = new QFutureWatcher<R>();
|
|
|
|
connect(selfWatcher, SIGNAL(canceled()), this, SLOT(cancelSelf()));
|
|
|
|
selfWatcher->setFuture(futureInterface.future());
|
|
|
|
loop = new QEventLoop;
|
|
|
|
loop->exec();
|
|
|
|
futureInterface.reportFinished();
|
|
|
|
QThreadPool::globalInstance()->reserveThread();
|
|
|
|
qDeleteAll(watchers.values());
|
|
|
|
delete selfWatcher;
|
|
|
|
delete loop;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
void cancelSelf()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
foreach(QFutureWatcher<R> *watcher, watchers)
|
|
|
|
watcher->future().cancel();
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setFinished()
|
|
|
|
{
|
|
|
|
updateProgress();
|
|
|
|
QFutureWatcher<R> *watcher = static_cast<QFutureWatcher<R> *>(sender());
|
2013-05-19 17:37:30 +03:00
|
|
|
if (finished.contains(watcher)) {
|
2010-02-01 14:10:06 +00:00
|
|
|
finished[watcher] = true;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
bool allFinished = true;
|
|
|
|
const QList<bool> finishedValues = finished.values();
|
2013-05-19 17:37:30 +03:00
|
|
|
foreach(bool isFinished, finishedValues) {
|
2010-02-01 14:10:06 +00:00
|
|
|
if (!isFinished) {
|
|
|
|
allFinished = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
if (allFinished) {
|
2010-02-01 14:10:06 +00:00
|
|
|
loop->quit();
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setProgressRange(int min, int max)
|
|
|
|
{
|
|
|
|
Q_UNUSED(min)
|
|
|
|
Q_UNUSED(max)
|
|
|
|
updateProgress();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setProgressValue(int value)
|
|
|
|
{
|
|
|
|
Q_UNUSED(value)
|
|
|
|
updateProgress();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setProgressText(QString value)
|
|
|
|
{
|
|
|
|
Q_UNUSED(value)
|
|
|
|
updateProgressText();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void updateProgress()
|
|
|
|
{
|
|
|
|
int progressSum = 0;
|
|
|
|
const QList<QFutureWatcher<R> *> watchersValues = watchers.values();
|
2013-05-19 17:37:30 +03:00
|
|
|
|
|
|
|
foreach(QFutureWatcher<R> *watcher, watchersValues) {
|
2010-02-01 14:10:06 +00:00
|
|
|
if (watcher->progressMinimum() == watcher->progressMaximum()) {
|
2013-05-19 17:37:30 +03:00
|
|
|
if (watcher->future().isFinished() && !watcher->future().isCanceled()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
progressSum += 100;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
} else {
|
2013-05-19 17:37:30 +03:00
|
|
|
progressSum += 100 * (watcher->progressValue() - watcher->progressMinimum()) / (watcher->progressMaximum() - watcher->progressMinimum());
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
futureInterface.setProgressValue(progressSum);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateProgressText()
|
|
|
|
{
|
|
|
|
QString text;
|
|
|
|
const QList<QFutureWatcher<R> *> watchersValues = watchers.values();
|
2013-05-19 17:37:30 +03:00
|
|
|
|
|
|
|
foreach(QFutureWatcher<R> *watcher, watchersValues) {
|
|
|
|
if (!watcher->progressText().isEmpty()) {
|
2010-02-01 14:10:06 +00:00
|
|
|
text += watcher->progressText() + "\n";
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
2010-02-01 14:10:06 +00:00
|
|
|
}
|
|
|
|
text = text.trimmed();
|
|
|
|
futureInterface.setProgressValueAndText(futureInterface.progressValue(), text);
|
|
|
|
}
|
|
|
|
|
|
|
|
QFutureInterface<R> futureInterface;
|
|
|
|
void (Class::*fn)(QFutureInterface<R> &);
|
|
|
|
QList<Class *> objects;
|
|
|
|
|
|
|
|
QFutureWatcher<R> *selfWatcher;
|
|
|
|
QMap<Class *, QFutureWatcher<R> *> watchers;
|
|
|
|
QMap<QFutureWatcher<R> *, bool> finished;
|
|
|
|
QEventLoop *loop;
|
|
|
|
int maxProgress;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Class, typename T>
|
|
|
|
QFuture<T> run(void (Class::*fn)(QFutureInterface<T> &), const QList<Class *> &objects, int priority = 0)
|
|
|
|
{
|
|
|
|
MultiTask<Class, T> *task = new MultiTask<Class, T>(fn, objects);
|
|
|
|
QFuture<T> future = task->future();
|
|
|
|
QThreadPool::globalInstance()->start(task, priority);
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
} // namespace QtConcurrent
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
#endif // MULTITASK_H
|