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
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 
69  void
71  {
72  addDeleteAction();
73  }
74 
75  // lol
76  void
77  armarx::AronTreeWidgetContextMenuVisitor::addAddAction()
78  {
79  actions.emplace_back("Add element", contextMenuParent);
80  action_callbacks.push_back([this]() mutable { this->executeAddElement(); });
81  }
82 
83  void
84  AronTreeWidgetContextMenuVisitor::executeAddElement()
85  {
86  AronTreeWidgetCreatorVisitor creator(parentItem);
87  aron::type::visit(creator, parentItem->aronType->getChildren()[0]);
88 
89  if (!creator.createdQWidgetItem)
90  {
91  throw std::runtime_error("Creation of TreeElementChild failed unexpectedly");
92  }
93  // if it is a list, we update the number of children at the top
94  auto* castedContainer = AronTreeWidgetItem::DynamicCast(parentItem);
95 
96  // if the parent item is a List, we need to redo the numbers
97  if (castedContainer &&
98  castedContainer->aronType->getDescriptor() == aron::type::Descriptor::LIST)
99  {
100  // This displays the number of children also when the list is collapsed
101  auto numElemsText = misc::generateNumElementsText(castedContainer->childCount());
102  castedContainer->setText(1, numElemsText);
103  // set italic
104  auto currFont = castedContainer->font(1);
105  currFont.setItalic(true);
106  castedContainer->setFont(1, currFont);
107  }
108  }
109 
110  void
112  {
113  addAddAction();
114  addDeleteAction();
115  }
116 
117  void
119  {
120  addDeleteAction();
121  }
122 
123  void
125  {
126  addDeleteAction();
127  }
128 
129  void
131  {
132  addAddAction();
133  addDeleteAction();
134  }
135 
136  void
138  {
139  addDeleteAction();
140  }
141 
142  void
144  {
145  addDeleteAction();
146  }
147 
148  void
150  {
151  addDeleteAction();
152  }
153 
154  void
156  {
157  addDeleteAction();
158  }
159 
160  void
162  {
163  addDeleteAction();
164  }
165 
166  void
168  {
169  addDeleteAction();
170  }
171 
172  void
174  {
175  addDeleteAction();
176  }
177 
178  void
180  {
181  addDeleteAction();
182  }
183 
184  void
186  {
187  addDeleteAction();
188  }
189 
190  void
192  {
193  addDeleteAction();
194  }
195 
196  void
198  {
199  addDeleteAction();
200  }
201 
202  void
204  {
205  addDeleteAction();
206  }
207 
208  void
210  {
211  ARMARX_WARNING << "Tried to open Context menu on unknown aron type";
212  }
213 
214  void
216  {
217  QMenu menu("Context Menu", contextMenuParent);
218  for (auto& el : actions)
219  {
220  menu.addAction(&el);
221  }
222  auto* chosenAction = menu.exec(contextMenuParent->mapToGlobal(pos));
223 
224  if (!chosenAction)
225  {
226  return;
227  }
228 
229  // not elegant, but is a small loop anyway
230  auto it = actions.begin();
231  size_t count = 0;
232  while (it != actions.end())
233  {
234  if (chosenAction == &*it)
235  {
236  action_callbacks[count]();
237  break;
238  }
239  ++it;
240  ++count;
241  }
242  }
243 } // namespace armarx
armarx::aron::type::VisitorBase< const type::VariantPtr >::Input
const type::VariantPtr Input
Definition: Visitor.h:94
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::AronTreeWidgetItem
Definition: AronTreeWidgetItem.h:19
AronTreeWidgetContextMenu.h
armarx::aron::type::Descriptor::LIST
@ LIST
armarx::AronTreeWidgetContextMenuVisitor::visitUnknown
void visitUnknown(Input &) final
Definition: AronTreeWidgetContextMenu.cpp:209
armarx::AronTreeWidgetContextMenuVisitor::AronTreeWidgetContextMenuVisitor
AronTreeWidgetContextMenuVisitor()=delete
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::AronTreeWidgetContextMenuVisitor::visitAronVariant
void visitAronVariant(const aron::type::ObjectPtr &) final
Definition: AronTreeWidgetContextMenu.cpp:70
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::AronTreeWidgetContextMenuVisitor::showMenuAndExecute
void showMenuAndExecute()
Definition: AronTreeWidgetContextMenu.cpp:215
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::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::AronTreeWidgetItem::DynamicCast
static AronTreeWidgetItem * DynamicCast(QTreeWidgetItem *)
Definition: AronTreeWidgetItem.cpp:15
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_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::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
armarx::AronTreeWidgetItem::aronType
aron::type::VariantPtr aronType
Definition: AronTreeWidgetItem.h:30