|
16 | 16 |
|
17 | 17 | #include "scenario_view.h" |
18 | 18 | #include "indicator_widget.h" |
19 | | - |
| 19 | +#include <QHeaderView> |
| 20 | +#include <QInputDialog> |
20 | 21 | using std::placeholders::_1; |
21 | 22 |
|
22 | 23 | namespace scenario_execution_rviz { |
23 | 24 |
|
24 | | -ScenarioView::ScenarioView(QWidget *parent) : rviz_common::Panel(parent) { |
| 25 | +ScenarioView::ScenarioView(QWidget *parent) |
| 26 | + : rviz_common::Panel(parent), mInitTimer(this) { |
| 27 | + mInitTimer.setSingleShot(true); |
| 28 | + connect(&mInitTimer, &QTimer::timeout, this, &ScenarioView::setupConnection); |
| 29 | + |
25 | 30 | QVBoxLayout *layout = new QVBoxLayout; |
26 | 31 |
|
27 | 32 | QFormLayout *formLayout = new QFormLayout; |
@@ -51,14 +56,58 @@ ScenarioView::ScenarioView(QWidget *parent) : rviz_common::Panel(parent) { |
51 | 56 | SLOT(handleItemCollapsed(QTreeWidgetItem *))); |
52 | 57 | connect(mScenarioView, SIGNAL(itemExpanded(QTreeWidgetItem *)), this, |
53 | 58 | SLOT(handleItemExpanded(QTreeWidgetItem *))); |
| 59 | + connect(mScenarioView->header(), SIGNAL(sectionDoubleClicked(int)), this, |
| 60 | + SLOT(onHeaderDoubleClicked(int))); |
| 61 | +} |
| 62 | + |
| 63 | +void ScenarioView::load(const rviz_common::Config &config) { |
| 64 | + rviz_common::Panel::load(config); |
| 65 | + QString topic; |
| 66 | + if (config.mapGetString("snapshot_topic", &topic)) { |
| 67 | + |
| 68 | + if (mSnapshotTopic != topic) { |
| 69 | + mInitTimer.stop(); // stop init trigger |
| 70 | + mSnapshotTopic = topic; |
| 71 | + setupConnection(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +void ScenarioView::save(rviz_common::Config config) const { |
| 77 | + rviz_common::Panel::save(config); |
| 78 | + config.mapSetValue("snapshot_topic", mSnapshotTopic); |
| 79 | +} |
| 80 | + |
| 81 | +void ScenarioView::onHeaderDoubleClicked(int idx) { |
| 82 | + (void)idx; |
| 83 | + bool ok; |
| 84 | + QString text = QInputDialog::getText(this, "Configuration", "Snapshot Topic", |
| 85 | + QLineEdit::Normal, mSnapshotTopic, &ok); |
| 86 | + if (ok && !text.isEmpty() && (mSnapshotTopic != text)) { |
| 87 | + mSnapshotTopic = text; |
| 88 | + setupConnection(); |
| 89 | + Q_EMIT configChanged(); |
| 90 | + } |
54 | 91 | } |
55 | 92 |
|
56 | 93 | void ScenarioView::onInitialize() { |
57 | 94 | _node = getDisplayContext()->getRosNodeAbstraction().lock()->get_raw_node(); |
| 95 | + mSnapshotTopic = "/scenario_execution/snapshots"; |
| 96 | + mInitTimer.start(250); // only executed if no config-load gets triggered. |
| 97 | +} |
| 98 | + |
| 99 | +void ScenarioView::setupConnection() { |
| 100 | + if (mSnapshotTopic.isEmpty()) { |
| 101 | + return; |
| 102 | + } |
| 103 | + if (mBehaviorTreeSubscriber) { |
| 104 | + mBehaviorTreeSubscriber.reset(); |
| 105 | + } |
58 | 106 |
|
| 107 | + qDebug() << "Subscribing to" << mSnapshotTopic; |
59 | 108 | mBehaviorTreeSubscriber = |
60 | 109 | _node->create_subscription<py_trees_ros_interfaces::msg::BehaviourTree>( |
61 | | - "/scenario_execution/snapshots", 10, |
| 110 | + mSnapshotTopic.toStdString().c_str(), 10, |
62 | 111 | std::bind(&ScenarioView::behaviorTreeChanged, this, _1)); |
63 | 112 | } |
64 | 113 |
|
|
0 commit comments