AronTreeWidgetContextMenu.cpp
Go to the documentation of this file.
2 
3 #include <QMenu>
4 #include <QPoint>
5 #include <QTreeWidget>
6 #include <QTreeWidgetItem>
7 
8 #include "../AronTreeWidgetItem.h"
9 #include "../ListDictHelper.h"
10 #include "AronTreeWidgetCreator.h"
11 
12 namespace armarx::skills::gui
13 {
16  const QPoint& pos,
17  QTreeWidget* contextMenuParent,
18  int x) :
19  parentItem(i), contextMenuParent(contextMenuParent), pos(pos), index(x)
20  {
21  }
22 
23  void
24  AronTreeWidgetContextMenuVisitor::addDeleteAction()
25  {
26  auto* castedParent = AronTreeWidgetItem::DynamicCast(parentItem->QTreeWidgetItem::parent());
27  if (!castedParent)
28  {
29  // must be top level element
30  return;
31  }
32  auto aronType = castedParent->aronType->getDescriptor();
33  if (aron::type::Descriptor::DICT == aronType || aron::type::Descriptor::LIST == aronType)
34  {
35  QMenu contextMenu("Context menu", contextMenuParent);
36  actions.emplace_back("remove element", contextMenuParent);
37  action_callbacks.push_back([this]() mutable { this->executeDelete(); });
38  }
39  }
40 
41  void
42  AronTreeWidgetContextMenuVisitor::executeDelete()
43  {
44  auto* containerPtr = parentItem->QTreeWidgetItem::parent();
45  containerPtr->removeChild(parentItem);
46  auto* castedContainer = AronTreeWidgetItem::DynamicCast(containerPtr);
47 
48  // if the parent item is a List, we need to redo the numbers
49  if (castedContainer &&
50  castedContainer->aronType->getDescriptor() == aron::type::Descriptor::LIST)
51  {
52  // start renumbering from the removed child onwards
53  for (int i = index; i < castedContainer->childCount(); ++i)
54  {
55  std::string numberString = std::to_string(i);
56  castedContainer->child(i)->setText(0, numberString.c_str());
57  }
58  // This displays the number of children also when the list is collapsed
59  QString numElemsText = misc::generateNumElementsText(castedContainer->childCount());
60  containerPtr->setText(1, numElemsText);
61  // set italic
62  auto currFont = castedContainer->font(1);
63  currFont.setItalic(true);
64  castedContainer->setFont(1, currFont);
65  }
66  }
67 
68  void
70  {
71  addDeleteAction();
72  }
73 
74  // lol
75  void
76  AronTreeWidgetContextMenuVisitor::addAddAction()
77  {
78  actions.emplace_back("Add element", contextMenuParent);
79  action_callbacks.push_back([this]() mutable { this->executeAddElement(); });
80  }
81 
82  void
83  AronTreeWidgetContextMenuVisitor::executeAddElement()
84  {
85  AronTreeWidgetCreatorVisitor creator(parentItem);
86  aron::type::visit(creator, parentItem->aronType->getChildren()[0]);
87 
88  if (!creator.createdQWidgetItem)
89  {
90  throw std::runtime_error("Creation of TreeElementChild failed unexpectedly");
91  }
92  // if it is a list, we update the number of children at the top
93  auto* castedContainer = AronTreeWidgetItem::DynamicCast(parentItem);
94 
95  // if the parent item is a List, we need to redo the numbers
96  if (castedContainer &&
97  castedContainer->aronType->getDescriptor() == aron::type::Descriptor::LIST)
98  {
99  // This displays the number of children also when the list is collapsed
100  auto numElemsText = misc::generateNumElementsText(castedContainer->childCount());
101  castedContainer->setText(1, numElemsText);
102  // set italic
103  auto currFont = castedContainer->font(1);
104  currFont.setItalic(true);
105  castedContainer->setFont(1, currFont);
106  }
107  }
108 
109  void
111  {
112  addAddAction();
113  addDeleteAction();
114  }
115 
116  void
118  {
119  addDeleteAction();
120  }
121 
122  void
124  {
125  addDeleteAction();
126  }
127 
128  void
130  {
131  addAddAction();
132  addDeleteAction();
133  }
134 
135  void
137  {
138  addDeleteAction();
139  }
140 
141  void
143  {
144  addDeleteAction();
145  }
146 
147  void
149  {
150  addDeleteAction();
151  }
152 
153  void
155  {
156  addDeleteAction();
157  }
158 
159  void
161  {
162  addDeleteAction();
163  }
164 
165  void
167  {
168  addDeleteAction();
169  }
170 
171  void
173  {
174  addDeleteAction();
175  }
176 
177  void
179  {
180  addDeleteAction();
181  }
182 
183  void
185  {
186  addDeleteAction();
187  }
188 
189  void
191  {
192  addDeleteAction();
193  }
194 
195  void
197  {
198  addDeleteAction();
199  }
200 
201  void
203  {
204  addDeleteAction();
205  }
206 
207  void
209  {
210  ARMARX_WARNING << "Tried to open Context menu on unknown aron type";
211  }
212 
213  void
215  {
216  QMenu menu("Context Menu", contextMenuParent);
217  for (auto& el : actions)
218  {
219  menu.addAction(&el);
220  }
221  auto* chosenAction = menu.exec(contextMenuParent->mapToGlobal(pos));
222 
223  if (!chosenAction)
224  {
225  return;
226  }
227 
228  // not elegant, but is a small loop anyway
229  auto it = actions.begin();
230  size_t count = 0;
231  while (it != actions.end())
232  {
233  if (chosenAction == &*it)
234  {
235  action_callbacks[count]();
236  break;
237  }
238  ++it;
239  ++count;
240  }
241  }
242 } // namespace armarx::skills::gui
armarx::aron::type::VisitorBase< const type::VariantPtr >::Input
const type::VariantPtr Input
Definition: Visitor.h:94
AronTreeWidgetContextMenu.h
armarx::aron::type::MatrixPtr
std::shared_ptr< class Matrix > MatrixPtr
Definition: forward_declarations.h:20
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
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::skills::gui::misc::generateNumElementsText
QString generateNumElementsText(int num)
Definition: ListDictHelper.cpp:6
armarx::skills::gui::AronTreeWidgetContextMenuVisitor::showMenuAndExecute
void showMenuAndExecute()
Definition: AronTreeWidgetContextMenu.cpp:214
armarx::aron::type::Descriptor::LIST
@ LIST
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::skills::gui::AronTreeWidgetItem::aronType
aron::type::VariantPtr aronType
Definition: AronTreeWidgetItem.h:30
armarx::aron::type::FloatPtr
std::shared_ptr< class Float > FloatPtr
Definition: forward_declarations.h:29
armarx::skills::gui::AronTreeWidgetItem::DynamicCast
static AronTreeWidgetItem * DynamicCast(QTreeWidgetItem *)
Definition: AronTreeWidgetItem.cpp:15
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
armarx::aron::type::StringPtr
std::shared_ptr< class String > StringPtr
Definition: forward_declarations.h:31
armarx::AronTreeWidgetCreatorVisitor
Definition: AronTreeWidgetCreator.h:35
armarx::skills::gui::AronTreeWidgetContextMenuVisitor::visitUnknown
void visitUnknown(Input &) final
Definition: AronTreeWidgetContextMenu.cpp:208
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::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
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::skills::gui::AronTreeWidgetItem
Definition: AronTreeWidgetItem.h:19
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
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::skills::gui::AronTreeWidgetContextMenuVisitor::AronTreeWidgetContextMenuVisitor
AronTreeWidgetContextMenuVisitor()=delete
armarx::aron::type::DoublePtr
std::shared_ptr< class Double > DoublePtr
Definition: forward_declarations.h:30
armarx::skills::gui::AronTreeWidgetContextMenuVisitor::visitAronVariant
void visitAronVariant(const aron::type::ObjectPtr &) final
Definition: AronTreeWidgetContextMenu.cpp:69