forked from icecc/icemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cc
388 lines (320 loc) · 12.3 KB
/
mainwindow.cc
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
This file is part of Icecream.
Copyright (c) 2003 Frerich Raabe <[email protected]>
Copyright (c) 2003,2004 Stephan Kulow <[email protected]>
Copyright (c) 2003,2004 Cornelius Schumacher <[email protected]>
Copyright (c) 2014 Kevin Funk <[email protected]>
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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "mainwindow.h"
#include "hostinfo.h"
#include "version.h"
#include "fakemonitor.h"
#include "icecreammonitor.h"
#include "statusview.h"
#include "statusviewfactory.h"
#include "utils.h"
#include <QDebug>
#include <QLabel>
#include <QMenuBar>
#include <QStatusBar>
#include <QMessageBox>
#include <QApplication>
#include <QSettings>
#include <QMenu>
#include <algorithm>
namespace {
struct PlatformStat
{
PlatformStat()
: jobs(0)
, maxJobs(0) {}
unsigned int jobs;
unsigned int maxJobs;
};
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_view(nullptr)
{
QIcon appIcon = QIcon();
appIcon.addFile(QStringLiteral(":/images/128-apps-icemon.png"), QSize(128, 128));
appIcon.addFile(QStringLiteral(":/images/48-apps-icemon.png"), QSize(48, 48));
appIcon.addFile(QStringLiteral(":/images/32-apps-icemon.png"), QSize(32, 32));
appIcon.addFile(QStringLiteral(":/images/22-apps-icemon.png"), QSize(22, 22));
appIcon.addFile(QStringLiteral(":/images/16-apps-icemon.png"), QSize(16, 16));
setWindowIcon(appIcon);
setWindowTitle(QApplication::translate("appName", Icemon::Version::appName));
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
m_schedStatusWidget = new QLabel;
statusBar()->addPermanentWidget(m_schedStatusWidget);
m_jobStatsWidget = new QLabel;
m_jobStatsWidget->setVisible(false);
statusBar()->addPermanentWidget(m_jobStatsWidget);
QAction *action = fileMenu->addAction(tr("&Quit"), this, SLOT(close()), tr("Ctrl+Q"));
action->setIcon(QIcon::fromTheme(QStringLiteral("application-exit")));
action->setMenuRole(QAction::QuitRole);
m_viewMode = new QActionGroup(this);
action = m_viewMode->addAction(tr("&List View"));
action->setCheckable(true);
action->setData(QStringLiteral("list"));
action = m_viewMode->addAction(tr("&Star View"));
action->setCheckable(true);
action->setData(QStringLiteral("star"));
action = m_viewMode->addAction(tr("&Gantt View"));
action->setCheckable(true);
action->setData(QStringLiteral("gantt"));
action = m_viewMode->addAction(tr("Summary &View"));
action->setCheckable(true);
action->setData(QStringLiteral("summary"));
action = m_viewMode->addAction(tr("&Flow View"));
action->setCheckable(true);
action->setData(QStringLiteral("flow"));
action = m_viewMode->addAction(tr("&Detailed Host View"));
action->setCheckable(true);
action->setData(QStringLiteral("detailedhost"));
connect(m_viewMode, SIGNAL(triggered(QAction *)), this, SLOT(handleViewModeActionTriggered(QAction *)));
viewMenu->addActions(m_viewMode->actions());
viewMenu->addSeparator();
action = viewMenu->addAction(tr("Pause"));
action->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-pause")));
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(pauseView()));
m_pauseViewAction = action;
viewMenu->addSeparator();
action = viewMenu->addAction(tr("Configure View..."));
action->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
connect(action, SIGNAL(triggered()), this, SLOT(configureView()));
m_configureViewAction = action;
action = helpMenu->addAction(tr("About Qt..."));
connect(action, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
action->setMenuRole(QAction::AboutQtRole);
action = helpMenu->addAction(tr("About..."));
action->setIcon(appIcon);
connect(action, SIGNAL(triggered()), this, SLOT(about()));
action->setMenuRole(QAction::AboutRole);
m_hostInfoManager = new HostInfoManager;
setMonitor(new IcecreamMonitor(m_hostInfoManager, this));
resize(600, 400);
readSettings();
}
MainWindow::~MainWindow()
{
delete m_hostInfoManager;
}
void MainWindow::closeEvent(QCloseEvent *e)
{
writeSettings();
QMainWindow::closeEvent(e);
}
void MainWindow::readSettings()
{
QSettings settings;
restoreGeometry(settings.value(QStringLiteral("geometry")).toByteArray());
restoreState(settings.value(QStringLiteral("windowState")).toByteArray());
QString viewId = settings.value(QStringLiteral("currentView")).toString();
auto view = StatusViewFactory::create(viewId, this);
setView(view);
}
void MainWindow::writeSettings()
{
QSettings settings;
settings.setValue(QStringLiteral("geometry"), saveGeometry());
settings.setValue(QStringLiteral("windowState"), saveState());
settings.setValue(QStringLiteral("currentView"), (m_view ? m_view->id() : QString()));
settings.sync();
}
Monitor *MainWindow::monitor() const
{
return m_monitor;
}
void MainWindow::setMonitor(Monitor *monitor)
{
if (m_monitor == monitor) {
return;
}
if (m_monitor) {
disconnect(m_monitor, SIGNAL(schedulerStateChanged(Monitor::SchedulerState)),
this, SLOT(updateSchedulerState(Monitor::SchedulerState)));
disconnect(m_monitor, SIGNAL(jobUpdated(const Job &)), this, SLOT(updateJob(Job)));
disconnect(m_monitor->hostInfoManager(), SIGNAL(hostMapChanged()), this, SLOT(updateJobStats()));
}
m_monitor = monitor;
if (m_monitor) {
connect(m_monitor, SIGNAL(schedulerStateChanged(Monitor::SchedulerState)),
this, SLOT(updateSchedulerState(Monitor::SchedulerState)));
connect(m_monitor, SIGNAL(jobUpdated(const Job &)), this, SLOT(updateJob(Job)));
connect(m_monitor->hostInfoManager(), SIGNAL(hostMapChanged()), this, SLOT(updateJobStats()));
}
if (m_view) {
m_view->setMonitor(m_monitor);
}
updateSchedulerState(m_monitor ? m_monitor->schedulerState() : Monitor::Offline);
}
StatusView *MainWindow::view() const
{
return m_view;
}
void MainWindow::setView(StatusView *view)
{
if (m_view == view) {
return;
}
delete m_view;
m_view = view;
if (m_view) {
m_configureViewAction->setEnabled(m_view->isConfigurable());
m_pauseViewAction->setEnabled(m_view->isPausable());
m_view->setMonitor(m_monitor);
setCentralWidget(m_view->widget());
}
// update action-group
const QString viewId = (m_view ? m_view->id() : QString());
foreach(QAction * action, m_viewMode->actions()) {
if (action->data().toString() == viewId) {
action->setChecked(true);
break;
}
}
}
void MainWindow::pauseView()
{
m_view->togglePause();
}
void MainWindow::configureView()
{
m_view->configureView();
}
void MainWindow::about()
{
QString about = tr("<p><strong>%1</strong><br/>"
"Version: %2</p>"
"<p><strong>%3</strong></p>"
"<p>Maintainer:<br/>"
"Kevin Funk <[email protected]></p>"
"<p>Contributors:<br/>"
"Daniel Molkentin <[email protected]>"
"<p>Based on Icemon for KDE written by:<br/>"
"Frerich Raabe <[email protected]><br/>"
"Stephan Kulow <[email protected]><br/>"
"Cornelius Schumacher <[email protected]></p>"
"Homepage: <a href=\"%4\">%4</a><br/><br/>"
"Licensed under the GPLv2.<br/>").arg(
QLatin1String(Icemon::Version::appName),
QLatin1String(Icemon::Version::version),
QLatin1String(Icemon::Version::description),
QLatin1String(Icemon::Version::homePage));
QMessageBox::about(this, tr("About %1")
.arg(QLatin1String(Icemon::Version::appShortName)), about);
}
void MainWindow::updateSchedulerState(Monitor::SchedulerState state)
{
if (state == Monitor::Online) {
QString statusText = m_hostInfoManager->schedulerName();
if (!m_hostInfoManager->networkName().isEmpty()) {
statusText.append(QStringLiteral(" @ ")).append(m_hostInfoManager->networkName());
}
m_schedStatusWidget->setText(statusText.isEmpty() ? tr("Scheduler is online.") : statusText);
} else
{
m_schedStatusWidget->setText(tr("Scheduler is offline."));
}
m_activeJobs.clear();
updateJobStats();
}
void MainWindow::updateJob(const Job &job)
{
if (job.isActive()) {
m_activeJobs[job.id] = job;
updateJobStats();
} else if (job.isDone()) {
m_activeJobs.remove(job.id);
updateJobStats();
}
}
void MainWindow::updateJobStats()
{
if (!m_monitor->schedulerState()) {
m_jobStatsWidget->clear();
m_jobStatsWidget->setVisible(false);
return;
}
QMap<QString, PlatformStat> perPlatformStats;
const HostInfoManager::HostMap hostMap = m_monitor->hostInfoManager()->hostMap();
for (auto i = hostMap.begin(); i != hostMap.end(); ++i) {
if (!i.value()->isOffline() && !i.value()->noRemote()) {
perPlatformStats[i.value()->platform()].maxJobs += i.value()->maxJobs();
}
}
for (JobList::const_iterator i = m_activeJobs.constBegin(); i != m_activeJobs.constEnd(); ++i) {
const HostInfo *server = hostMap[i.value().server != 0 ? i.value().server : i.value().client];
if (server && !server->isOffline() && !server->noRemote()) {
++perPlatformStats[server->platform()].jobs;
}
}
// Turn into something we can sort differently
QVector<QPair<QString, PlatformStat>> statistics;
for (auto it = perPlatformStats.constBegin(); it != perPlatformStats.constEnd(); ++it) {
statistics << qMakePair(it.key(), it.value());
}
// Sort, move the platform with the highest max jobs count to the front
std::sort(statistics.begin(), statistics.end(), [](const QPair<QString, PlatformStat>& a,
const QPair<QString, PlatformStat>& b) {
return a.second.maxJobs > b.second.maxJobs;
});
// Compose the text
QString text;
foreach (auto pair, statistics) {
const QString& platform = pair.first;
const PlatformStat& stat = pair.second;
if (stat.maxJobs == 0) {
continue;
}
if (!text.isEmpty()) {
text.append(QStringLiteral(" - "));
}
text.append(QStringLiteral("<strong>%2/%3</strong> on %1").arg(platform).arg(stat.jobs).arg(stat.maxJobs));
}
m_jobStatsWidget->setText(tr("| Active jobs: %1").arg(text));
m_jobStatsWidget->setVisible(true);
}
void MainWindow::setCurrentNet(const QByteArray &netname)
{
m_monitor->setCurrentNetname(netname);
}
void MainWindow::setCurrentSched(const QByteArray &schedname)
{
m_monitor->setCurrentSchedname(schedname);
}
void MainWindow::setCurrentPort(uint schedport)
{
m_monitor->setCurrentSchedport(schedport);
}
void MainWindow::handleViewModeActionTriggered(QAction *action)
{
const QString viewId = action->data().toString();
Q_ASSERT(!viewId.isEmpty());
setView(StatusViewFactory::create(viewId, this));
}
// It's nasty that we have to hard-code the implementations of Monitor
// But we can't just add a setMonitor() method because we require the host info manager
void MainWindow::setTestModeEnabled(bool testMode)
{
if (testMode) {
setMonitor(new FakeMonitor(m_hostInfoManager, this));
} else {
setMonitor(new IcecreamMonitor(m_hostInfoManager, this));
}
}