quaternion_property.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QStringList>
31 
33 
34 namespace rviz
35 {
36 
38  const Ogre::Quaternion& default_value,
39  const QString& description,
40  Property* parent,
41  const char *changed_slot,
42  QObject* receiver )
43  : Property( name, QVariant(), description, parent, changed_slot, receiver )
44  , quaternion_( default_value )
45  , ignore_child_updates_( false )
46 {
47  x_ = new Property( "X", quaternion_.x, "X coordinate", this );
48  y_ = new Property( "Y", quaternion_.y, "Y coordinate", this );
49  z_ = new Property( "Z", quaternion_.z, "Z coordinate", this );
50  w_ = new Property( "W", quaternion_.w, "W coordinate", this );
51  updateString();
52  connect( x_, SIGNAL( aboutToChange() ), this, SLOT( emitAboutToChange() ));
53  connect( y_, SIGNAL( aboutToChange() ), this, SLOT( emitAboutToChange() ));
54  connect( z_, SIGNAL( aboutToChange() ), this, SLOT( emitAboutToChange() ));
55  connect( w_, SIGNAL( aboutToChange() ), this, SLOT( emitAboutToChange() ));
56  connect( x_, SIGNAL( changed() ), this, SLOT( updateFromChildren() ));
57  connect( y_, SIGNAL( changed() ), this, SLOT( updateFromChildren() ));
58  connect( z_, SIGNAL( changed() ), this, SLOT( updateFromChildren() ));
59  connect( w_, SIGNAL( changed() ), this, SLOT( updateFromChildren() ));
60 }
61 
62 bool QuaternionProperty::setQuaternion( const Ogre::Quaternion& new_quaternion )
63 {
64  if( new_quaternion != quaternion_ ) {
65  Q_EMIT aboutToChange();
66  quaternion_ = new_quaternion;
67  ignore_child_updates_ = true;
68  x_->setValue( quaternion_.x );
69  y_->setValue( quaternion_.y );
70  z_->setValue( quaternion_.z );
71  w_->setValue( quaternion_.w );
72  ignore_child_updates_ = false;
73  updateString();
74  Q_EMIT changed();
75  return true;
76  }
77  return false;
78 }
79 
80 bool QuaternionProperty::setValue( const QVariant& new_value )
81 {
82  QStringList strings = new_value.toString().split( ';' );
83  if( strings.size() >= 4 )
84  {
85  bool x_ok = true;
86  float x = strings[ 0 ].toFloat( &x_ok );
87  bool y_ok = true;
88  float y = strings[ 1 ].toFloat( &y_ok );
89  bool z_ok = true;
90  float z = strings[ 2 ].toFloat( &z_ok );
91  bool w_ok = true;
92  float w = strings[ 3 ].toFloat( &w_ok );
93  if( x_ok && y_ok && z_ok && w_ok )
94  {
95  return setQuaternion( Ogre::Quaternion( w, x, y, z ));
96  }
97  }
98  return false;
99 }
100 
102 {
103  if( !ignore_child_updates_ )
104  {
105  quaternion_.x = x_->getValue().toFloat();
106  quaternion_.y = y_->getValue().toFloat();
107  quaternion_.z = z_->getValue().toFloat();
108  quaternion_.w = w_->getValue().toFloat();
109  updateString();
110  Q_EMIT changed();
111  }
112 }
113 
115 {
116  if( !ignore_child_updates_ )
117  {
118  Q_EMIT aboutToChange();
119  }
120 }
121 
123 {
124  value_ = QString( "%1; %2; %3; %4" )
125  .arg( quaternion_.x, 0, 'g', 5 )
126  .arg( quaternion_.y, 0, 'g', 5 )
127  .arg( quaternion_.z, 0, 'g', 5 )
128  .arg( quaternion_.w, 0, 'g', 5 );
129 }
130 
131 void QuaternionProperty::load( const Config& config )
132 {
133  float x, y, z, w;
134  if( config.mapGetFloat( "X", &x ) &&
135  config.mapGetFloat( "Y", &y ) &&
136  config.mapGetFloat( "Z", &z ) &&
137  config.mapGetFloat( "W", &w ))
138  {
139  // Calling setQuaternion() once explicitly is better than letting
140  // the Property class load the X, Y, Z, and W children
141  // independently, which would result in at least 4 calls to
142  // setQuaternion().
143  setQuaternion( Ogre::Quaternion( w, x, y, z ));
144  }
145 }
146 
147 void QuaternionProperty::save( Config config ) const
148 {
149  // Saving the child values explicitly avoids having Property::save()
150  // save the summary string version of the property.
151  config.mapSetValue( "X", x_->getValue() );
152  config.mapSetValue( "Y", y_->getValue() );
153  config.mapSetValue( "Z", z_->getValue() );
154  config.mapSetValue( "W", w_->getValue() );
155 }
156 
157 void QuaternionProperty::setReadOnly( bool read_only )
158 {
159  Property::setReadOnly( read_only );
160  x_->setReadOnly( read_only );
161  y_->setReadOnly( read_only );
162  z_->setReadOnly( read_only );
163  w_->setReadOnly( read_only );
164 }
165 
166 
167 } // end namespace rviz
void changed()
Emitted by setValue() just after the value has changed.
virtual bool setValue(const QVariant &new_value)
Set the new value for this property. Returns true if the new value is different from the old value...
Definition: property.cpp:130
A single element of a property tree, with a name, value, description, and possibly children...
Definition: property.h:100
bool mapGetFloat(const QString &key, float *value_out) const
Convenience function for looking up a named float.
Definition: config.cpp:245
virtual void save(Config config) const
Write the value of this property and/or its children into the given Config reference.
virtual void setReadOnly(bool read_only)
Overridden from Property to propagate read-only-ness to children.
TFSIMD_FORCE_INLINE const tfScalar & y() const
QVariant value_
This is the central property value. If you set it directly in a subclass, do so with care because man...
Definition: property.h:440
void mapSetValue(const QString &key, QVariant value)
Set a named child to the given value.
Definition: config.cpp:185
Configuration data storage class.
Definition: config.h:125
virtual bool setValue(const QVariant &new_value)
Set the new value for this property. Returns true if the new value is different from the old value...
virtual void load(const Config &config)
Load the value of this property and/or its children from the given Config node.
QuaternionProperty(const QString &name=QString(), const Ogre::Quaternion &default_value=Ogre::Quaternion::IDENTITY, const QString &description=QString(), Property *parent=0, const char *changed_slot=0, QObject *receiver=0)
virtual bool setQuaternion(const Ogre::Quaternion &quaternion)
TFSIMD_FORCE_INLINE const tfScalar & x() const
TFSIMD_FORCE_INLINE const tfScalar & z() const
TFSIMD_FORCE_INLINE const tfScalar & w() const
Property(const QString &name=QString(), const QVariant default_value=QVariant(), const QString &description=QString(), Property *parent=0, const char *changed_slot=0, QObject *receiver=0)
Constructor.
Definition: property.cpp:54
void aboutToChange()
Emitted by setValue() just before the value has changed.
virtual QVariant getValue() const
Return the value of this Property as a QVariant. If the value has never been set, an invalid QVariant...
Definition: property.cpp:145
virtual void setReadOnly(bool read_only)
Prevent or allow users to edit this property from a PropertyTreeWidget.
Definition: property.h:397


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat Apr 27 2019 02:33:42