OSDN Git Service

Merge branch '1.3'
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / progressmanager / progressview.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** 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
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include "progressview.h"
31 #include "futureprogress.h"
32
33 #include <utils/qtcassert.h>
34
35 #include <QtGui/QHBoxLayout>
36
37 using namespace Core;
38 using namespace Core::Internal;
39
40 ProgressView::ProgressView(QWidget *parent)
41     : QWidget(parent)
42 {
43     m_layout = new QVBoxLayout;
44     setLayout(m_layout);
45     m_layout->setMargin(0);
46     m_layout->setSpacing(0);
47     setWindowTitle(tr("Processes"));
48 }
49
50 ProgressView::~ProgressView()
51 {
52     qDeleteAll(m_taskList);
53     m_taskList.clear();
54     m_type.clear();
55     m_keep.clear();
56 }
57
58 FutureProgress *ProgressView::addTask(const QFuture<void> &future,
59                                       const QString &title,
60                                       const QString &type,
61                                       ProgressManager::ProgressFlags flags)
62 {
63     removeOldTasks(type);
64     if (m_taskList.size() == 3)
65         removeOneOldTask();
66     FutureProgress *progress = new FutureProgress(this);
67     progress->setTitle(title);
68     progress->setFuture(future);
69     m_layout->insertWidget(0, progress);
70     m_taskList.append(progress);
71     m_type.insert(progress, type);
72     m_keep.insert(progress, (flags & ProgressManager::KeepOnFinish));
73     connect(progress, SIGNAL(finished()), this, SLOT(slotFinished()));
74     return progress;
75 }
76
77 void ProgressView::removeOldTasks(const QString &type, bool keepOne)
78 {
79     bool firstFound = !keepOne; // start with false if we want to keep one
80     QList<FutureProgress *>::iterator i = m_taskList.end();
81     while (i != m_taskList.begin()) {
82         --i;
83         if (m_type.value(*i) == type) {
84             if (firstFound && (*i)->future().isFinished()) {
85                 deleteTask(*i);
86                 i = m_taskList.erase(i);
87             }
88             firstFound = true;
89         }
90     }
91 }
92
93 void ProgressView::deleteTask(FutureProgress *progress)
94 {
95     m_type.remove(progress);
96     m_keep.remove(progress);
97     layout()->removeWidget(progress);
98     progress->hide();
99     progress->deleteLater();
100 }
101
102 void ProgressView::removeOneOldTask()
103 {
104     if (m_taskList.isEmpty())
105         return;
106     // look for oldest ended process
107     for (QList<FutureProgress *>::iterator i = m_taskList.begin(); i != m_taskList.end(); ++i) {
108         if ((*i)->future().isFinished()) {
109             deleteTask(*i);
110             i = m_taskList.erase(i);
111             return;
112         }
113     }
114     // no ended process, look for a task type with multiple running tasks and remove the oldest one
115     for (QList<FutureProgress *>::iterator i = m_taskList.begin(); i != m_taskList.end(); ++i) {
116         QString type = m_type.value(*i);
117         if (m_type.keys(type).size() > 1) { // don't care for optimizations it's only a handful of entries
118             deleteTask(*i);
119             i = m_taskList.erase(i);
120             return;
121         }
122     }
123
124     // no ended process, no type with multiple processes, just remove the oldest task
125     FutureProgress *task = m_taskList.takeFirst();
126     deleteTask(task);
127 }
128
129 void ProgressView::removeTask(FutureProgress *task)
130 {
131     m_taskList.removeAll(task);
132     deleteTask(task);
133 }
134
135 void ProgressView::slotFinished()
136 {
137     FutureProgress *progress = qobject_cast<FutureProgress *>(sender());
138     QTC_ASSERT(progress, return);
139     if (m_keep.contains(progress) && !m_keep.value(progress) && !progress->hasError())
140         removeTask(progress);
141     removeOldTasks(m_type.value(progress), true);
142 }