SetDesiredPoseDialog.cpp
Go to the documentation of this file.
1 #include "SetDesiredPoseDialog.h"
2 
3 #include <QMessageBox>
4 
7 
8 #include <RobotComponents/gui-plugins/RobotIKPlugin/ui_SetDesiredPoseDialog.h>
9 
10 static std::vector<std::string>
11  expectedKeys({"agent", "frame", "qw", "qx", "qy", "qz", "x", "y", "z"});
12 
14  QDialog(parent), ui(new Ui::SetDesiredPoseDialog)
15 {
16  ui->setupUi(this);
17 
18  connect(ui->pushButton_formatJSON, SIGNAL(clicked()), this, SLOT(formatInput()));
19  connect(ui->plainTextEdit, SIGNAL(textChanged()), this, SLOT(checkJSON()));
20  connect(ui->buttonBox->button(QDialogButtonBox::Ok),
21  SIGNAL(clicked()),
22  this,
23  SLOT(parseInputAndSetPose()));
24 
25  checkJSON();
26 }
27 
29 {
30  delete ui;
31 }
32 
35 {
36  return result;
37 }
38 
39 void
40 SetDesiredPoseDialog::checkJSON()
41 {
42  std::string text = ui->plainTextEdit->toPlainText().toUtf8().data();
43 
45  parser.parse();
46 
47  // Check for keys
48  std::string errorMsgPart;
49  armarx::JsonObjectPtr json = std::dynamic_pointer_cast<armarx::JsonObject>(parser.parsedJson);
50  if (json)
51  {
52  std::vector<std::string> keys = json->getKeys();
53  for (std::string& k : expectedKeys)
54  {
55  if (std::find(keys.begin(), keys.end(), k) == keys.end())
56  {
57  errorMsgPart += k;
58  errorMsgPart += " ,";
59  }
60  }
61  }
62  else
63  {
64  for (std::string& k : expectedKeys)
65  {
66  errorMsgPart += k;
67  errorMsgPart += " ,";
68  }
69  }
70  if (errorMsgPart.size() >= 2)
71  {
72  errorMsgPart = errorMsgPart.substr(0, errorMsgPart.size() - 2);
73  }
74 
75  // Set error message
76  if (!parser.iserr() && errorMsgPart.empty())
77  {
78  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
79  ui->label_jsonValid->setStyleSheet("QLabel { color : green; }");
80  ui->label_jsonValid->setText("JSON-Format: valid");
81  }
82  else
83  {
84  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
85  ui->label_jsonValid->setStyleSheet("QLabel { color : red; }");
86  if (errorMsgPart.empty())
87  {
88  ui->label_jsonValid->setText("JSON-Format: not valid, Error at " +
89  QString(parser.getlongerrposstr().c_str()));
90  }
91  else
92  {
93 
94  ui->label_jsonValid->setText("JSON-Format: not valid, Missing following keys: \n{ " +
95  QString(errorMsgPart.c_str()) + " }");
96  }
97  }
98 }
99 
100 void
101 SetDesiredPoseDialog::formatInput()
102 {
103  std::string text = ui->plainTextEdit->toPlainText().toUtf8().data();
104  if (text.empty())
105  {
106  text += "{\n}"; // Minimal requirements for successful parsing
107  }
109  if (stringToJSON(text, json))
110  {
111  std::vector<std::string> keys = json->getKeys();
112  for (std::string& k : expectedKeys)
113  {
114  if (std::find(keys.begin(), keys.end(), k) == keys.end())
115  {
116  if (k == "agent" || k == "frame")
117  {
118  json->add(k, armarx::JsonValue(""));
119  }
120  else
121  {
122  json->add(k, armarx::JsonValue(0.0f));
123  }
124  }
125  }
126  ui->plainTextEdit->setPlainText(QString(json->toJsonString(2).c_str()));
127  }
128  else
129  {
130  QMessageBox msgBox;
131  msgBox.setText("Input cannot be parsed into json and therefore not formated.");
132  msgBox.setIcon(QMessageBox::Warning);
133  msgBox.setStandardButtons(QMessageBox::Ok);
134  msgBox.setDefaultButton(QMessageBox::Ok);
135  msgBox.exec();
136  }
137 }
138 
139 void
140 SetDesiredPoseDialog::parseInputAndSetPose()
141 {
142  std::string text = ui->plainTextEdit->toPlainText().toUtf8().data();
144  ARMARX_CHECK_EXPRESSION(stringToJSON(text, json));
145 
146  float x =
147  armarx::toFloat((std::dynamic_pointer_cast<armarx::JsonValue>(json->get("x")))->rawValue());
148  float y =
149  armarx::toFloat((std::dynamic_pointer_cast<armarx::JsonValue>(json->get("y")))->rawValue());
150  float z =
151  armarx::toFloat((std::dynamic_pointer_cast<armarx::JsonValue>(json->get("z")))->rawValue());
152  float qw = armarx::toFloat(
153  (std::dynamic_pointer_cast<armarx::JsonValue>(json->get("qw")))->rawValue());
154  float qx = armarx::toFloat(
155  (std::dynamic_pointer_cast<armarx::JsonValue>(json->get("qx")))->rawValue());
156  float qy = armarx::toFloat(
157  (std::dynamic_pointer_cast<armarx::JsonValue>(json->get("qy")))->rawValue());
158  float qz = armarx::toFloat(
159  (std::dynamic_pointer_cast<armarx::JsonValue>(json->get("qz")))->rawValue());
160  std::string agent =
161  (std::dynamic_pointer_cast<armarx::JsonValue>(json->get("agent")))->asString();
162  std::string frame =
163  (std::dynamic_pointer_cast<armarx::JsonValue>(json->get("frame")))->asString();
164 
165  armarx::Vector3Ptr pos = new armarx::Vector3(x, y, z);
166  armarx::QuaternionPtr quat = new armarx::Quaternion(qw, qx, qy, qz);
167 
168  if (pos && quat)
169  {
170  armarx::FramedPosePtr pose = new armarx::FramedPose(pos, quat, frame, agent);
171  this->result = pose;
172  }
173 }
174 
175 bool
176 SetDesiredPoseDialog::stringToJSON(std::string string, armarx::JsonObjectPtr& result) const
177 {
178  armarx::StructuralJsonParser parser(string, false);
179  parser.parse();
180  result = std::dynamic_pointer_cast<armarx::JsonObject>(parser.parsedJson);
181  return !parser.iserr();
182 }
SetDesiredPoseDialog
Definition: SetDesiredPoseDialog.h:15
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:36
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:100
IceStorm::Parser::parse
int parse(FILE *, bool)
SetDesiredPoseDialog::SetDesiredPoseDialog
SetDesiredPoseDialog(QWidget *parent=0)
Definition: SetDesiredPoseDialog.cpp:13
StringHelpers.h
armarx::VariantType::Quaternion
const VariantTypeId Quaternion
Definition: Pose.h:39
armarx::JsonValue
Definition: JsonValue.h:34
IceInternal::Handle< FramedPose >
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:54
SetDesiredPoseDialog::getDesiredPose
armarx::FramedPosePtr getDesiredPose()
Definition: SetDesiredPoseDialog.cpp:34
ExpressionException.h
SetDesiredPoseDialog.h
IceStorm::parser
Parser * parser
Definition: Parser.cpp:34
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
memoryx::KBM::Vector3
Eigen::Vector3d Vector3
Definition: kbm.h:43
armarx::StructuralJsonParser
Definition: StructuralJsonParser.h:35
SetDesiredPoseDialog::~SetDesiredPoseDialog
~SetDesiredPoseDialog()
Definition: SetDesiredPoseDialog.cpp:28
armarx::JsonObjectPtr
std::shared_ptr< JsonObject > JsonObjectPtr
Definition: JsonObject.h:34