-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTaskItem.cpp
More file actions
274 lines (232 loc) · 7.17 KB
/
TaskItem.cpp
File metadata and controls
274 lines (232 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <rtt/transports/corba/TaskContextProxy.hpp>
#include "TaskItem.hpp"
#include <rtt/typelib/TypelibMarshallerBase.hpp>
#include <lib_config/TypelibConfiguration.hpp>
#include <base-logging/Logging.hpp>
TaskItem::TaskItem(RTT::corba::TaskContextProxy* _task)
: task(_task),
nameItem(ItemType::TASK),
statusItem(ItemType::TASK),
refreshPorts(true),
stateChanged(false)
{
inputPorts.setText("InputPorts");
outputPorts.setText("OutputPorts");
properties.setText("Properties");
nameItem.appendRow(&inputPorts);
nameItem.appendRow(&outputPorts);
nameItem.appendRow(&properties);
nameItem.setData(this);
statusItem.setData(this);
statusItem.setText("");
}
TaskItem::~TaskItem()
{
}
void TaskItem::reset()
{
for (auto port: ports)
{
OutputPortItem *outPort = dynamic_cast<OutputPortItem*>(port.second);
if (outPort)
{
outPort->reset();
}
}
task = nullptr;
}
bool TaskItem::hasVisualizers()
{
for (auto &port: ports)
{
if (port.second->hasVisualizers())
{
return true;
}
}
return false;
}
bool TaskItem::update()
{
if (!task)
{
return false;
}
if (!task->server() || task->server()->_is_nil())
{
LOG_WARN_S << "TaskItem::update(): disconnect of task " << task->getName();
reset();
return false;
}
bool needsUpdate = false;
if (nameItem.text().isEmpty())
{
try
{
nameItem.setText(task->getName().c_str());
needsUpdate = true;
}
catch (...)
{
return false;
}
}
// check for task state update
stateChanged = updateState();
needsUpdate |= stateChanged;
// check for property update
if (propertyMap.empty() || stateChanged)
{
needsUpdate |= updateProperties();
}
// check for port update
bool hasVis = hasVisualizers();
if (!hasVis && !nameItem.isExpanded())
{
return needsUpdate;
}
needsUpdate |= updatePorts(hasVis);
return needsUpdate;
}
bool TaskItem::updatePorts(bool hasVisualizers)
{
bool needsUpdate = false;
bool refreshedOutputPorts = false;
if (ports.empty() || hasVisualizers || outputPorts.isExpanded())
{
const RTT::DataFlowInterface *dfi = task->ports();
for (RTT::base::PortInterface *pi : dfi->getPorts())
{
const std::string portName(pi->getName());
RTT::base::OutputPortInterface *outIf = dynamic_cast<RTT::base::OutputPortInterface *>(pi);
auto it = ports.find(portName);
PortItem *item = nullptr;
if (it == ports.end())
{
if (outIf)
{
item = new OutputPortItem(outIf);
outputPorts.appendRow(item->getRow());
item->getNameItem()->appendRow({new QStandardItem(QString("loading..")), new QStandardItem()});
}
else
{
item = new PortItem(pi->getName());
inputPorts.appendRow(item->getRow());
}
ports.insert(std::make_pair(portName, item));
needsUpdate = true;
}
else
{
item = it->second;
}
if ((item->hasVisualizers() || outputPorts.isExpanded()) && outIf)
{
OutputPortItem *outPortItem = static_cast<OutputPortItem *>(item);
if (refreshPorts)
{
outPortItem->updateOutputPortInterface(outIf);
refreshedOutputPorts = true;
}
needsUpdate |= outPortItem->updataValue();
}
}
}
if (refreshedOutputPorts)
{
refreshPorts = false;
}
return needsUpdate;
}
bool TaskItem::updateProperties()
{
bool needsUpdate = false;
RTT::PropertyBag *taskProperties = task->properties();
orogen_transports::TypelibMarshallerBase *transport;
orogen_transports::TypelibMarshallerBase::Handle *transportHandle;
RTT::base::DataSourceBase::shared_ptr sample;
for (std::size_t i=0; i<taskProperties->size(); i++)
{
RTT::base::PropertyBase *property = taskProperties->getItem(i);
RTT::types::TypeInfo const *type = property->getTypeInfo();
transport = dynamic_cast<orogen_transports::TypelibMarshallerBase *>(type->getProtocol(orogen_transports::TYPELIB_MARSHALLER_ID));
if (! transport)
{
LOG_ERROR_S << "cannot report ports of type " << type->getTypeName() << " as no typekit generated by orogen defines it";
continue;
}
const Typelib::Type *typelibType = transport->getRegistry().get(transport->getMarshallingType());
transportHandle = transport->createSample();
sample = transport->getDataSource(transportHandle);
transport->readDataSource(*(property->getDataSource()), transportHandle);
Typelib::Value val(transport->getTypelibSample(transportHandle), *(typelibType));
std::shared_ptr<ItemBase> item = nullptr;
auto it = propertyMap.find(property->getName());
if (it == propertyMap.end())
{
item = getItem(val);
propertyMap[property->getName()] = item;
item->setName(property->getName().c_str());
properties.appendRow(item->getRow());
needsUpdate = true;
}
else
{
item = propertyMap[property->getName()];
needsUpdate |= item->update(val, properties.isExpanded(), true);
}
}
return needsUpdate;
}
bool TaskItem::updateState()
{
std::string stateString = "";
RTT::base::TaskCore::TaskState state = task->getTaskState();
switch(state)
{
case RTT::base::TaskCore::Exception:
stateString = "Exception";
break;
case RTT::base::TaskCore::FatalError:
stateString = "FatalError";
break;
case RTT::base::TaskCore::Init:
stateString = "Init";
break;
case RTT::base::TaskCore::PreOperational:
stateString = "PreOperational";
break;
case RTT::base::TaskCore::Running:
stateString = "Running";
break;
case RTT::base::TaskCore::RunTimeError:
stateString = "RunTimeError";
break;
case RTT::base::TaskCore::Stopped:
stateString = "Stopped";
break;
}
if (statusItem.text().toStdString() != stateString)
{
statusItem.setText(stateString.c_str());
return true;
}
return false;
}
QList< QStandardItem* > TaskItem::getRow()
{
return {&nameItem, &statusItem};
}
QModelIndex TaskItem::updateLeft()
{
return nameItem.index();
}
QModelIndex TaskItem::updateRight()
{
return statusItem.index();
}
RTT::corba::TaskContextProxy* TaskItem::getTaskContext()
{
return task;
}