forked from krojew/evernus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveTasksDialog.cpp
More file actions
253 lines (209 loc) · 7.95 KB
/
Copy pathActiveTasksDialog.cpp
File metadata and controls
253 lines (209 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* 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
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDialogButtonBox>
#include <QProgressBar>
#include <QCloseEvent>
#include <QTreeWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QSettings>
#include <QLabel>
#include <QMovie>
#include <QFont>
#ifdef Q_OS_WIN
# include <QWinTaskbarProgress>
# include <QWinTaskbarButton>
#endif
#include "UISettings.h"
#include "ActiveTasksDialog.h"
namespace Evernus
{
#ifdef Q_OS_WIN
ActiveTasksDialog::ActiveTasksDialog(QWinTaskbarButton &taskbarButton, QWidget *parent)
#else
ActiveTasksDialog::ActiveTasksDialog(QWidget *parent)
#endif
: QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowTitleHint)
{
QSettings settings;
auto mainLayout = new QVBoxLayout{this};
auto throbberMovie = new QMovie{":/images/loader.gif", QByteArray{}, this};
QFont font;
font.setPixelSize(16);
auto throbberLayout = new QHBoxLayout{};
mainLayout->addLayout(throbberLayout);
auto throbber = new QLabel{this};
throbberLayout->addWidget(throbber);
throbber->setMovie(throbberMovie);
throbberMovie->start();
auto throbberText = new QLabel{tr("Please wait..."), this};
throbberLayout->addWidget(throbberText, 1, Qt::AlignLeft);
throbberText->setFont(font);
mTaskWidget = new QTreeWidget{this};
mainLayout->addWidget(mTaskWidget, 1);
mTaskWidget->setHeaderHidden(true);
mTaskWidget->setColumnCount(2);
auto progressLayout = new QHBoxLayout{};
mainLayout->addLayout(progressLayout);
mAutoCloseBtn = new QCheckBox{tr("Close automatically"), this};
progressLayout->addWidget(mAutoCloseBtn);
mAutoCloseBtn->setChecked(settings.value(UISettings::autoCloseTasksKey, UISettings::autoCloseTasksDefault).toBool());
connect(mAutoCloseBtn, &QCheckBox::toggled, this, &ActiveTasksDialog::autoCloseSave);
mTotalProgressWidget = new QProgressBar{this};
progressLayout->addWidget(mTotalProgressWidget);
mTotalProgressWidget->setMinimumWidth(500);
mTotalProgressWidget->setValue(0);
mTotalProgressWidget->setMaximum(0);
auto btnBox = new QDialogButtonBox{QDialogButtonBox::Close, this};
mainLayout->addWidget(btnBox);
connect(btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
#ifdef Q_OS_WIN
mTaskbarProgress = taskbarButton.progress();
#endif
setWindowTitle(tr("Active Tasks"));
}
void ActiveTasksDialog::done(int r)
{
if (mTaskItems.empty())
mTaskWidget->clear();
QDialog::done(r);
}
void ActiveTasksDialog::addNewTaskInfo(uint taskId, const QString &description)
{
if (mTaskItems.empty())
{
mHadError = false;
mTotalProgressWidget->setValue(0);
mTotalProgressWidget->setMaximum(1);
#ifdef Q_OS_WIN
mTaskbarProgress->setValue(0);
mTaskbarProgress->setMaximum(1);
mTaskbarProgress->setVisible(true);
#endif
}
else
{
mTotalProgressWidget->setMaximum(mTotalProgressWidget->maximum() + 1);
#ifdef Q_OS_WIN
mTaskbarProgress->setMaximum(mTaskbarProgress->maximum() + 1);
#endif
}
fillTaskItem(taskId, new QTreeWidgetItem{mTaskWidget}, description);
mTaskWidget->resizeColumnToContents(0);
emit taskCountChanged(mTaskItems.size());
}
void ActiveTasksDialog::addNewSubTaskInfo(uint taskId, uint parentTask, const QString &description)
{
auto item = mTaskItems.find(parentTask);
if (item == std::end(mTaskItems))
{
QMetaObject::invokeMethod(this, "addNewSubTaskInfo", Qt::QueuedConnection, Q_ARG(uint, taskId), Q_ARG(uint, parentTask), Q_ARG(QString, description));
return;
}
mTotalProgressWidget->setMaximum(mTotalProgressWidget->maximum() + 1);
#ifdef Q_OS_WIN
mTaskbarProgress->setMaximum(mTaskbarProgress->maximum() + 1);
#endif
fillTaskItem(taskId, new QTreeWidgetItem{item->second}, description);
mTaskWidget->resizeColumnToContents(0);
++mSubTaskInfo[parentTask].mCount;
emit taskCountChanged(mTaskItems.size());
}
void ActiveTasksDialog::setTaskInfo(uint taskId, const QString &description)
{
auto item = mTaskItems.find(taskId);
if (item == std::end(mTaskItems))
{
QMetaObject::invokeMethod(this, "setTaskInfo", Qt::QueuedConnection, Q_ARG(uint, taskId), Q_ARG(QString, description));
return;
}
item->second->setText(1, description);
}
void ActiveTasksDialog::endTask(uint taskId, const QString &error)
{
auto item = mTaskItems.find(taskId);
if (item == std::end(mTaskItems))
{
QMetaObject::invokeMethod(this, "endTask", Qt::QueuedConnection, Q_ARG(uint, taskId), Q_ARG(QString, error));
return;
}
const auto subTaskInfo = mSubTaskInfo.find(taskId);
const auto success = error.isEmpty() && (subTaskInfo == std::end(mSubTaskInfo) || !subTaskInfo->second.mError);
mHadError = mHadError || !success;
const auto parent = item->second->parent();
item->second->setIcon(0, QIcon{(success) ? (":/images/accept.png") : (":/images/exclamation.png")});
if (!success && !error.isEmpty())
{
const auto individualErrors = error.split('\n', QString::SkipEmptyParts);
for (const auto &individualError : individualErrors)
{
auto errorItem = new QTreeWidgetItem{item->second};
errorItem->setText(1, individualError);
}
}
mTaskItems.erase(item);
mTotalProgressWidget->setValue(mTotalProgressWidget->value() + 1);
#ifdef Q_OS_WIN
mTaskbarProgress->setValue(mTaskbarProgress->value() + 1);
#endif
emit taskCountChanged(mTaskItems.size());
if (parent != nullptr)
{
const auto it = mSubTaskInfo.find(parent->data(0, Qt::UserRole).toUInt());
Q_ASSERT(it != std::end(mSubTaskInfo));
it->second.mError = it->second.mError || !success;
--it->second.mCount;
if (it->second.mCount == 0)
{
endTask(it->first);
mSubTaskInfo.erase(it);
}
}
else if (mTaskItems.empty())
{
#ifdef Q_OS_WIN
mTaskbarProgress->setVisible(false);
#endif
if (mAutoCloseBtn->isChecked() && !mHadError)
QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);
}
}
void ActiveTasksDialog::closeEvent(QCloseEvent *event)
{
if (mTaskItems.empty())
{
mTaskWidget->clear();
QDialog::closeEvent(event);
}
else
{
event->ignore();
}
}
void ActiveTasksDialog::autoCloseSave(bool enabled)
{
QSettings settings;
settings.setValue(UISettings::autoCloseTasksKey, enabled);
}
void ActiveTasksDialog::fillTaskItem(uint taskId, QTreeWidgetItem *item, const QString &description)
{
item->setIcon(0, QIcon{":/images/information.png"});
item->setData(0, Qt::UserRole, taskId);
item->setText(1, description);
item->setExpanded(true);
mTaskItems[taskId] = item;
}
}