Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031
00032 #include <QColor>
00033
00034 #include "rviz/display_context.h"
00035 #include "rviz/display_factory.h"
00036 #include "rviz/failed_display.h"
00037 #include "rviz/properties/property_tree_model.h"
00038
00039 #include "display_group.h"
00040
00041 namespace rviz
00042 {
00043
00044 DisplayGroup::DisplayGroup()
00045 {
00046 }
00047
00048 DisplayGroup::~DisplayGroup()
00049 {
00050 removeAllDisplays();
00051 }
00052
00053 Qt::ItemFlags DisplayGroup::getViewFlags( int column ) const
00054 {
00055 return Display::getViewFlags( column ) | Qt::ItemIsDropEnabled;
00056 }
00057
00058 void DisplayGroup::load( const Config& config )
00059 {
00060 removeAllDisplays();
00061
00062
00063 Display::load( config );
00064
00065
00066 Config display_list_config = config.mapGetChild( "Displays" );
00067 int num_displays = display_list_config.listLength();
00068
00069 if( num_displays == 0 )
00070 return;
00071
00072 if( model_ )
00073 {
00074 model_->beginInsert( this, Display::numChildren(), num_displays );
00075 }
00076
00077 std::map<Display*,Config> display_config_map;
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 for( int i = 0; i < num_displays; i++ )
00091 {
00092 Config display_config = display_list_config.listChildAt( i );
00093 QString display_class = "(no class name found)";
00094 display_config.mapGetString( "Class", &display_class );
00095 Display* disp = createDisplay( display_class );
00096 addDisplayWithoutSignallingModel( disp );
00097 QString display_name;
00098 display_config.mapGetString( "Name", &display_name );
00099 disp->setObjectName( display_name );
00100
00101 display_config_map[ disp ] = display_config;
00102 }
00103
00104
00105 for( std::map<Display*,Config>::iterator it = display_config_map.begin(); it != display_config_map.end(); ++it )
00106 {
00107 Config display_config = it->second;
00108 Display* disp = it->first;
00109 disp->initialize( context_ );
00110 disp->load( display_config );
00111 }
00112
00113 if( model_ )
00114 {
00115 model_->endInsert();
00116 }
00117 }
00118
00119 Display* DisplayGroup::createDisplay( const QString& class_id )
00120 {
00121 DisplayFactory* factory = context_->getDisplayFactory();
00122 QString error;
00123 Display* disp = factory->make( class_id, &error );
00124 if( !disp )
00125 {
00126 return new FailedDisplay( class_id, error );
00127 }
00128 return disp;
00129 }
00130
00131 void DisplayGroup::onEnableChanged()
00132 {
00133 Display::onEnableChanged();
00134 for( int i = displays_.size() - 1; i >= 0; i-- )
00135 {
00136 displays_[ i ]->onEnableChanged();
00137 }
00138 }
00139
00140 void DisplayGroup::save( Config config ) const
00141 {
00142 Display::save( config );
00143
00144
00145 Config display_list_config = config.mapMakeChild( "Displays" );
00146
00147 int num_displays = displays_.size();
00148 for( int i = 0; i < num_displays; i++ )
00149 {
00150 displays_.at( i )->save( display_list_config.listAppendNew() );
00151 }
00152 }
00153
00154 void DisplayGroup::removeAllDisplays()
00155 {
00156 if(displays_.size() == 0)
00157 return;
00158
00159 int num_non_display_children = Display::numChildren();
00160
00161 if( model_ )
00162 {
00163 model_->beginRemove( this, num_non_display_children, displays_.size() );
00164 }
00165 for( int i = displays_.size() - 1; i >= 0; i-- )
00166 {
00167 Display* child = displays_.takeAt( i );
00168 Q_EMIT displayRemoved( child );
00169 child->setParent( NULL );
00170 child->setModel( NULL );
00171 child_indexes_valid_ = false;
00172 delete child;
00173 }
00174 if( model_ )
00175 {
00176 model_->endRemove();
00177 }
00178 Q_EMIT childListChanged( this );
00179 }
00180
00181 Display* DisplayGroup::takeDisplay( Display* child )
00182 {
00183 Display* result = NULL;
00184 int num_displays = displays_.size();
00185 for( int i = 0; i < num_displays; i++ )
00186 {
00187 if( displays_.at( i ) == child )
00188 {
00189 if( model_ )
00190 {
00191 model_->beginRemove( this, Display::numChildren() + i, 1 );
00192 }
00193 result = displays_.takeAt( i );
00194 Q_EMIT displayRemoved( result );
00195 result->setParent( NULL );
00196 result->setModel( NULL );
00197 child_indexes_valid_ = false;
00198 if( model_ )
00199 {
00200 model_->endRemove();
00201 }
00202 Q_EMIT childListChanged( this );
00203 break;
00204 }
00205 }
00206 return result;
00207 }
00208
00209 Display* DisplayGroup::getDisplayAt( int index ) const
00210 {
00211 if( 0 <= index && index < displays_.size() )
00212 {
00213 return displays_.at( index );
00214 }
00215 return NULL;
00216 }
00217
00218 DisplayGroup* DisplayGroup::getGroupAt( int index ) const
00219 {
00220 return qobject_cast<DisplayGroup*>( getDisplayAt( index ));
00221 }
00222
00223 void DisplayGroup::fixedFrameChanged()
00224 {
00225 int num_children = displays_.size();
00226 for( int i = 0; i < num_children; i++ )
00227 {
00228 displays_.at( i )->setFixedFrame( fixed_frame_ );
00229 }
00230 }
00231
00232 void DisplayGroup::update( float wall_dt, float ros_dt )
00233 {
00234 int num_children = displays_.size();
00235 for( int i = 0; i < num_children; i++ )
00236 {
00237 Display* display = displays_.at( i );
00238 if( display->isEnabled() )
00239 {
00240 display->update( wall_dt, ros_dt );
00241 }
00242 }
00243 }
00244
00245 void DisplayGroup::reset()
00246 {
00247 Display::reset();
00248
00249 int num_children = displays_.size();
00250 for( int i = 0; i < num_children; i++ )
00251 {
00252 displays_.at( i )->reset();
00253 }
00254 }
00255
00256 void DisplayGroup::addDisplayWithoutSignallingModel( Display* child )
00257 {
00258
00259 displays_.append( child );
00260 child_indexes_valid_ = false;
00261 child->setModel( model_ );
00262 child->setParent( this );
00263 Q_EMIT displayAdded( child );
00264 }
00265
00266 void DisplayGroup::addDisplay( Display* child )
00267 {
00268 if( model_ )
00269 {
00270 model_->beginInsert( this, numChildren(), 1 );
00271 }
00272 addDisplayWithoutSignallingModel( child );
00273 if( model_ )
00274 {
00275 model_->endInsert();
00276 }
00277 Q_EMIT childListChanged( this );
00278 }
00279
00280 void DisplayGroup::addChild( Property* child, int index )
00281 {
00282 Display* display = qobject_cast<Display*>( child );
00283 if( !display )
00284 {
00285 Display::addChild( child, index );
00286 return;
00287 }
00288 if( index < 0 || index > numChildren() )
00289 {
00290 index = numChildren();
00291 }
00292 int disp_index = index - Display::numChildren();
00293 if( disp_index < 0 )
00294 {
00295 disp_index = 0;
00296 }
00297 if( model_ )
00298 {
00299 model_->beginInsert( this, index );
00300 }
00301
00302 displays_.insert( disp_index, display );
00303 Q_EMIT displayAdded( display );
00304 child_indexes_valid_ = false;
00305 display->setModel( model_ );
00306 display->setParent( this );
00307
00308 if( model_ )
00309 {
00310 model_->endInsert();
00311 }
00312 Q_EMIT childListChanged( this );
00313 }
00314
00315 Property* DisplayGroup::takeChildAt( int index )
00316 {
00317 if( index < Display::numChildren() )
00318 {
00319 return Display::takeChildAt( index );
00320 }
00321 int disp_index = index - Display::numChildren();
00322 if( model_ )
00323 {
00324 model_->beginRemove( this, index, 1 );
00325 }
00326
00327 Display* child = displays_.takeAt( disp_index );
00328 Q_EMIT displayRemoved( child );
00329 child->setModel( NULL );
00330 child->setParent( NULL );
00331 child_indexes_valid_ = false;
00332 if( model_ )
00333 {
00334 model_->endRemove();
00335 }
00336 Q_EMIT childListChanged( this );
00337 return child;
00338 }
00339
00340 int DisplayGroup::numDisplays() const
00341 {
00342 return displays_.size();
00343 }
00344
00345 int DisplayGroup::numChildren() const
00346 {
00347 return Display::numChildren() + displays_.size();
00348 }
00349
00350 Property* DisplayGroup::childAtUnchecked( int index ) const
00351 {
00352 int first_child_count = Display::numChildren();
00353 if( index < first_child_count )
00354 {
00355 return Display::childAtUnchecked( index );
00356 }
00357 index -= first_child_count;
00358 return displays_.at( index );
00359 }
00360
00361 }