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 , m_isVirgin(true)
34 {
35 setAutoFillBackground(false);
36 setAttribute(Qt::WA_OpaquePaintEvent);
37 setAttribute(Qt::WA_NoSystemBackground);
38 m_backingStore = QImage(size(), QImage::Format_RGB32);
39 m_backingStore.fill(palette().base().color().rgb());
40 }
41
42 void ProgressWidget::paintEvent(QPaintEvent *)
43 {
44 QImage temp(size(), QImage::Format_RGB32);
45 QPainter p(&temp);
46
47 p.drawImage(-1, 0, m_backingStore);
48
49 if (m_isVirgin) {
50 p.fillRect(rect(), palette().base());
51 m_isVirgin = false;
52 }
53
54 if (m_currentJob.state == Job::Compiling ||
55 m_currentJob.state == Job::LocalOnly) {
56 QLinearGradient gradient;
57 gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
58 gradient.setColorAt(0, palette().base().color());
59 gradient.setColorAt(1, m_statusView->hostColor(m_currentJob.client));
60 p.fillRect(width() - 1, 0, 1, height(), gradient);
61 } else {
62 p.fillRect(width() - 1, 0, 1, height(), palette().base().color());
63 }
64
65 QPainter screenp(this);
66 screenp.drawImage(0, 0, temp);
67 m_backingStore = temp;
68 }
69
70 void ProgressWidget::resizeEvent(QResizeEvent *) {
71 m_isVirgin = true;
72 }
73
74 ////////////////////////////////////////////////////////////////////////////////
75
76 FlowTableView::FlowTableView(QObject *parent)
77 : StatusView(parent)
78 , m_widget(new QTableWidget)
79 , m_updateTimer(new QTimer)
80 {
81 m_widget->setColumnCount(4);
82 QStringList labels;
83 labels << tr("Host") << tr("File") << tr("History") << tr("State");
84 m_widget->setHorizontalHeaderLabels(labels);
85 m_widget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
86 m_widget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
87 m_widget->verticalHeader()->hide();
88 m_widget->setSelectionMode(QAbstractItemView::NoSelection);
89
90 m_updateTimer->setInterval(50);
91 m_updateTimer->start();
92
93 createKnownHosts();
94 }
95
96 void FlowTableView::update(const Job &job)
97 {
98 int serverId = job.server;
99 if (serverId == 0) {
100 return;
101 }
102
103 // checkNode hasn't been run for this server yet.
104 if (!m_idToRowMap.contains(serverId)) {
105 return;
106 }
107
108 int serverRow = m_idToRowMap.value(serverId);
109 QTableWidgetItem *fileNameItem = m_widget->item(serverRow, 1);
110 QTableWidgetItem *jobStateItem = m_widget->item(serverRow, 3);
111
112 if (job.state == Job::Finished) {
113 fileNameItem->setText(QLatin1String(""));
114 jobStateItem->setText(QLatin1String(""));
115 } else {
116 QString filePath = job.fileName;
117 QString fileName = filePath.mid(filePath.lastIndexOf(QLatin1Char('/')) + 1);
118 fileNameItem->setText(fileName);
119 fileNameItem->setToolTip(job.fileName);
120 fileNameItem->setFlags(Qt::ItemIsEnabled);
121 jobStateItem->setText(job.stateAsString());
122 jobStateItem->setToolTip(job.stateAsString());
123 jobStateItem->setFlags(Qt::ItemIsEnabled);
124 }
125
126 if (ProgressWidget * progressWidget = static_cast<ProgressWidget *>(m_widget->cellWidget(serverRow, 2))) {
127 progressWidget->setCurrentJob(job);
128 }
129
130 // update the host column for the server requesting the job
131 QTableWidgetItem *hostNameItem = m_widget->item(serverRow, 0);
132 HostInfo *hostInfo = hostInfoManager()->find(serverId);
133
134 QFont f = m_widget->font();
135 f.setBold(hostInfo->numJobs() > 0);
136 hostNameItem->setFont(f);
137 hostNameItem->setText(hostInfoText(hostInfo));
138 }
139
140 QWidget *FlowTableView::widget() const
141 {
142 return m_widget.data();
143 }
144
145 QString FlowTableView::hostInfoText(HostInfo *hostInfo) {
146 if ((hostInfo->serverSpeed() == 0) && (hostInfo->numJobs() == 0)) { // host disabled
147 return tr("%1 (Disabled)").arg(hostInfo->name());
148 } else {
149 return tr("%1 (%2/%3)").arg(hostInfo->name()).arg(hostInfo->numJobs()).arg(hostInfo->maxJobs());
150 }
151 }
152
153 void FlowTableView::createKnownHosts()
154 {
155 if (!hostInfoManager()) {
156 return;
157 }
158
159 m_widget->setRowCount(0);
160 m_idToRowMap.clear();
161
162 const HostInfoManager::HostMap hosts(hostInfoManager()->hostMap());
163
164 foreach(int hostid, hosts.keys()) {
165 checkNode(hostid);
166 }
167 }
168
169 void FlowTableView::setMonitor(Monitor *monitor)
170 {
171 StatusView::setMonitor(monitor);
172
173 if (monitor)
174 createKnownHosts();
175 }
176
177 void FlowTableView::checkNode(unsigned int hostId)
178 {
179 if (m_idToRowMap.contains(hostId)) {
180 return;
181 }
182
183 HostInfo *hostInfo = hostInfoManager()->hostMap().value(hostId);
184 QTableWidgetItem *widgetItem = new QTableWidgetItem(hostInfoText(hostInfo));
185 widgetItem->setIcon(QIcon(QStringLiteral(":/images/icemonnode.png")));
186 widgetItem->setToolTip(hostInfo->toolTip());
187 widgetItem->setBackgroundColor(hostInfo->color());
188 widgetItem->setFlags(Qt::ItemIsEnabled);
189 int insertRow = m_widget->rowCount();
190 m_widget->setRowCount(insertRow + 1);
191 m_idToRowMap.insert(hostId, insertRow);
192 m_widget->setItem(insertRow, 0, widgetItem);
193 // adjust column width
194 int width = QFontMetrics(widgetItem->font()).width(widgetItem->text()) + 32;
195 m_widget->horizontalHeader()->resizeSection(0, qMax(m_widget->horizontalHeader()->sectionSize(0), width));
196
197 widgetItem = new QTableWidgetItem;
198 widgetItem->setFlags(Qt::ItemIsEnabled);
199 m_widget->setItem(insertRow, 1, widgetItem);
200
201 widgetItem = new QTableWidgetItem;
202 widgetItem->setFlags(Qt::ItemIsEnabled);
203 m_widget->setItem(insertRow, 3, widgetItem);
204
205 auto pw = new ProgressWidget(hostInfo, this);
206 connect(m_updateTimer, SIGNAL(timeout()), pw, SLOT(update()));
207 m_widget->setCellWidget(insertRow, 2, pw);
208 }
209
210 void FlowTableView::removeNode(unsigned int hostId)
211 {
212 Q_UNUSED(hostId);
213
214 createKnownHosts();
215 }