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 "display_wrapper.h"
00031 #include "display.h"
00032 #include "visualization_manager.h"
00033
00034
00035
00036 #include "properties/property_manager.h"
00037 #include "properties/property.h"
00038
00039 #include <ros/assert.h>
00040
00041 #include "config.h"
00042
00043 namespace rviz
00044 {
00045
00046 DisplayWrapper::DisplayWrapper( const std::string& class_lookup_name,
00047 pluginlib::ClassLoader<Display>* class_loader,
00048 const std::string& name,
00049 VisualizationManager* manager )
00050 : manager_(manager)
00051 , class_loader_( class_loader )
00052 , name_(name)
00053 , class_lookup_name_(class_lookup_name)
00054 , display_(0)
00055 , property_manager_(0)
00056 , enabled_(true)
00057 {
00058 connect( manager, SIGNAL( displaysConfigLoaded( const boost::shared_ptr<Config>& )),
00059 this, SLOT( onDisplaysConfigLoaded( const boost::shared_ptr<Config>& )));
00060 connect( manager, SIGNAL( displaysConfigSaved( const boost::shared_ptr<Config>& )),
00061 this, SLOT( onDisplaysConfigSaved( const boost::shared_ptr<Config>& )));
00062 }
00063
00064 DisplayWrapper::~DisplayWrapper()
00065 {
00066 destroyDisplay();
00067
00068 if (property_manager_)
00069 {
00070 property_manager_->deleteProperty(category_.lock());
00071 }
00072 }
00073
00074 void DisplayWrapper::setName( const std::string& name )
00075 {
00076 std::string old_name = name_;
00077 name_ = name;
00078 M_string new_props;
00079 if (display_)
00080 {
00081 display_->setName(name);
00082
00083 if (config_)
00084 {
00085 for( Config::Iterator ci = config_->begin(); ci != config_->end(); )
00086 {
00087 std::string key = (*ci).first;
00088 std::string value = (*ci).second;
00089
00090 ci++;
00091
00092 if( key.find( old_name + "." ) == 0 )
00093 {
00094 std::string new_key = name + key.substr( old_name.size() + 1 );
00095 config_->set( new_key, value );
00096 config_->unset( key );
00097
00098 new_props[ key ] = value;
00099 }
00100 }
00101 }
00102
00103 if (property_manager_)
00104 {
00105 property_manager_->changePrefix(old_name + ".", name + ".");
00106 }
00107 }
00108 else
00109 {
00110 M_string::iterator it = properties_.begin();
00111 M_string::iterator end = properties_.end();
00112 for (; it != end; ++it)
00113 {
00114 const std::string& key = it->first;
00115 const std::string& val = it->second;
00116
00117 std::string new_key = name + "." + key.substr( old_name.size() + 1 );
00118 new_props[new_key] = val;
00119
00120 if (config_)
00121 {
00122 config_->unset( key );
00123 config_->set( new_key, val );
00124 }
00125 }
00126 }
00127
00128 properties_ = new_props;
00129 }
00130
00131 void DisplayWrapper::loadProperties()
00132 {
00133 if (!config_)
00134 {
00135 return;
00136 }
00137
00138 properties_.clear();
00139
00140 for( Config::Iterator ci = config_->begin(); ci != config_->end(); ci++ )
00141 {
00142 std::string name = (*ci).first;
00143 std::string value = (*ci).second;
00144
00145 if( name.find( name_ + "." ) == 0 )
00146 {
00147 properties_[ name ] = value;
00148 }
00149 }
00150 }
00151
00152 void DisplayWrapper::onDisplaysConfigLoaded(const boost::shared_ptr<Config>& config)
00153 {
00154 config_ = config;
00155
00156 loadProperties();
00157 }
00158
00159 void DisplayWrapper::onDisplaysConfigSaved(const boost::shared_ptr<Config>& config)
00160 {
00161 if (display_)
00162 {
00163 return;
00164 }
00165
00166 M_string::iterator it = properties_.begin();
00167 M_string::iterator end = properties_.end();
00168 for (; it != end; ++it)
00169 {
00170 const std::string& name = it->first;
00171 const std::string& value = it->second;
00172 config->set(name, value);
00173 }
00174 }
00175
00176 bool DisplayWrapper::isLoaded() const
00177 {
00178 return display_ != 0;
00179 }
00180
00181 void DisplayWrapper::createDisplay()
00182 {
00183 if( display_ )
00184 {
00185 return;
00186 }
00187
00188 Q_EMIT displayCreating( this );
00189
00190 try
00191 {
00192 display_ = class_loader_->createClassInstance( class_lookup_name_ );
00193 }
00194 catch( pluginlib::PluginlibException& ex )
00195 {
00196 ROS_ERROR( "The plugin for class '%s' failed to load. Error: %s",
00197 class_lookup_name_.c_str(), ex.what() );
00198 }
00199
00200 if (display_)
00201 {
00202 display_->initialize( name_, manager_ );
00203 if (property_manager_)
00204 {
00205 display_->setPropertyManager( property_manager_, category_ );
00206 }
00207
00208 Q_EMIT displayCreated( this );
00209 }
00210 }
00211
00212 void DisplayWrapper::destroyDisplay()
00213 {
00214 if (display_)
00215 {
00216 Q_EMIT displayDestroying( this );
00217
00218 display_->disable(false);
00219 delete display_;
00220 display_ = 0;
00221
00222 Q_EMIT displayDestroyed( this );
00223 }
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 bool DisplayWrapper::isEnabled()
00249 {
00250 if (display_)
00251 {
00252 return display_->isEnabled();
00253 }
00254
00255 return enabled_;
00256 }
00257
00258 void DisplayWrapper::setEnabled(bool enabled)
00259 {
00260 if (display_)
00261 {
00262 display_->setEnabled(enabled, false);
00263 }
00264
00265 enabled_ = enabled;
00266
00267 propertyChanged(category_);
00268 }
00269
00270 void DisplayWrapper::setPropertyManager(PropertyManager* property_manager, const CategoryPropertyWPtr& parent)
00271 {
00272 ROS_ASSERT(!property_manager_);
00273
00274 property_manager_ = property_manager;
00275
00276 category_ = property_manager_->createCheckboxCategory( getName(), "Enabled", getName() + ".", boost::bind( &DisplayWrapper::isEnabled, this ),
00277 boost::bind( &DisplayWrapper::setEnabled, this, _1 ), parent, this );
00278
00279 std::string help_description = class_loader_->getClassDescription( class_lookup_name_ );
00280 setPropertyHelpText(category_, help_description);
00281
00282 if (display_)
00283 {
00284 display_->setPropertyManager(property_manager, category_);
00285 }
00286 }
00287
00288 std::string DisplayWrapper::getClassDisplayName() const
00289 {
00290 return class_loader_->getName( class_lookup_name_ );
00291 }
00292
00293 }