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");
54  connect(property_model_, SIGNAL(configChanged()), this, SIGNAL(configChanged()));
55  connect(this, SIGNAL(currentChanged()), this, SIGNAL(configChanged()));
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, SIGNAL(destroyed(QObject*)), this, SLOT(onCurrentDestroyed(QObject*)));
134  }
135  new_current->setName("Current View");
136  connect(new_current, SIGNAL(destroyed(QObject*)), this, SLOT(onCurrentDestroyed(QObject*)));
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
ViewController * takeAt(int index)
Remove the ViewController at the given index from the list and return it. If the index is not valid...
RenderPanel * render_panel_
Definition: view_manager.h:150
~ViewManager() override
ViewController * getCurrent() const
Return the current ViewController in use for the main RenderWindow.
PluginlibFactory< ViewController > * factory_
Definition: view_manager.h:148
void setCurrent(ViewController *new_current, bool mimic_view)
Set new_current as current.
ViewController * getViewAt(int index) const
void setCurrentFrom(ViewController *view_to_copy)
Make a copy of view_to_copy and install that as the new current ViewController.
virtual void transitionFrom(ViewController *previous_view)
Called by ViewManager when this ViewController is being made current.
void currentChanged()
Emitted just after the current view controller changes.
Container property for ViewControllers which gets the drag/drop right for the funky way Current-View ...
Definition: view_manager.h:156
A single element of a property tree, with a name, value, description, and possibly children...
Definition: property.h:100
config
virtual void setName(const QString &name)
Set the name.
Definition: property.cpp:155
void setViewController(ViewController *controller)
Set the ViewController which should control the camera position for this view.
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:329
Config mapGetChild(const QString &key) const
If the referenced Node is a Map and it has a child with the given key, return a reference to the chil...
Definition: config.cpp:212
Qt::ItemFlags getViewFlags(int column) const override
Return item flags appropriate for the given column (0 or 1) for this Property.
ViewController * copy(ViewController *source)
Return a copy of source, made by saving source to a Config and instantiating and loading a new one fr...
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:293
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...
void addChildToFront(Property *child)
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...
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
void save(Config config) const override
Write the value of this property and/or its children into the given Config reference.
void addChild(Property *child, int index=-1) override
Add a child ViewController.
int getNumViews() const
Configuration data storage class.
Definition: config.h:124
PropertyTreeModel * property_model_
Definition: view_manager.h:147
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
Pure-virtual base class for objects which give Display subclasses context in which to work...
ViewController * current_
Definition: view_manager.h:149
ViewManager(DisplayContext *context)
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: property.cpp:289
void copyCurrentToList()
Make a copy of the current ViewController and add it to the end of the list of saved views...
Property * getRoot() const
void onCurrentDestroyed(QObject *obj)
void setRenderPanel(RenderPanel *render_panel)
Set the 3D view widget whose view will be controlled by ViewController instances from by this ViewMan...
Config mapMakeChild(const QString &key)
Create a child node stored with the given key, and return the child.
Definition: config.cpp:201
void add(ViewController *view, int index=-1)
void update(float wall_dt, float ros_dt)
A FailedViewController instance represents a ViewController class we tried and failed to instantiate...
ViewControllerContainer * root_property_
Definition: view_manager.h:146
void load(const Config &config) override
Load the value of this property and/or its children from the given Config reference.
DisplayContext * context_
Definition: view_manager.h:145
ViewController * create(const QString &type)
virtual Property * takeChildAt(int index)
Take a child out of the child list, but don&#39;t destroy it.
Definition: property.cpp:333
void setDragDropClass(const QString &drag_drop_class)
ViewController * take(ViewController *view)
Remove the given ViewController from the list and return it. If it is not in the list, NULL is returned and nothing changes.
virtual int numChildren() const
Return the number of child objects (Property or otherwise).
Definition: property.h:227
Config listAppendNew()
Ensure the referenced Node is of type List, append a new Empty Node to the end of the list...
Definition: config.cpp:346
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
Config listChildAt(int i) const
Return the i&#39;th child in the list, if the referenced Node has type List. Returns an Invalid Config if...
Definition: config.cpp:334
virtual void addChild(Property *child, int index=-1)
Add a child property.
Definition: property.cpp:355
void initialize(DisplayContext *context)
Do all setup that can&#39;t be done in the constructor.
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...
void save(Config config) const
void load(const Config &config)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Aug 15 2022 02:05:17