Debian Codegraph @

src/views/starview.cc

raw
1 /*
2 This file is part of Icecream.
3
4 Copyright (c) 2003 Frerich Raabe <[email protected]>
5 Copyright (c) 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 "starview.h"
23
24 #include "hostinfo.h"
25 #include "utils.h"
26 #include <monitor.h>
27
28 #include <qdebug.h>
29
30 #include <qlayout.h>
31 #include <qtooltip.h>
32 #include <qslider.h>
33 #include <qlabel.h>
34 #include <qpushbutton.h>
35 #include <qlineedit.h>
36 #include <qcheckbox.h>
37 #include <qdir.h>
38 #include <QSettings>
39 #include <QResizeEvent>
40 #include <QGraphicsScene>
41 #include <QGraphicsView>
42 #include <QDialogButtonBox>
43 #include <QRegularExpression>
44
45 #include <math.h>
46
47 // TODO: This shouldn't be global
48 static bool suppressDomain = false;
49
50 StarViewConfigDialog::StarViewConfigDialog(QWidget *parent)
51 : QDialog(parent)
52 {
53 setWindowTitle(tr("Star View Settings"));
54
55 QBoxLayout *topLayout = new QVBoxLayout(this);
56
57 auto *label = new QLabel(tr("Number of nodes per ring:"));
58 topLayout->addWidget(label);
59
60 QBoxLayout *nodesLayout = new QHBoxLayout();
61 topLayout->addLayout(nodesLayout);
62
63 mNodesPerRingSlider = new QSlider(Qt::Horizontal);
64 mNodesPerRingSlider->setMinimum(1);
65 mNodesPerRingSlider->setMaximum(50);
66 mNodesPerRingSlider->setSingleStep(1);
67 nodesLayout->addWidget(mNodesPerRingSlider);
68 connect(mNodesPerRingSlider, SIGNAL(valueChanged(int)),
69 SIGNAL(configChanged()));
70 connect(mNodesPerRingSlider, SIGNAL(valueChanged(int)),
71 SLOT(slotNodesPerRingChanged(int)));
72
73 mNodesPerRingLabel = new QLabel;
74 nodesLayout->addWidget(mNodesPerRingLabel);
75
76 label = new QLabel(tr("Architecture filter:"));
77 topLayout->addWidget(label);
78 mArchFilterEdit = new QLineEdit;
79 topLayout->addWidget(mArchFilterEdit);
80 connect(mArchFilterEdit, SIGNAL(textChanged(const QString &)),
81 SIGNAL(configChanged()));
82
83 mSuppressDomainName = new QCheckBox(tr("Suppress domain name"));
84 topLayout->addWidget(mSuppressDomainName);
85 auto hline = new QFrame;
86 hline->setFrameShape(QFrame::HLine);
87 hline->setFrameShadow(QFrame::Sunken);
88 topLayout->addWidget(hline);
89
90 auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
91 topLayout->addWidget(buttonBox);
92
93 connect(mSuppressDomainName, SIGNAL(toggled(bool)),
94 SLOT(slotSuppressDomainName(bool)));
95
96 connect(buttonBox, SIGNAL(rejected()), SLOT(accept()));
97 }
98
99 void StarViewConfigDialog::slotNodesPerRingChanged(int nodes)
100 {
101 mNodesPerRingLabel->setText(QString::number(nodes));
102 }
103
104 void StarViewConfigDialog::setMaxNodes(int maxNodes)
105 {
106 mNodesPerRingSlider->setMaximum(maxNodes + 1);
107 }
108
109 int StarViewConfigDialog::nodesPerRing()
110 {
111 return mNodesPerRingSlider->value();
112 }
113
114 void StarViewConfigDialog::setNodesPerRing(int nodes)
115 {
116 mNodesPerRingSlider->setValue(nodes);
117 slotNodesPerRingChanged(nodes);
118 configChanged();
119 }
120
121 bool StarViewConfigDialog::suppressDomainName() const
122 {
123 return ::suppressDomain;
124 }
125
126 void StarViewConfigDialog::setSuppressDomainName(bool suppress)
127 {
128 mSuppressDomainName->setChecked(suppress);
129 configChanged();
130 }
131
132 QString StarViewConfigDialog::archFilter()
133 {
134 return mArchFilterEdit->text();
135 }
136
137 void StarViewConfigDialog::slotSuppressDomainName(bool b)
138 {
139 suppressDomain = b;
140 configChanged();
141 }
142
143 HostItem::HostItem(const QString &text)
144 : QGraphicsItemGroup(nullptr)
145 , mHostInfo(nullptr)
146 , mHostInfoManager(nullptr)
147 , m_stateItem(nullptr)
148 {
149 init();
150
151 m_textItem->setPlainText(text);
152
153 updateName();
154 }
155
156 HostItem::HostItem(HostInfo *hostInfo, HostInfoManager *m)
157 : QGraphicsItemGroup(nullptr)
158 , mHostInfo(hostInfo)
159 , mHostInfoManager(m)
160 , m_stateItem(nullptr)
161 {
162 init();
163 }
164
165 HostItem::~HostItem()
166 {
167 }
168
169 void HostItem::init()
170 {
171 mBaseWidth = 0;
172 mBaseHeight = 0;
173
174 m_boxItem = new QGraphicsEllipseItem(this);
175 m_boxItem->setZValue(80);
176 m_boxItem->setPen(QPen(Qt::NoPen));
177
178 m_textItem = new QGraphicsTextItem(this);
179 m_textItem->setZValue(100);
180
181 setHostColor(QColor(200, 200, 200));
182
183 mIsActiveClient = false;
184 mIsCompiling = false;
185
186 m_client = 0;
187 }
188
189 void HostItem::setHostColor(const QColor &color)
190 {
191 m_boxItem->setBrush(color);
192 m_boxItem->setPen(color.darker(PenDarkerFactor));
193 m_textItem->setDefaultTextColor(Utils::textColor(color));
194 }
195
196 QString HostItem::hostName() const
197 {
198 QString name = mHostInfo->name();
199 if (suppressDomain) {
200 return name.section(QLatin1Char('.'), 0, 0);
201 }
202 return mHostInfo->name();
203 }
204
205 void HostItem::updateName()
206 {
207 // Autosize the textItem's width to determine the desired size...
208 m_textItem->setTextWidth(-1);
209
210 if (mHostInfo) {
211 QPen pen = m_boxItem->pen();
212 QString text;
213
214 if (mHostInfo->noRemote() || mHostInfo->maxJobs() == 0) {
215 text = QStringLiteral("<i>%1</i>").arg(hostName());
216 pen.setStyle(Qt::DotLine);
217 } else
218 {
219 text = QStringLiteral("%1<br/>[%2/%3]").arg(hostName()).arg(m_jobs.size()).arg(mHostInfo->maxJobs());
220 pen.setStyle(Qt::SolidLine);
221 }
222
223 m_boxItem->setPen(pen);
224 m_textItem->setHtml(text);
225 } else
226 {
227 m_textItem->setHtml(m_fixedText);
228 }
229
230 QTextBlockFormat format;
231 format.setAlignment(Qt::AlignCenter);
232 QTextCursor cursor = m_textItem->textCursor();
233 cursor.select(QTextCursor::Document);
234 cursor.mergeBlockFormat(format);
235 cursor.clearSelection();
236
237 QRectF rect = m_textItem->boundingRect();
238
239 // Set the textItem to fixed width based on previous autosize to apply the alignment.
240 m_textItem->setTextWidth(rect.width());
241
242 mBaseWidth = rect.width() * M_SQRT2;
243 mBaseHeight = rect.height() * M_SQRT2;
244
245 m_boxItem->setRect(-baseXMargin(), -baseYMargin(), mBaseWidth, mBaseHeight);
246
247 updateHalos();
248 }
249
250 void HostItem::setFixedText(const QString &text)
251 {
252 if (m_fixedText == text) {
253 return;
254 }
255
256 m_fixedText = text;
257 updateName();
258 }
259
260 void HostItem::setCenterPos(double x, double y)
261 {
262 // move all items (also the sub items)
263 setPos(x - m_textItem->boundingRect().width() / 2, y - m_textItem->boundingRect().height() / 2);
264 // setPos( x, y );
265 }
266
267 void HostItem::update(const Job &job)
268 {
269 setIsCompiling(job.state == Job::Compiling);
270 setClient(job.client);
271
272 if (job.state == Job::WaitingForCS) {
273 return;
274 }
275
276 bool finished = job.state == Job::Finished ||
277 job.state == Job::Failed;
278
279 JobList::Iterator it = m_jobs.find(job.id);
280 bool newJob = (it == m_jobs.end());
281
282 if (newJob && finished) {
283 return;
284 }
285 if (!newJob && !finished) {
286 return;
287 }
288
289 if (newJob) {
290 m_jobs.insert(job.id, job);
291 createJobHalo(job);
292 updateName();
293 } else if (finished) {
294 deleteJobHalo(job);
295 m_jobs.erase(it);
296 updateName();
297 }
298 }
299
300 void HostItem::createJobHalo(const Job &job)
301 {
302 auto halo = new QGraphicsEllipseItem(
303 centerPosX(), centerPosY(), mBaseWidth, mBaseHeight,
304 this);
305
306 halo->setPen(QPen(Qt::NoPen));
307 halo->show();
308
309 m_jobHalos.insert(job, halo);
310
311 updateHalos();
312 }
313
314 void HostItem::deleteJobHalo(const Job &job)
315 {
316 QMap<Job, QGraphicsEllipseItem *>::Iterator it = m_jobHalos.find(job);
317 if (it == m_jobHalos.end()) {
318 return;
319 }
320
321 QGraphicsEllipseItem *halo = *it;
322 delete halo;
323 m_jobHalos.erase(it);
324
325 updateHalos();
326 }
327
328 void HostItem::updateHalos()
329 {
330 Q_ASSERT(m_jobHalos.isEmpty() || mHostInfoManager);
331
332 int count = 1;
333
334 QMap<Job, QGraphicsEllipseItem *>::Iterator it;
335 for (it = m_jobHalos.begin(); it != m_jobHalos.end(); ++it) {
336 QGraphicsEllipseItem *halo = it.value();
337 halo->setZValue(70 - count);
338 halo->setRect(halo->x() - baseXMargin() - count * HaloMargin, halo->y() - baseYMargin() - count * HaloMargin, mBaseWidth + count * HaloMargin * 2, mBaseHeight + count * HaloMargin * 2);
339 halo->setBrush(mHostInfoManager->hostColor(it.key().client));
340 halo->setPen(mHostInfoManager->hostColor(it.key().client).darker(PenDarkerFactor));
341 ++count;
342 }
343 }
344
345 StarView::StarView(QObject *parent)
346 : StatusView(parent)
347 , m_canvas(new QGraphicsScene)
348 , m_widget(new StarViewGraphicsView(m_canvas, this))
349 {
350 mConfigDialog = new StarViewConfigDialog(m_widget.data());
351 connect(mConfigDialog, SIGNAL(configChanged()),
352 SLOT(slotConfigChanged()));
353
354 readSettings();
355
356 m_widget->setScene(m_canvas);
357 m_widget->arrangeItems();
358 }
359
360 StarView::~StarView()
361 {
362 writeSettings();
363 }
364
365 void StarView::readSettings()
366 {
367 QSettings settings;
368 settings.beginGroup(QStringLiteral("view_%1").arg(id()));
369 mConfigDialog->setNodesPerRing(settings.value(QStringLiteral("nodesPerRing"), 25).toInt());
370 mConfigDialog->setSuppressDomainName(settings.value(QStringLiteral("suppressDomainName"), true).toBool());
371 settings.endGroup();
372 }
373
374 void StarView::writeSettings()
375 {
376 QSettings settings;
377 settings.beginGroup(QStringLiteral("view_%1").arg(id()));
378 settings.setValue(QStringLiteral("nodesPerRing"), mConfigDialog->nodesPerRing());
379 settings.setValue(QStringLiteral("suppressDomainName"), mConfigDialog->suppressDomainName());
380 settings.endGroup();
381 settings.sync();
382 }
383
384 void StarView::update(const Job &job)
385 {
386 if (job.state == Job::WaitingForCS) {
387 m_widget->drawNodeStatus();
388 return;
389 }
390
391 unsigned int hostid = processor(job);
392 if (!hostid) {
393 return;
394 }
395
396 HostItem *hostItem = findHostItem(hostid);
397 if (!hostItem) {
398 return;
399 }
400
401 hostItem->update(job);
402
403 bool finished = job.state == Job::Finished || job.state == Job::Failed;
404
405 QMap<unsigned int, HostItem *>::Iterator it;
406 it = mJobMap.find(job.id);
407 if (it != mJobMap.end()) {
408 (*it)->update(job);
409 if (finished) {
410 mJobMap.erase(it);
411 unsigned int clientid = job.client;
412 HostItem *clientItem = findHostItem(clientid);
413 if (clientItem) {
414 clientItem->setIsActiveClient(false);
415 }
416 }
417 m_widget->drawNodeStatus();
418 return;
419 }
420
421 if (!finished) {
422 mJobMap.insert(job.id, hostItem);
423 }
424
425 if (job.state == Job::Compiling) {
426 unsigned int clientid = job.client;
427 HostItem *clientItem = findHostItem(clientid);
428 if (clientItem) {
429 clientItem->setClient(clientid);
430 clientItem->setIsActiveClient(true);
431 }
432 }
433
434 m_widget->drawNodeStatus();
435 }
436
437 QList<HostItem *> StarView::hostItems() const
438 {
439 return m_hostItems.values();
440 }
441
442 HostItem *StarView::findHostItem(unsigned int hostid) const
443 {
444 return m_hostItems.value(hostid);
445 }
446
447 void StarView::setMonitor(Monitor *monitor)
448 {
449 StatusView::setMonitor(monitor);
450
451 if (monitor) {
452 createKnownHosts();
453 m_widget->arrangeItems();
454 }
455 }
456
457 void StarView::checkNode(unsigned int hostid)
458 {
459 // qDebug() << "StarView::checkNode() " << hostid << endl;
460
461 if (!hostid) {
462 return;
463 }
464
465 if (!filterArch(hostid)) {
466 return;
467 }
468
469 HostItem *hostItem = findHostItem(hostid);
470 if (!hostItem) {
471 createHostItem(hostid);
472 m_widget->arrangeItems();
473 }
474 }
475
476 void StarView::removeNode(unsigned int hostid)
477 {
478 // qDebug() << "StarView::removeNode() " << hostid << endl;
479
480 HostItem *hostItem = findHostItem(hostid);
481
482 if (!hostItem)
483 return;
484
485 m_hostItems.remove(hostid);
486
487 QList<unsigned int> obsoleteJobs;
488
489 QMap<unsigned int, HostItem *>::Iterator it;
490 for (it = mJobMap.begin(); it != mJobMap.end(); ++it) {
491 if (it.value() == hostItem) {
492 obsoleteJobs.append(it.key());
493 }
494 }
495
496 QList<unsigned int>::ConstIterator it2;
497 for (it2 = obsoleteJobs.constBegin(); it2 != obsoleteJobs.constEnd(); ++it2) {
498 mJobMap.remove(*it2);
499 }
500
501 delete hostItem->stateItem();
502 delete hostItem;
503
504 m_widget->arrangeItems();
505 }
506
507 void StarView::updateSchedulerState(Monitor::SchedulerState state)
508 {
509 if (state == Monitor::Offline) {
510 QMap<unsigned int, HostItem *>::ConstIterator it;
511 for (it = m_hostItems.constBegin(); it != m_hostItems.constEnd(); ++it) {
512 delete *it;
513 }
514
515 m_hostItems.clear();
516 mJobMap.clear();
517 }
518
519 m_widget->arrangeItems();
520 }
521
522 QWidget *StarView::widget() const
523 {
524 return m_widget.data();
525 }
526
527 StarViewGraphicsView::StarViewGraphicsView(QGraphicsScene *scene, StarView *starView, QWidget *parent)
528 : QGraphicsView(scene, parent)
529 , m_starView(starView)
530 {
531 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
532 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
533 setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
534 scene->setSceneRect(0, 0, width(), height());
535
536 // add scheduler item
537 m_schedulerItem = new HostItem(QString());
538 scene->addItem(m_schedulerItem);
539 m_schedulerItem->setZValue(150);
540 m_schedulerItem->show();
541 arrangeSchedulerItem();
542 }
543
544 void StarViewGraphicsView::resizeEvent(QResizeEvent *)
545 {
546 scene()->setSceneRect(0, 0, width(), height());
547
548 arrangeSchedulerItem();
549 arrangeHostItems();
550 drawNodeStatus();
551 }
552
553 bool StarViewGraphicsView::event(QEvent *e)
554 {
555 if (e->type() != QEvent::ToolTip) {
556 return QGraphicsView::event(e);
557 }
558
559 QPoint p(static_cast<QHelpEvent *>(e)->pos());
560
561 HostItem *item = nullptr;
562 QGraphicsItem *graphicsItem = itemAt(p);
563 if (graphicsItem) {
564 item = dynamic_cast<HostItem *>(graphicsItem->parentItem());
565 }
566 if (item) {
567 HostInfo *hostInfo = item->hostInfo();
568 const QPoint gp(static_cast<QHelpEvent *>(e)->globalPos());
569 const QRect itemRect = mapFromScene(graphicsItem->sceneBoundingRect()).boundingRect();
570 if (hostInfo) {
571 QToolTip::showText(gp + QPoint(10, 10), hostInfo->toolTip(), this, itemRect);
572 } else {
573 QToolTip::showText(gp + QPoint(10, 10), QStringLiteral(
574 "<h3><b>%1</b></h3>"
575 "<table>"
576 "<tr><td>%2</td></tr>"
577 "<tr><td>%3</td></tr>"
578 "</table>"
579 "<p><img align=\"right\" src=\":/images/icemonnode.png\"/></p>")
580 .arg(tr("Scheduler"),
581 tr("Host: %1").arg(m_starView->hostInfoManager()->schedulerName()),
582 tr("Network name: %1").arg(m_starView->hostInfoManager()->networkName())
583 ),
584 this, itemRect);
585 }
586 } else {
587 QToolTip::hideText();
588 }
589 return QGraphicsView::event(e);
590 }
591
592 void StarViewGraphicsView::arrangeSchedulerItem()
593 {
594 const Monitor *monitor = m_starView->monitor();
595 const bool isOnline = (monitor ? monitor->schedulerState() == Monitor::Online : false);
596 m_schedulerItem->setFixedText(isOnline ? tr("Scheduler") : QStringLiteral("<b>No scheduler available</b>"));
597 m_schedulerItem->setCenterPos(width() / 2, height() / 2);
598 }
599
600 void StarView::slotConfigChanged()
601 {
602 if (!hostInfoManager()) {
603 return;
604 }
605
606 HostInfoManager::HostMap hostMap = hostInfoManager()->hostMap();
607 HostInfoManager::HostMap::ConstIterator it;
608 for (it = hostMap.constBegin(); it != hostMap.constEnd(); ++it) {
609 if (filterArch(*it)) {
610 checkNode(it.key());
611 } else {
612 removeNode(it.key());
613 }
614 }
615
616 m_widget->arrangeItems();
617 }
618
619 void StarViewGraphicsView::arrangeItems()
620 {
621 arrangeHostItems();
622 arrangeSchedulerItem();
623 }
624
625 void StarViewGraphicsView::arrangeHostItems()
626 {
627 // qDebug() << "StarView::arrangeHostItems()" << endl;
628
629 const QList<HostItem *> hostItems = m_starView->hostItems();
630 int count = hostItems.count();
631
632 // qDebug() << " Count: " << count << endl;
633
634 int nodesPerRing = m_starView->configDialog()->nodesPerRing();
635
636 int ringCount = int( count / nodesPerRing ) + 1;
637
638 // qDebug() << " Rings: " << ringCount << endl;
639 const double radiusFactor = 3.0;
640 const int xRadius = qRound(scene()->width() / radiusFactor);
641 const int yRadius = qRound(scene()->height() / radiusFactor);
642
643 const double step = 2 * M_PI / count;
644
645 double angle = 0.0;
646 int i = 0;
647 foreach(HostItem * item, hostItems) {
648 double factor = 1 - (1.0 / (ringCount + 1)) * (i % ringCount);
649
650 double xr = xRadius * factor;
651 double yr = yRadius * factor;
652
653 item->updateName();
654 item->setCenterPos(width() / 2 + cos(angle) * xr,
655 height() / 2 + sin(angle) * yr);
656
657 angle += step;
658 ++i;
659 }
660 }
661
662 HostItem *StarView::createHostItem(unsigned int hostid)
663 {
664 HostInfo *i = hostInfoManager()->find(hostid);
665
666 if (!i || i->isOffline() || i->name().isEmpty()) {
667 return nullptr;
668 }
669
670 // qDebug() << "New node for " << hostid << " (" << i->name() << ")" << endl;
671
672 //assert( !i->name().isEmpty() );
673
674 auto hostItem = new HostItem(i, hostInfoManager());
675 m_canvas->addItem(hostItem);
676 hostItem->setHostColor(hostColor(hostid));
677 m_hostItems.insert(hostid, hostItem);
678 hostItem->show();
679
680 if (m_hostItems.count() > 25) {
681 mConfigDialog->setMaxNodes(m_hostItems.count());
682 }
683
684 return hostItem;
685 }
686
687 void StarViewGraphicsView::drawNodeStatus()
688 {
689 const QList<HostItem *> hostItems = m_starView->hostItems();
690 foreach(HostItem * item, hostItems) {
691 drawState(item);
692 }
693 }
694
695 void StarViewGraphicsView::drawState(HostItem *node)
696 {
697 delete node->stateItem();
698 QGraphicsLineItem *newItem = nullptr;
699
700 unsigned int client = node->client();
701 QColor color = client ? m_starView->hostColor(client) : Qt::green;
702
703 if (node->isCompiling() || node->isActiveClient()) {
704 newItem = new QGraphicsLineItem(qRound(node->centerPosX()),
705 qRound(node->centerPosY()),
706 qRound(m_schedulerItem->centerPosX()),
707 qRound(m_schedulerItem->centerPosY()));
708 if (node->isCompiling()) {
709 newItem->setPen(QPen(color, 0));
710 newItem->setZValue(-301);
711 } else if (node->isActiveClient()) {
712 newItem->setPen(QPen(color, 1, Qt::DashLine));
713 newItem->setZValue(-300);
714 }
715 scene()->addItem(newItem);
716 }
717
718 node->setStateItem(newItem);
719 }
720
721 void StarView::createKnownHosts()
722 {
723 HostInfoManager::HostMap hosts = hostInfoManager()->hostMap();
724
725 HostInfoManager::HostMap::ConstIterator it;
726 for (it = hosts.constBegin(); it != hosts.constEnd(); ++it) {
727 unsigned int id = (*it)->id();
728 if (!findHostItem(id)) {
729 createHostItem(id);
730 }
731 }
732 }
733
734 void StarView::configureView()
735 {
736 mConfigDialog->show();
737 mConfigDialog->raise();
738 }
739
740 bool StarView::filterArch(unsigned int hostid)
741 {
742 HostInfo *i = hostInfoManager()->find(hostid);
743 if (!i) {
744 return false;
745 }
746
747 return filterArch(i);
748 }
749
750 bool StarView::filterArch(HostInfo *i)
751 {
752 if (mConfigDialog->archFilter().isEmpty()) {
753 return true;
754 }
755
756 static QRegularExpression regExp(mConfigDialog->archFilter());
757
758 if (regExp.match(i->platform()).hasMatch()) {
759 return true;
760 }
761
762 return false;
763 }
764
765 StarViewConfigDialog *StarView::configDialog() const
766 {
767 return mConfigDialog;
768 }