display.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <stdio.h>
00031 
00032 #include <QApplication>
00033 #include <QColor>
00034 #include <QDockWidget>
00035 #include <QFont>
00036 #include <QMetaObject>
00037 #include <QWidget>
00038 
00039 #include <OGRE/OgreSceneManager.h>
00040 #include <OGRE/OgreSceneNode.h>
00041 
00042 #include "rviz/display_context.h"
00043 #include "rviz/ogre_helpers/apply_visibility_bits.h"
00044 #include "rviz/properties/property_tree_model.h"
00045 #include "rviz/properties/status_list.h"
00046 #include "rviz/window_manager_interface.h"
00047 #include "rviz/panel_dock_widget.h"
00048 
00049 #include "display.h"
00050 
00051 #include <boost/filesystem.hpp>
00052 
00053 namespace rviz
00054 {
00055 
00056 Display::Display()
00057   : context_( 0 )
00058   , scene_node_( NULL )
00059   , status_( 0 )
00060   , initialized_( false )
00061   , visibility_bits_( 0xFFFFFFFF )
00062   , associated_widget_( NULL )
00063   , associated_widget_panel_( NULL )
00064 {
00065   // Needed for timeSignal (see header) to work across threads
00066   qRegisterMetaType<ros::Time>();
00067 
00068   // Make the display-enable checkbox show up, and make it unchecked by default.
00069   setValue( false );
00070 
00071   connect( this, SIGNAL( changed() ), this, SLOT( onEnableChanged() ));
00072 
00073   setDisableChildrenIfFalse(true);
00074 }
00075 
00076 Display::~Display()
00077 {
00078   if( scene_node_ )
00079   {
00080     scene_manager_->destroySceneNode( scene_node_ );
00081   }
00082 }
00083 
00084 void Display::initialize( DisplayContext* context )
00085 {
00086   context_ = context;
00087   scene_manager_ = context_->getSceneManager();
00088   scene_node_ = scene_manager_->getRootSceneNode()->createChildSceneNode();
00089   
00090   update_nh_.setCallbackQueue( context_->getUpdateQueue() );
00091   threaded_nh_.setCallbackQueue( context_->getThreadedQueue() );
00092   fixed_frame_ = context_->getFixedFrame();
00093 
00094   onInitialize();
00095 
00096   initialized_ = true;
00097 }
00098 
00099 void Display::queueRender()
00100 {
00101   if( context_ )
00102   {
00103     context_->queueRender();
00104   }
00105 }
00106 
00107 QVariant Display::getViewData( int column, int role ) const
00108 {
00109   switch( role )
00110   {
00111   case Qt::BackgroundRole:
00112   {
00113     /*
00114     QLinearGradient q( 0,0, 0,5 );
00115     q.setColorAt( 0.0, QColor(230,230,230) );
00116     q.setColorAt( 1.0, Qt::white );
00117     return QBrush( q );
00118     */
00119     return QColor( Qt::white );
00120   }
00121   case Qt::ForegroundRole:
00122   {
00123     // if we're item-enabled (not greyed out) and in warn/error state, set appropriate color
00124     if ( getViewFlags( column ) & Qt::ItemIsEnabled )
00125     {
00126       if ( isEnabled() )
00127       {
00128         if ( status_ && status_->getLevel() != StatusProperty::Ok )
00129         {
00130           return StatusProperty::statusColor( status_->getLevel() );
00131         }
00132         else
00133         {
00134           // blue means that the enabled checkmark is set
00135           return QColor( 40, 120, 197 );
00136         }
00137       }
00138       else
00139       {
00140         return QColor( Qt::black );
00141       }
00142     }
00143     break;
00144   }
00145   case Qt::FontRole:
00146   {
00147     QFont font;
00148     if ( isEnabled() )
00149     {
00150       font.setBold( true );
00151     }
00152     return font;
00153   }
00154   case Qt::DecorationRole:
00155   {
00156     if( column == 0 )
00157     {
00158       if ( isEnabled() )
00159       {
00160         StatusProperty::Level level = status_ ? status_->getLevel() : StatusProperty::Ok;
00161         switch( level )
00162         {
00163         case StatusProperty::Ok:
00164           return getIcon();
00165         case StatusProperty::Warn:
00166         case StatusProperty::Error:
00167           return status_->statusIcon( status_->getLevel() );
00168         }
00169       }
00170       else
00171       {
00172         return getIcon();
00173       }
00174     }
00175     break;
00176   }
00177   }
00178   return BoolProperty::getViewData( column, role );
00179 }
00180 
00181 Qt::ItemFlags Display::getViewFlags( int column ) const
00182 {
00183   return BoolProperty::getViewFlags( column ) | Qt::ItemIsDragEnabled;
00184 }
00185 
00186 void Display::setStatus( StatusProperty::Level level, const QString& name, const QString& text )
00187 {
00188   QMetaObject::invokeMethod( this, "setStatusInternal", Qt::QueuedConnection,
00189                              Q_ARG( int, level ),
00190                              Q_ARG( QString, name ),
00191                              Q_ARG( QString, text ));
00192 }
00193 
00194 void Display::setStatusInternal( int level, const QString& name, const QString& text )
00195 {
00196   if( !status_ )
00197   {
00198     status_ = new StatusList( "Status" );
00199     addChild( status_, 0 );
00200   }
00201   StatusProperty::Level old_level = status_->getLevel();
00202   
00203   status_->setStatus( (StatusProperty::Level) level, name, text );
00204   if( model_ && old_level != status_->getLevel() )
00205   {
00206     model_->emitDataChanged( this );
00207   }
00208 }
00209 
00210 void Display::deleteStatus( const QString& name )
00211 {
00212   QMetaObject::invokeMethod( this, "deleteStatusInternal", Qt::QueuedConnection,
00213                              Q_ARG( QString, name ));
00214 }
00215 
00216 void Display::deleteStatusInternal( const QString& name )
00217 {
00218   status_->deleteStatus( name );
00219 }
00220 
00221 void Display::clearStatuses()
00222 {
00223   QMetaObject::invokeMethod( this, "clearStatusesInternal", Qt::QueuedConnection );
00224 }
00225 
00226 void Display::clearStatusesInternal()
00227 {
00228   if( status_ )
00229   {
00230     StatusProperty::Level old_level = status_->getLevel();
00231     status_->clear();
00232     if( model_ && old_level != StatusProperty::Ok )
00233     {
00234       model_->emitDataChanged( this );
00235     }
00236   }
00237 }
00238 
00239 void Display::load( const Config& config )
00240 {
00241   // Base class loads sub-properties.
00242   BoolProperty::load( config );
00243 
00244   QString name;
00245   if( config.mapGetString( "Name", &name ))
00246   {
00247     setObjectName( name );
00248   }
00249 
00250   bool enabled;
00251   if( config.mapGetBool( "Enabled", &enabled ))
00252   {
00253     setEnabled( enabled );
00254   }
00255 }
00256 
00257 void Display::save( Config config ) const
00258 {
00259   // Base class saves sub-properties.
00260   BoolProperty::save( config );
00261 
00262   config.mapSetValue( "Class", getClassId() );
00263   config.mapSetValue( "Name", getName() );
00264   config.mapSetValue( "Enabled", getBool() );
00265 }
00266 
00267 void Display::setEnabled( bool enabled )
00268 {
00269   if ( enabled == isEnabled() ) return;
00270   setValue( enabled );
00271 }
00272 
00273 void Display::disable()
00274 {
00275   setEnabled( false );
00276 }
00277 
00278 bool Display::isEnabled() const
00279 {
00280   return getBool() && (getViewFlags( 0 ) & Qt::ItemIsEnabled);
00281 }
00282 
00283 void Display::setFixedFrame( const QString& fixed_frame )
00284 {
00285   fixed_frame_ = fixed_frame;
00286   if( initialized_ )
00287   {
00288     fixedFrameChanged();
00289   }
00290 }
00291 
00292 void Display::emitTimeSignal( ros::Time time )
00293 {
00294   Q_EMIT( timeSignal( this, time ) );
00295 }
00296 
00297 void Display::reset()
00298 {
00299   clearStatuses();
00300 }
00301 
00302 void Display::onEnableChanged()
00303 {
00304   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
00305   queueRender();
00306   if( isEnabled() )
00307   {
00308     scene_node_->setVisible( true );
00309 
00310     if( associated_widget_panel_ )
00311     {
00312       associated_widget_panel_->show();
00313     }
00314     else if( associated_widget_ )
00315     {
00316       associated_widget_->show();
00317     }
00318 
00319     onEnable();
00320   }
00321   else
00322   {
00323     onDisable();
00324 
00325     if( associated_widget_panel_ )
00326     {
00327       if( associated_widget_panel_->isVisible() )
00328       {
00329         associated_widget_panel_->hide();
00330       }
00331     }
00332     else if( associated_widget_ && associated_widget_->isVisible() )
00333     {
00334       associated_widget_->hide();
00335     }
00336 
00337     scene_node_->setVisible( false );
00338   }
00339   QApplication::restoreOverrideCursor();
00340 }
00341 
00342 void Display::setVisibilityBits( uint32_t bits )
00343 {
00344   visibility_bits_ |= bits;
00345   applyVisibilityBits( visibility_bits_, scene_node_ );
00346 }
00347 
00348 void Display::unsetVisibilityBits( uint32_t bits )
00349 {
00350   visibility_bits_ &= ~bits;
00351   applyVisibilityBits( visibility_bits_, scene_node_ );
00352 }
00353 
00354 void Display::setAssociatedWidget( QWidget* widget )
00355 {
00356   if( associated_widget_panel_ )
00357   {
00358     disconnect( associated_widget_panel_, SIGNAL( visibilityChanged( bool ) ), this, SLOT( associatedPanelVisibilityChange( bool ) ));
00359     disconnect( associated_widget_panel_, SIGNAL( closed( ) ), this, SLOT( disable( )));
00360   }
00361 
00362   associated_widget_ = widget;
00363   if( associated_widget_ )
00364   {
00365     WindowManagerInterface* wm = context_->getWindowManager();
00366     if( wm )
00367     {
00368       associated_widget_panel_ = wm->addPane( getName(), associated_widget_ );
00369       connect( associated_widget_panel_, SIGNAL( visibilityChanged( bool ) ), this, SLOT( associatedPanelVisibilityChange( bool ) ));
00370       connect( associated_widget_panel_, SIGNAL( closed( ) ), this, SLOT( disable( )));
00371       associated_widget_panel_->setIcon( getIcon() );
00372     }
00373     else
00374     {
00375       associated_widget_panel_ = NULL;
00376       associated_widget_->setWindowTitle( getName() );
00377     }
00378   }
00379   else
00380   {
00381     associated_widget_panel_ = NULL;
00382   }
00383 }
00384 
00385 void Display::associatedPanelVisibilityChange( bool visible )
00386 {
00387   // if something external makes the panel visible, make sure we're enabled
00388   if ( visible )
00389   {
00390     setEnabled( true );
00391   }
00392 }
00393 
00394 void Display::setIcon( const QIcon& icon )
00395 {
00396   icon_=icon;
00397   if ( associated_widget_panel_ )
00398   {
00399     associated_widget_panel_->setIcon( getIcon() );
00400   }
00401 }
00402 
00403 void Display::setName( const QString& name )
00404 {
00405   BoolProperty::setName( name );
00406 
00407   if( associated_widget_panel_ )
00408   {
00409     associated_widget_panel_->setWindowTitle( name );
00410     associated_widget_panel_->setObjectName( name ); // QMainWindow::saveState() needs objectName to be set.
00411   }
00412   else if( associated_widget_ )
00413   {
00414     associated_widget_->setWindowTitle( name );
00415   }
00416 }
00417 
00418 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:35