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 <QStringList>
00031
00032 #include <yaml-cpp/emitter.h>
00033 #include <yaml-cpp/node.h>
00034
00035 #include "rviz/properties/vector_property.h"
00036
00037 namespace rviz
00038 {
00039
00040 VectorProperty::VectorProperty( const QString& name,
00041 const Ogre::Vector3& default_value,
00042 const QString& description,
00043 Property* parent,
00044 const char *changed_slot,
00045 QObject* receiver )
00046 : Property( name, QVariant(), description, parent, changed_slot, receiver )
00047 , vector_( default_value )
00048 , ignore_child_updates_( false )
00049 {
00050 x_ = new Property( "X", vector_.x, "X coordinate", this );
00051 y_ = new Property( "Y", vector_.y, "Y coordinate", this );
00052 z_ = new Property( "Z", vector_.z, "Z coordinate", this );
00053 updateString();
00054 connect( x_, SIGNAL( aboutToChange() ), this, SLOT( emitAboutToChange() ));
00055 connect( y_, SIGNAL( aboutToChange() ), this, SLOT( emitAboutToChange() ));
00056 connect( z_, SIGNAL( aboutToChange() ), this, SLOT( emitAboutToChange() ));
00057 connect( x_, SIGNAL( changed() ), this, SLOT( updateFromChildren() ));
00058 connect( y_, SIGNAL( changed() ), this, SLOT( updateFromChildren() ));
00059 connect( z_, SIGNAL( changed() ), this, SLOT( updateFromChildren() ));
00060 }
00061
00062 bool VectorProperty::setVector( const Ogre::Vector3& new_vector )
00063 {
00064 if( new_vector != vector_ ) {
00065 Q_EMIT aboutToChange();
00066 vector_ = new_vector;
00067 ignore_child_updates_ = true;
00068 x_->setValue( vector_.x );
00069 y_->setValue( vector_.y );
00070 z_->setValue( vector_.z );
00071 ignore_child_updates_ = false;
00072 updateString();
00073 Q_EMIT changed();
00074 return true;
00075 }
00076 return false;
00077 }
00078
00079 bool VectorProperty::setValue( const QVariant& new_value )
00080 {
00081 QStringList strings = new_value.toString().split( ';' );
00082 if( strings.size() >= 3 )
00083 {
00084 bool x_ok = true;
00085 float x = strings[ 0 ].toFloat( &x_ok );
00086 bool y_ok = true;
00087 float y = strings[ 1 ].toFloat( &y_ok );
00088 bool z_ok = true;
00089 float z = strings[ 2 ].toFloat( &z_ok );
00090 if( x_ok && y_ok && z_ok )
00091 {
00092 return setVector( Ogre::Vector3( x, y, z ));
00093 }
00094 }
00095 return false;
00096 }
00097
00098 void VectorProperty::updateFromChildren()
00099 {
00100 if( !ignore_child_updates_ )
00101 {
00102 vector_.x = x_->getValue().toFloat();
00103 vector_.y = y_->getValue().toFloat();
00104 vector_.z = z_->getValue().toFloat();
00105 updateString();
00106 Q_EMIT changed();
00107 }
00108 }
00109
00110 void VectorProperty::emitAboutToChange()
00111 {
00112 if( !ignore_child_updates_ )
00113 {
00114 Q_EMIT aboutToChange();
00115 }
00116 }
00117
00118 void VectorProperty::updateString()
00119 {
00120 value_ = QString( "%1; %2; %3" )
00121 .arg( vector_.x, 0, 'g', 5 )
00122 .arg( vector_.y, 0, 'g', 5 )
00123 .arg( vector_.z, 0, 'g', 5 );
00124 }
00125
00126 void VectorProperty::load( const YAML::Node& yaml_node )
00127 {
00128 float x, y, z;
00129 yaml_node[ "X" ] >> x;
00130 yaml_node[ "Y" ] >> y;
00131 yaml_node[ "Z" ] >> z;
00132 setVector( Ogre::Vector3( x, y, z ));
00133 }
00134
00135 void VectorProperty::save( YAML::Emitter& emitter )
00136 {
00137 emitter << YAML::Flow;
00138 emitter << YAML::BeginMap;
00139 emitter << YAML::Key << "X" << YAML::Value;
00140 x_->save( emitter );
00141 emitter << YAML::Key << "Y" << YAML::Value;
00142 y_->save( emitter );
00143 emitter << YAML::Key << "Z" << YAML::Value;
00144 z_->save( emitter );
00145 emitter << YAML::EndMap;
00146 }
00147
00148 void VectorProperty::setReadOnly( bool read_only )
00149 {
00150 Property::setReadOnly( read_only );
00151 x_->setReadOnly( read_only );
00152 y_->setReadOnly( read_only );
00153 z_->setReadOnly( read_only );
00154 }
00155
00156 }