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
14namespace armarx
15{
16
19 {
20 return dynamic_cast<QuaternionWidget*>(elem);
21 }
22
25 {
26 if (!elem)
27 {
28 return nullptr;
29 }
30 auto* casted = DynamicCast(elem);
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
49 armarx::QuaternionWidget::QuaternionWidget(aron::type::quaternion::ElementType elType,
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 {
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
std::string str(const T &t)
static std::vector< unsigned char > toByteVector(const std::string &str)
void elemChanged(QTreeWidgetItem *elem, int col)
virtual void setSupressSignals(bool doSupress)
QTreeWidgetItem * overlayingItem
CustomWidget(QTreeWidgetItem *overlayingItem)
static QuaternionWidget * DynamicCast(QWidget *)
QuaternionWidget(aron::type::quaternion::ElementType type, QTreeWidgetItem *currentItem)
void setText(QuaternionComponents col, const std::string &str)
std::vector< unsigned char > parseAllToNDArray()
static QuaternionWidget * DynamicCastAndCheck(QWidget *)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#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...
This file offers overloads of toIce() and fromIce() functions for STL container types.