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
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::INT16:
226  type = "<int16>";
227  break;
228  case armarx::aron::type::matrix::INT32:
229  type = "<int32>";
230  break;
231  case armarx::aron::type::matrix::INT64:
232  type = "<int64>";
233  break;
234  case armarx::aron::type::matrix::FLOAT32:
235  type = "<float>";
236  break;
237  case armarx::aron::type::matrix::FLOAT64:
238  type = "<double>";
239  break;
240  }
241  type = createdQWidgetItem->text(2) + type;
242  createdQWidgetItem->setText(2, type);
243 
244  // Widget fiddling source: https://blog.manash.io/quick-qt-6-how-to-add-qpushbutton-or-widgets-to-a-qtreewidget-as-qtreewidgetitem-2ae9f54c0e5f
245  // overlay custom widget in column 1
246  auto* toplevelWidget = createdQWidgetItem->treeWidget();
247  EditMatrixWidget* matWidget = new EditMatrixWidget(
248  i->getRows(), i->getCols(), i->getElementType(), createdQWidgetItem);
249  toplevelWidget->setItemWidget(createdQWidgetItem, 1, matWidget);
250  }
251 
252  void
254  {
255  editableValue = false;
257  // special code to print the type of base type used
258  QString type = "";
259  switch (i->getElementType())
260  {
261  case armarx::aron::type::quaternion::FLOAT32:
262  type = "<float>";
263  break;
264  case armarx::aron::type::quaternion::FLOAT64:
265  type = "<double>";
266  break;
267  }
268  type = createdQWidgetItem->text(2) + type;
269  createdQWidgetItem->setText(2, type);
270 
271  auto* toplevelWidget = createdQWidgetItem->treeWidget();
272  QuaternionWidget* quatWidget =
273  new QuaternionWidget(i->getElementType(), createdQWidgetItem);
274  toplevelWidget->setItemWidget(createdQWidgetItem, 1, quatWidget);
275  }
276 
277  void
279  {
281  }
282 
283  void
285  {
287  }
288 
289  void
291  {
292  editableValue = false;
294  ARMARX_CHECK_GREATER(i->getAcceptedValueNames().size(), 0);
295 
298  createdQWidgetItem->treeWidget()->setItemWidget(createdQWidgetItem, 1, widget);
299  }
300 
301  void
303  {
304  editableValue = true;
305  insertNewTreeViewWidget(i, "0");
306  }
307 
308  void
310  {
311  editableValue = true;
312  insertNewTreeViewWidget(i, "0");
313  }
314 
315  void
317  {
318  editableValue = true;
319  insertNewTreeViewWidget(i, "0.0");
320  }
321 
322  void
324  {
325  editableValue = true;
326  insertNewTreeViewWidget(i, "0.0");
327  }
328 
329  void
331  {
332  editableValue = true;
333  insertNewTreeViewWidget(i, "false");
334  }
335 
336  void
338  {
340  }
341 
342  void
344  {
345  ARMARX_WARNING_S << "Received an unknown type when trying to create a tree view widget for "
346  "a skill argument type.";
347  }
348 
349  void
351  {
352  toplevelWidget = widget;
353  }
354 } // namespace armarx
armarx::aron::type::VisitorBase< const type::VariantPtr >::Input
const type::VariantPtr Input
Definition: Visitor.h:94
armarx::QuaternionWidget
Definition: QuaternionWidget.h:17
armarx::aron::type::MatrixPtr
std::shared_ptr< class Matrix > MatrixPtr
Definition: forward_declarations.h:20
armarx::AronTreeWidgetCreatorVisitor::createdQWidgetItem
AronTreeWidgetItem * createdQWidgetItem
Definition: AronTreeWidgetCreator.h:38
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::AronTreeWidgetItem
Definition: AronTreeWidgetItem.h:19
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::AronTreeWidgetCreatorVisitor::visitAronVariant
void visitAronVariant(const aron::type::ObjectPtr &) final
Definition: AronTreeWidgetCreator.cpp:125
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
AronTreeWidgetContextMenu.h
armarx::aron::type::Descriptor::LIST
@ LIST
armarx::IntEnumWidget
Definition: IntEnumWidget.h:16
armarx::AronTreeWidgetCreatorVisitor::visitUnknown
void visitUnknown(Input &) final
Definition: AronTreeWidgetCreator.cpp:343
armarx::aron::type::TuplePtr
std::shared_ptr< class Tuple > TuplePtr
Definition: forward_declarations.h:17
armarx::misc::generateNumElementsText
QString generateNumElementsText(int num)
Definition: ListDictHelper.cpp:6
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::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::AronTreeWidgetCreatorVisitor::AronTreeWidgetCreatorVisitor
AronTreeWidgetCreatorVisitor()=delete
armarx::AronTreeWidgetCreatorVisitor
Definition: AronTreeWidgetCreator.h:35
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::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::AronTreeWidgetCreatorVisitor::insertNewTreeViewWidget
void insertNewTreeViewWidget(Input &i, const std::string &)
Definition: AronTreeWidgetCreator.cpp:73
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::AronTreeWidgetItem::DynamicCast
static AronTreeWidgetItem * DynamicCast(QTreeWidgetItem *)
Definition: AronTreeWidgetItem.cpp:15
armarx::aron_tree_widget::constantes::ITEM_EMPTY_MESSAGE
const std::string ITEM_EMPTY_MESSAGE
Definition: Data.h:11
armarx::EditMatrixWidget
Definition: EditMatrixWidget.h:19
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::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
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::AronTreeWidgetCreatorVisitor::setTopLevelWidget
void setTopLevelWidget(QTreeWidget *widget)
Definition: AronTreeWidgetCreator.cpp:350
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
AronTreeWidgetCreator.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28