display.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 <QApplication>
33 #include <QColor>
34 #include <QDockWidget>
35 #include <QFont>
36 #include <QMetaObject>
37 #include <QWidget>
38 
39 #include <OgreSceneManager.h>
40 #include <OgreSceneNode.h>
41 
42 #include "rviz/display_context.h"
47 #include "rviz/panel_dock_widget.h"
48 
49 #include "display.h"
50 
51 #include <boost/filesystem.hpp>
52 
53 namespace rviz
54 {
56  : context_(nullptr)
57  , scene_node_(nullptr)
58  , status_(nullptr)
59  , initialized_(false)
60  , visibility_bits_(0xFFFFFFFF)
61  , associated_widget_(nullptr)
62  , associated_widget_panel_(nullptr)
63 {
64  // Needed for timeSignal (see header) to work across threads
65  qRegisterMetaType<ros::Time>();
66 
67  // Make the display-enable checkbox show up, and make it unchecked by default.
68  setValue(false);
69 
70  connect(this, SIGNAL(changed()), this, SLOT(onEnableChanged()));
71 
73 }
74 
76 {
77  if (scene_node_)
78  {
79  scene_manager_->destroySceneNode(scene_node_);
80  }
81 }
82 
84 {
85  context_ = context;
87  scene_node_ = scene_manager_->getRootSceneNode()->createChildSceneNode();
88 
92 
93  onInitialize();
94 
95  initialized_ = true;
96 }
97 
99 {
100  if (context_)
101  {
103  }
104 }
105 
106 QVariant Display::getViewData(int column, int role) const
107 {
108  switch (role)
109  {
110  case Qt::ForegroundRole:
111  {
112  // if we're item-enabled (not greyed out) and in warn/error state, set appropriate color
113  if (getViewFlags(column) & Qt::ItemIsEnabled)
114  {
115  if (isEnabled())
116  {
118  {
120  }
121  else
122  {
123  // blue means that the enabled checkmark is set
124  return QColor(40, 120, 197);
125  }
126  }
127  else
128  {
129  return QApplication::palette().color(QPalette::Text);
130  }
131  }
132  break;
133  }
134  case Qt::FontRole:
135  {
136  QFont font;
137  if (isEnabled())
138  {
139  font.setBold(true);
140  }
141  return font;
142  }
143  case Qt::DecorationRole:
144  {
145  if (column == 0)
146  {
147  if (isEnabled())
148  {
150  switch (level)
151  {
152  case StatusProperty::Ok:
153  return getIcon();
156  return status_->statusIcon(status_->getLevel());
157  }
158  }
159  else
160  {
161  return getIcon();
162  }
163  }
164  break;
165  }
166  }
167  return BoolProperty::getViewData(column, role);
168 }
169 
170 Qt::ItemFlags Display::getViewFlags(int column) const
171 {
172  return BoolProperty::getViewFlags(column) | Qt::ItemIsDragEnabled;
173 }
174 
175 void Display::setStatus(StatusProperty::Level level, const QString& name, const QString& text)
176 {
177  QMetaObject::invokeMethod(this, "setStatusInternal", Qt::QueuedConnection, Q_ARG(int, level),
178  Q_ARG(QString, name), Q_ARG(QString, text));
179 }
180 
181 void Display::setStatusInternal(int level, const QString& name, const QString& text)
182 {
183  if (!status_)
184  {
185  status_ = new StatusList("Status");
186  addChild(status_, 0);
187  }
188  StatusProperty::Level old_level = status_->getLevel();
189 
190  status_->setStatus((StatusProperty::Level)level, name, text);
191  if (model_ && old_level != status_->getLevel())
192  {
193  model_->emitDataChanged(this);
194  }
195 }
196 
197 void Display::deleteStatus(const QString& name)
198 {
199  QMetaObject::invokeMethod(this, "deleteStatusInternal", Qt::QueuedConnection, Q_ARG(QString, name));
200 }
201 
202 void Display::deleteStatusInternal(const QString& name)
203 {
204  if (status_)
205  {
206  status_->deleteStatus(name);
207  }
208 }
209 
211 {
212  QMetaObject::invokeMethod(this, "clearStatusesInternal", Qt::QueuedConnection);
213 }
214 
216 {
217  if (status_)
218  {
219  StatusProperty::Level old_level = status_->getLevel();
220  status_->clear();
221  if (model_ && old_level != StatusProperty::Ok)
222  {
223  model_->emitDataChanged(this);
224  }
225  }
226 }
227 
228 void Display::load(const Config& config)
229 {
230  // Base class loads sub-properties.
231  BoolProperty::load(config);
232 
233  QString name;
234  if (config.mapGetString("Name", &name))
235  {
236  setObjectName(name);
237  }
238 
239  bool enabled;
240  if (config.mapGetBool("Enabled", &enabled))
241  {
242  setEnabled(enabled);
243  }
244 }
245 
246 void Display::save(Config config) const
247 {
248  // Base class saves sub-properties.
249  BoolProperty::save(config);
250 
251  config.mapSetValue("Class", getClassId());
252  config.mapSetValue("Name", getName());
253  config.mapSetValue("Enabled", getBool());
254 }
255 
256 void Display::setEnabled(bool enabled)
257 {
258  if (enabled == isEnabled())
259  return;
260  setValue(enabled);
261 }
262 
264 {
265  setEnabled(false);
266 }
267 
268 bool Display::isEnabled() const
269 {
270  return getBool() && (getViewFlags(0) & Qt::ItemIsEnabled);
271 }
272 
273 void Display::setFixedFrame(const QString& fixed_frame)
274 {
275  fixed_frame_ = fixed_frame;
276  if (initialized_)
277  {
279  }
280 }
281 
283 {
284  Q_EMIT(timeSignal(time, QPrivateSignal()));
285 }
286 
288 {
289  clearStatuses();
290 }
291 
292 static std::map<PanelDockWidget*, bool> associated_widgets_visibility;
293 inline void setVisible(PanelDockWidget* widget, bool visible)
294 {
295  associated_widgets_visibility[widget] = visible;
296 }
297 inline bool isVisible(PanelDockWidget* widget)
298 {
299  auto it = associated_widgets_visibility.find(widget);
300  return it != associated_widgets_visibility.end() && it->second;
301 }
302 
304 {
305  QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
306  queueRender();
307  /* We get here, by two different routes:
308  * - First, we might have disabled the display.
309  * In this case we want to close/hide the associated widget.
310  * But there is an exception: tabbed DockWidgets shouldn't be hidden, because then we would loose the
311  * tab.
312  * - Second, the corresponding widget changed visibility and we got here via
313  * associatedPanelVisibilityChange().
314  * In this case, it's usually counterproductive to show/hide the widget here.
315  * Typical cases are: main window was minimized/unminimized, tab was switched.
316  */
317  if (isEnabled())
318  {
319  scene_node_->setVisible(true);
320 
322  {
324  associated_widget_panel_->show();
325  }
326  else if (associated_widget_)
327  associated_widget_->show();
328 
329  if (isEnabled()) // status might have changed, e.g. if show() failed
330  onEnable();
331  }
332  else
333  {
334  onDisable();
335 
337  {
339  associated_widget_panel_->hide();
340  }
341  else if (associated_widget_)
342  associated_widget_->hide();
343 
344  scene_node_->setVisible(false);
345  }
346  QApplication::restoreOverrideCursor();
347 }
348 
349 void Display::setVisibilityBits(uint32_t bits)
350 {
351  visibility_bits_ |= bits;
353 }
354 
355 void Display::unsetVisibilityBits(uint32_t bits)
356 {
357  visibility_bits_ &= ~bits;
359 }
360 
361 void Display::setAssociatedWidget(QWidget* widget)
362 {
364  {
365  disconnect(associated_widget_panel_, SIGNAL(visibilityChanged(bool)), this,
366  SLOT(associatedPanelVisibilityChange(bool)));
367  disconnect(associated_widget_panel_, SIGNAL(closed()), this, SLOT(disable()));
368  }
369 
370  associated_widget_ = widget;
371  if (associated_widget_)
372  {
374  if (wm)
375  {
378  connect(associated_widget_panel_, SIGNAL(visibilityChanged(bool)), this,
379  SLOT(associatedPanelVisibilityChange(bool)));
380  connect(associated_widget_panel_, SIGNAL(closed()), this, SLOT(disable()));
382  }
383  else
384  {
385  associated_widget_panel_ = nullptr;
386  associated_widget_->setWindowTitle(getName());
387  }
388  }
389  else
390  {
391  associated_widget_panel_ = nullptr;
392  }
393 }
394 
396 {
398  // If something external makes the panel visible/invisible, make sure to enable/disable the display
399  setEnabled(visible);
400  // Remark: vice versa, in Display::onEnableChanged(),
401  // the panel is made visible/invisible when the display is enabled/disabled
402 }
403 
404 void Display::setIcon(const QIcon& icon)
405 {
406  icon_ = icon;
408  {
410  }
411 }
412 
413 void Display::setName(const QString& name)
414 {
415  BoolProperty::setName(name);
416 
418  {
420  // QMainWindow::saveState() needs objectName to be set.
421  associated_widget_panel_->setObjectName(name);
422  }
423  else if (associated_widget_)
424  {
425  associated_widget_->setWindowTitle(name);
426  }
427 }
428 
429 } // end namespace rviz
static std::map< PanelDockWidget *, bool > associated_widgets_visibility
Definition: display.cpp:292
void setIcon(QIcon icon)
virtual QIcon getIcon() const
Definition: property.h:198
PanelDockWidget * associated_widget_panel_
Definition: display.h:330
virtual void save(Config config) const
Write the value of this property and/or its children into the given Config reference.
Definition: property.cpp:481
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz...
Definition: display.h:287
void changed()
Emitted by setValue() just after the value has changed.
PropertyTreeModel * model_
Pointer to the PropertyTreeModel managing this property tree.
Definition: property.h:496
void setDisableChildrenIfFalse(bool disable)
virtual void load(const Config &config)
Load the value of this property and/or its children from the given Config reference.
Definition: property.cpp:431
void emitTimeSignal(ros::Time time)
Emit a time signal that other Displays can synchronize to.
Definition: display.cpp:282
virtual bool setValue(const QVariant &new_value)
Set the new value for this property. Returns true if the new value is different from the old value...
Definition: property.cpp:134
ros::NodeHandle update_nh_
A NodeHandle whose CallbackQueue is run from the main GUI thread (the "update" thread).
Definition: display.h:300
void timeSignal(ros::Time time, QPrivateSignal)
virtual void onEnableChanged()
Definition: display.cpp:303
virtual PanelDockWidget * addPane(const QString &name, QWidget *pane, Qt::DockWidgetArea area=Qt::LeftDockWidgetArea, bool floating=false)=0
virtual void setName(const QString &name)
Set the name.
Definition: property.cpp:155
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
QIcon statusIcon(Level level) const
virtual ros::CallbackQueueInterface * getUpdateQueue()=0
Return the CallbackQueue using the main GUI thread.
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:293
void setWindowTitle(QString title)
uint32_t visibility_bits_
Definition: display.h:328
Ogre::SceneNode * scene_node_
The Ogre::SceneNode to hold all 3D scene elements shown by this Display.
Definition: display.h:295
void unsetVisibilityBits(uint32_t bits)
Definition: display.cpp:355
void setVisible(PanelDockWidget *widget, bool visible)
Definition: display.cpp:293
void mapSetValue(const QString &key, QVariant value)
Set a named child to the given value.
Definition: config.cpp:196
Configuration data storage class.
Definition: config.h:124
QString fixed_frame_
A convenience variable equal to context_->getFixedFrame().
Definition: display.h:312
virtual Level getLevel() const
QVariant getViewData(int column, int role) const override
Return data appropriate for the given column (0 or 1) and role for this Display.
Definition: display.cpp:106
Pure-virtual base class for objects which give Display subclasses context in which to work...
virtual void clearStatuses()
Delete all status children. This is thread-safe.
Definition: display.cpp:210
void clearStatusesInternal()
Definition: display.cpp:215
virtual void reset()
Called to tell the display to clear its state.
Definition: display.cpp:287
virtual QVariant getViewData(int column, int role) const
Return data appropriate for the given column (0 or 1) and role for this Property. ...
Definition: property.cpp:241
QWidget * associated_widget_
Definition: display.h:329
virtual void onEnable()
Derived classes override this to do the actual work of enabling themselves.
Definition: display.h:255
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 setCallbackQueue(CallbackQueueInterface *queue)
void setAssociatedWidget(QWidget *widget)
Associate the given widget with this Display.
Definition: display.cpp:361
static QColor statusColor(Level level)
Return the color appropriate for the given status level.
void queueRender()
Convenience function which calls context_->queueRender().
Definition: display.cpp:98
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:164
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
StatusList * status_
Definition: display.h:325
void setVisibilityBits(uint32_t bits)
Definition: display.cpp:349
void setFixedFrame(const QString &fixed_frame)
Set the fixed frame in this display.
Definition: display.cpp:273
Ogre::SceneManager * scene_manager_
A convenience variable equal to context_->getSceneManager().
Definition: display.h:292
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
void disable()
Definition: display.cpp:263
virtual void queueRender()=0
Queues a render. Multiple calls before a render happens will only cause a single render.
bool isEnabled() const
Return true if this Display is enabled, false if not.
Definition: display.cpp:268
bool mapGetBool(const QString &key, bool *value_out) const
Convenience function for looking up a named boolean.
Definition: config.cpp:282
virtual QString getFixedFrame() const =0
Return the fixed frame name.
ros::NodeHandle threaded_nh_
A NodeHandle whose CallbackQueue is run from a different thread than the GUI.
Definition: display.h:305
virtual void onDisable()
Derived classes override this to do the actual work of disabling themselves.
Definition: display.h:260
void setEnabled(bool enabled)
Enable or disable this Display.
Definition: display.cpp:256
void setStatus(Level level, const QString &name, const QString &text)
Definition: status_list.cpp:50
virtual void fixedFrameChanged()
Called by setFixedFrame(). Override to respond to changes to fixed_frame_.
Definition: display.h:270
virtual void addChild(Property *child, int index=-1)
Add a child property.
Definition: property.cpp:355
void save(Config config) const override
Write this display to the given Config node.
Definition: display.cpp:246
void setName(const QString &name) override
Overridden from Property to set associated widget title to the new name.
Definition: display.cpp:413
void emitDataChanged(Property *property)
bool isVisible(PanelDockWidget *widget)
Definition: display.cpp:297
virtual void onInitialize()
Override this function to do subclass-specific initialization.
Definition: display.h:250
void initialize(DisplayContext *context)
Main initialization, called after constructor, before load() or setEnabled().
Definition: display.cpp:83
virtual void deleteStatus(const QString &name)
Delete the status entry with the given name. This is thread-safe.
Definition: display.cpp:197
void associatedPanelVisibilityChange(bool visible)
Definition: display.cpp:395
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
Definition: display.h:85
virtual bool getBool() const
Dock widget class for docking widgets into VisualizationFrame.
void applyVisibilityBits(uint32_t bits, Ogre::SceneNode *node)
virtual void setStatus(StatusProperty::Level level, const QString &name, const QString &text)
Show status level and text. This is thread-safe.
Definition: display.cpp:175
void deleteStatusInternal(const QString &name)
Definition: display.cpp:202
bool initialized_
Definition: display.h:327
virtual WindowManagerInterface * getWindowManager() const =0
Return the window manager, if any.
~Display() override
Definition: display.cpp:75
virtual ros::CallbackQueueInterface * getThreadedQueue()=0
Return a CallbackQueue using a different thread than the main GUI one.
void setIcon(const QIcon &icon) override
Set the Display&#39;s icon.
Definition: display.cpp:404
void deleteStatus(const QString &name)
Definition: status_list.cpp:75
void setStatusInternal(int level, const QString &name, const QString &text)
Definition: display.cpp:181


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