From be15e7f800a293dda290d0d228afeada03c53bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20C=2E=20Monteiro?= Date: Wed, 16 Dec 2020 16:38:25 +0100 Subject: [PATCH 1/2] [tool] emit a signal when the tool name is changed --- src/rviz/tool.cpp | 1 + src/rviz/tool.h | 1 + 2 files changed, 2 insertions(+) diff --git a/src/rviz/tool.cpp b/src/rviz/tool.cpp index 9e27df0a42..1acab1e697 100644 --- a/src/rviz/tool.cpp +++ b/src/rviz/tool.cpp @@ -73,6 +73,7 @@ void Tool::setName(const QString& name) { name_ = name; property_container_->setName(name_); + Q_EMIT nameChanged(this); } void Tool::setDescription(const QString& description) diff --git a/src/rviz/tool.h b/src/rviz/tool.h index 05a2be8ed6..e513d7e7b0 100644 --- a/src/rviz/tool.h +++ b/src/rviz/tool.h @@ -192,6 +192,7 @@ class RVIZ_EXPORT Tool : public QObject Q_SIGNALS: void close(); + void nameChanged(Tool* tool); protected: /** Override onInitialize to do any setup needed after the From a1ad870d2881a3266f40e0cba6a267220033827f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20C=2E=20Monteiro?= Date: Thu, 17 Dec 2020 16:28:08 +0100 Subject: [PATCH 2/2] [tool] update the toolbar when nameChanged is emitted --- src/rviz/tool.cpp | 7 ++++++- src/rviz/tool.h | 2 +- src/rviz/visualization_frame.cpp | 13 +++++++++++++ src/rviz/visualization_frame.h | 3 +++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/rviz/tool.cpp b/src/rviz/tool.cpp index 1acab1e697..4cc6cc9938 100644 --- a/src/rviz/tool.cpp +++ b/src/rviz/tool.cpp @@ -71,9 +71,14 @@ void Tool::setCursor(const QCursor& cursor) void Tool::setName(const QString& name) { + // Early return if the name did not change + if (name == name_) + return; + + // Change the name and emit a signal to let slots know name_ = name; property_container_->setName(name_); - Q_EMIT nameChanged(this); + Q_EMIT nameChanged(name_); } void Tool::setDescription(const QString& description) diff --git a/src/rviz/tool.h b/src/rviz/tool.h index e513d7e7b0..6b6e393472 100644 --- a/src/rviz/tool.h +++ b/src/rviz/tool.h @@ -192,7 +192,7 @@ class RVIZ_EXPORT Tool : public QObject Q_SIGNALS: void close(); - void nameChanged(Tool* tool); + void nameChanged(const QString& name); protected: /** Override onInitialize to do any setup needed after the diff --git a/src/rviz/visualization_frame.cpp b/src/rviz/visualization_frame.cpp index 0839998957..14c3fe36c6 100644 --- a/src/rviz/visualization_frame.cpp +++ b/src/rviz/visualization_frame.cpp @@ -1185,6 +1185,19 @@ void VisualizationFrame::addTool(Tool* tool) tool_to_action_map_[tool] = action; remove_tool_menu_->addAction(tool->getName()); + + QObject::connect(tool, &Tool::nameChanged, this, &VisualizationFrame::onToolNameChanged); +} + +void VisualizationFrame::onToolNameChanged(const QString& name) +{ + // Early return if the tool is not present + auto it = tool_to_action_map_.find(qobject_cast(sender())); + if (it == tool_to_action_map_.end()) + return; + + // Change the name of the action + it->second->setIconText(name); } void VisualizationFrame::onToolbarActionTriggered(QAction* action) diff --git a/src/rviz/visualization_frame.h b/src/rviz/visualization_frame.h index 28177aceb0..4b8c707f79 100644 --- a/src/rviz/visualization_frame.h +++ b/src/rviz/visualization_frame.h @@ -227,6 +227,9 @@ protected Q_SLOTS: * the shortcut key, onToolbarActionTriggered() is called. */ void addTool(Tool* tool); + /** @brief React to name changes of a tool, updating the name of the associated QAction */ + void onToolNameChanged(const QString& name); + /** @brief Remove the given tool from the frame's toolbar. */ void removeTool(Tool* tool);