AronTreeWidgetCreator.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * \package RobotAPI::gui-plugins::SkillManagerMonitorWidgetController
17 * \author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * \date 2020
19 * \copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include <string>
24
28
29// base class
31
32// data
33#include <QComboBox>
34
36#include "../Data.h"
37#include "../ListDictHelper.h"
39
40namespace armarx
41{
42
44 parentOfCreatedObj(parentInstance)
45 {
46 // The parent of the root aron Tree Widget will not be aron type.
47 // there an explicit setTopLevelWidget() is required.
48 auto* aronParent = AronTreeWidgetItem::DynamicCast(parentInstance);
49 if (aronParent)
50 {
51 toplevelWidget = aronParent->treeWidget();
52 }
53 }
54
55 std::string
56 AronTreeWidgetCreatorVisitor::generateUniqueKeyFromSet(std::set<std::string>&& usedKeys)
57 {
58 size_t num = 0;
59 while (true)
60 {
61 std::string proposedName = this->defaultMapKeyName + std::to_string(num);
62 auto it = usedKeys.find(proposedName);
63 if (it == usedKeys.end())
64 {
65 break;
66 }
67 ++num;
68 }
69 return this->defaultMapKeyName + std::to_string(num);
70 }
71
72 void
74 {
76
77 auto key = i->getPath().getLastElement();
78 bool isDictChild = false;
79 // edit key, to be a unique string, if the parent is a dict
80 auto* aronParent = AronTreeWidgetItem::DynamicCast(parentOfCreatedObj);
81 if (aronParent && aronParent->aronType->getDescriptor() == aron::type::Descriptor::DICT)
82 {
83 isDictChild = true;
84 std::set<std::string> usedKeys;
85 for (int i = 0; i < parentOfCreatedObj->childCount(); ++i)
86 {
87 auto* sibling = AronTreeWidgetItem::DynamicCast(parentOfCreatedObj->child(i));
88 if (sibling)
89 {
90 usedKeys.insert(sibling->text(0).toStdString());
91 }
92 }
93 key = generateUniqueKeyFromSet(std::move(usedKeys));
94 }
95 // if it's a list -> choose the right number
96 else if (aronParent &&
97 aronParent->aronType->getDescriptor() == aron::type::Descriptor::LIST)
98 {
99 key = std::to_string(parentOfCreatedObj->childCount());
100 }
101
103 new AronTreeWidgetItem(isDictChild, editableValue, QString::fromStdString(key), i);
104
105 if (i->getMaybe() != armarx::aron::type::Maybe::NONE)
106 {
107 createdQWidgetItem->setCheckState(1, Qt::CheckState::Unchecked);
108 }
109 createdQWidgetItem->setText(1, QString::fromStdString(defaul));
110 createdQWidgetItem->setText(2, QString::fromStdString(i->getShortName()));
111 createdQWidgetItem->setText(
112 3,
113 QString::fromStdString(
115 ITEM_EMPTY_MESSAGE) /*QString::fromStdString(i->getDefaultFromString())*/);
116
117 if (editableValue || isDictChild)
118 {
119 createdQWidgetItem->setFlags(createdQWidgetItem->flags() | Qt::ItemIsEditable);
120 }
121 parentOfCreatedObj->addChild(createdQWidgetItem);
122 }
123
124 void
126 {
128
129 auto key = i->getObjectName();
130 if (i->getPath().hasElement())
131 {
132 key = i->getPath().getLastElement();
133 }
134
136 new AronTreeWidgetItem(editableValue, false, QString::fromStdString(key), i);
137
138 if (i->getMaybe() != armarx::aron::type::Maybe::NONE)
139 {
140 createdQWidgetItem->setCheckState(1, Qt::CheckState::Unchecked);
141 }
142
143 parentOfCreatedObj->addChild(createdQWidgetItem);
144
145 for (const auto& [key, value] : i->getMemberTypes())
146 {
148 aron::type::visit(v, value);
149
150 assert(v.createdQWidgetItem);
151 }
152 }
153
154 void
156 {
158
160 // The DictType has only one member, its key-type. This also must be present
161 ARMARX_CHECK_EQUAL(i->getChildren().size(), 1);
162 }
163
164 void
166 {
167 // create default, uneditable tree widget item.
168 insertNewTreeViewWidget(pair, "");
169 // attach two children
170 ARMARX_CHECK(pair->getChildren().size() == 2);
171 for (size_t i = 0; i < 2; ++i)
172 {
174 aron::type::visit(v, pair->getChildren()[i]);
175 if (v.createdQWidgetItem)
176 {
177 std::string descr = "p[" + std::to_string(i) + "]";
178 v.createdQWidgetItem->setText(0, descr.c_str());
179 }
180 }
181 }
182
183 void
185 {
186 // CAUTION; UNTESTED
187 // create default, uneditable tree widget item.
188 insertNewTreeViewWidget(tuple, "");
189 // attach all children
190 for (size_t i = 0; i < tuple->getChildren().size(); ++i)
191 {
193 aron::type::visit(v, tuple->getChildren()[i]);
194 if (v.createdQWidgetItem)
195 {
196 std::string descr = "tup[" + std::to_string(i) + "]";
197 v.createdQWidgetItem->setText(0, descr.c_str());
198 }
199 }
200 }
201
202 void
209
210 void
215
216 void
218 {
219 editableValue = false;
221 // special code to print the type of base type used
222 QString type = "";
223 switch (i->getElementType())
224 {
225 case armarx::aron::type::matrix::UINT8:
226 type = "<uint8>";
227 break;
228 case armarx::aron::type::matrix::UINT16:
229 type = "<uint16>";
230 break;
231 case armarx::aron::type::matrix::UINT32:
232 type = "<uint32>";
233 break;
234 case armarx::aron::type::matrix::INT8:
235 type = "<int8>";
236 break;
237 case armarx::aron::type::matrix::INT16:
238 type = "<int16>";
239 break;
240 case armarx::aron::type::matrix::INT32:
241 type = "<int32>";
242 break;
243 case armarx::aron::type::matrix::INT64:
244 type = "<int64>";
245 break;
246 case armarx::aron::type::matrix::FLOAT32:
247 type = "<float>";
248 break;
249 case armarx::aron::type::matrix::FLOAT64:
250 type = "<double>";
251 break;
252 }
253 type = createdQWidgetItem->text(2) + type;
254 createdQWidgetItem->setText(2, type);
255
256 // Widget fiddling source: https://blog.manash.io/quick-qt-6-how-to-add-qpushbutton-or-widgets-to-a-qtreewidget-as-qtreewidgetitem-2ae9f54c0e5f
257 // overlay custom widget in column 1
258 auto* toplevelWidget = createdQWidgetItem->treeWidget();
259 EditMatrixWidget* matWidget = new EditMatrixWidget(
260 i->getRows(), i->getCols(), i->getElementType(), createdQWidgetItem);
261 toplevelWidget->setItemWidget(createdQWidgetItem, 1, matWidget);
262 }
263
264 void
266 {
267 editableValue = false;
269 // special code to print the type of base type used
270 QString type = "";
271 switch (i->getElementType())
272 {
273 case armarx::aron::type::quaternion::FLOAT32:
274 type = "<float>";
275 break;
276 case armarx::aron::type::quaternion::FLOAT64:
277 type = "<double>";
278 break;
279 }
280 type = createdQWidgetItem->text(2) + type;
281 createdQWidgetItem->setText(2, type);
282
283 auto* toplevelWidget = createdQWidgetItem->treeWidget();
284 QuaternionWidget* quatWidget =
285 new QuaternionWidget(i->getElementType(), createdQWidgetItem);
286 toplevelWidget->setItemWidget(createdQWidgetItem, 1, quatWidget);
287 }
288
289 void
294
295 void
300
301 void
303 {
304 editableValue = false;
306 ARMARX_CHECK_GREATER(i->getAcceptedValueNames().size(), 0);
307
310 createdQWidgetItem->treeWidget()->setItemWidget(createdQWidgetItem, 1, widget);
311 }
312
313 void
315 {
316 editableValue = true;
318 }
319
320 void
322 {
323 editableValue = true;
325 }
326
327 void
329 {
330 editableValue = true;
331 insertNewTreeViewWidget(i, "0.0");
332 }
333
334 void
336 {
337 editableValue = true;
338 insertNewTreeViewWidget(i, "0.0");
339 }
340
341 void
343 {
344 editableValue = true;
345 insertNewTreeViewWidget(i, "false");
346 }
347
348 void
353
354 void
356 {
357 ARMARX_WARNING_S << "Received an unknown type when trying to create a tree view widget for "
358 "a skill argument type.";
359 }
360
361 void
363 {
364 toplevelWidget = widget;
365 }
366} // namespace armarx
void insertNewTreeViewWidget(Input &i, const std::string &)
void visitAronVariant(const aron::type::ObjectPtr &) final
static AronTreeWidgetItem * DynamicCast(QTreeWidgetItem *)
#define ARMARX_CHECK_GREATER(lhs, rhs)
This macro evaluates whether lhs is greater (>) than rhs and if it turns out to be false it will thro...
#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...
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
void visit(VisitorImplementation &v, typename VisitorImplementation::Input &t)
The visit function.
Definition Visitor.h:39
std::shared_ptr< class Quaternion > QuaternionPtr
std::shared_ptr< Object > ObjectPtr
Definition Object.h:36
std::shared_ptr< class Double > DoublePtr
std::shared_ptr< class Matrix > MatrixPtr
std::shared_ptr< class Image > ImagePtr
std::shared_ptr< class Long > LongPtr
std::shared_ptr< class String > StringPtr
std::shared_ptr< class PointCloud > PointCloudPtr
std::shared_ptr< class Pair > PairPtr
std::shared_ptr< class NDArray > NDArrayPtr
std::shared_ptr< class Int > IntPtr
std::shared_ptr< class Float > FloatPtr
std::shared_ptr< class Bool > BoolPtr
std::shared_ptr< class Tuple > TuplePtr
std::shared_ptr< class Dict > DictPtr
std::shared_ptr< IntEnum > IntEnumPtr
Definition IntEnum.h:36
std::shared_ptr< class List > ListPtr
QString generateNumElementsText(int num)
This file offers overloads of toIce() and fromIce() functions for STL container types.
typename VisitorBase< const type::VariantPtr >::Input Input
Definition Visitor.h:103