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