vector_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 {
37 VectorProperty::VectorProperty(const QString& name,
38  const Ogre::Vector3& default_value,
39  const QString& description,
40  Property* parent)
41  : Property(name, QVariant(), description, parent), vector_(default_value), ignore_child_updates_(false)
42 {
43  x_ = new Property("X", vector_.x, "X coordinate", this);
44  y_ = new Property("Y", vector_.y, "Y coordinate", this);
45  z_ = new Property("Z", vector_.z, "Z coordinate", this);
46  updateString();
53 }
54 
55 bool VectorProperty::setVector(const Ogre::Vector3& new_vector)
56 {
57  if (new_vector != vector_)
58  {
59  Q_EMIT aboutToChange();
60  vector_ = new_vector;
61  ignore_child_updates_ = true;
62  x_->setValue(vector_.x);
63  y_->setValue(vector_.y);
64  z_->setValue(vector_.z);
65  ignore_child_updates_ = false;
66  updateString();
67  Q_EMIT changed();
68  if (model_)
69  model_->emitDataChanged(this);
70  return true;
71  }
72  return false;
73 }
74 
75 bool VectorProperty::setValue(const QVariant& new_value)
76 {
77  QStringList strings = new_value.toString().split(';');
78  if (strings.size() >= 3)
79  {
80  bool x_ok = true;
81  float x = strings[0].toFloat(&x_ok);
82  bool y_ok = true;
83  float y = strings[1].toFloat(&y_ok);
84  bool z_ok = true;
85  float z = strings[2].toFloat(&z_ok);
86  if (x_ok && y_ok && z_ok)
87  {
88  return setVector(Ogre::Vector3(x, y, z));
89  }
90  }
91  return false;
92 }
93 
95 {
97  {
98  vector_.x = x_->getValue().toFloat();
99  vector_.y = y_->getValue().toFloat();
100  vector_.z = z_->getValue().toFloat();
101  updateString();
102  Q_EMIT changed();
103  }
104 }
105 
107 {
109  {
110  Q_EMIT aboutToChange();
111  }
112 }
113 
115 {
116  value_ =
117  QString("%1; %2; %3").arg(vector_.x, 0, 'g', 5).arg(vector_.y, 0, 'g', 5).arg(vector_.z, 0, 'g', 5);
118 }
119 
120 void VectorProperty::load(const Config& config)
121 {
122  float x, y, z;
123  if (config.mapGetFloat("X", &x) && config.mapGetFloat("Y", &y) && config.mapGetFloat("Z", &z))
124  {
125  // Calling setVector() once explicitly is better than letting the
126  // Property class load the X, Y, and Z children independently,
127  // which would result in at least 3 calls to setVector().
128  setVector(Ogre::Vector3(x, y, z));
129  }
130 }
131 
132 void VectorProperty::save(Config config) const
133 {
134  // Saving the child values explicitly avoids having Property::save()
135  // save the summary string version of the property.
136  if (getReadOnly())
137  return;
138  config.mapSetValue("X", x_->getValue());
139  config.mapSetValue("Y", y_->getValue());
140  config.mapSetValue("Z", z_->getValue());
141 }
142 
143 void VectorProperty::setReadOnly(bool read_only)
144 {
145  Property::setReadOnly(read_only);
146  x_->setReadOnly(read_only);
147  y_->setReadOnly(read_only);
148  z_->setReadOnly(read_only);
149 }
150 
151 } // end namespace rviz
rviz::VectorProperty::emitAboutToChange
void emitAboutToChange()
Definition: vector_property.cpp:106
description
description
rviz::VectorProperty::updateFromChildren
void updateFromChildren()
Definition: vector_property.cpp:94
rviz::VectorProperty::x_
Property * x_
Definition: vector_property.h:98
rviz::VectorProperty::load
void load(const Config &config) override
Load the value of this property and/or its children from the given Config reference.
Definition: vector_property.cpp:120
rviz::VectorProperty::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: vector_property.cpp:75
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::VectorProperty::VectorProperty
VectorProperty(const QString &name=QString(), const Ogre::Vector3 &default_value=Ogre::Vector3::ZERO, const QString &description=QString(), Property *parent=nullptr)
Definition: vector_property.cpp:37
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
rviz::Property
A single element of a property tree, with a name, value, description, and possibly children.
Definition: property.h:100
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::VectorProperty::setVector
virtual bool setVector(const Ogre::Vector3 &vector)
Definition: vector_property.cpp:55
rviz::VectorProperty::y_
Property * y_
Definition: vector_property.h:99
rviz::Property::model_
PropertyTreeModel * model_
Pointer to the PropertyTreeModel managing this property tree.
Definition: property.h:560
property_tree_model.h
rviz::VectorProperty::z_
Property * z_
Definition: vector_property.h:100
rviz::Property::setReadOnly
virtual void setReadOnly(bool read_only)
Prevent or allow users to edit this property from a PropertyTreeWidget.
Definition: property.h:497
vector_property.h
rviz::VectorProperty::setReadOnly
void setReadOnly(bool read_only) override
Overridden from Property to propagate read-only-ness to children.
Definition: vector_property.cpp:143
default_value
def default_value(type_)
rviz::Property::changed
void changed()
Emitted by setValue() just after the value has changed.
rviz::VectorProperty::save
void save(Config config) const override
Write the value of this property and/or its children into the given Config reference.
Definition: vector_property.cpp:132
rviz::VectorProperty::updateString
void updateString()
Definition: vector_property.cpp:114
rviz::VectorProperty::ignore_child_updates_
bool ignore_child_updates_
Definition: vector_property.h:102
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::Config
Configuration data storage class.
Definition: config.h:124
rviz::VectorProperty::vector_
Ogre::Vector3 vector_
Definition: vector_property.h:97
rviz::Property::getReadOnly
virtual bool getReadOnly() const
Return the read-only-ness of this property.
Definition: property.h:504


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Sat Jun 1 2024 02:31:53