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