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.
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 {
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  Display* display = displays_.at(i);
151  if (display->shouldBeSaved())
152  display->save(display_list_config.listAppendNew());
153  }
154 }
155 
157 {
158  if (displays_.empty())
159  return;
160 
161  int num_non_display_children = Display::numChildren();
162 
163  if (model_)
164  {
165  model_->beginRemove(this, num_non_display_children, displays_.size());
166  }
167  for (int i = displays_.size() - 1; i >= 0; i--)
168  {
169  Display* child = displays_.takeAt(i);
170  Q_EMIT displayRemoved(child);
171  child->setParent(nullptr); // prevent child destructor from calling getParent()->takeChild().
172  child->setModel(nullptr);
173  child_indexes_valid_ = false;
174  delete child;
175  }
176  if (model_)
177  {
178  model_->endRemove();
179  }
180  Q_EMIT childListChanged(this);
181 }
182 
184 {
185  Display* result = nullptr;
186  int num_displays = displays_.size();
187  for (int i = 0; i < num_displays; i++)
188  {
189  if (displays_.at(i) == child)
190  {
191  if (model_)
192  {
193  model_->beginRemove(this, Display::numChildren() + i, 1);
194  }
195  result = displays_.takeAt(i);
196  Q_EMIT displayRemoved(result);
197  result->setParent(nullptr);
198  result->setModel(nullptr);
199  child_indexes_valid_ = false;
200  if (model_)
201  {
202  model_->endRemove();
203  }
204  Q_EMIT childListChanged(this);
205  break;
206  }
207  }
208  return result;
209 }
210 
212 {
213  if (0 <= index && index < displays_.size())
214  {
215  return displays_.at(index);
216  }
217  return nullptr;
218 }
219 
221 {
222  return qobject_cast<DisplayGroup*>(getDisplayAt(index));
223 }
224 
226 {
227  int num_children = displays_.size();
228  for (int i = 0; i < num_children; i++)
229  {
230  displays_.at(i)->setFixedFrame(fixed_frame_);
231  }
232 }
233 
234 void DisplayGroup::update(float wall_dt, float ros_dt)
235 {
236  int num_children = displays_.size();
237  for (int i = 0; i < num_children; i++)
238  {
239  Display* display = displays_.at(i);
240  if (display->isEnabled())
241  {
242  display->update(wall_dt, ros_dt);
243  }
244  }
245 }
246 
248 {
249  Display::reset();
250 
251  int num_children = displays_.size();
252  for (int i = 0; i < num_children; i++)
253  {
254  displays_.at(i)->reset();
255  }
256 }
257 
259 {
260  // printf(" displaygroup4 displays_.append( child )\n" );
261  displays_.append(child);
262  child_indexes_valid_ = false;
263  child->setModel(model_);
264  child->setParent(this);
265  Q_EMIT displayAdded(child);
266 }
267 
269 {
270  if (model_)
271  {
272  model_->beginInsert(this, numChildren(), 1);
273  }
275  if (model_)
276  {
277  model_->endInsert();
278  }
279  Q_EMIT childListChanged(this);
280 }
281 
282 void DisplayGroup::addChild(Property* child, int index)
283 {
284  Display* display = qobject_cast<Display*>(child);
285  if (!display)
286  {
287  Display::addChild(child, index);
288  return;
289  }
290  if (index < 0 || index > numChildren())
291  {
292  index = numChildren();
293  }
294  int disp_index = index - Display::numChildren();
295  if (disp_index < 0)
296  {
297  disp_index = 0;
298  }
299  if (model_)
300  {
301  model_->beginInsert(this, index);
302  }
303 
304  displays_.insert(disp_index, display);
305  Q_EMIT displayAdded(display);
306  child_indexes_valid_ = false;
307  display->setModel(model_);
308  display->setParent(this);
309 
310  if (model_)
311  {
312  model_->endInsert();
313  }
314  Q_EMIT childListChanged(this);
315 }
316 
318 {
319  if (index < Display::numChildren())
320  {
321  return Display::takeChildAt(index);
322  }
323  int disp_index = index - Display::numChildren();
324  if (model_)
325  {
326  model_->beginRemove(this, index, 1);
327  }
328  // printf(" displaygroup5 displays_.takeAt( %d ) ( index = %d )\n", disp_index, index );
329  Display* child = displays_.takeAt(disp_index);
330  Q_EMIT displayRemoved(child);
331  child->setModel(nullptr);
332  child->setParent(nullptr);
333  child_indexes_valid_ = false;
334  if (model_)
335  {
336  model_->endRemove();
337  }
338  Q_EMIT childListChanged(this);
339  return child;
340 }
341 
343 {
344  return displays_.size();
345 }
346 
348 {
349  return Display::numChildren() + displays_.size();
350 }
351 
353 {
354  int first_child_count = Display::numChildren();
355  if (index < first_child_count)
356  {
357  return Display::childAtUnchecked(index);
358  }
359  index -= first_child_count;
360  return displays_.at(index);
361 }
362 
363 } // end namespace rviz
rviz::DisplayGroup::displayAdded
void displayAdded(rviz::Display *display)
rviz::DisplayGroup::reset
void reset() override
Reset this and all child Displays.
Definition: display_group.cpp:247
rviz::Display::isEnabled
bool isEnabled() const
Return true if this Display is enabled, false if not.
Definition: display.cpp:271
rviz::DisplayGroup::removeAllDisplays
virtual void removeAllDisplays()
Remove and destroy all child Displays, but preserve any non-Display children.
Definition: display_group.cpp:156
rviz::DisplayGroup::save
void save(Config config) const override
Save subproperties and the list of displays in this group to the given Config node.
Definition: display_group.cpp:140
rviz::Display::initialize
void initialize(DisplayContext *context)
Main initialization, called after constructor, before load() or setEnabled().
Definition: display.cpp:84
display_factory.h
rviz::PropertyTreeModel::endInsert
void endInsert()
Definition: property_tree_model.cpp:314
rviz::DisplayGroup::getGroupAt
virtual DisplayGroup * getGroupAt(int index) const
Find the index-th child Display in this group. If the child is itself a DisplayGroup,...
Definition: display_group.cpp:220
rviz::FailedDisplay
A FailedDisplay instance represents a Display class we tried and failed to instantiate.
Definition: failed_display.h:46
rviz::DisplayGroup::displays_
QList< Display * > displays_
Definition: display_group.h:156
rviz::DisplayGroup::getDisplayAt
virtual Display * getDisplayAt(int index) const
Return the index-th Display in this group, or NULL if the index is invalid.
Definition: display_group.cpp:211
rviz::Property::addChild
virtual void addChild(Property *child, int index=-1)
Add a child property.
Definition: property.cpp:356
rviz::DisplayContext::getDisplayFactory
virtual DisplayFactory * getDisplayFactory() const =0
Return a factory for creating Display subclasses based on a class id string.
rviz::Display::save
void save(Config config) const override
Write this display to the given Config node.
Definition: display.cpp:249
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::shouldBeSaved
bool shouldBeSaved() const
Returns true if the property has data worth saving.
Definition: property.h:441
rviz::Property::numChildren
virtual int numChildren() const
Return the number of child objects (Property or otherwise).
Definition: property.h:287
rviz::Display::fixed_frame_
QString fixed_frame_
A convenience variable equal to context_->getFixedFrame().
Definition: display.h:312
rviz::DisplayGroup::addChild
void addChild(Property *child, int index=-1) override
Add a child Property or Display.
Definition: display_group.cpp:282
failed_display.h
rviz::DisplayGroup::createDisplay
Display * createDisplay(const QString &class_id)
Definition: display_group.cpp:119
rviz::DisplayGroup::update
void update(float wall_dt, float ros_dt) override
Call update() on all child Displays.
Definition: display_group.cpp:234
rviz::Display
Definition: display.h:63
rviz::Property
A single element of a property tree, with a name, value, description, and possibly children.
Definition: property.h:100
rviz::DisplayGroup::childAtUnchecked
Property * childAtUnchecked(int index) const override
Return the child with the given index, without checking whether the index is within bounds.
Definition: display_group.cpp:352
rviz::DisplayGroup::displayRemoved
void displayRemoved(rviz::Display *display)
rviz::DisplayGroup::~DisplayGroup
~DisplayGroup() override
Definition: display_group.cpp:47
rviz
Definition: add_display_dialog.cpp:54
rviz::PropertyTreeModel::endRemove
void endRemove()
Definition: property_tree_model.cpp:329
rviz::DisplayGroup::load
void load(const Config &config) override
Load subproperties and the list of displays in this group from the given Config node,...
Definition: display_group.cpp:57
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::Property::model_
PropertyTreeModel * model_
Pointer to the PropertyTreeModel managing this property tree.
Definition: property.h:560
rviz::DisplayGroup::takeDisplay
virtual Display * takeDisplay(Display *child)
Remove a child Display from the the list of Displays, but don't destroy it.
Definition: display_group.cpp:183
rviz::DisplayGroup::numDisplays
virtual int numDisplays() const
Return the number of child Displays.
Definition: display_group.cpp:342
rviz::Display::load
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:231
property_tree_model.h
display_group.h
rviz::ClassIdRecordingFactory::make
virtual Type * make(const QString &class_id, QString *error_return=nullptr)
Instantiate and return a instance of a subclass of Type using makeRaw().
Definition: class_id_recording_factory.h:55
rviz::Property::childListChanged
void childListChanged(Property *this_property)
Emitted after insertions and deletions of child Properties.
rviz::DisplayGroup::getViewFlags
Qt::ItemFlags getViewFlags(int column) const override
Return item flags appropriate for the given column (0 or 1) for this DisplayGroup.
Definition: display_group.cpp:52
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::PropertyTreeModel::beginInsert
void beginInsert(Property *parent_property, int row_within_parent, int count=1)
Definition: property_tree_model.cpp:305
rviz::Property::child_indexes_valid_
bool child_indexes_valid_
True if row_number_within_parent_ of all children is valid, false if not.
Definition: property.h:567
rviz::DisplayGroup::addDisplay
virtual void addDisplay(Display *child)
Add a child Display to the end of the list of Displays.
Definition: display_group.cpp:268
rviz::PropertyTreeModel::beginRemove
void beginRemove(Property *parent_property, int row_within_parent, int count=1)
Definition: property_tree_model.cpp:320
rviz::DisplayFactory
Definition: display_factory.h:42
rviz::DisplayGroup::onEnableChanged
void onEnableChanged() override
Definition: display_group.cpp:131
rviz::Display::onEnableChanged
virtual void onEnableChanged()
Definition: display.cpp:295
rviz::Display::getViewFlags
Qt::ItemFlags getViewFlags(int column) const override
Return item flags appropriate for the given column (0 or 1) for this Display.
Definition: display.cpp:171
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::Display::context_
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz....
Definition: display.h:287
rviz::DisplayGroup::addDisplayWithoutSignallingModel
virtual void addDisplayWithoutSignallingModel(Display *child)
Add a child Display to the end of the list of Displays, but without telling the model.
Definition: display_group.cpp:258
rviz::Property::setParent
void setParent(Property *new_parent)
Set parent property, without telling the parent.
Definition: property.cpp:236
rviz::DisplayGroup::DisplayGroup
DisplayGroup()
Definition: display_group.cpp:43
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::DisplayGroup::takeChildAt
Property * takeChildAt(int index) override
Take a child out of the child list, but don't destroy it.
Definition: display_group.cpp:317
rviz::Display::reset
virtual void reset()
Called to tell the display to clear its state.
Definition: display.cpp:290
display_context.h
rviz::Property::childAtUnchecked
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
config
rviz::DisplayGroup::fixedFrameChanged
void fixedFrameChanged() override
Update the fixed frame in all contained displays.
Definition: display_group.cpp:225
rviz::Config
Configuration data storage class.
Definition: config.h:124
rviz::DisplayGroup
A Display object which stores other Displays as children.
Definition: display_group.h:47
rviz::Display::update
virtual void update(float wall_dt, float ros_dt)
Called periodically by the visualization manager.
Definition: display.h:137
rviz::Property::setModel
void setModel(PropertyTreeModel *model)
Set the model managing this Property and all its child properties, recursively.
Definition: property.cpp:394
rviz::DisplayGroup::numChildren
int numChildren() const override
Return the number of child objects (Property and Display).
Definition: display_group.cpp:347


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Sat Jun 1 2024 02:31:52