OSDN Git Service

Update license.
[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) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "progressview.h"
34 #include "futureprogress.h"
35
36 #include <utils/qtcassert.h>
37
38 #include <QtGui/QHBoxLayout>
39 #include <QtGui/QVBoxLayout>
40
41 using namespace Core;
42 using namespace Core::Internal;
43
44 ProgressView::ProgressView(QWidget *parent)
45     : QWidget(parent)
46 {
47     m_layout = new QVBoxLayout;
48     setLayout(m_layout);
49     m_layout->setMargin(0);
50     m_layout->setSpacing(0);
51     setWindowTitle(tr("Processes"));
52 }
53
54 ProgressView::~ProgressView()
55 {
56     qDeleteAll(m_taskList);
57     m_taskList.clear();
58 }
59
60 FutureProgress *ProgressView::addTask(const QFuture<void> &future,
61                                       const QString &title,
62                                       const QString &type,
63                                       ProgressManager::ProgressFlags flags)
64 {
65     removeOldTasks(type);
66     if (m_taskList.size() == 3)
67         removeOneOldTask();
68     FutureProgress *progress = new FutureProgress(this);
69     progress->setTitle(title);
70     progress->setFuture(future);
71
72     m_layout->insertWidget(0, progress);
73     m_taskList.append(progress);
74     progress->setType(type);
75     if (flags.testFlag(ProgressManager::KeepOnFinish)) {
76         progress->setKeepOnFinish(FutureProgress::KeepOnFinishTillUserInteraction);
77     } else {
78         progress->setKeepOnFinish(FutureProgress::DontKeepOnFinish);
79     }
80     connect(progress, SIGNAL(removeMe()), this, SLOT(slotRemoveTask()));
81     return progress;
82 }
83
84 void ProgressView::removeOldTasks(const QString &type, bool keepOne)
85 {
86     bool firstFound = !keepOne; // start with false if we want to keep one
87     QList<FutureProgress *>::iterator i = m_taskList.end();
88     while (i != m_taskList.begin()) {
89         --i;
90         if ((*i)->type() == type) {
91             if (firstFound && (*i)->future().isFinished()) {
92                 deleteTask(*i);
93                 i = m_taskList.erase(i);
94             }
95             firstFound = true;
96         }
97     }
98 }
99
100 void ProgressView::deleteTask(FutureProgress *progress)
101 {
102     layout()->removeWidget(progress);
103     progress->hide();
104     progress->deleteLater();
105 }
106
107 void ProgressView::removeOneOldTask()
108 {
109     if (m_taskList.isEmpty())
110         return;
111     // look for oldest ended process
112     for (QList<FutureProgress *>::iterator i = m_taskList.begin(); i != m_taskList.end(); ++i) {
113         if ((*i)->future().isFinished()) {
114             deleteTask(*i);
115             i = m_taskList.erase(i);
116             return;
117         }
118     }
119     // no ended process, look for a task type with multiple running tasks and remove the oldest one
120     for (QList<FutureProgress *>::iterator i = m_taskList.begin(); i != m_taskList.end(); ++i) {
121         QString type = (*i)->type();
122
123         int taskCount = 0;
124         foreach (FutureProgress *p, m_taskList)
125             if (p->type() == type)
126                 ++taskCount;
127
128         if (taskCount > 1) { // don't care for optimizations it's only a handful of entries
129             deleteTask(*i);
130             i = m_taskList.erase(i);
131             return;
132         }
133     }
134
135     // no ended process, no type with multiple processes, just remove the oldest task
136     FutureProgress *task = m_taskList.takeFirst();
137     deleteTask(task);
138 }
139
140 void ProgressView::removeTask(FutureProgress *task)
141 {
142     m_taskList.removeAll(task);
143     deleteTask(task);
144 }
145
146 void ProgressView::slotRemoveTask()
147 {
148     FutureProgress *progress = qobject_cast<FutureProgress *>(sender());
149     QTC_ASSERT(progress, return);
150     QString type = progress->type();
151     removeTask(progress);
152     removeOldTasks(type, true);
153 }