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 {
44 {
45 }
46 
48 {
50 }
51 
52 Qt::ItemFlags DisplayGroup::getViewFlags(int column) const
53 {
54  return Display::getViewFlags(column) | Qt::ItemIsDropEnabled;
55 }
56 
57 void DisplayGroup::load(const Config& config)
58 {
59  removeAllDisplays(); // Only remove Display children, property children must stay.
60 
61  // Load Property values, plus name and enabled/disabled.
62  Display::load(config);
63 
64  // Now load Displays.
65  Config display_list_config = config.mapGetChild("Displays");
66  int num_displays = display_list_config.listLength();
67 
68  if (num_displays == 0)
69  return;
70 
71  if (model_)
72  {
73  model_->beginInsert(this, Display::numChildren(), num_displays);
74  }
75 
76  std::map<Display*, Config> display_config_map;
77 
78  // The following two-step loading procedure was motivated by the
79  // 'display group visibility' property, which needs all other displays
80  // to be created and named before it can load its settings.
81 
82  // hersh says: Is this really necessary? Can't we make
83  // DisplayGroupVisibilityProperty self-sufficient in this regard?
84  // Also, does it really work? What about saving and loading a
85  // hierarchy of Displays, will they really all have the right
86  // visibility settings?
87 
88  // first, create all displays and set their names
89  for (int i = 0; i < num_displays; i++)
90  {
91  Config display_config = display_list_config.listChildAt(i);
92  QString display_class = "(no class name found)";
93  display_config.mapGetString("Class", &display_class);
94  Display* disp = createDisplay(display_class);
96  QString display_name;
97  display_config.mapGetString("Name", &display_name);
98  disp->setObjectName(display_name);
99 
100  display_config_map[disp] = display_config;
101  }
102 
103  // now, initialize all displays and load their properties.
104  for (std::map<Display*, Config>::iterator it = display_config_map.begin();
105  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_.empty())
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(nullptr); // prevent child destructor from calling getParent()->takeChild().
170  child->setModel(nullptr);
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 = nullptr;
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(nullptr);
196  result->setModel(nullptr);
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 nullptr;
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(nullptr);
330  child->setParent(nullptr);
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
void fixedFrameChanged() override
Update the fixed frame in all contained displays.
int numChildren() const override
Return the number of child objects (Property and Display).
void load(const Config &config) override
Load subproperties and the list of displays in this group from the given Config node, which must be a map.
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz...
Definition: display.h:287
PropertyTreeModel * model_
Pointer to the PropertyTreeModel managing this property tree.
Definition: property.h:499
bool child_indexes_valid_
True if row_number_within_parent_ of all children is valid, false if not.
Definition: property.h:506
virtual Display * takeDisplay(Display *child)
Remove a child Display from the the list of Displays, but don&#39;t destroy it.
A single element of a property tree, with a name, value, description, and possibly children...
Definition: property.h:100
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 childListChanged(Property *this_property)
Emitted after insertions and deletions of child Properties.
virtual void onEnableChanged()
Definition: display.cpp:303
virtual int numDisplays() const
Return the number of child Displays.
void load(const Config &config) override
Load the settings for this display from the given Config node, which must be a map.
Definition: display.cpp:228
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
void update(float wall_dt, float ros_dt) override
Call update() on all child Displays.
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:293
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.
void displayAdded(rviz::Display *display)
Display * createDisplay(const QString &class_id)
Configuration data storage class.
Definition: config.h:124
QString fixed_frame_
A convenience variable equal to context_->getFixedFrame().
Definition: display.h:312
Property * takeChildAt(int index) override
Take a child out of the child list, but don&#39;t destroy it.
Property * childAtUnchecked(int index) const override
Return the child with the given index, without checking whether the index is within bounds...
void setParent(Property *new_parent)
Set parent property, without telling the parent.
Definition: property.cpp:236
void addChild(Property *child, int index=-1) override
Add a child Property or Display.
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:287
void setModel(PropertyTreeModel *model)
Set the model managing this Property and all its child properties, recursively.
Definition: property.cpp:393
QList< Display * > displays_
void displayRemoved(rviz::Display *display)
virtual Display * getDisplayAt(int index) const
Return the index-th Display in this group, or NULL if the index is invalid.
Config mapMakeChild(const QString &key)
Create a child node stored with the given key, and return the child.
Definition: config.cpp:201
Qt::ItemFlags getViewFlags(int column) const override
Return item flags appropriate for the given column (0 or 1) for this Display.
Definition: display.cpp:170
virtual void removeAllDisplays()
Remove and destroy all child Displays, but preserve any non-Display children.
virtual Property * takeChildAt(int index)
Take a child out of the child list, but don&#39;t destroy it.
Definition: property.cpp:333
virtual int numChildren() const
Return the number of child objects (Property or otherwise).
Definition: property.h:227
void reset() override
Reset this and all child Displays.
bool isEnabled() const
Return true if this Display is enabled, false if not.
Definition: display.cpp:268
virtual Type * make(const QString &class_id, QString *error_return=nullptr)
Instantiate and return a instance of a subclass of Type using makeRaw().
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:213
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
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
void onEnableChanged() override
virtual void addChild(Property *child, int index=-1)
Add a child property.
Definition: property.cpp:355
virtual DisplayFactory * getDisplayFactory() const =0
Return a factory for creating Display subclasses based on a class id string.
Qt::ItemFlags getViewFlags(int column) const override
Return item flags appropriate for the given column (0 or 1) for this DisplayGroup.
void save(Config config) const override
Write this display to the given Config node.
Definition: display.cpp:246
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:83
virtual void update(float wall_dt, float ros_dt)
Called periodically by the visualization manager.
Definition: display.h:137
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.
~DisplayGroup() override
void save(Config config) const override
Save subproperties and the list of displays in this group to the given Config node.


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:24