Skip to content

Commit

Permalink
[tool] update the toolbar when nameChanged is emitted
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmonteiro authored and João Carlos Espiúca Monteiro committed Dec 18, 2020
1 parent be15e7f commit a1ad870
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/rviz/tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/rviz/tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/rviz/visualization_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tool*>(sender()));
if (it == tool_to_action_map_.end())
return;

// Change the name of the action
it->second->setIconText(name);
}

void VisualizationFrame::onToolbarActionTriggered(QAction* action)
Expand Down
3 changes: 3 additions & 0 deletions src/rviz/visualization_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit a1ad870

Please sign in to comment.