AronTreeWidgetContextMenu.cpp
Go to the documentation of this file.
2
3#include <QMenu>
4#include <QPoint>
5#include <QTreeWidget>
6#include <QTreeWidgetItem>
7
9#include "../ListDictHelper.h"
11
12namespace 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();
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
73
74 // lol
75 void
76 armarx::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
121
122 void
127
128 void
130 {
131 addAddAction();
132 addDeleteAction();
133 }
134
135 void
140
141 void
146
147 void
152
153 void
158
159 void
164
165 void
170
171 void
176
177 void
182
183 void
188
189 void
194
195 void
200
201 void
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
void visitAronVariant(const aron::type::ObjectPtr &) final
static AronTreeWidgetItem * DynamicCast(QTreeWidgetItem *)
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
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