tool_manager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QKeyEvent>
31 #include <QRegularExpression>
32 
33 #include <ros/assert.h>
34 
35 #include <rviz/failed_tool.h>
38 
39 #include <rviz/tool_manager.h>
40 
41 namespace rviz
42 {
43 QString addSpaceToCamelCase(QString input)
44 {
45  QRegularExpression re = QRegularExpression("([A-Z])([a-z]*)");
46  input.replace(re, " \\1\\2");
47  return input.trimmed();
48 }
49 
51  : factory_(new PluginlibFactory<Tool>("rviz", "rviz::Tool"))
52  , property_tree_model_(new PropertyTreeModel(new Property()))
53  , context_(context)
54  , current_tool_(nullptr)
55  , default_tool_(nullptr)
56 {
58 }
59 
61 {
62  removeAll();
63  delete factory_;
64  delete property_tree_model_;
65 }
66 
68 {
69  // Possibly this should be done with a loop over
70  // factory_->getDeclaredClassIds(), but then I couldn't control the
71  // order.
72  addTool("rviz/MoveCamera");
73  addTool("rviz/Interact");
74  addTool("rviz/Select");
75  addTool("rviz/SetInitialPose");
76  addTool("rviz/SetGoal");
77 }
78 
80 {
81  for (int i = tools_.size() - 1; i >= 0; i--)
82  {
83  removeTool(i);
84  }
85 }
86 
87 void ToolManager::load(const Config& config)
88 {
89  removeAll();
90 
91  int num_tools = config.listLength();
92  for (int i = 0; i < num_tools; i++)
93  {
94  Config tool_config = config.listChildAt(i);
95 
96  QString class_id;
97  if (tool_config.mapGetString("Class", &class_id))
98  {
99  Tool* tool = addTool(class_id);
100  tool->load(tool_config);
101  }
102  }
103 }
104 
105 void ToolManager::save(Config config) const
106 {
107  for (int i = 0; i < tools_.size(); i++)
108  {
109  tools_[i]->save(config.listAppendNew());
110  }
111 }
112 
113 bool ToolManager::toKey(QString const& str, uint& key)
114 {
115  QKeySequence seq(str);
116 
117  // We should only working with a single key here
118  if (seq.count() == 1)
119  {
120  key = seq[0];
121  return true;
122  }
123  else
124  {
125  return false;
126  }
127 }
128 
129 void ToolManager::handleChar(QKeyEvent* event, RenderPanel* panel)
130 {
131  // if the incoming key is ESC fallback to the default tool
132  if (event->key() == Qt::Key_Escape)
133  {
135  return;
136  }
137 
138  // check if the incoming key triggers the activation of another tool
139  auto tool_it = shortkey_to_tool_map_.find(event->key());
140  if (tool_it != shortkey_to_tool_map_.end())
141  {
142  Tool* tool = tool_it->second;
143  // if tool matches the current tool
144  if (current_tool_ == tool)
145  {
146  // ... deactivate the current tool and fallback to default
148  }
149  else
150  {
151  // if no, check if the current tool accesses all key events
153  {
154  // if yes, pass the key
155  current_tool_->processKeyEvent(event, panel);
156  }
157  else
158  {
159  // if no, switch the tool
160  setCurrentTool(tool);
161  }
162  }
163  }
164  else if (current_tool_)
165  {
166  // if the incoming key triggers no other tool,
167  // just hand down the key event
168  current_tool_->processKeyEvent(event, panel);
169  }
170 }
171 
173 {
174  if (current_tool_)
175  {
177  }
178 
179  current_tool_ = tool;
180 
181  if (current_tool_)
182  {
184  }
185 
186  Q_EMIT toolChanged(current_tool_);
187 }
188 
190 {
191  default_tool_ = tool;
192 }
193 
195 {
196  ROS_ASSERT(index >= 0);
197  ROS_ASSERT(index < (int)tools_.size());
198 
199  return tools_[index];
200 }
201 
203 {
204  if (container->numChildren() > 0)
205  {
206  if (!property_tree_model_->getRoot()->contains(container))
207  {
208  property_tree_model_->getRoot()->addChild(container);
209  container->expand();
210  }
211  }
212  else
213  {
214  property_tree_model_->getRoot()->takeChild(container);
215  }
216 }
217 
219 {
220  setCurrentTool(getDefaultTool());
221 }
222 
223 Tool* ToolManager::addTool(const QString& class_id)
224 {
225  QString error;
226  bool failed = false;
227  Tool* tool = factory_->make(class_id, &error);
228  if (!tool)
229  {
230  tool = new FailedTool(class_id, error);
231  failed = true;
232  }
233 
234  tools_.append(tool);
235  tool->setName(addSpaceToCamelCase(factory_->getClassName(class_id)));
236  tool->setIcon(factory_->getIcon(class_id));
237  tool->initialize(context_);
238 
239  if (tool->getShortcutKey() != '\0')
240  {
241  uint key;
242  QString str = QString(tool->getShortcutKey());
243 
244  if (toKey(str, key))
245  {
246  shortkey_to_tool_map_[key] = tool;
247  }
248  }
249 
250  Property* container = tool->getPropertyContainer();
252  updatePropertyVisibility(container);
253 
254  Q_EMIT toolAdded(tool);
255 
256  // If the default tool is unset and this tool loaded correctly, set
257  // it as the default and current.
258  if (default_tool_ == nullptr && !failed)
259  {
260  setDefaultTool(tool);
261  setCurrentTool(tool);
262  }
263 
264  QObject::connect(tool, &Tool::close, this, &ToolManager::closeTool);
265 
266  Q_EMIT configChanged();
267 
268  return tool;
269 }
270 
271 void ToolManager::removeTool(int index)
272 {
273  Tool* tool = tools_.takeAt(index);
274  Tool* fallback = nullptr;
275  if (!tools_.empty())
276  {
277  fallback = tools_[0];
278  }
279  if (tool == current_tool_)
280  {
281  setCurrentTool(fallback);
282  }
283  if (tool == default_tool_)
284  {
285  setDefaultTool(fallback);
286  }
287  Q_EMIT toolRemoved(tool);
288 
289  uint key;
290  if (toKey(QString(tool->getShortcutKey()), key))
291  {
292  shortkey_to_tool_map_.erase(key);
293  }
294  delete tool;
295  Q_EMIT configChanged();
296 }
297 
299 {
300  Q_EMIT toolRefreshed(tool);
301 }
302 
304 {
305  QStringList class_names;
306  for (int i = 0; i < tools_.size(); i++)
307  {
308  class_names.append(tools_[i]->getClassId());
309  }
310  return class_names;
311 }
312 
313 } // end namespace rviz
rviz::ToolManager::getToolClasses
QStringList getToolClasses()
Definition: tool_manager.cpp:303
rviz::ToolManager::toolAdded
void toolAdded(Tool *)
Emitted by addTool() after the tool is added to the list of tools.
rviz::ToolManager::tools_
QList< Tool * > tools_
Definition: tool_manager.h:164
rviz::ToolManager::toolRemoved
void toolRemoved(Tool *)
rviz::Tool::load
virtual void load(const Config &config)
Load properties from the given Config.
Definition: tool.cpp:90
rviz::Tool
Definition: tool.h:56
rviz::ToolManager::save
void save(Config config) const
Definition: tool_manager.cpp:105
rviz::PropertyTreeModel
Definition: property_tree_model.h:38
rviz::PluginlibFactory
Definition: pluginlib_factory.h:51
property.h
rviz::ToolManager::shortkey_to_tool_map_
std::map< int, Tool * > shortkey_to_tool_map_
Definition: tool_manager.h:168
rviz::ToolManager::getDefaultTool
Tool * getDefaultTool()
Get the default tool.
Definition: tool_manager.h:122
rviz::ToolManager::setDefaultTool
void setDefaultTool(Tool *tool)
Set the default tool.
Definition: tool_manager.cpp:189
rviz::Property::addChild
virtual void addChild(Property *child, int index=-1)
Add a child property.
Definition: property.cpp:356
failed_tool.h
rviz::ToolManager::closeTool
void closeTool()
Deactivates the current tool and sets the default tool.
Definition: tool_manager.cpp:218
rviz::Property::numChildren
virtual int numChildren() const
Return the number of child objects (Property or otherwise).
Definition: property.h:287
rviz::ToolManager::property_tree_model_
PropertyTreeModel * property_tree_model_
Definition: tool_manager.h:163
rviz::Tool::accessAllKeys
bool accessAllKeys()
Definition: tool.h:86
rviz::Property::contains
bool contains(Property *possible_child) const
Return true if the list of children includes possible_child, false if not.
Definition: property.cpp:218
rviz::addSpaceToCamelCase
QString addSpaceToCamelCase(QString input)
Definition: tool_manager.cpp:43
rviz::ToolManager::configChanged
void configChanged()
Emitted when anything changes which will change the saved config file contents.
rviz::ToolManager::handleChar
void handleChar(QKeyEvent *event, RenderPanel *panel)
Definition: tool_manager.cpp:129
rviz::ToolManager::~ToolManager
~ToolManager() override
Definition: tool_manager.cpp:60
rviz::Property
A single element of a property tree, with a name, value, description, and possibly children.
Definition: property.h:100
rviz::ToolManager::current_tool_
Tool * current_tool_
Definition: tool_manager.h:166
rviz::Tool::deactivate
virtual void deactivate()=0
rviz::ToolManager::load
void load(const Config &config)
Definition: tool_manager.cpp:87
rviz::ToolManager::getTool
Tool * getTool(int index)
Return the tool at a given index in the Tool list. If index is less than 0 or greater than the number...
Definition: tool_manager.cpp:194
rviz::Tool::activate
virtual void activate()=0
rviz::Property::expand
virtual void expand()
Expand (show the children of) this Property.
Definition: property.cpp:578
rviz::ToolManager::removeTool
void removeTool(int index)
Definition: tool_manager.cpp:271
rviz
Definition: add_display_dialog.cpp:54
rviz::ToolManager::toolChanged
void toolChanged(Tool *)
Emitted by setCurrentTool() after the newly chosen tool is activated.
rviz::PropertyTreeModel::configChanged
void configChanged()
Emitted when a Property which should be saved changes.
rviz::Tool::processKeyEvent
virtual int processKeyEvent(QKeyEvent *event, RenderPanel *panel)
Definition: tool.h:117
rviz::ToolManager::toKey
bool toKey(QString const &str, uint &key_out)
Definition: tool_manager.cpp:113
rviz::Tool::getShortcutKey
char getShortcutKey()
Definition: tool.h:81
rviz::Tool::setIcon
void setIcon(const QIcon &icon)
Set the toolbar icon for this tool (will also set its cursor).
Definition: tool.cpp:61
rviz::ToolManager::default_tool_
Tool * default_tool_
Definition: tool_manager.h:167
rviz::PropertyTreeModel::getRoot
Property * getRoot() const
Definition: property_tree_model.h:114
property_tree_model.h
rviz::Property::childListChanged
void childListChanged(Property *this_property)
Emitted after insertions and deletions of child Properties.
rviz::ToolManager::removeAll
void removeAll()
Definition: tool_manager.cpp:79
rviz::DisplayContext
Pure-virtual base class for objects which give Display subclasses context in which to work.
Definition: display_context.h:81
rviz::Tool::getPropertyContainer
virtual Property * getPropertyContainer() const
Return the container for properties of this Tool.
Definition: tool.h:76
rviz::ToolManager::ToolManager
ToolManager(DisplayContext *context)
Definition: tool_manager.cpp:50
rviz::ToolManager::addTool
Tool * addTool(const QString &tool_class_lookup_name)
Create a tool by class lookup name, add it to the list, and return it.
Definition: tool_manager.cpp:223
rviz::ToolManager::refreshTool
void refreshTool(Tool *tool)
Triggers redrawing the tool's icon/text in the toolbar.
Definition: tool_manager.cpp:298
rviz::ToolManager::updatePropertyVisibility
void updatePropertyVisibility(Property *property)
If property has children, it is added to the tool property tree, and if it does not,...
Definition: tool_manager.cpp:202
rviz::Tool::setName
void setName(const QString &name)
Set the name of the tool.
Definition: tool.cpp:72
rviz::Config::mapGetString
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:288
rviz::Tool::close
void close()
rviz::ToolManager::initialize
void initialize()
Initialization for after the DisplayContext is created. Loads standard RViz tools.
Definition: tool_manager.cpp:67
tool_manager.h
rviz::RenderPanel
Definition: render_panel.h:74
rviz::ToolManager::context_
DisplayContext * context_
Definition: tool_manager.h:165
rviz::FailedTool
A FailedTool instance represents a Tool class we tried and failed to instantiate.
Definition: failed_tool.h:46
assert.h
rviz::ToolManager::toolRefreshed
void toolRefreshed(Tool *)
Emitted by refreshTool() to gedraw the tool's icon in the toolbar'.
rviz::Tool::initialize
void initialize(DisplayContext *context)
Definition: tool.cpp:52
rviz::ToolManager::setCurrentTool
void setCurrentTool(Tool *tool)
Set the current tool. The current tool is given all mouse and keyboard events which VisualizationMana...
Definition: tool_manager.cpp:172
ROS_ASSERT
#define ROS_ASSERT(cond)
config
config
rviz::Property::takeChild
Property * takeChild(Property *child)
Remove a given child object and return a pointer to it.
Definition: property.cpp:322
rviz::ToolManager::factory_
PluginlibFactory< Tool > * factory_
Definition: tool_manager.h:162
rviz::Config
Configuration data storage class.
Definition: config.h:124


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10