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


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Tue Oct 3 2017 03:19:31