PropertyBrowserWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 /* Qt headers */
25 #include <QGridLayout>
26 #include <QLabel>
27 #include <QIcon>
28 #include <QMap>
29 #include <VirtualRobot/MathTools.h>
30 
31 /* boost headers */
32 #include <boost/format.hpp>
33 
34 /* QtPropertyBrower headers */
35 #include <ArmarXGui/libraries/qtpropertybrowser/src/QtProperty>
36 
37 #include "../controller/RotateTranslateOperation.h"
38 #include "PropertyBrowserWidget.h"
39 
41  QtTreePropertyBrowser(parent),
43  spinBoxFactory(new QtDoubleSpinBoxFactory(this)),
44  doubleManager(new QtDoublePropertyManager(this)),
45  groupManager(new QtGroupPropertyManager(this)),
46  stringManager(new QtStringPropertyManager(this))
47 {
48  setHeaderVisible(false);
49 
50  connect(doubleManager, SIGNAL(valueChanged(QtProperty*, double)),
51  this, SLOT(valueChanged(QtProperty*, double)));
52 }
53 
54 void gui::PropertyBrowserWidget::setProperties(const memoryx::ObjectClassPtr& objectClass, std::string collection)
55 {
56  this->clearAll();
57 
58  if (objectClass && collection.length() != 0)
59  {
60  this->setClassAttributes(objectClass, collection, true);
61  this->setFactoryForManager(qobject_cast<QtDoublePropertyManager*>(doubleManager), qobject_cast<QtDoubleSpinBoxFactory*>(spinBoxFactory));
62  }
63 
64 }
65 
67 {
68  this->clearAll();
69 
70  if (sceneObject)
71  {
72  currentSceneObject = sceneObject;
73  this->setInstanceAttributes();
74  this->setFactoryForManager(qobject_cast<QtDoublePropertyManager*>(doubleManager), qobject_cast<QtDoubleSpinBoxFactory*>(spinBoxFactory));
75  }
76 
77 }
78 
79 void gui::PropertyBrowserWidget::valueChanged(QtProperty* property, double newValue)
80 {
81  //only react to changes done in propertyBrowser
82  if (!propertyToId.contains(property) || !currentSceneObject || propertyToValue[property] == newValue)
83  {
84  return;
85  }
86 
88  {
89  propertyToValue[property] = newValue;
90  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
91  // Create RotateTranslateOperation for each object
92  controller::OperationPtr operation(new controller::RotateTranslateOperation(controller->getMemoryXController(),
93  controller->getScene(),
94  currentSceneObject->getObjectId(),
95  currentSceneObject->getRotation(),
96  this->createNewRotation(property, newValue),
97  currentSceneObject->getTranslation(),
98  this->createNewTranslation(property, newValue)));
99  operations->push_back(operation);
101  }
102 
103 }
104 
105 void gui::PropertyBrowserWidget::setInstanceAttributes()
106 {
107  QtProperty* instanceAttributes = groupManager->addProperty(tr("Instance Attributes"));
108 
109  QtProperty* objectID = stringManager->addProperty(tr("Object ID"));
110  stringManager->setValue(objectID, QString::fromStdString(currentSceneObject->getObjectId()));
111  instanceAttributes->addSubProperty(objectID);
112 
113  // rotation as quaternion properties
114  QtProperty* quaternion = groupManager->addProperty(tr("Rotation as Quaternion"));
115 
116  float xQuat, yQuat, zQuat, wQuat;
117  currentSceneObject->getRotation().getValue(xQuat, yQuat, zQuat, wQuat);
118 
119  QtProperty* quaternionX = doubleManager->addProperty(tr("x"));
120  QtProperty* quaternionY = doubleManager->addProperty(tr("y"));
121  QtProperty* quaternionZ = doubleManager->addProperty(tr("z"));
122  QtProperty* quaternionW = doubleManager->addProperty(tr("w"));
123 
124  // update values in propertyToValue to not react to the comming changes
125  propertyToValue[quaternionX] = (double) xQuat;
126  propertyToValue[quaternionY] = (double) yQuat;
127  propertyToValue[quaternionZ] = (double) zQuat;
128  propertyToValue[quaternionW] = (double) wQuat;
129 
130  doubleManager->setValue(quaternionX, (double) xQuat);
131  doubleManager->setValue(quaternionY, (double) yQuat);
132  doubleManager->setValue(quaternionZ, (double) zQuat);
133  doubleManager->setValue(quaternionW, (double) wQuat);
134 
135  doubleManager->setSingleStep(quaternionW, 0.01);
136  doubleManager->setSingleStep(quaternionX, 0.01);
137  doubleManager->setSingleStep(quaternionY, 0.01);
138  doubleManager->setSingleStep(quaternionZ, 0.01);
139 
140  quaternion->addSubProperty(quaternionX);
141  quaternion->addSubProperty(quaternionY);
142  quaternion->addSubProperty(quaternionZ);
143  quaternion->addSubProperty(quaternionW);
144 
145  // rotation as roll-pitch-yaw properties
146  QtProperty* rpy = groupManager->addProperty(tr("Roll-Pitch-Yaw"));
147 
148  QtProperty* roll = doubleManager->addProperty(tr("Roll in deg"));
149  QtProperty* pitch = doubleManager->addProperty(tr("Pitch in deg"));
150  QtProperty* yaw = doubleManager->addProperty(tr("Yaw in deg"));
151 
152  Eigen::Vector3f rpyVector;
153  VirtualRobot::MathTools::eigen4f2rpy(VirtualRobot::MathTools::quat2eigen4f(xQuat, yQuat, zQuat, wQuat), rpyVector);
154 
155  float rollDeg, pitchDeg, yawDeg;
156 
157  rollDeg = VirtualRobot::MathTools::rad2deg(rpyVector(0));
158  pitchDeg = VirtualRobot::MathTools::rad2deg(rpyVector(1));
159  yawDeg = VirtualRobot::MathTools::rad2deg(rpyVector(2));
160 
161  propertyToValue[roll] = rollDeg;
162  propertyToValue[pitch] = pitchDeg;
163  propertyToValue[yaw] = yawDeg;
164 
165  doubleManager->setValue(roll, rollDeg);
166  doubleManager->setValue(pitch, pitchDeg);
167  doubleManager->setValue(yaw, yawDeg);
168 
169  doubleManager->setSingleStep(roll, 1);
170  doubleManager->setSingleStep(pitch, 1);
171  doubleManager->setSingleStep(yaw, 1);
172 
173  rpy->addSubProperty(roll);
174  rpy->addSubProperty(pitch);
175  rpy->addSubProperty(yaw);
176 
177  // translation properties
178  QtProperty* translation = groupManager->addProperty(tr("Translation"));
179 
180  float xPos, yPos, zPos;
181  currentSceneObject->getTranslation().getValue(xPos, yPos, zPos);
182 
183  QtProperty* translationX = doubleManager->addProperty(tr("X Position"));
184  QtProperty* translationY = doubleManager->addProperty(tr("Y Position"));
185  QtProperty* translationZ = doubleManager->addProperty(tr("Z Position"));
186 
187  // update values in propertyToValue to not react to the comming changes
188  propertyToValue[translationX] = (double) xPos;
189  propertyToValue[translationY] = (double) yPos;
190  propertyToValue[translationZ] = (double) zPos;
191 
192  doubleManager->setValue(translationX, (double) xPos);
193  doubleManager->setValue(translationY, (double) yPos);
194  doubleManager->setValue(translationZ, (double) zPos);
195 
196  doubleManager->setSingleStep(translationX, 0.01);
197  doubleManager->setSingleStep(translationY, 0.01);
198  doubleManager->setSingleStep(translationZ, 0.01);
199 
200  translation->addSubProperty(translationX);
201  translation->addSubProperty(translationY);
202  translation->addSubProperty(translationZ);
203 
204  QtProperty* matrix = stringManager->addProperty(tr("Matrix"));
205 
206  stringManager->setValue(matrix, QString::fromStdString(this->getMatrix()));
207 
208  instanceAttributes->addSubProperty(quaternion);
209  instanceAttributes->addSubProperty(rpy);
210  instanceAttributes->addSubProperty(translation);
211  instanceAttributes->addSubProperty(matrix);
212 
213  // store ids of changable properties
214  this->insertInMap(quaternionX, "x");
215  this->insertInMap(quaternionY, "y");
216  this->insertInMap(quaternionZ, "z");
217  this->insertInMap(quaternionW, "w");
218  this->insertInMap(roll, "Roll in deg");
219  this->insertInMap(pitch, "Pitch in deg");
220  this->insertInMap(yaw, "Yaw in deg");
221  this->insertInMap(translationX, "X Position");
222  this->insertInMap(translationY, "Y Position");
223  this->insertInMap(translationZ, "Z Position");
224  this->insertInMap(matrix, "Matrix");
225 
226  QtBrowserItem* instancesSection = this->addProperty(instanceAttributes);
227  setExpanded(instancesSection, true);
228 
229  std::string collection = currentSceneObject->getCollection();
230  std::string objectClass = currentSceneObject->getClassId();
231 
233  {
234  this->setClassAttributes(controller->getMemoryXController()->getPriorKnowlegdeController()->getObjectClassPtr(objectClass, collection), collection, false);
235  }
236 }
237 
238 void gui::PropertyBrowserWidget::setClassAttributes(const memoryx::ObjectClassPtr& objectClass, const std::string& collection, bool expanded)
239 {
240  QtProperty* classAttributes = groupManager->addProperty(tr("Class Attributes"));
241 
242  QtProperty* className = stringManager->addProperty(tr("Class Name"));
243  stringManager->setValue(className, QString::fromStdString(objectClass->getName()));
244  classAttributes->addSubProperty(className);
245 
246  QtProperty* collectionName = stringManager->addProperty(tr("Collection"));
247  stringManager->setValue(collectionName, QString::fromStdString(collection));
248  classAttributes->addSubProperty(collectionName);
249 
250  QtProperty* objectClassID = stringManager->addProperty(tr("Object Class ID"));
251  stringManager->setValue(objectClassID, QString::fromStdString(objectClass->getId()));
252  classAttributes->addSubProperty(objectClassID);
253 
254  std::map<std::string, std::string>::iterator iterator;
255 
256 
258  {
259  std::map<std::string, std::string> attributes = controller->getMemoryXController()->getPriorKnowlegdeController()->getAllAttributes(objectClass);
260 
261  for (iterator = attributes.begin(); iterator != attributes.end(); ++iterator)
262  {
263  QtProperty* property = stringManager->addProperty(QString::fromStdString(iterator->first));
264  stringManager->setValue(property, QString::fromStdString(iterator->second));
265  classAttributes->addSubProperty(property);
266  }
267  }
268 
269  QtBrowserItem* classesSection = this->addProperty(classAttributes);
270 
271  setExpanded(classesSection, expanded);
272 }
273 
274 void gui::PropertyBrowserWidget::clearAll()
275 {
276  this->clear();
277  groupManager->clear();
278  doubleManager->clear();
279  stringManager->clear();
280  propertyToId.clear();
281  idToProperty.clear();
282  propertyToValue.clear();
283  currentSceneObject = NULL;
284 }
285 
287 {
288  if (!currentSceneObject || objectID != currentSceneObject->getObjectId())
289  {
290  return;
291  }
292 
293  // Set new rotation values
294  float xQuat, yQuat, zQuat, wQuat;
295  currentSceneObject->getRotation().getValue(xQuat, yQuat, zQuat, wQuat);
296 
297  // update values in propertyToValue to not react to the comming changes
298  propertyToValue[idToProperty[QString("x")]] = (double) xQuat;
299  propertyToValue[idToProperty[QString("y")]] = (double) yQuat;
300  propertyToValue[idToProperty[QString("z")]] = (double) zQuat;
301  propertyToValue[idToProperty[QString("w")]] = (double) wQuat;
302 
303  doubleManager->setValue(idToProperty[QString("x")], (double) xQuat);
304  doubleManager->setValue(idToProperty[QString("y")], (double) yQuat);
305  doubleManager->setValue(idToProperty[QString("z")], (double) zQuat);
306  doubleManager->setValue(idToProperty[QString("w")], (double) wQuat);
307 
308  Eigen::Vector3f rpyVector;
309  VirtualRobot::MathTools::eigen4f2rpy(VirtualRobot::MathTools::quat2eigen4f(xQuat, yQuat, zQuat, wQuat), rpyVector);
310 
311  float rollDeg, pitchDeg, yawDeg;
312 
313  rollDeg = VirtualRobot::MathTools::rad2deg(rpyVector(0));
314  pitchDeg = VirtualRobot::MathTools::rad2deg(rpyVector(1));
315  yawDeg = VirtualRobot::MathTools::rad2deg(rpyVector(2));
316 
317 
318  propertyToValue[idToProperty[QString("Roll in deg")]] = rollDeg;
319  propertyToValue[idToProperty[QString("Pitch in deg")]] = pitchDeg;
320  propertyToValue[idToProperty[QString("Yaw in deg")]] = yawDeg;
321 
322  doubleManager->setValue(idToProperty[QString("Roll in deg")], rollDeg);
323  doubleManager->setValue(idToProperty[QString("Pitch in deg")], pitchDeg);
324  doubleManager->setValue(idToProperty[QString("Yaw in deg")], yawDeg);
325 
326  // Set new translation values
327  float xPos, yPos, zPos;
328  currentSceneObject->getTranslation().getValue(xPos, yPos, zPos);
329 
330  // update values in propertyToValue to not react to the comming changes
331  propertyToValue[idToProperty[QString("X Position")]] = (double) xPos;
332  propertyToValue[idToProperty[QString("Y Position")]] = (double) yPos;
333  propertyToValue[idToProperty[QString("Z Position")]] = (double) zPos;
334 
335  doubleManager->setValue(idToProperty[QString("X Position")], (double) xPos);
336  doubleManager->setValue(idToProperty[QString("Y Position")], (double) yPos);
337  doubleManager->setValue(idToProperty[QString("Z Position")], (double) zPos);
338 
339 
340  // Set new matrix
341  stringManager->setValue(idToProperty[QString("Matrix")], QString::fromStdString(this->getMatrix()));
342 }
343 
344 void gui::PropertyBrowserWidget::insertInMap(QtProperty* property, QString id)
345 {
346  propertyToId[property] = id;
347  idToProperty[id] = property;
348 
349 }
350 
351 SbRotation gui::PropertyBrowserWidget::createNewRotation(QtProperty* property, double val)
352 {
353  SbRotation newRotation = currentSceneObject->getRotation();
354  float xQuat, yQuat, zQuat, wQuat;
355  newRotation.getValue(xQuat, yQuat, zQuat, wQuat);
356 
357  Eigen::Vector3f rpy;
358  VirtualRobot::MathTools::eigen4f2rpy(VirtualRobot::MathTools::quat2eigen4f(xQuat, yQuat, zQuat, wQuat), rpy);
359  Eigen::Matrix4f rotationMatrix = Eigen::Matrix4f::Identity();
361 
362  QString id = propertyToId[property];
363  if (id == QString("x") && !((float) val == yQuat && yQuat == zQuat && zQuat == wQuat && wQuat == 0.0f))
364  {
365  newRotation = SbRotation((float) val, yQuat, zQuat, wQuat);
366  }
367  else if (id == QString("y") && !((float) val == xQuat && xQuat == zQuat && zQuat == wQuat && wQuat == 0.0f))
368  {
369  newRotation = SbRotation(xQuat, (float) val, zQuat, wQuat);
370  }
371  else if (id == QString("z") && !((float) val == xQuat && xQuat == yQuat && yQuat == wQuat && wQuat == 0.0f))
372  {
373  newRotation = SbRotation(xQuat, yQuat, (float) val, wQuat);
374  }
375  else if (id == QString("w") && !((float) val == xQuat && xQuat == yQuat && yQuat == zQuat && zQuat == 0.0f))
376  {
377  newRotation = SbRotation(xQuat, yQuat, zQuat, (float) val);
378  }
379  else if (id == QString("Roll in deg"))
380  {
381  VirtualRobot::MathTools::rpy2eigen4f(VirtualRobot::MathTools::deg2rad((float) val), rpy(1), rpy(2), rotationMatrix);
382  quaternion = VirtualRobot::MathTools::eigen4f2quat(rotationMatrix);
383  newRotation = SbRotation(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
384  }
385  else if (id == QString("Pitch in deg"))
386  {
387  VirtualRobot::MathTools::rpy2eigen4f(rpy(0), VirtualRobot::MathTools::deg2rad((float) val), rpy(2), rotationMatrix);
388  quaternion = VirtualRobot::MathTools::eigen4f2quat(rotationMatrix);
389  newRotation = SbRotation(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
390  }
391  else if (id == QString("Yaw in deg"))
392  {
393  VirtualRobot::MathTools::rpy2eigen4f(rpy(0), rpy(1), VirtualRobot::MathTools::deg2rad((float) val), rotationMatrix);
394  quaternion = VirtualRobot::MathTools::eigen4f2quat(rotationMatrix);
395  newRotation = SbRotation(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
396  }
397  else
398  {
399  newRotation = currentSceneObject->getRotation();
400  }
401 
402  // for debug purpose
403  float x, y, z, w;
404  newRotation.getValue(x, y, z, w);
405  Eigen::Vector3f rot;
406  VirtualRobot::MathTools::eigen4f2rpy(VirtualRobot::MathTools::quat2eigen4f(x, y, z, w), rot);
407 
408  ARMARX_INFO_S << "Quat: \n" << x << ", \n" << y << ", \n" << z << ", \n" << w;
409  ARMARX_INFO_S << "RPY: \n" << rot;
410 
411  return newRotation;
412 }
413 
414 SbVec3f gui::PropertyBrowserWidget::createNewTranslation(QtProperty* property, double val)
415 {
416  SbVec3f newPosition;
417  float xPos, yPos, zPos;
418  currentSceneObject->getTranslation().getValue(xPos, yPos, zPos);
419 
420  QString id = propertyToId[property];
421 
422  if (id == QString("X Position"))
423  {
424  newPosition = SbVec3f((float) val, yPos, zPos);
425  }
426  else if (id == QString("Y Position"))
427  {
428  newPosition = SbVec3f(xPos, (float) val, zPos);
429  }
430  else if (id == QString("Z Position"))
431  {
432  newPosition = SbVec3f(xPos, yPos, (float) val);
433  }
434  else
435  {
436  newPosition = currentSceneObject->getTranslation();
437  }
438 
439  return newPosition;
440 
441 }
442 
443 std::string gui::PropertyBrowserWidget::getMatrix()
444 {
445  SbMatrix sbMatrix;
446  sbMatrix.setRotate(currentSceneObject->getRotation());
447  sbMatrix.setTranslate(currentSceneObject->getTranslation());
448  std::string matrixToString;
449 
450  for (int i = 0; i < 4; ++i)
451  {
452  matrixToString.append(boost::str(boost::format("%3.2g %3.2g %3.2g %3.2g") %
453  sbMatrix[i][0] % sbMatrix[i][1] %
454  sbMatrix[i][2] % sbMatrix[i][3]));
455 
456  if (i < 3)
457  {
458  matrixToString.append("\n");
459  }
460  }
461 
462  return matrixToString;
463 }
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
QtTreePropertyBrowser
The QtTreePropertyBrowser class provides QTreeWidget based property browser.
Definition: qttreepropertybrowser.h:51
controller::Controller::EXECUTE_ON_SCENE
static const int EXECUTE_ON_SCENE
A flag to execute operations on the Scene.
Definition: Controller.h:73
QtStringPropertyManager
The QtStringPropertyManager provides and manages QString properties.
Definition: qtpropertymanager.h:176
QtBrowserItem
The QtBrowserItem class represents a property in a property browser instance.
Definition: qtpropertybrowser.h:273
QtDoubleSpinBoxFactory
The QtDoubleSpinBoxFactory class provides QDoubleSpinBox widgets for properties created by QtDoublePr...
Definition: qteditorfactory.h:146
QtProperty
The QtProperty class encapsulates an instance of a property.
Definition: qtpropertybrowser.h:71
gui::PropertyBrowserWidget::updateSceneObject
void updateSceneObject(std::string objectID)
Updates the instance properties of the selected object in the scene given by its ID.
Definition: PropertyBrowserWidget.cpp:286
QtGroupPropertyManager
The QtGroupPropertyManager provides and manages group properties.
Definition: qtpropertymanager.h:54
armarx::VariantType::Quaternion
const VariantTypeId Quaternion
Definition: Pose.h:39
IceInternal::Handle< ObjectClass >
gui::PropertyBrowserWidget::setProperties
void setProperties(const memoryx::ObjectClassPtr &objectClass, std::string collection)
Fill property browser with class attributes of the given objectclass.
Definition: PropertyBrowserWidget.cpp:54
QtTreePropertyBrowser::setHeaderVisible
void setHeaderVisible(bool visible)
Definition: qttreepropertybrowser.cpp:1030
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
controller::Controller::UNDOABLE
static const int UNDOABLE
A flag to save the executed operations to the history.
Definition: Controller.h:80
controller
Definition: AddOperation.h:39
controller::ControllerPtr
std::shared_ptr< Controller > ControllerPtr
Definition: ClassDefinitions.h:41
PropertyBrowserWidget.h
QtDoublePropertyManager
The QtDoublePropertyManager provides and manages double properties.
Definition: qtpropertymanager.h:136
gui::PropertyBrowserWidget::PropertyBrowserWidget
PropertyBrowserWidget(const controller::ControllerPtr &control, QWidget *parent=0)
Constructor.
Definition: PropertyBrowserWidget.cpp:40
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
controller::OperationPtr
std::shared_ptr< Operation > OperationPtr
Definition: ClassDefinitions.h:54
QtProperty::addSubProperty
void addSubProperty(QtProperty *property)
Definition: qtpropertybrowser.cpp:427
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
controller::RotateTranslateOperation
A operation to rotate and translate a object.
Definition: RotateTranslateOperation.h:46
controller::Controller::EXECUTE_ON_WM
static const int EXECUTE_ON_WM
A Flag to execute operations on the WorkingMemory.
Definition: Controller.h:66
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
control
This file is part of ArmarX.