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 {
55 
57  : context_( 0 )
58  , scene_node_( NULL )
59  , status_( 0 )
60  , initialized_( false )
61  , visibility_bits_( 0xFFFFFFFF )
62  , associated_widget_( NULL )
63  , associated_widget_panel_( NULL )
64 {
65  // Needed for timeSignal (see header) to work across threads
66  qRegisterMetaType<ros::Time>();
67 
68  // Make the display-enable checkbox show up, and make it unchecked by default.
69  setValue( false );
70 
71  connect( this, SIGNAL( changed() ), this, SLOT( onEnableChanged() ));
72 
74 }
75 
77 {
78  if( scene_node_ )
79  {
80  scene_manager_->destroySceneNode( scene_node_ );
81  }
82 }
83 
85 {
86  context_ = context;
88  scene_node_ = scene_manager_->getRootSceneNode()->createChildSceneNode();
89 
93 
94  onInitialize();
95 
96  initialized_ = true;
97 }
98 
100 {
101  if( context_ )
102  {
104  }
105 }
106 
107 QVariant Display::getViewData( int column, int role ) const
108 {
109  switch( role )
110  {
111  case Qt::BackgroundRole:
112  {
113  /*
114  QLinearGradient q( 0,0, 0,5 );
115  q.setColorAt( 0.0, QColor(230,230,230) );
116  q.setColorAt( 1.0, Qt::white );
117  return QBrush( q );
118  */
119  return QColor( Qt::white );
120  }
121  case Qt::ForegroundRole:
122  {
123  // if we're item-enabled (not greyed out) and in warn/error state, set appropriate color
124  if ( getViewFlags( column ) & Qt::ItemIsEnabled )
125  {
126  if ( isEnabled() )
127  {
129  {
131  }
132  else
133  {
134  // blue means that the enabled checkmark is set
135  return QColor( 40, 120, 197 );
136  }
137  }
138  else
139  {
140  return QColor( Qt::black );
141  }
142  }
143  break;
144  }
145  case Qt::FontRole:
146  {
147  QFont font;
148  if ( isEnabled() )
149  {
150  font.setBold( true );
151  }
152  return font;
153  }
154  case Qt::DecorationRole:
155  {
156  if( column == 0 )
157  {
158  if ( isEnabled() )
159  {
161  switch( level )
162  {
163  case StatusProperty::Ok:
164  return getIcon();
167  return status_->statusIcon( status_->getLevel() );
168  }
169  }
170  else
171  {
172  return getIcon();
173  }
174  }
175  break;
176  }
177  }
178  return BoolProperty::getViewData( column, role );
179 }
180 
181 Qt::ItemFlags Display::getViewFlags( int column ) const
182 {
183  return BoolProperty::getViewFlags( column ) | Qt::ItemIsDragEnabled;
184 }
185 
186 void Display::setStatus( StatusProperty::Level level, const QString& name, const QString& text )
187 {
188  QMetaObject::invokeMethod( this, "setStatusInternal", Qt::QueuedConnection,
189  Q_ARG( int, level ),
190  Q_ARG( QString, name ),
191  Q_ARG( QString, text ));
192 }
193 
194 void Display::setStatusInternal( int level, const QString& name, const QString& text )
195 {
196  if( !status_ )
197  {
198  status_ = new StatusList( "Status" );
199  addChild( status_, 0 );
200  }
201  StatusProperty::Level old_level = status_->getLevel();
202 
203  status_->setStatus( (StatusProperty::Level) level, name, text );
204  if( model_ && old_level != status_->getLevel() )
205  {
206  model_->emitDataChanged( this );
207  }
208 }
209 
210 void Display::deleteStatus( const QString& name )
211 {
212  QMetaObject::invokeMethod( this, "deleteStatusInternal", Qt::QueuedConnection,
213  Q_ARG( QString, name ));
214 }
215 
216 void Display::deleteStatusInternal( const QString& name )
217 {
218  if( status_ )
219  {
220  status_->deleteStatus( name );
221  }
222 }
223 
225 {
226  QMetaObject::invokeMethod( this, "clearStatusesInternal", Qt::QueuedConnection );
227 }
228 
230 {
231  if( status_ )
232  {
233  StatusProperty::Level old_level = status_->getLevel();
234  status_->clear();
235  if( model_ && old_level != StatusProperty::Ok )
236  {
237  model_->emitDataChanged( this );
238  }
239  }
240 }
241 
242 void Display::load( const Config& config )
243 {
244  // Base class loads sub-properties.
245  BoolProperty::load( config );
246 
247  QString name;
248  if( config.mapGetString( "Name", &name ))
249  {
250  setObjectName( name );
251  }
252 
253  bool enabled;
254  if( config.mapGetBool( "Enabled", &enabled ))
255  {
256  setEnabled( enabled );
257  }
258 }
259 
260 void Display::save( Config config ) const
261 {
262  // Base class saves sub-properties.
263  BoolProperty::save( config );
264 
265  config.mapSetValue( "Class", getClassId() );
266  config.mapSetValue( "Name", getName() );
267  config.mapSetValue( "Enabled", getBool() );
268 }
269 
270 void Display::setEnabled( bool enabled )
271 {
272  if ( enabled == isEnabled() ) return;
273  setValue( enabled );
274 }
275 
277 {
278  setEnabled( false );
279 }
280 
281 bool Display::isEnabled() const
282 {
283  return getBool() && (getViewFlags( 0 ) & Qt::ItemIsEnabled);
284 }
285 
286 void Display::setFixedFrame( const QString& fixed_frame )
287 {
288  fixed_frame_ = fixed_frame;
289  if( initialized_ )
290  {
292  }
293 }
294 
296 {
297  Q_EMIT( timeSignal( this, time ) );
298 }
299 
301 {
302  clearStatuses();
303 }
304 
306 {
307  QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
308  queueRender();
309  if( isEnabled() )
310  {
311  scene_node_->setVisible( true );
312 
314  {
315  associated_widget_panel_->show();
316  }
317  else if( associated_widget_ )
318  {
319  associated_widget_->show();
320  }
321 
322  onEnable();
323  }
324  else
325  {
326  onDisable();
327 
329  {
330  if( associated_widget_panel_->isVisible() )
331  {
332  associated_widget_panel_->hide();
333  }
334  }
335  else if( associated_widget_ && associated_widget_->isVisible() )
336  {
337  associated_widget_->hide();
338  }
339 
340  scene_node_->setVisible( false );
341  }
342  QApplication::restoreOverrideCursor();
343 }
344 
345 void Display::setVisibilityBits( uint32_t bits )
346 {
347  visibility_bits_ |= bits;
349 }
350 
351 void Display::unsetVisibilityBits( uint32_t bits )
352 {
353  visibility_bits_ &= ~bits;
355 }
356 
357 void Display::setAssociatedWidget( QWidget* widget )
358 {
360  {
361  disconnect( associated_widget_panel_, SIGNAL( visibilityChanged( bool ) ), this, SLOT( associatedPanelVisibilityChange( bool ) ));
362  disconnect( associated_widget_panel_, SIGNAL( closed( ) ), this, SLOT( disable( )));
363  }
364 
365  associated_widget_ = widget;
366  if( associated_widget_ )
367  {
369  if( wm )
370  {
372  connect( associated_widget_panel_, SIGNAL( visibilityChanged( bool ) ), this, SLOT( associatedPanelVisibilityChange( bool ) ));
373  connect( associated_widget_panel_, SIGNAL( closed( ) ), this, SLOT( disable( )));
375  }
376  else
377  {
379  associated_widget_->setWindowTitle( getName() );
380  }
381  }
382  else
383  {
385  }
386 }
387 
389 {
390  // if something external makes the panel visible, make sure we're enabled
391  if ( visible )
392  {
393  setEnabled( true );
394  }
395  else
396  {
397  setEnabled( false );
398  }
399 }
400 
401 void Display::setIcon( const QIcon& icon )
402 {
403  icon_=icon;
405  {
407  }
408 }
409 
410 void Display::setName( const QString& name )
411 {
412  BoolProperty::setName( name );
413 
415  {
417  associated_widget_panel_->setObjectName( name ); // QMainWindow::saveState() needs objectName to be set.
418  }
419  else if( associated_widget_ )
420  {
421  associated_widget_->setWindowTitle( name );
422  }
423 }
424 
425 } // end namespace rviz
virtual void setIcon(const QIcon &icon)
Set the Display&#39;s icon.
Definition: display.cpp:401
#define NULL
Definition: global.h:37
void setIcon(QIcon icon)
PanelDockWidget * associated_widget_panel_
Definition: display.h:299
DisplayContext * context_
This DisplayContext pointer is the main connection a Display has into the rest of rviz...
Definition: display.h:256
void changed()
Emitted by setValue() just after the value has changed.
PropertyTreeModel * model_
Pointer to the PropertyTreeModel managing this property tree.
Definition: property.h:454
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:426
void emitTimeSignal(ros::Time time)
Emit a time signal that other Displays can synchronize to.
Definition: display.cpp:295
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:130
virtual QIcon getIcon() const
Definition: property.h:189
ros::NodeHandle update_nh_
A NodeHandle whose CallbackQueue is run from the main GUI thread (the "update" thread).
Definition: display.h:269
virtual void onEnableChanged()
Definition: display.cpp:305
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:236
virtual void setName(const QString &name)
Set the name.
Definition: property.cpp:150
virtual ros::CallbackQueueInterface * getUpdateQueue()=0
Return the CallbackQueue using the main GUI thread.
virtual Level getLevel() const
void setWindowTitle(QString title)
uint32_t visibility_bits_
Definition: display.h:297
Ogre::SceneNode * scene_node_
The Ogre::SceneNode to hold all 3D scene elements shown by this Display.
Definition: display.h:264
void unsetVisibilityBits(uint32_t bits)
Definition: display.cpp:351
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:281
void timeSignal(rviz::Display *display, ros::Time time)
void mapSetValue(const QString &key, QVariant value)
Set a named child to the given value.
Definition: config.cpp:185
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
virtual bool getBool() const
QString fixed_frame_
A convenience variable equal to context_->getFixedFrame().
Definition: display.h:281
Pure-virtual base class for objects which give Display subclasses context in which to work...
virtual void save(Config config) const
Write the value of this property and/or its children into the given Config reference.
Definition: property.cpp:467
virtual void clearStatuses()
Delete all status children. This is thread-safe.
Definition: display.cpp:224
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 PanelDockWidget * addPane(const QString &name, QWidget *pane, Qt::DockWidgetArea area=Qt::LeftDockWidgetArea, bool floating=true)=0
void clearStatusesInternal()
Definition: display.cpp:229
virtual void reset()
Called to tell the display to clear its state.
Definition: display.cpp:300
QWidget * associated_widget_
Definition: display.h:298
virtual void onEnable()
Derived classes override this to do the actual work of enabling themselves.
Definition: display.h:233
virtual QVariant getViewData(int column, int role) const
Return data appropriate for the given column (0 or 1) and role for this Display.
Definition: display.cpp:107
void setCallbackQueue(CallbackQueueInterface *queue)
void setAssociatedWidget(QWidget *widget)
Associate the given widget with this Display.
Definition: display.cpp:357
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:99
bool mapGetBool(const QString &key, bool *value_out) const
Convenience function for looking up a named boolean.
Definition: config.cpp:270
QIcon statusIcon(Level level) const
virtual void save(Config config) const
Write this display to the given Config node.
Definition: display.cpp:260
StatusList * status_
Definition: display.h:294
void setVisibilityBits(uint32_t bits)
Definition: display.cpp:345
void setFixedFrame(const QString &fixed_frame)
Set the fixed frame in this display.
Definition: display.cpp:286
Ogre::SceneManager * scene_manager_
A convenience variable equal to context_->getSceneManager().
Definition: display.h:261
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
void disable()
Definition: display.cpp:276
virtual void queueRender()=0
Queues a render. Multiple calls before a render happens will only cause a single render.
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
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:274
virtual void onDisable()
Derived classes override this to do the actual work of disabling themselves.
Definition: display.h:236
void setEnabled(bool enabled)
Enable or disable this Display.
Definition: display.cpp:270
void setStatus(Level level, const QString &name, const QString &text)
Definition: status_list.cpp:52
void setName(const QString &name)
Overridden from Property to set associated widget title to the new name.
Definition: display.cpp:410
virtual void fixedFrameChanged()
Called by setFixedFrame(). Override to respond to changes to fixed_frame_.
Definition: display.h:244
virtual ~Display()
Definition: display.cpp:76
virtual void addChild(Property *child, int index=-1)
Add a child property.
Definition: property.cpp:350
void emitDataChanged(Property *property)
virtual void onInitialize()
Override this function to do subclass-specific initialization.
Definition: display.h:230
void initialize(DisplayContext *context)
Main initialization, called after constructor, before load() or setEnabled().
Definition: display.cpp:84
virtual void deleteStatus(const QString &name)
Delete the status entry with the given name. This is thread-safe.
Definition: display.cpp:210
void associatedPanelVisibilityChange(bool visible)
Definition: display.cpp:388
void applyVisibilityBits(uint32_t bits, Ogre::SceneNode *node)
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 setStatus(StatusProperty::Level level, const QString &name, const QString &text)
Show status level and text. This is thread-safe.
Definition: display.cpp:186
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:159
void deleteStatusInternal(const QString &name)
Definition: display.cpp:216
bool initialized_
Definition: display.h:296
virtual WindowManagerInterface * getWindowManager() const =0
Return the window manager, if any.
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
virtual ros::CallbackQueueInterface * getThreadedQueue()=0
Return a CallbackQueue using a different thread than the main GUI one.
void deleteStatus(const QString &name)
Definition: status_list.cpp:77
void setStatusInternal(int level, const QString &name, const QString &text)
Definition: display.cpp:194


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