-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqSlicerROS2ModuleWidget.cxx
422 lines (366 loc) · 15.8 KB
/
qSlicerROS2ModuleWidget.cxx
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*==============================================================================
Program: 3D Slicer
Portions (c) Copyright Brigham and Women's Hospital (BWH) All Rights Reserved.
See COPYRIGHT.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// Qt includes
#include <QDebug>
#include <QtGui>
#include <QCloseEvent>
#include <QButtonGroup>
#include <QWidget>
#include <QVBoxLayout>
#include <QLayout>
#include <QTableWidgetItem>
#include <QString>
#include <QVariant>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>
#include <QLineEdit>
#include <QUiLoader>
#include <QLineEdit>
// Slicer includes
#include "qSlicerROS2ModuleWidget.h"
#include "ui_qSlicerROS2ModuleWidget.h"
// #include "qSlicerApplication.h"
#include "ui_qSlicerROS2RobotWidget.h"
// MRML includes
#include <vtkMRMLScene.h>
#include <vtkMRMLROS2NodeNode.h>
#include <vtkMRMLROS2SubscriberNode.h>
#include <vtkMRMLROS2PublisherNode.h>
#include <vtkMRMLROS2RobotNode.h>
// Native includes
#include <iostream>
#include <filesystem>
// reference to Logic
#include "vtkSlicerROS2Logic.h"
//-----------------------------------------------------------------------------
/// \ingroup Slicer_QtModules_ExtensionTemplate
class qSlicerROS2ModuleWidgetPrivate: public Ui_qSlicerROS2ModuleWidget
{
public:
qSlicerROS2ModuleWidgetPrivate();
vtkSlicerROS2Logic* logic() const;
};
//-----------------------------------------------------------------------------
qSlicerROS2ModuleWidgetPrivate::qSlicerROS2ModuleWidgetPrivate()
{
}
//-----------------------------------------------------------------------------
// qSlicerROS2ModuleWidget methods
//-----------------------------------------------------------------------------
qSlicerROS2ModuleWidget::qSlicerROS2ModuleWidget(QWidget* _parent)
: Superclass( _parent )
, d_ptr( new qSlicerROS2ModuleWidgetPrivate )
{
}
//-----------------------------------------------------------------------------
qSlicerROS2ModuleWidget::~qSlicerROS2ModuleWidget()
{
}
//-----------------------------------------------------------------------------
void qSlicerROS2ModuleWidget::setup(void)
{
Q_D(qSlicerROS2ModuleWidget);
d->setupUi(this);
this->Superclass::setup();
// Set up signals / slots for dynamically loaded widgets
this->connect(d->rosSubscriberTableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(subscriberClicked(int, int)));
this->connect(d->rosPublisherTableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(publisherClicked(int, int)));
this->connect(d->addNewRobotButton, SIGNAL(clicked(bool)), this, SLOT(onAddNewRobotClicked()));
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
this->qvtkConnect(logic->mDefaultROS2Node, vtkMRMLNode::ReferenceAddedEvent,this, SLOT(updateWidget()));
this->qvtkConnect(logic->mDefaultROS2Node, vtkMRMLNode::ReferenceRemovedEvent,this, SLOT(updateWidget()));
updateWidget(); // if the scene is loaded before the widget is activated
}
void qSlicerROS2ModuleWidget::updateWidget()
{
Q_D(qSlicerROS2ModuleWidget);
this->Superclass::setup();
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
// Check how many subscriber references are on the node vs. how many rows are in the table
int visibleSubscriberRefs = d->rosSubscriberTableWidget->rowCount();
int visiblePublisherRefs = d->rosPublisherTableWidget->rowCount();
// The following needs to be updated to list all ROS nodes in logic
int subscriberRefs = logic->mDefaultROS2Node->GetNumberOfNodeReferences("subscriber");
int publisherRefs = logic->mDefaultROS2Node->GetNumberOfNodeReferences("publisher");
// update subscriber table
if (visibleSubscriberRefs < subscriberRefs) {
refreshSubTable();
}
// update publisher table
if (visiblePublisherRefs < publisherRefs) {
refreshPubTable();
}
// update the robot widgets based on the default node robot connections
int numRobots = logic->mDefaultROS2Node->GetNumberOfNodeReferences("robot");
auto robotsAddedToTheNode = logic->mDefaultROS2Node->mRobotNames;
if (robotsAddedToTheNode.size() != robotsAddedToTheWidget.size()) {
for (int i = 0; i < numRobots; i++) {
vtkMRMLROS2RobotNode * robot = vtkMRMLROS2RobotNode::SafeDownCast(logic->mDefaultROS2Node->GetNthNodeReference("robot", i));
if (std::find(robotsAddedToTheWidget.begin(), robotsAddedToTheWidget.end(), robot->GetRobotName())
!= robotsAddedToTheWidget.end()) {
continue;
} else {
onAddNewRobotClicked(robot->GetRobotName(), true);
robotsAddedToTheWidget.push_back(robot->GetRobotName());
return;
}
}
}
}
void qSlicerROS2ModuleWidget::onAddNewRobotClicked(const std::string & robotName, bool active)
{
Q_D(qSlicerROS2ModuleWidget);
this->Superclass::setup();
// Instantiate a robot widget
QWidget * robotWidget = new QWidget();
Ui_qSlicerROS2RobotWidget * robotWidgetUi = new Ui_qSlicerROS2RobotWidget();
robotWidgetUi->setupUi(robotWidget);
d->robotTabLayout->addWidget(robotWidget);
auto loadRobotButton = robotWidgetUi->loadRobotButton;
auto removeRobotButton = robotWidgetUi->removeRobotButton;
// Set up the lambda connections
this->connect(loadRobotButton, &QPushButton::clicked, this,
[=]() {
onLoadRobotClicked(robotWidgetUi->robotNameLineEdit,
robotWidgetUi->parameterNodeNameLineEdit,
robotWidgetUi->parameterLineEdit,
robotWidgetUi->fixedFrameLineEdit,
robotWidgetUi->tfPrefixLineEdit,
loadRobotButton, removeRobotButton);
});
this->connect(removeRobotButton, &QPushButton::clicked, this,
[=]() {
onRemoveRobotClicked(robotWidgetUi->robotNameLineEdit,
robotWidgetUi->parameterNodeNameLineEdit,
robotWidgetUi->parameterLineEdit,
robotWidgetUi->fixedFrameLineEdit,
robotWidgetUi->tfPrefixLineEdit,
loadRobotButton, removeRobotButton, robotWidget);
});
// This handles the case where a robot is added from the python console (instead of by button press)
if (active == true) {
robotWidgetUi->robotNameLineEdit->setEnabled(false);
robotWidgetUi->parameterNodeNameLineEdit->setEnabled(false);
robotWidgetUi->parameterLineEdit->setEnabled(false);
removeRobotButton->setEnabled(true);
QString name = QString::fromStdString(robotName);
QLineEdit * robotNameLineEdit = robotWidgetUi->robotNameLineEdit;
robotNameLineEdit->setText(name);
}
}
void qSlicerROS2ModuleWidget::refreshSubTable()
{
Q_D(qSlicerROS2ModuleWidget);
this->Superclass::setup();
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
// Update the subscriber table widget
size_t subRow = 0;
d->rosSubscriberTableWidget->clearContents();
// // Resize the table
d->rosSubscriberTableWidget->setRowCount(logic->mDefaultROS2Node->GetNumberOfNodeReferences("subscriber"));
// // Iterate through the references
for (int index = 0; index < logic->mDefaultROS2Node->GetNumberOfNodeReferences("subscriber"); ++index) {
const char * id = logic->mDefaultROS2Node->GetNthNodeReferenceID("subscriber", index);
vtkMRMLROS2SubscriberNode *sub = vtkMRMLROS2SubscriberNode::SafeDownCast(logic->mDefaultROS2Node->GetScene()->GetNodeByID(id));
if (sub == nullptr) {
} else {
updateSubscriberTable(sub, subRow);
subRow++;
}
}
}
void qSlicerROS2ModuleWidget::refreshPubTable()
{
Q_D(qSlicerROS2ModuleWidget);
this->Superclass::setup();
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
// Update the publisher table widget
size_t pubRow = 0;
d->rosPublisherTableWidget->clearContents();
// Resize the table
d->rosPublisherTableWidget->setRowCount(logic->mDefaultROS2Node->GetNumberOfNodeReferences("publisher"));
// Iterate through the references
for (int index = 0; index < logic->mDefaultROS2Node->GetNumberOfNodeReferences("publisher"); ++index) {
const char * id = logic->mDefaultROS2Node->GetNthNodeReferenceID("publisher", index);
vtkMRMLROS2PublisherNode *pub = vtkMRMLROS2PublisherNode::SafeDownCast(logic->mDefaultROS2Node->GetScene()->GetNodeByID(id));
if (pub == nullptr) {
} else {
updatePublisherTable(pub, pubRow);
pubRow++;
}
}
}
void qSlicerROS2ModuleWidget::updateSubscriberTable(vtkMRMLROS2SubscriberNode* sub, size_t row)
{
Q_D(qSlicerROS2ModuleWidget);
this->Superclass::setup();
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
QTableWidgetItem *topic_item = d->rosSubscriberTableWidget->item(row, 0);
QTableWidgetItem *type_item = d->rosSubscriberTableWidget->item(row, 1);
if (!topic_item) {
topic_item = new QTableWidgetItem;
d->rosSubscriberTableWidget->setItem(row, 0, topic_item);
topic_item->setText(sub->GetTopic().c_str());
type_item = new QTableWidgetItem;
d->rosSubscriberTableWidget->setItem(row, 1, type_item);
type_item->setText(sub->GetROSType());
}
row++;
}
void qSlicerROS2ModuleWidget::updatePublisherTable(vtkMRMLROS2PublisherNode* sub, size_t row){
Q_D(qSlicerROS2ModuleWidget);
this->Superclass::setup();
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
QTableWidgetItem *topic_item = d->rosPublisherTableWidget->item(row, 0);
QTableWidgetItem *type_item = d->rosPublisherTableWidget->item(row, 2);
if (!topic_item) {
topic_item = new QTableWidgetItem;
d->rosPublisherTableWidget->setItem(row, 0, topic_item);
topic_item->setText(sub->GetTopic().c_str());
type_item = new QTableWidgetItem;
d->rosPublisherTableWidget->setItem(row, 1, type_item);
type_item->setText(sub->GetROSType());
}
row++;
}
void qSlicerROS2ModuleWidget::subscriberClicked(int row, int col)
{
// Row is a reference to the message index
Q_D(qSlicerROS2ModuleWidget);
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
if (col == 1) { // only invoked when users click the number of messages cell
QString subName = d->rosSubscriberTableWidget->item(row,0)->text();
std::string topic = subName.toStdString();
vtkMRMLROS2SubscriberNode *sub = vtkMRMLROS2SubscriberNode::SafeDownCast(logic->GetMRMLScene()->GetFirstNodeByName(("ros2:sub:" + topic).c_str()));
if (!sub) {
std::cerr << "No subscriber by this name in the scene" << std::endl;
return;
}
QMessageBox msgBox;
msgBox.setText(QStringLiteral("Number of messages: %1\nLast message: %2")
.arg(sub->GetNumberOfMessages())
.arg(sub->GetLastMessageYAML().c_str()));
msgBox.exec();
}
}
void qSlicerROS2ModuleWidget::publisherClicked(int row, int col)
{
// Row is a reference to the message index
Q_D(qSlicerROS2ModuleWidget);
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
if (col == 1) { // only invoked when users click the number of messages cell
QString pubName = d->rosPublisherTableWidget->item(row,0)->text();
std::string topic = pubName.toStdString();
vtkMRMLROS2PublisherNode *pub = vtkMRMLROS2PublisherNode::SafeDownCast(logic->GetMRMLScene()->GetFirstNodeByName(("ros2:pub:" + topic).c_str()));
if (!pub) {
std::cerr << "No publisher by this name in the scene" << std::endl;
return;
}
QMessageBox msgBox;
msgBox.setText(QStringLiteral("Number of calls: %1\nNumber of messages sent: %2")
.arg(pub->GetNumberOfCalls())
.arg(pub->GetNumberOfMessagesSent()));
msgBox.exec();
}
}
void qSlicerROS2ModuleWidget::onLoadRobotClicked(QLineEdit * robotNameLineEdit,
QLineEdit * parameterNodeNameLineEdit,
QLineEdit * parameterNameLineEdit,
QLineEdit * fixedFrameLineEdit,
QLineEdit * tfPrefixLineEdit,
QPushButton * loadRobotButton,
QPushButton * removeRobotButton)
{
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
robotsAddedToTheWidget.push_back(robotNameLineEdit->text().toStdString());
logic->AddRobot(robotNameLineEdit->text().toStdString(),
parameterNodeNameLineEdit->text().toStdString(),
parameterNameLineEdit->text().toStdString(),
fixedFrameLineEdit->text().toStdString(),
tfPrefixLineEdit->text().toStdString());
loadRobotButton->setEnabled(false);
robotNameLineEdit->setEnabled(false);
parameterNodeNameLineEdit->setEnabled(false);
parameterNameLineEdit->setEnabled(false);
fixedFrameLineEdit->setEnabled(false);
tfPrefixLineEdit->setEnabled(false);
removeRobotButton->setEnabled(true);
}
void qSlicerROS2ModuleWidget::onRemoveRobotClicked(QLineEdit * robotNameLineEdit,
QLineEdit * parameterNodeNameLineEdit,
QLineEdit * parameterNameLineEdit,
QLineEdit * fixedFrameLineEdit,
QLineEdit * tfPrefixLineEdit,
QPushButton * loadRobotButton,
QPushButton * removeRobotButton,
QWidget * robotWidget)
{
Q_D(qSlicerROS2ModuleWidget);
vtkSlicerROS2Logic* logic = vtkSlicerROS2Logic::SafeDownCast(this->logic());
if (!logic) {
qWarning() << Q_FUNC_INFO << " failed: Invalid SlicerROS2 logic";
return;
}
std::string robot = robotNameLineEdit->text().toStdString();
logic->RemoveRobot(robot);
loadRobotButton->setEnabled(true);
robotNameLineEdit->setEnabled(true);
parameterNodeNameLineEdit->setEnabled(true);
parameterNameLineEdit->setEnabled(true);
fixedFrameLineEdit->setEnabled(true);
tfPrefixLineEdit->setEnabled(true);
removeRobotButton->setEnabled(false);
d->robotTabLayout->removeWidget(robotWidget);
delete robotWidget;
for (size_t robotName = 0; robotName < robotsAddedToTheWidget.size(); robotName++) {
if (robotsAddedToTheWidget[robotName] == robot) {
robotsAddedToTheWidget.erase(robotsAddedToTheWidget.begin() + robotName);
}
}
}