QuaternionWidget.cpp
Go to the documentation of this file.
1 #include "QuaternionWidget.h"
2 
3 #include <QHBoxLayout>
4 #include <QLabel>
5 #include <QLineEdit>
6 
8 
9 #include <RobotAPI/interface/aron/Aron.h>
10 
11 #include "../../ColorPalettes.h"
12 #include "NDArrayHelper.h"
13 
14 namespace armarx
15 {
16 
17  QuaternionWidget*
19  {
20  return dynamic_cast<QuaternionWidget*>(elem);
21  }
22 
25  {
26  if (!elem)
27  {
28  return nullptr;
29  }
30  auto* casted = DynamicCast(elem);
31  ARMARX_CHECK_NOT_NULL(casted);
32  return casted;
33  }
34 
35  void
37  {
38  size_t idx = (size_t)col;
39  xyzw_components.at(idx)->setText(str.c_str());
40  highlightUnparsableEntries();
41  }
42 
43  bool
45  {
46  return parseErrors;
47  }
48 
50  QTreeWidgetItem* currentItem) :
51  CustomWidget(currentItem), element_type(elType)
52  {
53  auto outerLayout = new QHBoxLayout();
54  auto labelCol1 = new QVBoxLayout();
55  auto labelCol2 = new QVBoxLayout();
56  auto xzEdit = new QVBoxLayout();
57  auto ywEdit = new QVBoxLayout();
58 
59  outerLayout->addLayout(labelCol1);
60  outerLayout->addLayout(xzEdit);
61  outerLayout->addLayout(labelCol2);
62  outerLayout->addLayout(ywEdit);
63 
64  xyzw_components.reserve(4);
65  for (size_t i = 0; i < 4; ++i)
66  {
67  xyzw_components.push_back(new QLineEdit());
68  }
69 
70  // Tabbing order >> Viewing order. Thats why the indeces are a bit switched
71  labelCol1->addWidget(new QLabel("x"));
72  xzEdit->addWidget(xyzw_components.at(0));
73 
74  labelCol2->addWidget(new QLabel("z"));
75  ywEdit->addWidget(xyzw_components.at(2));
76 
77  labelCol1->addWidget(new QLabel("y"));
78  xzEdit->addWidget(xyzw_components.at(1));
79 
80  labelCol2->addWidget(new QLabel("w"));
81  ywEdit->addWidget(xyzw_components.at(3));
82 
83  for (const auto& el : xyzw_components)
84  {
86  connect(el, SIGNAL(editingFinished()), this, SLOT(quaternionElementChanged())));
87  }
88 
89  setLayout(outerLayout);
90  highlightUnparsableEntries();
91  }
92 
93  void
94  QuaternionWidget::highlightUnparsableEntries()
95  {
96  parseErrors = false;
97  for (size_t i = 0; i < 4; ++i)
98  {
99  bool ok = false;
100  switch (element_type)
101  {
102  case armarx::aron::type::quaternion::FLOAT32:
103  {
104  float flt;
105  std::tie(ok, flt) = parseQuatVals<float>((QuaternionComponents)i);
106  break;
107  }
108  case armarx::aron::type::quaternion::FLOAT64:
109  {
110  double dbl;
111  std::tie(ok, dbl) = parseQuatVals<double>((QuaternionComponents)i);
112  break;
113  }
114  }
115  if (ok)
116  {
117  xyzw_components.at(i)->setPalette(gui_color_palette::getNormalPalette());
118  }
119  else
120  {
121  xyzw_components.at(i)->setPalette(gui_color_palette::getErrorPalette());
122  parseErrors = true;
123  }
124  }
125  }
126 
127  std::vector<unsigned char>
129  {
130  std::vector<unsigned char> res;
131  bool success = true;
132  if (element_type == aron::type::quaternion::FLOAT32)
133  {
134  res.reserve(16);
135  for (size_t i = 0; i < 4; ++i)
136  {
137  auto bytevec =
138  NDArrayHelper::toByteVector<float>(xyzw_components.at(i)->text().toStdString());
139  success &= !bytevec.empty();
140  res.insert(res.end(), bytevec.begin(), bytevec.end());
141  }
142  }
143  else
144  {
145  res.reserve(32);
146  for (size_t i = 0; i < 4; ++i)
147  {
148  auto bytevec = NDArrayHelper::toByteVector<double>(
149  xyzw_components.at(i)->text().toStdString());
150  success &= !bytevec.empty();
151  res.insert(res.end(), bytevec.begin(), bytevec.end());
152  }
153  }
154  return (success) ? res : std::vector<unsigned char>();
155  }
156 
157  void
158  QuaternionWidget::quaternionElementChanged()
159  {
161  highlightUnparsableEntries();
164  }
165 } // namespace armarx
armarx::QuaternionWidget
Definition: QuaternionWidget.h:21
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
armarx::gui_color_palette::getErrorPalette
QPalette getErrorPalette()
Definition: ColorPalettes.cpp:6
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::QuaternionWidget::hasParseErrors
bool hasParseErrors()
Definition: QuaternionWidget.cpp:44
armarx::CustomWidget::elemChanged
void elemChanged(QTreeWidgetItem *elem, int col)
armarx::CustomWidget::setSupressSignals
virtual void setSupressSignals(bool doSupress)
Definition: CustomWidget.cpp:36
armarx::QuaternionWidget::DynamicCast
static QuaternionWidget * DynamicCast(QWidget *)
Definition: QuaternionWidget.cpp:18
visionx::imrecman::ok
@ ok
Definition: ImageRecordingManagerInterface.ice:45
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::QuaternionWidget::setText
void setText(QuaternionComponents col, const std::string &str)
Definition: QuaternionWidget.cpp:36
armarx::CustomWidget::overlayingItem
QTreeWidgetItem * overlayingItem
Definition: CustomWidget.h:21
armarx::QuaternionWidget::QuaternionComponents
QuaternionComponents
Definition: QuaternionWidget.h:30
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::QuaternionWidget::DynamicCastAndCheck
static QuaternionWidget * DynamicCastAndCheck(QWidget *)
Definition: QuaternionWidget.cpp:24
ExpressionException.h
armarx::QuaternionWidget::parseAllToNDArray
std::vector< unsigned char > parseAllToNDArray()
Definition: QuaternionWidget.cpp:128
armarx::CustomWidget
Definition: CustomWidget.h:9
QuaternionWidget.h
armarx::QuaternionWidget::QuaternionWidget
QuaternionWidget(aron::type::quaternion::ElementType type, QTreeWidgetItem *currentItem)
Definition: QuaternionWidget.cpp:49
armarx::gui_color_palette::getNormalPalette
QPalette getNormalPalette()
Definition: ColorPalettes.cpp:14
NDArrayHelper.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::status::success
@ success