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