progressmanager.cpp 4.7 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

30
#include "progressmanager_p.h"
con's avatar
con committed
31 32 33
#include "progressview.h"
#include "baseview.h"
#include "coreconstants.h"
34
#include "icore.h"
con's avatar
con committed
35 36
#include "uniqueidmanager.h"

hjk's avatar
hjk committed
37 38
#include <utils/qtcassert.h>

con's avatar
con committed
39 40 41
using namespace Core;
using namespace Core::Internal;

42
ProgressManagerPrivate::ProgressManagerPrivate(QObject *parent)
43 44
  : ProgressManager(parent),
    m_applicationTask(0)
con's avatar
con committed
45 46
{
    m_progressView = new ProgressView;
47
    ICore *core = ICore::instance();
con's avatar
con committed
48 49 50
    connect(core, SIGNAL(coreAboutToClose()), this, SLOT(cancelAllRunningTasks()));
}

51
ProgressManagerPrivate::~ProgressManagerPrivate()
con's avatar
con committed
52 53 54
{
}

55
void ProgressManagerPrivate::init()
con's avatar
con committed
56 57 58
{
}

59
void ProgressManagerPrivate::cancelTasks(const QString &type)
con's avatar
con committed
60
{
61
    bool found = false;
con's avatar
con committed
62 63 64 65 66 67
    QMap<QFutureWatcher<void> *, QString>::iterator task = m_runningTasks.begin();
    while (task != m_runningTasks.end()) {
        if (task.value() != type) {
            ++task;
            continue;
        }
68
        found = true;
con's avatar
con committed
69
        disconnect(task.key(), SIGNAL(finished()), this, SLOT(taskFinished()));
70 71
        if (m_applicationTask == task.key())
            disconnectApplicationTask();
con's avatar
con committed
72 73 74 75
        task.key()->cancel();
        delete task.key();
        task = m_runningTasks.erase(task);
    }
76 77 78
    if (found) {
        emit allTasksFinished(type);
    }
con's avatar
con committed
79 80
}

81
void ProgressManagerPrivate::cancelAllRunningTasks()
con's avatar
con committed
82 83 84 85
{
    QMap<QFutureWatcher<void> *, QString>::const_iterator task = m_runningTasks.constBegin();
    while (task != m_runningTasks.constEnd()) {
        disconnect(task.key(), SIGNAL(finished()), this, SLOT(taskFinished()));
86 87
        if (m_applicationTask == task.key())
            disconnectApplicationTask();
con's avatar
con committed
88 89 90 91 92 93 94
        task.key()->cancel();
        delete task.key();
        ++task;
    }
    m_runningTasks.clear();
}

95 96
FutureProgress *ProgressManagerPrivate::addTask(const QFuture<void> &future, const QString &title,
                                                const QString &type, ProgressFlags flags)
con's avatar
con committed
97 98 99 100
{
    QFutureWatcher<void> *watcher = new QFutureWatcher<void>();
    m_runningTasks.insert(watcher, type);
    connect(watcher, SIGNAL(finished()), this, SLOT(taskFinished()));
101 102 103 104 105 106 107 108
    if (flags & ShowInApplicationIcon) {
        m_applicationTask = watcher;
        connect(m_applicationTask, SIGNAL(progressRangeChanged(int,int)),
                this, SLOT(setApplicationProgressRange(int,int)));
        connect(m_applicationTask, SIGNAL(progressValueChanged(int)),
                this, SLOT(setApplicationProgressValue(int)));
        setApplicationProgressVisible(true);
    }
con's avatar
con committed
109
    watcher->setFuture(future);
110
    emit taskStarted(type);
111
    return m_progressView->addTask(future, title, type, flags);
con's avatar
con committed
112 113
}

114
QWidget *ProgressManagerPrivate::progressView()
con's avatar
con committed
115 116 117 118
{
    return m_progressView;
}

119
void ProgressManagerPrivate::taskFinished()
con's avatar
con committed
120 121
{
    QObject *taskObject = sender();
hjk's avatar
hjk committed
122
    QTC_ASSERT(taskObject, return);
con's avatar
con committed
123
    QFutureWatcher<void> *task = static_cast<QFutureWatcher<void> *>(taskObject);
124 125
    if (m_applicationTask == task)
        disconnectApplicationTask();
126
    QString type = m_runningTasks.value(task);
con's avatar
con committed
127 128
    m_runningTasks.remove(task);
    delete task;
129 130 131 132

    if (!m_runningTasks.values().contains(type)) {
        emit allTasksFinished(type);
    }
con's avatar
con committed
133
}
134 135 136 137 138 139 140 141 142 143

void ProgressManagerPrivate::disconnectApplicationTask()
{
    disconnect(m_applicationTask, SIGNAL(progressRangeChanged(int,int)),
            this, SLOT(setApplicationProgressRange(int,int)));
    disconnect(m_applicationTask, SIGNAL(progressValueChanged(int)),
            this, SLOT(setApplicationProgressValue(int)));
    setApplicationProgressVisible(false);
    m_applicationTask = 0;
}