property.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, 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 #ifndef RVIZ_PROPERTY_H
00031 #define RVIZ_PROPERTY_H
00032 
00033 #include <float.h>
00034 #include <limits.h>
00035 
00036 #include "rviz/helpers/color.h"
00037 #include "forwards.h"
00038 #include "rviz/status_level.h"
00039 
00040 #include <boost/function.hpp>
00041 #include <boost/enable_shared_from_this.hpp>
00042 #include <boost/thread/mutex.hpp>
00043 
00044 #include <ros/console.h>
00045 #include <ros/assert.h>
00046 
00047 #include <OGRE/OgreVector3.h>
00048 #include <OGRE/OgreQuaternion.h>
00049 
00050 #include <string>
00051 #include <vector>
00052 
00053 class QColor;
00054 
00055 namespace rviz
00056 {
00057 
00058 class Config;
00059 class PropertyTreeWidget;
00060 class PropertyWidgetItem;
00061 class CategoryProperty;
00062 class VisualizationManager;
00063 
00064 void setPropertyHelpText(PropertyTreeWidget* grid, PropertyWidgetItem* property, const std::string& text);
00065 void setPropertyToColors(PropertyTreeWidget* grid, PropertyWidgetItem* property, const QColor& fg_color, const QColor& bg_color, uint32_t column = 0);
00066 void setPropertyToError(PropertyTreeWidget* grid, PropertyWidgetItem* property, uint32_t column = 0);
00067 void setPropertyToWarn(PropertyTreeWidget* grid, PropertyWidgetItem* property, uint32_t column = 0);
00068 void setPropertyToOK(PropertyTreeWidget* grid, PropertyWidgetItem* property, uint32_t column = 0);
00069 void setPropertyToDisabled(PropertyTreeWidget* grid, PropertyWidgetItem* property, uint32_t column = 0);
00070 
00074 class PropertyBase : public boost::enable_shared_from_this<PropertyBase>
00075 {
00076 public:
00077   PropertyBase();
00078   virtual ~PropertyBase();
00079   void writeToGrid();
00080   virtual void doWriteToGrid() = 0;
00081   virtual void readFromGrid() = 0;
00082   virtual void saveToConfig( Config* config ) = 0;
00083   virtual void loadFromConfig( Config* config ) = 0;
00084 
00085   virtual std::string getName() = 0;
00086   virtual std::string getPrefix() = 0;
00087   virtual void setPrefix(const std::string& prefix) = 0;
00088   virtual bool getSave() = 0;
00089 
00090   virtual PropertyWidgetItem* getWidgetItem() = 0;
00091 
00092   virtual CategoryPropertyWPtr getParent() = 0;
00093 
00094   virtual void addLegacyName(const std::string& name) = 0;
00095 
00096   virtual void setPropertyTreeWidget(PropertyTreeWidget* grid);
00097   virtual PropertyTreeWidget* getPropertyTreeWidget() { return grid_; }
00098 
00099   virtual void setUserData(void* user_data) { user_data_ = user_data; }
00100   void* getUserData() { return user_data_; }
00101 
00102   virtual void reset();
00103 
00104   virtual void show();
00105   virtual void hide();
00106 
00107   virtual bool isSelected();
00108 
00112   void changed();
00113 
00115   void configChanged();
00116 
00117 protected:
00118   PropertyTreeWidget* grid_;
00119   PropertyWidgetItem* widget_item_;
00120   void* user_data_;
00121 
00122 private:
00123   // I needed to purge boost::signal, and I didn't really want to make
00124   // every Property a QObject.  There is only one class which needs to
00125   // be notified of property changes, and that is PropertyManager.
00126   // Therefore I'm making an explicit function call connection instead
00127   // of a Qt signal or a boost signal.
00128   friend class PropertyManager;
00129   PropertyManager* manager_;
00130 };
00131 
00132 class StatusProperty : public PropertyBase
00133 {
00134 public:
00135   StatusProperty(const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, void* user_data);
00136   ~StatusProperty();
00137 
00138   virtual void doWriteToGrid();
00139   virtual void readFromGrid() {}
00140   virtual void saveToConfig(Config* config) {}
00141   virtual void loadFromConfig(Config* config) {}
00142 
00143   virtual std::string getName() { return name_; }
00144   virtual std::string getPrefix() { return prefix_; }
00145   virtual void setPrefix(const std::string& prefix);
00146   virtual bool getSave() { return false; }
00147 
00148   virtual CategoryPropertyWPtr getParent() { return parent_; }
00149 
00150   virtual PropertyWidgetItem* getWidgetItem() { return 0; }
00151   virtual void addLegacyName(const std::string& name) {}
00152 
00153   void setStatus(StatusLevel status, const std::string& name, const std::string& text);
00154   void deleteStatus(const std::string& name);
00155   void clear();
00156 
00157   void disable();
00158   void enable();
00159 
00160   StatusLevel getTopLevelStatus();
00161 
00162 private:
00163   void updateTopLevelStatus();
00164 
00165   std::string name_;
00166   std::string prefix_;
00167   CategoryPropertyWPtr parent_;
00168 
00169   PropertyWidgetItem* top_widget_item_;
00170 
00171   struct Status
00172   {
00173     Status()
00174     : level(status_levels::Ok)
00175     , widget_item(0)
00176     , kill(false)
00177     {}
00178 
00179     StatusLevel level;
00180     std::string name;
00181     std::string text;
00182     PropertyWidgetItem* widget_item;
00183     bool kill;
00184   };
00185   typedef std::map<std::string, Status> M_StringToStatus;
00186   boost::mutex status_mutex_;
00187   M_StringToStatus statuses_;
00188 
00189   bool enabled_;
00190   bool prefix_changed_;
00191 
00192   StatusLevel top_status_;
00193 };
00194 
00204 template<typename T>
00205 class Property : public PropertyBase
00206 {
00207 public:
00208   typedef boost::function<T (void)> Getter;
00209   typedef boost::function<void (const T&)> Setter;
00210 
00211 public:
00223   Property( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00224   : name_( name )
00225   , prefix_( prefix )
00226   , parent_( parent )
00227   , save_( true )
00228   , getter_( getter )
00229   , setter_( setter )
00230   {
00231     if ( setter_ == 0 )
00232     {
00233       save_ = false;
00234     }
00235   }
00236 
00240   virtual ~Property()
00241   {
00242   }
00243 
00248   T get() { return getter_(); }
00253   void set( const T& val )
00254   {
00255     if( hasSetter() )
00256     {
00257       setter_( val );
00258       changed();
00259       configChanged();
00260     }
00261   }
00262 
00263   bool hasGetter() { return getter_ != 0; }
00264   bool hasSetter() { return setter_ != 0; }
00265 
00270   void setSave( bool save ) { save_ = save; }
00274   bool getSave() { return save_; }
00275 
00280   virtual std::string getName() { return name_; }
00285   virtual std::string getPrefix() { return prefix_; }
00286 
00291   virtual PropertyWidgetItem* getWidgetItem()
00292   {
00293     return widget_item_;
00294   }
00295 
00296   virtual CategoryPropertyWPtr getParent() { return parent_; }
00297 
00298   virtual void addLegacyName(const std::string& name)
00299   {
00300     legacy_names_.push_back(name);
00301   }
00302 
00303   virtual void setToError()
00304   {
00305     setPropertyToError(grid_, widget_item_);
00306   }
00307 
00308   virtual void setToWarn()
00309   {
00310     setPropertyToWarn(grid_, widget_item_);
00311   }
00312 
00313   virtual void setToOK()
00314   {
00315     setPropertyToOK(grid_, widget_item_);
00316   }
00317 
00318   virtual void setToDisabled()
00319   {
00320     setPropertyToDisabled(grid_, widget_item_);
00321   }
00322 
00323   virtual void setHelpText(const std::string& text)
00324   {
00325     help_text_ = text;
00326     changed();
00327   }
00328 
00329   virtual void setPrefix(const std::string& prefix)
00330   {
00331     prefix_ = prefix;
00332   }
00333 
00334 protected:
00335   std::string name_;
00336   std::string prefix_;
00337   CategoryPropertyWPtr parent_;
00338 
00339   bool save_;
00340 
00341   typedef std::vector<std::string> V_string;
00342   V_string legacy_names_;
00343 
00344   std::string help_text_;
00345 
00346 private:
00347   Getter getter_;
00348   Setter setter_;
00349 };
00350 
00351 class BoolProperty : public Property<bool>
00352 {
00353 public:
00354   BoolProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00355   : Property<bool>( name, prefix, parent, getter, setter )
00356   {
00357   }
00358 
00359   virtual void doWriteToGrid();
00360   virtual void readFromGrid();
00361   virtual void saveToConfig( Config* config );
00362   virtual void loadFromConfig( Config* config );
00363 };
00364 
00365 class IntProperty : public Property<int>
00366 {
00367 public:
00368   IntProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00369   : Property<int>( name, prefix, parent, getter, setter )
00370   , min_( INT_MIN )
00371   , max_( INT_MAX )
00372   {
00373   }
00374 
00375   void setMin( int min );
00376   void setMax( int max );
00377 
00378   virtual void doWriteToGrid();
00379   virtual void readFromGrid();
00380   virtual void saveToConfig( Config* config );
00381   virtual void loadFromConfig( Config* config );
00382 
00383 private:
00384   int min_;
00385   int max_;
00386 };
00387 
00388 class FloatProperty : public Property<float>
00389 {
00390 public:
00391   FloatProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00392   : Property<float>( name, prefix, parent, getter, setter )
00393   , min_( -FLT_MAX )
00394   , max_( FLT_MAX )
00395   {
00396   }
00397 
00398   void setMin( float min );
00399   void setMax( float max );
00400 
00401   virtual void doWriteToGrid();
00402   virtual void readFromGrid();
00403   virtual void saveToConfig( Config* config );
00404   virtual void loadFromConfig( Config* config );
00405 
00406 private:
00407   float min_;
00408   float max_;
00409 };
00410 
00411 class StringProperty : public Property<std::string>
00412 {
00413 public:
00414   StringProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00415   : Property<std::string>( name, prefix, parent, getter, setter )
00416   {
00417   }
00418 
00419   virtual void doWriteToGrid();
00420   virtual void readFromGrid();
00421   virtual void saveToConfig( Config* config );
00422   virtual void loadFromConfig( Config* config );
00423 };
00424 
00425 class ROSTopicStringProperty : public StringProperty
00426 {
00427 public:
00428   ROSTopicStringProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00429   : StringProperty( name, prefix, parent, getter, setter )
00430   {
00431   }
00432 
00433   void setMessageType(const std::string& message_type) { message_type_ = message_type; }
00434 
00435   virtual void doWriteToGrid();
00436   virtual void readFromGrid();
00437 
00438 private:
00439   std::string message_type_;
00440 };
00441 
00442 class ColorProperty : public Property<Color>
00443 {
00444 public:
00445   ColorProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00446   : Property<Color>( name, prefix, parent, getter, setter )
00447   {
00448   }
00449 
00450   virtual void doWriteToGrid();
00451   virtual void readFromGrid();
00452   virtual void saveToConfig( Config* config );
00453   virtual void loadFromConfig( Config* config );
00454 };
00455 
00456 typedef std::pair<std::string, int> Choice;
00457 typedef std::vector<Choice> Choices;
00458 
00459 class EnumProperty : public Property<int>
00460 {
00461 public:
00462   EnumProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00463   : Property<int>( name, prefix, parent, getter, setter )
00464   {
00465   }
00466 
00467   void addOption( const std::string& name, int value );
00468   void clear ();
00469 
00470   virtual void doWriteToGrid();
00471   virtual void readFromGrid();
00472   virtual void saveToConfig( Config* config );
00473   virtual void loadFromConfig( Config* config );
00474 
00475 private:
00476   Choices choices_;
00477 
00478   boost::mutex mutex_;
00479 };
00480 
00481 class EditEnumProperty : public Property<std::string>
00482 {
00483 public:
00484   EditEnumProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00485   : Property<std::string>( name, prefix, parent, getter, setter )
00486     {}
00487 
00488   void addOption( const std::string& name );
00489   void clear ();
00490 
00491   void setOptionCallback(const EditEnumOptionCallback& cb);
00492 
00493   virtual void doWriteToGrid();
00494   virtual void readFromGrid();
00495   virtual void saveToConfig( Config* config );
00496   virtual void loadFromConfig( Config* config );
00497 
00498 private:
00499   std::vector<std::string> choices_;
00500   EditEnumOptionCallback option_cb_;
00501 
00502   boost::mutex mutex_;
00503 };
00504 
00505 class TFFrameProperty : public EditEnumProperty
00506 {
00507 public:
00508   TFFrameProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00509   : EditEnumProperty( name, prefix, parent, getter, setter )
00510   {
00511   }
00512 
00513   void optionCallback( V_string& options_out );
00514 
00515   virtual void doWriteToGrid();
00516 };
00517 
00518 
00519 class CategoryProperty : public Property<bool>
00520 {
00521 public:
00522   CategoryProperty( const std::string& label, const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter, bool checkbox )
00523   : Property<bool>( name, prefix, parent, getter, setter )
00524   , label_(label)
00525   , checkbox_(checkbox)
00526   {
00527   }
00528 
00529   virtual ~CategoryProperty();
00530   virtual void reset();
00531 
00532   void setLabel( const std::string& label );
00533   void expand();
00534   void collapse();
00535 
00536   virtual void doWriteToGrid();
00537   virtual void readFromGrid();
00538   virtual void saveToConfig( Config* config );
00539   virtual void loadFromConfig( Config* config );
00540 
00541   virtual void setToOK();
00542 
00543 private:
00544   std::string label_;
00545   bool checkbox_;
00546 };
00547 
00548 class Vector3Property : public Property<Ogre::Vector3>
00549 {
00550 public:
00551   Vector3Property( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00552   : Property<Ogre::Vector3>( name, prefix, parent, getter, setter )
00553   , x_( NULL )
00554   , y_( NULL )
00555   , z_( NULL )
00556   {
00557   }
00558 
00559   virtual void doWriteToGrid();
00560   virtual void readFromGrid();
00561   virtual void saveToConfig( Config* config );
00562   virtual void loadFromConfig( Config* config );
00563   virtual void reset();
00564 
00565   virtual void setToError()
00566   {
00567     setPropertyToError(grid_, widget_item_);
00568     setPropertyToError(grid_, x_);
00569     setPropertyToError(grid_, y_);
00570     setPropertyToError(grid_, z_);
00571   }
00572 
00573   virtual void setToWarn()
00574   {
00575     setPropertyToWarn(grid_, widget_item_);
00576     setPropertyToWarn(grid_, x_);
00577     setPropertyToWarn(grid_, y_);
00578     setPropertyToWarn(grid_, z_);
00579   }
00580 
00581   virtual void setToOK()
00582   {
00583     setPropertyToOK(grid_, widget_item_);
00584     setPropertyToOK(grid_, x_);
00585     setPropertyToOK(grid_, y_);
00586     setPropertyToOK(grid_, z_);
00587   }
00588 
00589   virtual void setToDisabled()
00590   {
00591     setPropertyToDisabled(grid_, widget_item_);
00592     setPropertyToDisabled(grid_, x_);
00593     setPropertyToDisabled(grid_, y_);
00594     setPropertyToDisabled(grid_, z_);
00595   }
00596 
00597 protected:
00598   PropertyWidgetItem* x_;
00599   PropertyWidgetItem* y_;
00600   PropertyWidgetItem* z_;
00601 };
00602 
00603 class QuaternionProperty : public Property<Ogre::Quaternion>
00604 {
00605 public:
00606   QuaternionProperty( const std::string& name, const std::string& prefix, const CategoryPropertyWPtr& parent, const Getter& getter, const Setter& setter )
00607   : Property<Ogre::Quaternion>( name, prefix, parent, getter, setter )
00608   , x_( NULL )
00609   , y_( NULL )
00610   , z_( NULL )
00611   , w_( NULL )
00612   {
00613   }
00614 
00615   virtual void doWriteToGrid();
00616   virtual void readFromGrid();
00617   virtual void saveToConfig( Config* config );
00618   virtual void loadFromConfig( Config* config );
00619   virtual void reset();
00620 
00621   virtual void setToError()
00622   {
00623     setPropertyToError( grid_, widget_item_);
00624     setPropertyToError( grid_, x_);
00625     setPropertyToError( grid_, y_);
00626     setPropertyToError( grid_, z_);
00627     setPropertyToError( grid_, w_);
00628   }
00629 
00630   virtual void setToWarn()
00631   {
00632     setPropertyToWarn( grid_, widget_item_);
00633     setPropertyToWarn( grid_, x_);
00634     setPropertyToWarn( grid_, y_);
00635     setPropertyToWarn( grid_, z_);
00636     setPropertyToWarn( grid_, w_);
00637   }
00638 
00639   virtual void setToOK()
00640   {
00641     setPropertyToOK( grid_, widget_item_);
00642     setPropertyToOK( grid_, x_);
00643     setPropertyToOK( grid_, y_);
00644     setPropertyToOK( grid_, z_);
00645     setPropertyToOK( grid_, w_);
00646   }
00647 
00648   virtual void setToDisabled()
00649   {
00650     setPropertyToDisabled( grid_, widget_item_);
00651     setPropertyToDisabled( grid_, x_);
00652     setPropertyToDisabled( grid_, y_);
00653     setPropertyToDisabled( grid_, z_);
00654     setPropertyToDisabled( grid_, w_);
00655   }
00656 
00657 protected:
00658   PropertyWidgetItem* x_;
00659   PropertyWidgetItem* y_;
00660   PropertyWidgetItem* z_;
00661   PropertyWidgetItem* w_;
00662 };
00663 
00664 
00665 } // namespace rviz
00666 
00667 #endif


rviz
Author(s): Dave Hershberger, Josh Faust
autogenerated on Mon Jan 6 2014 11:54:32