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 <OgreSceneManager.h>
00040 #include <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   if( status_ )
00219   {
00220     status_->deleteStatus( name );
00221   }
00222 }
00223 
00224 void Display::clearStatuses()
00225 {
00226   QMetaObject::invokeMethod( this, "clearStatusesInternal", Qt::QueuedConnection );
00227 }
00228 
00229 void Display::clearStatusesInternal()
00230 {
00231   if( status_ )
00232   {
00233     StatusProperty::Level old_level = status_->getLevel();
00234     status_->clear();
00235     if( model_ && old_level != StatusProperty::Ok )
00236     {
00237       model_->emitDataChanged( this );
00238     }
00239   }
00240 }
00241 
00242 void Display::load( const Config& config )
00243 {
00244   // Base class loads sub-properties.
00245   BoolProperty::load( config );
00246 
00247   QString name;
00248   if( config.mapGetString( "Name", &name ))
00249   {
00250     setObjectName( name );
00251   }
00252 
00253   bool enabled;
00254   if( config.mapGetBool( "Enabled", &enabled ))
00255   {
00256     setEnabled( enabled );
00257   }
00258 }
00259 
00260 void Display::save( Config config ) const
00261 {
00262   // Base class saves sub-properties.
00263   BoolProperty::save( config );
00264 
00265   config.mapSetValue( "Class", getClassId() );
00266   config.mapSetValue( "Name", getName() );
00267   config.mapSetValue( "Enabled", getBool() );
00268 }
00269 
00270 void Display::setEnabled( bool enabled )
00271 {
00272   if ( enabled == isEnabled() ) return;
00273   setValue( enabled );
00274 }
00275 
00276 void Display::disable()
00277 {
00278   setEnabled( false );
00279 }
00280 
00281 bool Display::isEnabled() const
00282 {
00283   return getBool() && (getViewFlags( 0 ) & Qt::ItemIsEnabled);
00284 }
00285 
00286 void Display::setFixedFrame( const QString& fixed_frame )
00287 {
00288   fixed_frame_ = fixed_frame;
00289   if( initialized_ )
00290   {
00291     fixedFrameChanged();
00292   }
00293 }
00294 
00295 void Display::emitTimeSignal( ros::Time time )
00296 {
00297   Q_EMIT( timeSignal( this, time ) );
00298 }
00299 
00300 void Display::reset()
00301 {
00302   clearStatuses();
00303 }
00304 
00305 void Display::onEnableChanged()
00306 {
00307   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
00308   queueRender();
00309   if( isEnabled() )
00310   {
00311     scene_node_->setVisible( true );
00312 
00313     if( associated_widget_panel_ )
00314     {
00315       associated_widget_panel_->show();
00316     }
00317     else if( associated_widget_ )
00318     {
00319       associated_widget_->show();
00320     }
00321 
00322     onEnable();
00323   }
00324   else
00325   {
00326     onDisable();
00327 
00328     if( associated_widget_panel_ )
00329     {
00330       if( associated_widget_panel_->isVisible() )
00331       {
00332         associated_widget_panel_->hide();
00333       }
00334     }
00335     else if( associated_widget_ && associated_widget_->isVisible() )
00336     {
00337       associated_widget_->hide();
00338     }
00339 
00340     scene_node_->setVisible( false );
00341   }
00342   QApplication::restoreOverrideCursor();
00343 }
00344 
00345 void Display::setVisibilityBits( uint32_t bits )
00346 {
00347   visibility_bits_ |= bits;
00348   applyVisibilityBits( visibility_bits_, scene_node_ );
00349 }
00350 
00351 void Display::unsetVisibilityBits( uint32_t bits )
00352 {
00353   visibility_bits_ &= ~bits;
00354   applyVisibilityBits( visibility_bits_, scene_node_ );
00355 }
00356 
00357 void Display::setAssociatedWidget( QWidget* widget )
00358 {
00359   if( associated_widget_panel_ )
00360   {
00361     disconnect( associated_widget_panel_, SIGNAL( visibilityChanged( bool ) ), this, SLOT( associatedPanelVisibilityChange( bool ) ));
00362     disconnect( associated_widget_panel_, SIGNAL( closed( ) ), this, SLOT( disable( )));
00363   }
00364 
00365   associated_widget_ = widget;
00366   if( associated_widget_ )
00367   {
00368     WindowManagerInterface* wm = context_->getWindowManager();
00369     if( wm )
00370     {
00371       associated_widget_panel_ = wm->addPane( getName(), associated_widget_ );
00372       connect( associated_widget_panel_, SIGNAL( visibilityChanged( bool ) ), this, SLOT( associatedPanelVisibilityChange( bool ) ));
00373       connect( associated_widget_panel_, SIGNAL( closed( ) ), this, SLOT( disable( )));
00374       associated_widget_panel_->setIcon( getIcon() );
00375     }
00376     else
00377     {
00378       associated_widget_panel_ = NULL;
00379       associated_widget_->setWindowTitle( getName() );
00380     }
00381   }
00382   else
00383   {
00384     associated_widget_panel_ = NULL;
00385   }
00386 }
00387 
00388 void Display::associatedPanelVisibilityChange( bool visible )
00389 {
00390   // if something external makes the panel visible, make sure we're enabled
00391   if ( visible )
00392   {
00393     setEnabled( true );
00394   }
00395   else
00396   {
00397     setEnabled( false );
00398   }
00399 }
00400 
00401 void Display::setIcon( const QIcon& icon )
00402 {
00403   icon_=icon;
00404   if ( associated_widget_panel_ )
00405   {
00406     associated_widget_panel_->setIcon( getIcon() );
00407   }
00408 }
00409 
00410 void Display::setName( const QString& name )
00411 {
00412   BoolProperty::setName( name );
00413 
00414   if( associated_widget_panel_ )
00415   {
00416     associated_widget_panel_->setWindowTitle( name );
00417     associated_widget_panel_->setObjectName( name ); // QMainWindow::saveState() needs objectName to be set.
00418   }
00419   else if( associated_widget_ )
00420   {
00421     associated_widget_->setWindowTitle( name );
00422   }
00423 }
00424 
00425 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Tue Oct 3 2017 03:19:30