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


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:28