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 
25 #include "../widgets/EditMatrixWidget.h"
26 #include "../widgets/IntEnumWidget.h"
27 #include "../widgets/QuaternionWidget.h"
28 
29 // base class
30 #include "AronTreeWidgetCreator.h"
31 
32 // data
33 #include <QComboBox>
34 
35 #include "../AronTreeWidgetItem.h"
36 #include "../Data.h"
37 #include "../ListDictHelper.h"
39 
40 namespace armarx::skills::gui
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(
114  aron_tree_widget::constantes::
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  {
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
204  {
206  auto txt = misc::generateNumElementsText(0);
207  createdQWidgetItem->setText(1, txt);
208  }
209 
210  void
212  {
214  }
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
291  {
293  }
294 
295  void
297  {
299  }
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;
317  insertNewTreeViewWidget(i, "0");
318  }
319 
320  void
322  {
323  editableValue = true;
324  insertNewTreeViewWidget(i, "0");
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
350  {
352  }
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::skills::gui
armarx::aron::type::VisitorBase< const type::VariantPtr >::Input
const type::VariantPtr Input
Definition: Visitor.h:94
AronTreeWidgetContextMenu.h
armarx::skills::gui::AronTreeWidgetCreatorVisitor::visitAronVariant
void visitAronVariant(const aron::type::ObjectPtr &) final
Definition: AronTreeWidgetCreator.cpp:125
armarx::aron::type::MatrixPtr
std::shared_ptr< class Matrix > MatrixPtr
Definition: forward_declarations.h:20
armarx::skills::gui::AronTreeWidgetCreatorVisitor::AronTreeWidgetCreatorVisitor
AronTreeWidgetCreatorVisitor()=delete
armarx::aron::type::ImagePtr
std::shared_ptr< class Image > ImagePtr
Definition: forward_declarations.h:22
armarx::aron::type::NDArrayPtr
std::shared_ptr< class NDArray > NDArrayPtr
Definition: forward_declarations.h:19
armarx::skills::gui::IntEnumWidget
Definition: IntEnumWidget.h:16
armarx::skills::gui::misc::generateNumElementsText
QString generateNumElementsText(int num)
Definition: ListDictHelper.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_CHECK_GREATER
#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...
Definition: ExpressionException.h:116
armarx::aron::type::Descriptor::LIST
@ LIST
armarx::skills::gui::EditMatrixWidget
Definition: EditMatrixWidget.h:19
armarx::aron::type::TuplePtr
std::shared_ptr< class Tuple > TuplePtr
Definition: forward_declarations.h:17
AronTreeWidgetCreator.h
armarx::aron::type::PointCloudPtr
std::shared_ptr< class PointCloud > PointCloudPtr
Definition: forward_declarations.h:23
armarx::aron::type::FloatPtr
std::shared_ptr< class Float > FloatPtr
Definition: forward_declarations.h:29
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::skills::gui::AronTreeWidgetItem::DynamicCast
static AronTreeWidgetItem * DynamicCast(QTreeWidgetItem *)
Definition: AronTreeWidgetItem.cpp:15
armarx::skills::gui::AronTreeWidgetCreatorVisitor::visitUnknown
void visitUnknown(Input &) final
Definition: AronTreeWidgetCreator.cpp:355
armarx::aron::type::ListPtr
std::shared_ptr< class List > ListPtr
Definition: forward_declarations.h:14
armarx::aron::type::LongPtr
std::shared_ptr< class Long > LongPtr
Definition: forward_declarations.h:28
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::type::StringPtr
std::shared_ptr< class String > StringPtr
Definition: forward_declarations.h:31
armarx::aron::type::PairPtr
std::shared_ptr< class Pair > PairPtr
Definition: forward_declarations.h:16
armarx::aron::type::DictPtr
std::shared_ptr< class Dict > DictPtr
Definition: forward_declarations.h:13
armarx::skills::gui
Definition: PeriodicUpdateWidget.cpp:11
armarx::aron::type::BoolPtr
std::shared_ptr< class Bool > BoolPtr
Definition: forward_declarations.h:32
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::skills::gui::QuaternionWidget
Definition: QuaternionWidget.h:16
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:11
armarx::skills::gui::AronTreeWidgetCreatorVisitor
Definition: AronTreeWidgetCreator.h:35
armarx::aron_tree_widget::constantes::ITEM_EMPTY_MESSAGE
const std::string ITEM_EMPTY_MESSAGE
Definition: Data.h:11
armarx::skills::gui::AronTreeWidgetCreatorVisitor::insertNewTreeViewWidget
void insertNewTreeViewWidget(Input &i, const std::string &)
Definition: AronTreeWidgetCreator.cpp:73
armarx::aron::type::Descriptor::DICT
@ DICT
armarx::aron::type::visit
void visit(VisitorImplementation &v, typename VisitorImplementation::Input &t)
The visit function.
Definition: Visitor.h:42
armarx::aron::type::IntEnumPtr
std::shared_ptr< IntEnum > IntEnumPtr
Definition: IntEnum.h:36
armarx::skills::gui::AronTreeWidgetCreatorVisitor::setTopLevelWidget
void setTopLevelWidget(QTreeWidget *widget)
Definition: AronTreeWidgetCreator.cpp:362
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
armarx::skills::gui::AronTreeWidgetItem
Definition: AronTreeWidgetItem.h:19
ARMARX_CHECK_EQUAL
#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...
Definition: ExpressionException.h:130
armarx::aron::type::QuaternionPtr
std::shared_ptr< class Quaternion > QuaternionPtr
Definition: forward_declarations.h:21
armarx::aron::type::IntPtr
std::shared_ptr< class Int > IntPtr
Definition: forward_declarations.h:27
armarx::aron::type::DoublePtr
std::shared_ptr< class Double > DoublePtr
Definition: forward_declarations.h:30
armarx::skills::gui::AronTreeWidgetCreatorVisitor::createdQWidgetItem
AronTreeWidgetItem * createdQWidgetItem
Definition: AronTreeWidgetCreator.h:38