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