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