display_group.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> // for debug-write printf
31 
32 #include <QColor>
33 
34 #include "rviz/display_context.h"
35 #include "rviz/display_factory.h"
36 #include "rviz/failed_display.h"
38 
39 #include "display_group.h"
40 
41 namespace rviz
42 {
43 
45 {
46 }
47 
49 {
51 }
52 
53 Qt::ItemFlags DisplayGroup::getViewFlags( int column ) const
54 {
55  return Display::getViewFlags( column ) | Qt::ItemIsDropEnabled;
56 }
57 
58 void DisplayGroup::load( const Config& config )
59 {
60  removeAllDisplays(); // Only remove Display children, property children must stay.
61 
62  // Load Property values, plus name and enabled/disabled.
63  Display::load( config );
64 
65  // Now load Displays.
66  Config display_list_config = config.mapGetChild( "Displays" );
67  int num_displays = display_list_config.listLength();
68 
69  if( num_displays == 0 )
70  return;
71 
72  if( model_ )
73  {
74  model_->beginInsert( this, Display::numChildren(), num_displays );
75  }
76 
77  std::map<Display*,Config> display_config_map;
78 
79  // The following two-step loading procedure was motivated by the
80  // 'display group visibility' property, which needs all other displays
81  // to be created and named before it can load its settings.
82 
83  // hersh says: Is this really necessary? Can't we make
84  // DisplayGroupVisibilityProperty self-sufficient in this regard?
85  // Also, does it really work? What about saving and loading a
86  // hierarchy of Displays, will they really all have the right
87  // visibility settings?
88 
89  // first, create all displays and set their names
90  for( int i = 0; i < num_displays; i++ )
91  {
92  Config display_config = display_list_config.listChildAt( i );
93  QString display_class = "(no class name found)";
94  display_config.mapGetString( "Class", &display_class );
95  Display* disp = createDisplay( display_class );
97  QString display_name;
98  display_config.mapGetString( "Name", &display_name );
99  disp->setObjectName( display_name );
100 
101  display_config_map[ disp ] = display_config;
102  }
103 
104  // now, initialize all displays and load their properties.
105  for( std::map<Display*,Config>::iterator it = display_config_map.begin(); it != display_config_map.end(); ++it )
106  {
107  Config display_config = it->second;
108  Display* disp = it->first;
109  disp->initialize( context_ );
110  disp->load( display_config );
111  }
112 
113  if( model_ )
114  {
115  model_->endInsert();
116  }
117 }
118 
119 Display* DisplayGroup::createDisplay( const QString& class_id )
120 {
122  QString error;
123  Display* disp = factory->make( class_id, &error );
124  if( !disp )
125  {
126  return new FailedDisplay( class_id, error );
127  }
128  return disp;
129 }
130 
132 {
134  for( int i = displays_.size() - 1; i >= 0; i-- )
135  {
136  displays_[ i ]->onEnableChanged();
137  }
138 }
139 
140 void DisplayGroup::save( Config config ) const
141 {
142  Display::save( config );
143 
144  // Save Displays in a sequence under the key "Displays".
145  Config display_list_config = config.mapMakeChild( "Displays" );
146 
147  int num_displays = displays_.size();
148  for( int i = 0; i < num_displays; i++ )
149  {
150  displays_.at( i )->save( display_list_config.listAppendNew() );
151  }
152 }
153 
155 {
156  if(displays_.size() == 0)
157  return;
158 
159  int num_non_display_children = Display::numChildren();
160 
161  if( model_ )
162  {
163  model_->beginRemove( this, num_non_display_children, displays_.size() );
164  }
165  for( int i = displays_.size() - 1; i >= 0; i-- )
166  {
167  Display* child = displays_.takeAt( i );
168  Q_EMIT displayRemoved( child );
169  child->setParent( NULL ); // prevent child destructor from calling getParent()->takeChild().
170  child->setModel( NULL );
171  child_indexes_valid_ = false;
172  delete child;
173  }
174  if( model_ )
175  {
176  model_->endRemove();
177  }
178  Q_EMIT childListChanged( this );
179 }
180 
182 {
183  Display* result = NULL;
184  int num_displays = displays_.size();
185  for( int i = 0; i < num_displays; i++ )
186  {
187  if( displays_.at( i ) == child )
188  {
189  if( model_ )
190  {
191  model_->beginRemove( this, Display::numChildren() + i, 1 );
192  }
193  result = displays_.takeAt( i );
194  Q_EMIT displayRemoved( result );
195  result->setParent( NULL );
196  result->setModel( NULL );
197  child_indexes_valid_ = false;
198  if( model_ )
199  {
200  model_->endRemove();
201  }
202  Q_EMIT childListChanged( this );
203  break;
204  }
205  }
206  return result;
207 }
208 
210 {
211  if( 0 <= index && index < displays_.size() )
212  {
213  return displays_.at( index );
214  }
215  return NULL;
216 }
217 
219 {
220  return qobject_cast<DisplayGroup*>( getDisplayAt( index ));
221 }
222 
224 {
225  int num_children = displays_.size();
226  for( int i = 0; i < num_children; i++ )
227  {
228  displays_.at( i )->setFixedFrame( fixed_frame_ );
229  }
230 }
231 
232 void DisplayGroup::update( float wall_dt, float ros_dt )
233 {
234  int num_children = displays_.size();
235  for( int i = 0; i < num_children; i++ )
236  {
237  Display* display = displays_.at( i );
238  if( display->isEnabled() )
239  {
240  display->update( wall_dt, ros_dt );
241  }
242  }
243 }
244 
246 {
247  Display::reset();
248 
249  int num_children = displays_.size();
250  for( int i = 0; i < num_children; i++ )
251  {
252  displays_.at( i )->reset();
253  }
254 }
255 
257 {
258 // printf(" displaygroup4 displays_.append( child )\n" );
259  displays_.append( child );
260  child_indexes_valid_ = false;
261  child->setModel( model_ );
262  child->setParent( this );
263  Q_EMIT displayAdded( child );
264 }
265 
267 {
268  if( model_ )
269  {
270  model_->beginInsert( this, numChildren(), 1 );
271  }
273  if( model_ )
274  {
275  model_->endInsert();
276  }
277  Q_EMIT childListChanged( this );
278 }
279 
280 void DisplayGroup::addChild( Property* child, int index )
281 {
282  Display* display = qobject_cast<Display*>( child );
283  if( !display )
284  {
285  Display::addChild( child, index );
286  return;
287  }
288  if( index < 0 || index > numChildren() )
289  {
290  index = numChildren();
291  }
292  int disp_index = index - Display::numChildren();
293  if( disp_index < 0 )
294  {
295  disp_index = 0;
296  }
297  if( model_ )
298  {
299  model_->beginInsert( this, index );
300  }
301 
302  displays_.insert( disp_index, display );
303  Q_EMIT displayAdded( display );
304  child_indexes_valid_ = false;
305  display->setModel( model_ );
306  display->setParent( this );
307 
308  if( model_ )
309  {
310  model_->endInsert();
311  }
312  Q_EMIT childListChanged( this );
313 }
314 
316 {
317  if( index < Display::numChildren() )
318  {
319  return Display::takeChildAt( index );
320  }
321  int disp_index = index - Display::numChildren();
322  if( model_ )
323  {
324  model_->beginRemove( this, index, 1 );
325  }
326 // printf(" displaygroup5 displays_.takeAt( %d ) ( index = %d )\n", disp_index, index );
327  Display* child = displays_.takeAt( disp_index );
328  Q_EMIT displayRemoved( child );
329  child->setModel( NULL );
330  child->setParent( NULL );
331  child_indexes_valid_ = false;
332  if( model_ )
333  {
334  model_->endRemove();
335  }
336  Q_EMIT childListChanged( this );
337  return child;
338 }
339 
341 {
342  return displays_.size();
343 }
344 
346 {
347  return Display::numChildren() + displays_.size();
348 }
349 
351 {
352  int first_child_count = Display::numChildren();
353  if( index < first_child_count )
354  {
355  return Display::childAtUnchecked( index );
356  }
357  index -= first_child_count;
358  return displays_.at( index );
359 }
360 
361 } // end namespace rviz
virtual void save(Config config) const
Save subproperties and the list of displays in this group to the given Config node.
#define NULL
Definition: global.h:37
virtual void fixedFrameChanged()
Update the fixed frame in all contained displays.
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz...
Definition: display.h:256
PropertyTreeModel * model_
Pointer to the PropertyTreeModel managing this property tree.
Definition: property.h:454
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
bool child_indexes_valid_
True if row_number_within_parent_ of all children is valid, false if not.
Definition: property.h:461
virtual Display * takeDisplay(Display *child)
Remove a child Display from the the list of Displays, but don&#39;t destroy it.
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
virtual void load(const Config &config)
Load subproperties and the list of displays in this group from the given Config node, which must be a map.
void childListChanged(Property *this_property)
Emitted after insertions and deletions of child Properties.
virtual void onEnableChanged()
Definition: display.cpp:305
virtual int numDisplays() const
Return the number of child Displays.
virtual Property * childAtUnchecked(int index) const
Return the child with the given index, without checking whether the index is within bounds...
void beginRemove(Property *parent_property, int row_within_parent, int count=1)
A FailedDisplay instance represents a Display class we tried and failed to instantiate.
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
void displayAdded(rviz::Display *display)
Display * createDisplay(const QString &class_id)
bool isEnabled() const
Return true if this Display is enabled, false if not.
Definition: display.cpp:281
Configuration data storage class.
Definition: config.h:125
QString fixed_frame_
A convenience variable equal to context_->getFixedFrame().
Definition: display.h:281
virtual int numChildren() const
Return the number of child objects (Property and Display).
void setParent(Property *new_parent)
Set parent property, without telling the parent.
Definition: property.cpp:231
A Display object which stores other Displays as children.
Definition: display_group.h:47
virtual void reset()
Called to tell the display to clear its state.
Definition: display.cpp:300
virtual DisplayGroup * getGroupAt(int index) const
Find the index-th child Display in this group. If the child is itself a DisplayGroup, return the pointer to it. If it is not, return NULL.
void setModel(PropertyTreeModel *model)
Set the model managing this Property and all its child properties, recursively.
Definition: property.cpp:379
QList< Display * > displays_
void displayRemoved(rviz::Display *display)
Config mapMakeChild(const QString &key)
Create a child node stored with the given key, and return the child.
Definition: config.cpp:190
virtual void save(Config config) const
Write this display to the given Config node.
Definition: display.cpp:260
virtual void removeAllDisplays()
Remove and destroy all child Displays, but preserve any non-Display children.
virtual void reset()
Reset this and all child Displays.
virtual void update(float wall_dt, float ros_dt)
Call update() on all child Displays.
virtual Display * getDisplayAt(int index) const
Return the index-th Display in this group, or NULL if the index is invalid.
virtual void onEnableChanged()
virtual Property * takeChildAt(int index)
Take a child out of the child list, but don&#39;t destroy it.
Definition: property.cpp:328
virtual void load(const Config &config)
Load the settings for this display from the given Config node, which must be a map.
Definition: display.cpp:242
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
virtual DisplayFactory * getDisplayFactory() const =0
Return a factory for creating Display subclasses based on a class id string.
void beginInsert(Property *parent_property, int row_within_parent, int count=1)
void initialize(DisplayContext *context)
Main initialization, called after constructor, before load() or setEnabled().
Definition: display.cpp:84
virtual void update(float wall_dt, float ros_dt)
Called periodically by the visualization manager.
Definition: display.h:131
virtual Type * make(const QString &class_id, QString *error_return=NULL)
Instantiate and return a instance of a subclass of Type using makeRaw().
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
virtual void addDisplay(Display *child)
Add a child Display to the end of the list of Displays.
virtual void addDisplayWithoutSignallingModel(Display *child)
Add a child Display to the end of the list of Displays, but without telling the model.
virtual Property * takeChildAt(int index)
Take a child out of the child list, but don&#39;t destroy it.
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Display.
Definition: display.cpp:181
virtual void addChild(Property *child, int index=-1)
Add a child Property or Display.
virtual Property * childAtUnchecked(int index) const
Return the child Property with the given index, without checking whether the index is within bounds...
Definition: property.cpp:208
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this DisplayGroup.


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