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 
34 
35 namespace rviz
36 {
38  const Ogre::Quaternion& default_value,
39  const QString& description,
40  Property* parent)
41  : Property(name, QVariant(), description, parent)
42  , quaternion_(default_value)
43  , ignore_child_updates_(false)
44 {
45  x_ = new Property("X", quaternion_.x, "X coordinate", this);
46  y_ = new Property("Y", quaternion_.y, "Y coordinate", this);
47  z_ = new Property("Z", quaternion_.z, "Z coordinate", this);
48  w_ = new Property("W", quaternion_.w, "W coordinate", this);
49  updateString();
58 }
59 
60 bool QuaternionProperty::setQuaternion(const Ogre::Quaternion& new_quaternion)
61 {
62  if (new_quaternion != quaternion_)
63  {
64  Q_EMIT aboutToChange();
65  quaternion_ = new_quaternion;
66  ignore_child_updates_ = true;
71  ignore_child_updates_ = false;
72  updateString();
73  Q_EMIT changed();
74  if (model_)
75  model_->emitDataChanged(this);
76  return true;
77  }
78  return false;
79 }
80 
81 bool QuaternionProperty::setValue(const QVariant& new_value)
82 {
83  QStringList strings = new_value.toString().split(';');
84  if (strings.size() >= 4)
85  {
86  bool x_ok = true;
87  float x = strings[0].toFloat(&x_ok);
88  bool y_ok = true;
89  float y = strings[1].toFloat(&y_ok);
90  bool z_ok = true;
91  float z = strings[2].toFloat(&z_ok);
92  bool w_ok = true;
93  float w = strings[3].toFloat(&w_ok);
94  if (x_ok && y_ok && z_ok && w_ok)
95  {
96  return setQuaternion(Ogre::Quaternion(w, x, y, z));
97  }
98  }
99  return false;
100 }
101 
103 {
105  {
106  quaternion_.x = x_->getValue().toFloat();
107  quaternion_.y = y_->getValue().toFloat();
108  quaternion_.z = z_->getValue().toFloat();
109  quaternion_.w = w_->getValue().toFloat();
110  updateString();
111  Q_EMIT changed();
112  }
113 }
114 
116 {
118  {
119  Q_EMIT aboutToChange();
120  }
121 }
122 
124 {
125  value_ = QString("%1; %2; %3; %4")
126  .arg(quaternion_.x, 0, 'g', 5)
127  .arg(quaternion_.y, 0, 'g', 5)
128  .arg(quaternion_.z, 0, 'g', 5)
129  .arg(quaternion_.w, 0, 'g', 5);
130 }
131 
132 void QuaternionProperty::load(const Config& config)
133 {
134  float x, y, z, w;
135  if (config.mapGetFloat("X", &x) && config.mapGetFloat("Y", &y) && config.mapGetFloat("Z", &z) &&
136  config.mapGetFloat("W", &w))
137  {
138  // Calling setQuaternion() once explicitly is better than letting
139  // the Property class load the X, Y, Z, and W children
140  // independently, which would result in at least 4 calls to
141  // setQuaternion().
142  setQuaternion(Ogre::Quaternion(w, x, y, z));
143  }
144 }
145 
147 {
148  // Saving the child values explicitly avoids having Property::save()
149  // save the summary string version of the property.
150  if (getReadOnly())
151  return;
152  config.mapSetValue("X", x_->getValue());
153  config.mapSetValue("Y", y_->getValue());
154  config.mapSetValue("Z", z_->getValue());
155  config.mapSetValue("W", w_->getValue());
156 }
157 
159 {
160  Property::setReadOnly(read_only);
161  x_->setReadOnly(read_only);
162  y_->setReadOnly(read_only);
163  z_->setReadOnly(read_only);
164  w_->setReadOnly(read_only);
165 }
166 
167 
168 } // end namespace rviz
rviz::QuaternionProperty::updateFromChildren
void updateFromChildren()
Definition: quaternion_property.cpp:102
rviz::QuaternionProperty::setValue
bool setValue(const QVariant &new_value) override
Set the new value for this property. Returns true if the new value is different from the old value,...
Definition: quaternion_property.cpp:81
rviz::QuaternionProperty::z_
Property * z_
Definition: quaternion_property.h:98
rviz::QuaternionProperty::QuaternionProperty
QuaternionProperty(const QString &name=QString(), const Ogre::Quaternion &default_value=Ogre::Quaternion::IDENTITY, const QString &description=QString(), Property *parent=nullptr)
Definition: quaternion_property.cpp:37
rviz::QuaternionProperty::x_
Property * x_
Definition: quaternion_property.h:96
description
description
rviz::QuaternionProperty::setReadOnly
void setReadOnly(bool read_only) override
Overridden from Property to propagate read-only-ness to children.
Definition: quaternion_property.cpp:158
rviz::Property::getValue
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:150
rviz::Property::Property
Property(const QString &name=QString(), const QVariant &default_value=QVariant(), const QString &description=QString(), Property *parent=nullptr)
Constructor.
Definition: property.cpp:59
rviz::Property::connect
QMetaObject::Connection connect(const QObject *receiver, const char *slot, Qt::ConnectionType type=Qt::AutoConnection)
Connect changed() signal to given slot of receiver.
Definition: property.cpp:78
quaternion_property.h
rviz::Property
A single element of a property tree, with a name, value, description, and possibly children.
Definition: property.h:100
rviz::QuaternionProperty::load
void load(const Config &config) override
Load the value of this property and/or its children from the given Config node.
Definition: quaternion_property.cpp:132
rviz::PropertyTreeModel::emitDataChanged
void emitDataChanged(Property *property, bool emit_config_changed=true)
Definition: property_tree_model.cpp:294
rviz::Property::setValue
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:134
rviz
Definition: add_display_dialog.cpp:54
rviz::QuaternionProperty::ignore_child_updates_
bool ignore_child_updates_
Definition: quaternion_property.h:100
rviz::QuaternionProperty::setQuaternion
virtual bool setQuaternion(const Ogre::Quaternion &quaternion)
Definition: quaternion_property.cpp:60
rviz::Property::model_
PropertyTreeModel * model_
Pointer to the PropertyTreeModel managing this property tree.
Definition: property.h:560
rviz::QuaternionProperty::emitAboutToChange
void emitAboutToChange()
Definition: quaternion_property.cpp:115
rviz::QuaternionProperty::y_
Property * y_
Definition: quaternion_property.h:97
property_tree_model.h
rviz::Property::setReadOnly
virtual void setReadOnly(bool read_only)
Prevent or allow users to edit this property from a PropertyTreeWidget.
Definition: property.h:497
default_value
def default_value(type_)
rviz::Property::changed
void changed()
Emitted by setValue() just after the value has changed.
rviz::QuaternionProperty::quaternion_
Ogre::Quaternion quaternion_
Definition: quaternion_property.h:95
rviz::QuaternionProperty::updateString
void updateString()
Definition: quaternion_property.cpp:123
rviz::Property::value_
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:546
rviz::Property::aboutToChange
void aboutToChange()
Emitted by setValue() just before the value has changed.
config
config
rviz::QuaternionProperty::save
void save(Config config) const override
Write the value of this property and/or its children into the given Config reference.
Definition: quaternion_property.cpp:146
rviz::Config
Configuration data storage class.
Definition: config.h:124
rviz::Property::getReadOnly
virtual bool getReadOnly() const
Return the read-only-ness of this property.
Definition: property.h:504
rviz::QuaternionProperty::w_
Property * w_
Definition: quaternion_property.h:99


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10