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 {
45 
47  : context_( context )
48  , root_property_( new ViewControllerContainer )
49  , property_model_( new PropertyTreeModel( root_property_ ))
50  , factory_( new PluginlibFactory<ViewController>( "rviz", "rviz::ViewController" ))
51  , current_( NULL )
52  , render_panel_( NULL )
53 {
54  property_model_->setDragDropClass( "view-controller" );
55  connect( property_model_, SIGNAL( configChanged() ), 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 == NULL )
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_ = NULL;
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  Q_EMIT currentChanged();
149 }
150 
151 void ViewManager::setCurrentViewControllerType( const QString& new_class_id )
152 {
153  setCurrent( create( new_class_id ), true );
154 }
155 
157 {
158  ViewController* current = getCurrent();
159  if( current )
160  {
161  ViewController* new_copy = copy( current );
162  new_copy->setName( factory_->getClassName( new_copy->getClassId() ));
163  root_property_->addChild( new_copy );
164  }
165 }
166 
168 {
169  if( index < 0 )
170  {
171  index = 0;
172  }
173  return qobject_cast<ViewController*>( root_property_->childAt( index + 1 ));
174 }
175 
177 {
178  int count = root_property_->numChildren();
179  if( count <= 0 )
180  {
181  return 0;
182  }
183  else
184  {
185  return count-1;
186  }
187 }
188 
189 void ViewManager::add( ViewController* view, int index )
190 {
191  if( index < 0 )
192  {
193  index = root_property_->numChildren();
194  }
195  else
196  {
197  index++;
198  }
199  property_model_->getRoot()->addChild( view, index );
200 }
201 
203 {
204  for( int i = 0; i < getNumViews(); i++ )
205  {
206  if( getViewAt( i ) == view )
207  {
208  return qobject_cast<ViewController*>( root_property_->takeChildAt( i + 1 ));
209  }
210  }
211  return NULL;
212 }
213 
215 {
216  if( index < 0 )
217  {
218  return NULL;
219  }
220  return qobject_cast<ViewController*>( root_property_->takeChildAt( index + 1 ));
221 }
222 
223 void ViewManager::load( const Config& config )
224 {
225  Config current_config = config.mapGetChild( "Current" );
226  QString class_id;
227  if( current_config.mapGetString( "Class", &class_id ))
228  {
229  ViewController* new_current = create( class_id );
230  new_current->load( current_config );
231  setCurrent( new_current, false );
232  }
233 
234  Config saved_views_config = config.mapGetChild( "Saved" );
236  int num_saved = saved_views_config.listLength();
237  for( int i = 0; i < num_saved; i++ )
238  {
239  Config view_config = saved_views_config.listChildAt( i );
240 
241  if( view_config.mapGetString( "Class", &class_id ))
242  {
243  ViewController* view = create( class_id );
244  view->load( view_config );
245  add( view );
246  }
247  }
248 }
249 
250 void ViewManager::save( Config config ) const
251 {
252  getCurrent()->save( config.mapMakeChild( "Current" ));
253 
254  Config saved_views_config = config.mapMakeChild( "Saved" );
255  for( int i = 0; i < getNumViews(); i++ )
256  {
257  getViewAt( i )->save( saved_views_config.listAppendNew() );
258  }
259 }
260 
262 {
263  Config config;
264  source->save( config );
265 
266  ViewController* copy_of_source = create( source->getClassId() );
267  copy_of_source->load( config );
268 
269  return copy_of_source;
270 }
271 
273 {
274  render_panel_ = render_panel;
275 }
276 
277 Qt::ItemFlags ViewControllerContainer::getViewFlags( int column ) const
278 {
279  return Property::getViewFlags( column ) | Qt::ItemIsDropEnabled;
280 }
281 
283 {
284  if( index == 0 )
285  {
286  index = 1;
287  }
288  Property::addChild( child, index );
289 }
290 
292 {
293  Property::addChild( child, 0 );
294 }
295 
296 } // 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...
virtual void load(const Config &config)
Load the value of this property and/or its children from the given Config reference.
int getNumViews() const
#define NULL
Definition: global.h:37
RenderPanel * render_panel_
Definition: view_manager.h:138
PluginlibFactory< ViewController > * factory_
Definition: view_manager.h:136
ViewController * getCurrent() const
Return the current ViewController in use for the main RenderWindow.
void setCurrent(ViewController *new_current, bool mimic_view)
Set new_current as current.
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.
Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Container property for ViewControllers which gets the drag/drop right for the funky way Current-View ...
Definition: view_manager.h:144
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:317
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:322
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:150
virtual void save(Config config) const
Write the value of this property and/or its children into the given Config reference.
void setViewController(ViewController *controller)
Set the ViewController which should control the camera position for this view.
ViewController * copy(ViewController *source)
Return a copy of source, made by saving source to a Config and instantiating and loading a new one fr...
virtual void addChild(Property *child, int index=-1)
Add a child ViewController.
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)
ViewController * getViewAt(int index) const
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 int numChildren() const
Return the number of child objects (Property or otherwise).
Definition: property.h:215
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:281
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
Configuration data storage class.
Definition: config.h:125
PropertyTreeModel * property_model_
Definition: view_manager.h:135
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:100
Pure-virtual base class for objects which give Display subclasses context in which to work...
ViewController * current_
Definition: view_manager.h:137
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:197
ViewManager(DisplayContext *context)
void copyCurrentToList()
Make a copy of the current ViewController and add it to the end of the list of saved views...
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:190
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:134
DisplayContext * context_
Definition: view_manager.h:133
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:328
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.
Config listAppendNew()
Ensure the referenced Node is of type List, append a new Empty Node to the end of the list...
Definition: config.cpp:334
virtual void addChild(Property *child, int index=-1)
Add a child property.
Definition: property.cpp:350
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:201
void initialize(DisplayContext *context)
Do all setup that can&#39;t be done in the constructor.
void save(Config config) const
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...
Property * getRoot() const
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: property.cpp:285
void load(const Config &config)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51