view_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 <stdio.h>
31 
32 #include <sstream>
33 
34 #include <rviz/display_context.h>
38 #include <rviz/render_panel.h>
39 #include <rviz/view_controller.h>
40 
41 #include <rviz/view_manager.h>
42 
43 namespace rviz
44 {
46  : context_(context)
47  , root_property_(new ViewControllerContainer)
48  , property_model_(new PropertyTreeModel(root_property_))
49  , factory_(new PluginlibFactory<ViewController>("rviz", "rviz::ViewController"))
50  , current_(nullptr)
51  , render_panel_(nullptr)
52 {
53  property_model_->setDragDropClass("view-controller");
56 }
57 
59 {
60  delete property_model_;
61  delete factory_;
62 }
63 
65 {
66  setCurrent(create("rviz/Orbit"), false);
67 }
68 
69 void ViewManager::update(float wall_dt, float ros_dt)
70 {
71  if (getCurrent())
72  {
73  getCurrent()->update(wall_dt, ros_dt);
74  }
75 }
76 
77 ViewController* ViewManager::create(const QString& class_id)
78 {
79  QString error;
80  ViewController* view = factory_->make(class_id, &error);
81  if (!view)
82  {
83  view = new FailedViewController(class_id, error);
84  }
85  view->initialize(context_);
86 
87  return view;
88 }
89 
91 {
92  return current_;
93 }
94 
96 {
97  if (source_view == nullptr)
98  {
99  return;
100  }
101 
102  ViewController* previous = getCurrent();
103  if (source_view != previous)
104  {
105  ViewController* new_current = copy(source_view);
106 
107  setCurrent(new_current, false);
108  Q_EMIT configChanged();
109  }
110 }
111 
113 {
114  if (obj == current_)
115  {
116  current_ = nullptr;
117  }
118 }
119 
120 void ViewManager::setCurrent(ViewController* new_current, bool mimic_view)
121 {
122  ViewController* previous = getCurrent();
123  if (previous)
124  {
125  if (mimic_view)
126  {
127  new_current->mimic(previous);
128  }
129  else
130  {
131  new_current->transitionFrom(previous);
132  }
133  disconnect(previous, &QObject::destroyed, this, &ViewManager::onCurrentDestroyed);
134  }
135  new_current->setName("Current View");
136  connect(new_current, &QObject::destroyed, this, &ViewManager::onCurrentDestroyed);
137  current_ = new_current;
138  root_property_->addChildToFront(new_current);
139  delete previous;
140 
141  if (render_panel_)
142  {
143  // This setViewController() can indirectly call
144  // ViewManager::update(), so make sure getCurrent() will return the
145  // new one by this point.
146  render_panel_->setViewController(new_current);
147  }
148  if (current_ != previous)
149  Q_EMIT currentChanged();
150 }
151 
152 void ViewManager::setCurrentViewControllerType(const QString& new_class_id)
153 {
154  setCurrent(create(new_class_id), true);
155 }
156 
158 {
159  ViewController* current = getCurrent();
160  if (current)
161  {
162  ViewController* new_copy = copy(current);
163  new_copy->setName(factory_->getClassName(new_copy->getClassId()));
164  root_property_->addChild(new_copy);
165  }
166 }
167 
169 {
170  if (index < 0)
171  {
172  index = 0;
173  }
174  return qobject_cast<ViewController*>(root_property_->childAt(index + 1));
175 }
176 
178 {
179  int count = root_property_->numChildren();
180  if (count <= 0)
181  {
182  return 0;
183  }
184  else
185  {
186  return count - 1;
187  }
188 }
189 
190 void ViewManager::add(ViewController* view, int index)
191 {
192  if (index < 0)
193  {
194  index = root_property_->numChildren();
195  }
196  else
197  {
198  index++;
199  }
200  property_model_->getRoot()->addChild(view, index);
201 }
202 
204 {
205  for (int i = 0; i < getNumViews(); i++)
206  {
207  if (getViewAt(i) == view)
208  {
209  return qobject_cast<ViewController*>(root_property_->takeChildAt(i + 1));
210  }
211  }
212  return nullptr;
213 }
214 
216 {
217  if (index < 0)
218  {
219  return nullptr;
220  }
221  return qobject_cast<ViewController*>(root_property_->takeChildAt(index + 1));
222 }
223 
224 void ViewManager::load(const Config& config)
225 {
226  Config current_config = config.mapGetChild("Current");
227  QString class_id;
228  if (current_config.mapGetString("Class", &class_id))
229  {
230  ViewController* new_current = create(class_id);
231  new_current->load(current_config);
232  setCurrent(new_current, false);
233  }
234 
235  Config saved_views_config = config.mapGetChild("Saved");
237  int num_saved = saved_views_config.listLength();
238  for (int i = 0; i < num_saved; i++)
239  {
240  Config view_config = saved_views_config.listChildAt(i);
241 
242  if (view_config.mapGetString("Class", &class_id))
243  {
244  ViewController* view = create(class_id);
245  view->load(view_config);
246  add(view);
247  }
248  }
249 }
250 
251 void ViewManager::save(Config config) const
252 {
253  getCurrent()->save(config.mapMakeChild("Current"));
254 
255  Config saved_views_config = config.mapMakeChild("Saved");
256  for (int i = 0; i < getNumViews(); i++)
257  {
258  getViewAt(i)->save(saved_views_config.listAppendNew());
259  }
260 }
261 
263 {
264  Config config;
265  source->save(config);
266 
267  ViewController* copy_of_source = create(source->getClassId());
268  copy_of_source->load(config);
269 
270  return copy_of_source;
271 }
272 
274 {
275  render_panel_ = render_panel;
276 }
277 
278 Qt::ItemFlags ViewControllerContainer::getViewFlags(int column) const
279 {
280  return Property::getViewFlags(column) | Qt::ItemIsDropEnabled;
281 }
282 
284 {
285  if (index == 0)
286  {
287  index = 1;
288  }
289  Property::addChild(child, index);
290 }
291 
293 {
294  Property::addChild(child, 0);
295 }
296 
297 } // end namespace rviz
rviz::ViewController::load
void load(const Config &config) override
Load the value of this property and/or its children from the given Config reference.
Definition: view_controller.cpp:185
rviz::Property::childAt
Property * childAt(int index) const
Return the child Property with the given index, or NULL if the index is out of bounds or if the child...
Definition: property.cpp:202
view_manager.h
rviz::ViewManager::takeAt
ViewController * takeAt(int index)
Remove the ViewController at the given index from the list and return it. If the index is not valid,...
Definition: view_manager.cpp:215
rviz::Property::setName
virtual void setName(const QString &name)
Set the name.
Definition: property.cpp:155
rviz::ViewManager::update
void update(float wall_dt, float ros_dt)
Definition: view_manager.cpp:69
rviz::ViewManager::root_property_
ViewControllerContainer * root_property_
Definition: view_manager.h:146
rviz::PropertyTreeModel
Definition: property_tree_model.h:38
rviz::PluginlibFactory
Definition: pluginlib_factory.h:51
rviz::ViewManager::ViewManager
ViewManager(DisplayContext *context)
Definition: view_manager.cpp:45
rviz::ViewManager::copyCurrentToList
void copyCurrentToList()
Make a copy of the current ViewController and add it to the end of the list of saved views.
Definition: view_manager.cpp:157
rviz::Property::addChild
virtual void addChild(Property *child, int index=-1)
Add a child property.
Definition: property.cpp:356
rviz::ViewManager::setCurrent
void setCurrent(ViewController *new_current, bool mimic_view)
Set new_current as current.
Definition: view_manager.cpp:120
rviz::Config::listLength
int listLength() const
Returns the length of the List in this Node, or 0 if this Node does not have type List.
Definition: config.cpp:324
rviz::Property::numChildren
virtual int numChildren() const
Return the number of child objects (Property or otherwise).
Definition: property.h:287
failed_view_controller.h
rviz::ViewManager::load
void load(const Config &config)
Definition: view_manager.cpp:224
rviz::ViewManager::render_panel_
RenderPanel * render_panel_
Definition: view_manager.h:150
rviz::RenderPanel::setViewController
void setViewController(ViewController *controller)
Set the ViewController which should control the camera position for this view.
Definition: render_panel.cpp:153
rviz::ViewControllerContainer::getViewFlags
Qt::ItemFlags getViewFlags(int column) const override
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: view_manager.cpp:278
rviz::ViewManager::~ViewManager
~ViewManager() override
Definition: view_manager.cpp:58
rviz::Property
A single element of a property tree, with a name, value, description, and possibly children.
Definition: property.h:100
rviz::ViewManager::factory_
PluginlibFactory< ViewController > * factory_
Definition: view_manager.h:148
rviz::ViewControllerContainer::addChildToFront
void addChildToFront(Property *child)
Definition: view_manager.cpp:292
rviz::ViewManager::currentChanged
void currentChanged()
Emitted just after the current view controller changes.
rviz
Definition: add_display_dialog.cpp:54
rviz::PropertyTreeModel::configChanged
void configChanged()
Emitted when a Property which should be saved changes.
rviz::ViewController::initialize
void initialize(DisplayContext *context)
Do all setup that can't be done in the constructor.
Definition: view_controller.cpp:85
rviz::Property::takeChildAt
virtual Property * takeChildAt(int index)
Take a child out of the child list, but don't destroy it.
Definition: property.cpp:334
rviz::ViewControllerContainer
Container property for ViewControllers which gets the drag/drop right for the funky way Current-View ...
Definition: view_manager.h:156
rviz::ViewManager::current_
ViewController * current_
Definition: view_manager.h:149
rviz::ViewManager::getCurrent
ViewController * getCurrent() const
Return the current ViewController in use for the main RenderWindow.
Definition: view_manager.cpp:90
rviz::ViewController::transitionFrom
virtual void transitionFrom(ViewController *previous_view)
Called by ViewManager when this ViewController is being made current.
Definition: view_controller.h:138
rviz::PropertyTreeModel::getRoot
Property * getRoot() const
Definition: property_tree_model.h:114
property_tree_model.h
rviz::DisplayContext
Pure-virtual base class for objects which give Display subclasses context in which to work.
Definition: display_context.h:81
rviz::Config::listAppendNew
Config listAppendNew()
Ensure the referenced Node is of type List, append a new Empty Node to the end of the list,...
Definition: config.cpp:341
rviz::ViewManager::getViewAt
ViewController * getViewAt(int index) const
Definition: view_manager.cpp:168
rviz::FailedViewController
A FailedViewController instance represents a ViewController class we tried and failed to instantiate.
Definition: failed_view_controller.h:46
render_panel.h
rviz::ViewControllerContainer::addChild
void addChild(Property *child, int index=-1) override
Add a child ViewController.
Definition: view_manager.cpp:283
rviz::ViewController::update
virtual void update(float dt, float ros_dt)
Called at 30Hz by ViewManager::update() while this view is active. Override with code that needs to r...
Definition: view_controller.h:86
rviz::Property::getViewFlags
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: property.cpp:290
rviz::Config::listChildAt
Config listChildAt(int i) const
Return the i'th child in the list, if the referenced Node has type List. Returns an Invalid Config if...
Definition: config.cpp:329
rviz::ViewController::save
void save(Config config) const override
Write the value of this property and/or its children into the given Config reference.
Definition: view_controller.cpp:197
rviz::ViewController
Definition: view_controller.h:55
rviz::ViewManager::context_
DisplayContext * context_
Definition: view_manager.h:145
rviz::ViewManager::save
void save(Config config) const
Definition: view_manager.cpp:251
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::ViewManager::take
ViewController * take(ViewController *view)
Remove the given ViewController from the list and return it. If it is not in the list,...
Definition: view_manager.cpp:203
rviz::ViewManager::setCurrentFrom
void setCurrentFrom(ViewController *view_to_copy)
Make a copy of view_to_copy and install that as the new current ViewController.
Definition: view_manager.cpp:95
rviz::ViewManager::initialize
void initialize()
Definition: view_manager.cpp:64
rviz::ViewManager::setCurrentViewControllerType
void setCurrentViewControllerType(const QString &new_class_id)
Create a new view controller of the given type and set it up to mimic and replace the previous curren...
Definition: view_manager.cpp:152
view_controller.h
rviz::ViewManager::copy
ViewController * copy(ViewController *source)
Return a copy of source, made by saving source to a Config and instantiating and loading a new one fr...
Definition: view_manager.cpp:262
rviz::ViewManager::onCurrentDestroyed
void onCurrentDestroyed(QObject *obj)
Definition: view_manager.cpp:112
rviz::ViewManager::setRenderPanel
void setRenderPanel(RenderPanel *render_panel)
Set the 3D view widget whose view will be controlled by ViewController instances from by this ViewMan...
Definition: view_manager.cpp:273
display_context.h
rviz::ViewManager::getNumViews
int getNumViews() const
Definition: view_manager.cpp:177
rviz::RenderPanel
Definition: render_panel.h:74
rviz::Property::removeChildren
virtual void removeChildren(int start_index=0, int count=-1)
Remove and delete some or all child Properties. Does not change the value of this Property.
Definition: property.cpp:104
rviz::ViewController::getClassId
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
Definition: view_controller.h:155
rviz::ViewManager::add
void add(ViewController *view, int index=-1)
Definition: view_manager.cpp:190
config
config
rviz::ViewManager::property_model_
PropertyTreeModel * property_model_
Definition: view_manager.h:147
rviz::ViewManager::configChanged
void configChanged()
rviz::Config
Configuration data storage class.
Definition: config.h:124
rviz::PropertyTreeModel::setDragDropClass
void setDragDropClass(const QString &drag_drop_class)
Definition: property_tree_model.h:53
enum_property.h
rviz::ViewController::mimic
virtual void mimic(ViewController *source_view)
Configure the settings of this view controller to give, as much as possible, a similar view as that g...
Definition: view_controller.h:125
rviz::ViewManager::create
ViewController * create(const QString &type)
Definition: view_manager.cpp:77


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