Debian Codegraph @

src/views/flowtableview.cc

raw
1 /*
2 This file is part of Icecream.
3
4 Copyright (c) 2011 Daniel Molkentin <[email protected]>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "flowtableview.h"
22
23 #include <QHeaderView>
24 #include <QIcon>
25 #include <QDebug>
26 #include <QPainter>
27 #include <QTimer>
28
29 ProgressWidget::ProgressWidget(HostInfo *info, StatusView *statusView, QWidget *parent)
30 : QWidget(parent)
31 , m_hostInfo(info)
32 , m_statusView(statusView)
33 {
34 setAutoFillBackground(false);
35 setAttribute(Qt::WA_OpaquePaintEvent);
36 setAttribute(Qt::WA_NoSystemBackground);
37 m_backingStore = QImage(size(), QImage::Format_RGB32);
38 m_backingStore.fill(palette().base().color().rgb());
39 }
40
41 void ProgressWidget::paintEvent(QPaintEvent *)
42 {
43 QImage temp(size(), QImage::Format_RGB32);
44 QPainter p(&temp);
45
46 p.drawImage(-1, 0, m_backingStore);
47
48 if (m_isVirgin) {
49 p.fillRect(rect(), palette().base());
50 m_isVirgin = false;
51 }
52
53 if (m_currentJob.state == Job::Compiling ||
54 m_currentJob.state == Job::LocalOnly) {
55 QLinearGradient gradient;
56 gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
57 gradient.setColorAt(0, palette().base().color());
58 gradient.setColorAt(1, m_statusView->hostColor(m_currentJob.client));
59 p.fillRect(width() - 1, 0, 1, height(), gradient);
60 } else {
61 p.fillRect(width() - 1, 0, 1, height(), palette().base().color());
62 }
63
64 QPainter screenp(this);
65 screenp.drawImage(0, 0, temp);
66 m_backingStore = temp;
67 }
68
69 void ProgressWidget::resizeEvent(QResizeEvent *) {
70 m_isVirgin = true;
71 }
72
73 ////////////////////////////////////////////////////////////////////////////////
74
75 FlowTableView::FlowTableView(QObject *parent)
76 : StatusView(parent)
77 , m_widget(new QTableWidget)
78 , m_updateTimer(new QTimer)
79 {
80 m_widget->setColumnCount(4);
81 QStringList labels;
82 labels << tr("Host") << tr("File") << tr("History") << tr("State");
83 m_widget->setHorizontalHeaderLabels(labels);
84 m_widget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
85 m_widget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
86 m_widget->verticalHeader()->hide();
87 m_widget->setSelectionMode(QAbstractItemView::NoSelection);
88
89 m_updateTimer->setInterval(50);
90 m_updateTimer->start();
91
92 createKnownHosts();
93 }
94
95 void FlowTableView::update(const Job &job)
96 {
97 int serverId = job.server;
98 if (serverId == 0) {
99 return;
100 }
101
102 // checkNode hasn't been run for this server yet.
103 if (!m_idToRowMap.contains(serverId)) {
104 return;
105 }
106
107 int serverRow = m_idToRowMap.value(serverId);
108 QTableWidgetItem *fileNameItem = m_widget->item(serverRow, 1);
109 QTableWidgetItem *jobStateItem = m_widget->item(serverRow, 3);
110
111 if (job.state == Job::Finished) {
112 fileNameItem->setText(QLatin1String(""));
113 jobStateItem->setText(QLatin1String(""));
114 } else {
115 QString filePath = job.fileName;
116 QString fileName = filePath.mid(filePath.lastIndexOf(QLatin1Char('/')) + 1);
117 fileNameItem->setText(fileName);
118 fileNameItem->setToolTip(job.fileName);
119 fileNameItem->setFlags(Qt::ItemIsEnabled);
120 jobStateItem->setText(job.stateAsString());
121 jobStateItem->setToolTip(job.stateAsString());
122 jobStateItem->setFlags(Qt::ItemIsEnabled);
123 }
124
125 if (auto * progressWidget = static_cast<ProgressWidget *>(m_widget->cellWidget(serverRow, 2))) {
126 progressWidget->setCurrentJob(job);
127 }
128
129 // update the host column for the server requesting the job
130 QTableWidgetItem *hostNameItem = m_widget->item(serverRow, 0);
131 HostInfo *hostInfo = hostInfoManager()->find(serverId);
132
133 QFont f = m_widget->font();
134 hostNameItem->setFont(f);
135 hostNameItem->setText(hostInfoText(hostInfo));
136 }
137
138 QWidget *FlowTableView::widget() const
139 {
140 return m_widget.data();
141 }
142
143 QString FlowTableView::hostInfoText(HostInfo *hostInfo) {
144 if ((hostInfo->serverSpeed() == 0) && (hostInfo->numJobs() == 0)) { // host disabled
145 return tr("%1 (not accepting jobs)").arg(hostInfo->name());
146 } else {
147 return tr("%1 (%2/%3)").arg(hostInfo->name()).arg(hostInfo->numJobs()).arg(hostInfo->maxJobs());
148 }
149 }
150
151 void FlowTableView::createKnownHosts()
152 {
153 if (!hostInfoManager()) {
154 return;
155 }
156
157 m_widget->setRowCount(0);
158 m_idToRowMap.clear();
159
160 const HostInfoManager::HostMap hosts(hostInfoManager()->hostMap());
161
162 foreach(int hostid, hosts.keys()) {
163 checkNode(hostid);
164 }
165 }
166
167 void FlowTableView::setMonitor(Monitor *monitor)
168 {
169 StatusView::setMonitor(monitor);
170
171 if (monitor)
172 createKnownHosts();
173 }
174
175 void FlowTableView::checkNode(unsigned int hostId)
176 {
177 if (m_idToRowMap.contains(hostId)) {
178 return;
179 }
180
181 HostInfo *hostInfo = hostInfoManager()->hostMap().value(hostId);
182 auto *widgetItem = new QTableWidgetItem(hostInfoText(hostInfo));
183 widgetItem->setIcon(QIcon(QStringLiteral(":/images/icemonnode.png")));
184 widgetItem->setToolTip(hostInfo->toolTip());
185 widgetItem->setBackground(hostInfo->color());
186 widgetItem->setFlags(Qt::ItemIsEnabled);
187 int insertRow = m_widget->rowCount();
188 m_widget->setRowCount(insertRow + 1);
189 m_idToRowMap.insert(hostId, insertRow);
190 m_widget->setItem(insertRow, 0, widgetItem);
191 // adjust column width
192 int width = QFontMetrics(widgetItem->font()).horizontalAdvance(widgetItem->text()) + 32;
193 m_widget->horizontalHeader()->resizeSection(0, qMax(m_widget->horizontalHeader()->sectionSize(0), width));
194
195 widgetItem = new QTableWidgetItem;
196 widgetItem->setFlags(Qt::ItemIsEnabled);
197 m_widget->setItem(insertRow, 1, widgetItem);
198
199 widgetItem = new QTableWidgetItem;
200 widgetItem->setFlags(Qt::ItemIsEnabled);
201 m_widget->setItem(insertRow, 3, widgetItem);
202
203 auto pw = new ProgressWidget(hostInfo, this);
204 connect(m_updateTimer, SIGNAL(timeout()), pw, SLOT(update()));
205 m_widget->setCellWidget(insertRow, 2, pw);
206 }
207
208 void FlowTableView::removeNode(unsigned int hostId)
209 {
210 Q_UNUSED(hostId);
211
212 createKnownHosts();
213 }