Debian Codegraph @

src/views/ganttstatusview.cc

raw
1 /*
2 This file is part of Icecream.
3
4 Copyright (c) 2003 Frerich Raabe <[email protected]>
5 Copyright (c) 2003,2004 Cornelius Schumacher <[email protected]>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "ganttstatusview.h"
23
24 #include "job.h"
25 #include "hostinfo.h"
26 #include "utils.h"
27
28 #include <QDebug>
29 #include <qlabel.h>
30 #include <qlayout.h>
31 #include <qpainter.h>
32 #include <qpixmap.h>
33 #include <qtimer.h>
34 #include <qcheckbox.h>
35 #include <qpushbutton.h>
36 #include <QBoxLayout>
37 #include <QList>
38 #include <QGridLayout>
39 #include <QFrame>
40 #include <QResizeEvent>
41 #include <QPaintEvent>
42 #include <QScrollBar>
43 #include <QDialogButtonBox>
44
45 GanttConfigDialog::GanttConfigDialog(QWidget *parent)
46 : QDialog(parent)
47 {
48 QBoxLayout *topLayout = new QVBoxLayout(this);
49 topLayout->setContentsMargins({10, 10, 10, 10});
50 topLayout->setSpacing(10);
51
52 mTimeScaleVisibleCheck = new QCheckBox(tr("Show time scale"), this);
53 topLayout->addWidget(mTimeScaleVisibleCheck);
54 connect(mTimeScaleVisibleCheck, SIGNAL(clicked()),
55 SIGNAL(configChanged()));
56
57 auto hline = new QFrame(this);
58 hline->setFrameShape(QFrame::HLine);
59 topLayout->addWidget(hline);
60
61 QBoxLayout *buttonLayout = new QHBoxLayout();
62 topLayout->addLayout(buttonLayout);
63
64 buttonLayout->addStretch(1);
65
66 auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
67 buttonLayout->addWidget(buttonBox);
68 connect(buttonBox, SIGNAL(rejected()), SLOT(hide()));
69
70 setWindowTitle(tr("Configure Gantt View"));
71 // apply some minimum size
72 resize(300, 100);
73 }
74
75 bool GanttConfigDialog::isTimeScaleVisible()
76 {
77 return mTimeScaleVisibleCheck->isChecked();
78 }
79
80 GanttTimeScaleWidget::GanttTimeScaleWidget(QWidget *parent)
81 : QWidget(parent)
82 {
83 QPalette pal = palette();
84 pal.setColor(backgroundRole(), Qt::white);
85 setPalette(pal);
86 }
87
88 void GanttTimeScaleWidget::setPixelsPerSecond(int v)
89 {
90 mPixelsPerSecond = v;
91 }
92
93 void GanttTimeScaleWidget::paintEvent(QPaintEvent *pe)
94 {
95 const QRect r = pe->rect();
96
97 QPainter p(this);
98 const QFontMetrics fm = p.fontMetrics();
99
100 // Maybe the rectangle overlaps the right half of a number, check whether
101 // that is the case and repaint that number if necessary.
102 if (r.x() % 100 != 0) {
103 const int lastNumberXPos = r.x() - (r.x() % 100);
104 QString lastNumber = QString::number(lastNumberXPos / 100 * 5);
105 if (r.x() % 100 < p.fontMetrics().horizontalAdvance(lastNumber)) {
106 p.drawText(lastNumberXPos - r.x() + 2, fm.ascent(), lastNumber);
107 }
108 }
109
110 // Now draw all the bars and numbers, very straightforward.
111 for (int x = 0; x < r.width(); ++x) {
112 const int absX = x + r.x();
113 if (absX % (mPixelsPerSecond * 10) == 0) {
114 p.drawLine(x, -r.y(), x, height() / 2 - r.y());
115 p.drawText(x + 2, fm.ascent() - r.y(),
116 QString::number(absX /
117 mPixelsPerSecond));
118 } else if (absX % (mPixelsPerSecond * 5) == 0) {
119 p.drawLine(x, -r.y(), x, height() / 4 - r.y());
120 p.drawText(x + 2, fm.ascent() - r.y(),
121 QString::number(absX /
122 mPixelsPerSecond));
123 } else if (absX % (mPixelsPerSecond) == 0) {
124 p.drawLine(x, -r.y(), x, height() / 8 - r.y());
125 }
126 }
127 }
128
129 GanttProgress::GanttProgress(StatusView *statusView, QWidget *parent)
130 : QWidget(parent)
131 , mStatusView(statusView)
132 {
133 QPalette pal = palette();
134 pal.setColor(backgroundRole(), Qt::white);
135 setPalette(pal);
136 }
137
138 void GanttProgress::progress()
139 {
140 mClock += 1;
141 adjustGraph();
142 QWidget::update();
143 }
144
145 void GanttProgress::adjustGraph()
146 {
147 // Remove non-visible jobs
148 if (m_jobs.count() >= 2 &&
149 mClock - m_jobs[m_jobs.count() - 2].clock > width()) {
150 m_jobs.removeAt(m_jobs.count() - 1);
151 }
152 }
153
154 void GanttProgress::update(const Job &job)
155 {
156 if (!m_jobs.isEmpty() && m_jobs.first().job == job) {
157 if (job.state == Job::Finished || job.state == Job::Failed) {
158 Job j = IdleJob();
159 m_jobs.prepend(JobData(j, mClock));
160 mIsFree = true;
161 }
162 } else {
163 m_jobs.prepend(JobData(job, mClock));
164 mIsFree = (job.state == Job::Idle);
165 }
166 }
167
168 void GanttProgress::drawGraph(QPainter &p)
169 {
170 if (height() == 0) {
171 return;
172 }
173
174 bool lastBox = false;
175 int xStart = 0;
176 QList<JobData>::ConstIterator it = m_jobs.constBegin();
177 for (; (it != m_jobs.constEnd()) && !lastBox; ++it) {
178 int xEnd = mClock - (*it).clock;
179
180 if (xEnd > width()) {
181 xEnd = width();
182 lastBox = true;
183 }
184
185 int xWidth = xEnd - xStart;
186 if (xWidth == 0) {
187 continue;
188 }
189
190 // Draw the rectangle for the current job
191 QColor color = colorForStatus((*it).job);
192 p.fillRect(xStart, 0, xWidth, height(), color);
193 p.setPen(color.darker());
194 p.drawRect(xStart, 0, xWidth, height());
195
196 if (xWidth > 4 && height() > 4) {
197 int width = xWidth - 4;
198 QString s = (*it).job.fileName;
199 if (!s.isEmpty()) {
200 s = s.mid(s.lastIndexOf(QLatin1Char('/')) + 1, s.length());
201
202 // Optimization - cache the drawn text in a pixmap, and update the cache
203 // only if the pixmap height doesn't match, if the pixmap width is too large,
204 // or if the shortened text with another character added (next_text_width) would fit
205 if (width >= (*it).next_text_width || width < (*it).text_cache.width()
206 || height() - 4 != (*it).text_cache.height()) {
207 // If we print the filename, check whether we need to truncate it and
208 // append "..." at the end.
209 int text_width = p.fontMetrics().horizontalAdvance(s);
210 if (text_width > width) {
211 int threeDotsWidth = p.fontMetrics().horizontalAdvance(QStringLiteral("..."));
212 int next_width = 0;
213 int newLength = 0;
214 for (;
215 next_width <= width;
216 ++newLength) {
217 text_width = next_width;
218 next_width = p.fontMetrics().horizontalAdvance(s.left(newLength)) +
219 threeDotsWidth;
220 }
221
222 (*it).next_text_width = next_width;
223 s = s.left(newLength > 2 ? newLength - 2 : 0) + QStringLiteral("...");
224 } else {
225 (*it).next_text_width = 1000000; // large number (no next width)
226 }
227 // Finally draw the text.
228 if (text_width > 0) {
229 (*it).text_cache = QPixmap(text_width, height() - 4);
230 (*it).text_cache.fill(color);
231 QPainter painter(&(*it).text_cache);
232 painter.setPen(Utils::textColor(color));
233 painter.drawText(0, 0, text_width, height() - 4,
234 Qt::AlignVCenter | Qt::AlignLeft, s);
235 }
236 }
237 if (!(*it).text_cache.isNull()) {
238 p.drawPixmap(xStart + 2, 2, (*it).text_cache);
239 }
240 }
241 }
242 xStart = xEnd;
243 }
244 }
245
246 QColor GanttProgress::colorForStatus(const Job &job) const
247 {
248 if (job.state == Job::Idle) {
249 return Qt::gray;
250 } else {
251 QColor c = mStatusView->hostColor(job.client);
252 if (job.state == Job::LocalOnly) {
253 return c.lighter();
254 } else {
255 return c;
256 }
257 }
258 }
259
260 void GanttProgress::paintEvent(QPaintEvent *)
261 {
262 QPainter p(this);
263 p.setBackgroundMode(Qt::OpaqueMode);
264 p.setBackground(palette().window());
265 drawGraph(p);
266 }
267
268 void GanttProgress::resizeEvent(QResizeEvent *)
269 {
270 adjustGraph();
271 }
272
273 GanttStatusView::GanttStatusView(QObject *parent)
274 : StatusView(parent)
275 , m_widget(new QScrollArea)
276 , mTopWidget(new QWidget)
277 {
278 mConfigDialog = new GanttConfigDialog(m_widget.data());
279 connect(mConfigDialog, SIGNAL(configChanged()),
280 SLOT(slotConfigChanged()));
281
282 m_widget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
283 m_widget->setWidget(mTopWidget);
284 m_widget->setWidgetResizable(true);
285
286 QPalette palette = mTopWidget->palette();
287 palette.setColor(mTopWidget->backgroundRole(), Qt::white);
288 mTopWidget->setPalette(palette);
289
290 m_topLayout = new QGridLayout(mTopWidget);
291 m_topLayout->setSpacing(5);
292 m_topLayout->setContentsMargins({4, 4, 4, 4});
293 m_topLayout->setColumnStretch(1, 10);
294
295 mTimeScale = new GanttTimeScaleWidget(mTopWidget);
296 mTimeScale->setFixedHeight(50);
297 m_topLayout->addWidget(mTimeScale, 0, 1);
298
299 m_progressTimer = new QTimer(this);
300 connect(m_progressTimer, SIGNAL(timeout()), SLOT(updateGraphs()));
301 m_ageTimer = new QTimer(this);
302 connect(m_ageTimer, SIGNAL(timeout()), SLOT(checkAge()));
303
304 mUpdateInterval = 25;
305 mTimeScale->setPixelsPerSecond(1000 / mUpdateInterval);
306
307 mMinimumProgressHeight = QFontMetrics(m_widget->font()).height() + 6;
308
309 slotConfigChanged();
310
311 start();
312 }
313
314 void GanttStatusView::update(const Job &job)
315 {
316 if (!mRunning) {
317 return;
318 }
319
320 if (job.state == Job::WaitingForCS) {
321 return;
322 }
323
324 QMap<unsigned int, GanttProgress *>::Iterator it;
325
326 it = mJobMap.find(job.id);
327
328 if (it != mJobMap.end()) {
329 it.value()->update(job);
330 if (job.state == Job::Finished || job.state == Job::Failed) {
331 mJobMap.erase(it);
332 }
333 return;
334 }
335
336 if (job.state == Job::Finished || job.state == Job::Failed) {
337 return;
338 }
339
340 GanttProgress *slot = nullptr;
341
342 unsigned int processor;
343 if (job.state == Job::LocalOnly) {
344 processor = job.client;
345 } else {
346 processor = job.server;
347 }
348
349 if (!processor) {
350 return;
351 }
352
353 NodeMap::ConstIterator it2 = mNodeMap.constFind(processor);
354 if (it2 == mNodeMap.constEnd()) {
355 slot = registerNode(processor);
356 } else {
357 SlotList slotList = it2.value();
358 SlotList::ConstIterator it3;
359 for (it3 = slotList.constBegin(); it3 != slotList.constEnd(); ++it3) {
360 if ((*it3)->isFree()) {
361 slot = *it3;
362 break;
363 }
364 }
365
366 if (it3 == slotList.constEnd()) {
367 slot = registerNode(processor);
368 }
369 }
370
371 Q_ASSERT(slot);
372 mJobMap.insert(job.id, slot);
373 slot->update(job);
374 mAgeMap[processor] = 0;
375 }
376
377 QWidget *GanttStatusView::widget() const
378 {
379 return m_widget.data();
380 }
381
382 void GanttStatusView::removeNode(unsigned int hostid)
383 {
384 unregisterNode(hostid);
385 }
386
387 void GanttStatusView::checkNode(unsigned int hostid)
388 {
389 if (!mRunning) {
390 return;
391 }
392
393 if (mNodeMap.find(hostid) == mNodeMap.end()) {
394 registerNode(hostid)->update(IdleJob());
395 }
396 unsigned int max_kids = hostInfoManager()->maxJobs(hostid);
397 for (unsigned int i = mNodeMap[hostid].count();
398 i < max_kids;
399 ++i) {
400 registerNode(hostid)->update(IdleJob());
401 }
402
403 mAgeMap[hostid] = 0;
404
405 SlotList slotList = mNodeMap[hostid]; // make a copy
406 int to_remove = slotList.count() - max_kids;
407 if (to_remove <= 0) {
408 return;
409 }
410
411 QListIterator<GanttProgress *> it2(slotList);
412 it2.toBack();
413 while (it2.hasPrevious()) {
414 GanttProgress *progress = it2.previous();
415 if (progress->isFree() && progress->fullyIdle()) {
416 removeSlot(hostid, progress);
417 if (--to_remove == 0) {
418 return;
419 }
420 }
421 }
422 }
423
424 GanttProgress *GanttStatusView::registerNode(unsigned int hostid)
425 {
426 // qDebug() << "GanttStatusView::registerNode(): " << ip << endl;
427
428 QColor color = hostColor(hostid);
429
430 QVBoxLayout *nodeLayout;
431
432 NodeLayoutMap::ConstIterator it = mNodeLayouts.constFind(hostid);
433 if (it == mNodeLayouts.constEnd()) {
434 static int lastRow = 0;
435 ++lastRow;
436
437 nodeLayout = new QVBoxLayout();
438 nodeLayout->setObjectName(QStringLiteral("%1_layout").arg(hostid));
439 m_topLayout->addLayout(nodeLayout, lastRow, 1);
440 mNodeLayouts.insert(hostid, nodeLayout);
441 mNodeRows.insert(hostid, lastRow);
442 } else {
443 nodeLayout = it.value();
444 }
445
446 NodeRowMap::ConstIterator rowIt = mNodeRows.constFind(hostid);
447 if (rowIt == mNodeRows.constEnd()) {
448 } else {
449 int row = *rowIt;
450 NodeLabelMap::ConstIterator labelIt = mNodeLabels.constFind(hostid);
451 if (labelIt == mNodeLabels.constEnd()) {
452 QString name = nameForHost(hostid);
453 auto l = new QLabel(name, mTopWidget);
454 QPalette palette = l->palette();
455 palette.setColor(l->foregroundRole(), color);
456 palette.setColor(l->backgroundRole(), Qt::white);
457 l->setPalette(palette);
458 m_topLayout->addWidget(l, row, 0);
459
460 QFont f = l->font();
461 f.setBold(true);
462 l->setFont(f);
463
464 l->show();
465 mNodeLabels.insert(hostid, l);
466 }
467 }
468
469 auto w = new GanttProgress(this, mTopWidget);
470 w->setMinimumHeight(mMinimumProgressHeight);
471 nodeLayout->addWidget(w);
472
473 mNodeMap[hostid].append(w);
474 mAgeMap[hostid] = 0;
475
476 m_topLayout->setRowStretch(mNodeRows[hostid], mNodeMap[hostid].size());
477
478 w->show();
479
480 return w;
481 }
482
483 void GanttStatusView::removeSlot(unsigned int hostid, GanttProgress *slot)
484 {
485 NodeLayoutMap::ConstIterator it = mNodeLayouts.constFind(hostid);
486 if (it == mNodeLayouts.constEnd()) {
487 return;
488 }
489
490 mNodeMap[hostid].removeAll(slot);
491 JobMap newJobMap;
492 for (QMap<unsigned int, GanttProgress *>::Iterator it = mJobMap.begin();
493 it != mJobMap.end(); // QMap::remove doesn't return an iterator like
494 ++it) { // e.g. in QValueList, and I'm not sure if 'it'
495 if ((*it) != slot) { // or '++it' would be still valid, so let's copy
496 newJobMap[it.key()] = *it; // still valid items to a new map
497 }
498 }
499
500 mJobMap = newJobMap;
501
502 m_topLayout->setRowStretch(mNodeRows[hostid], mNodeMap[hostid].size());
503 delete slot;
504 }
505
506 void GanttStatusView::unregisterNode(unsigned int hostid)
507 {
508 NodeLayoutMap::ConstIterator it = mNodeLayouts.constFind(hostid);
509 if (it == mNodeLayouts.constEnd()) {
510 return;
511 }
512 while (!mNodeMap[hostid].isEmpty())
513 removeSlot(hostid, mNodeMap[hostid].first());
514 NodeLabelMap::Iterator labelIt = mNodeLabels.find(hostid);
515 if (labelIt != mNodeLabels.end()) {
516 delete *labelIt;
517 mNodeLabels.erase(labelIt);
518 }
519 mAgeMap[hostid] = -1;
520 }
521
522 void GanttStatusView::updateGraphs()
523 {
524 NodeMap::ConstIterator it;
525 for (it = mNodeMap.constBegin(); it != mNodeMap.constEnd(); ++it) {
526 SlotList::ConstIterator it2;
527 for (it2 = (*it).constBegin(); it2 != (*it).constEnd(); ++it2) {
528 (*it2)->progress();
529 }
530 }
531 }
532
533 void GanttStatusView::stop()
534 {
535 mRunning = false;
536 m_progressTimer->stop();
537 m_ageTimer->stop();
538 }
539
540 void GanttStatusView::start()
541 {
542 mRunning = true;
543 m_progressTimer->start(mUpdateInterval);
544 m_ageTimer->start(10000);
545 }
546
547 void GanttStatusView::checkAge()
548 {
549 QList<unsigned int> to_unregister;
550 for (AgeMap::Iterator it = mAgeMap.begin();
551 it != mAgeMap.end();
552 ++it) {
553 if (*it > 1) {
554 to_unregister.append(it.key());
555 } else if (*it < 0) {
556 ; // unregistered ones
557 } else {
558 ++(*it);
559 }
560 }
561
562 for (QList<unsigned int>::ConstIterator it = to_unregister.constBegin();
563 it != to_unregister.constEnd();
564 ++it) {
565 unregisterNode(*it);
566 }
567 }
568
569 void GanttStatusView::configureView()
570 {
571 mConfigDialog->show();
572 mConfigDialog->raise();
573 }
574
575 void GanttStatusView::slotConfigChanged()
576 {
577 if (mConfigDialog->isTimeScaleVisible()) {
578 mTimeScale->show();
579 } else {
580 mTimeScale->hide();
581 }
582 }