OSDN Git Service

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