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