SkillExecutionTreeWidget.cpp
Go to the documentation of this file.
2
3#include <mutex>
4
5#include <QMenu>
6#include <QTreeWidgetItem>
7
9
11
12namespace armarx::skills::gui
13{
14
15 void
16 SkillExecutionTreeWidget::runContextMenu(const QPoint& pos)
17 {
18 // sanity check
19 ARMARX_CHECK(selectionValid());
20
21 QMenu* menu = new QMenu();
22
23 // Stop skill
24 QAction* stopSkillAction = new QAction("Stop execution", this);
25 const auto& executions = memory->getExecutions();
26 if (executions.count(selectedExecution.skillExecutionId) == 0)
27 return;
28 skills::SkillStatus currentStatus =
29 memory->getExecutions().at(selectedExecution.skillExecutionId).status;
30 stopSkillAction->setDisabled(currentStatus == skills::SkillStatus::Aborted ||
31 currentStatus == skills::SkillStatus::Failed ||
32 currentStatus == skills::SkillStatus::Succeeded);
33
34 QAction* rerunSkillAction = new QAction("Re-execute with similar parameters", this);
35 menu->addAction(stopSkillAction);
36 menu->addAction(rerunSkillAction);
37 connect(stopSkillAction,
38 &QAction::triggered,
39 this,
40 &SkillExecutionTreeWidget::stopSelectedExecution);
41 connect(rerunSkillAction,
42 &QAction::triggered,
43 this,
44 &SkillExecutionTreeWidget::rerunSkillWithSimilarParams);
45
46 // open menu
47 menu->popup(this->viewport()->mapToGlobal(pos));
48 }
49
50 void
51 SkillExecutionTreeWidget::stopSelectedExecution()
52 {
53 if (!selectionValid())
54 return;
55 memory->stopExecution(this->selectedExecution.skillExecutionId);
56 }
57
58 void
59 SkillExecutionTreeWidget::rerunSkillWithSimilarParams()
60 {
61 if (!selectionValid())
62 return;
63 // we don't want to hold state in the gui, so we need to get the parameters from memory:
64 skills::SkillExecutionID currentExecutionId = this->selectedExecution.skillExecutionId;
65 auto executions = memory->getExecutions();
66 if (executions.empty())
67 return;
68
69 if (executions.count(currentExecutionId) == 0)
70 {
71 // we didn't find an entry for the execution id
72 ARMARX_IMPORTANT << "The selected execution was not found in memory. The GUI is unable "
73 "to determine the parametrization for this execution.";
74 return;
75 }
76 auto params = executions[currentExecutionId].parameters;
77
78 ARMARX_INFO << "Re-executing the skill " << currentExecutionId.skillId
79 << " with previous parameters.";
80
81 // give all information to manager
82 this->memory->startExecutionWithParams(currentExecutionId.skillId, params);
83 }
84
85 void
87 {
88 this->selectedExecution = SelectedExecution();
89 this->clear();
90 }
91
92 void
93 SkillExecutionTreeWidget::setupUi()
94 {
95 this->setColumnCount(6);
96
97 this->setContextMenuPolicy(Qt::CustomContextMenu);
98
99 QTreeWidgetItem* qtreewidgetitem = this->headerItem();
100 qtreewidgetitem->setText(5, "");
101 qtreewidgetitem->setText(4, "");
102 qtreewidgetitem->setText(3, "Status");
103 qtreewidgetitem->setText(2, "SkillID");
104 qtreewidgetitem->setText(1, "Executor");
105 qtreewidgetitem->setText(0, "Timestamp");
106
107 this->setColumnWidth(4, 30);
108
109 connectSignals();
110 }
111
112 void
113 SkillExecutionTreeWidget::connectSignals()
114 {
115 connect(this,
116 &QTreeWidget::customContextMenuRequested,
117 this,
118 &SkillExecutionTreeWidget::runContextMenu);
119 connect(this,
120 &QTreeWidget::currentItemChanged,
121 this,
122 &SkillExecutionTreeWidget::executionSelectionChanged);
123 }
124
125 inline bool
126 SkillExecutionTreeWidget::selectionValid()
127 {
129 }
130
131 void
132 SkillExecutionTreeWidget::executionSelectionChanged(QTreeWidgetItem* current,
133 QTreeWidgetItem* previous)
134 {
135 // update internal state
136 SkillExecutionTreeWidgetItem* selected =
137 dynamic_cast<SkillExecutionTreeWidgetItem*>(current);
138 if (selected)
139 {
140 this->selectedExecution.skillExecutionId = selected->getExecutionId();
141 }
142 }
143
144 void
146 {
147 if (update.statuses.empty())
148 {
149 return;
150 }
151
152 for (const auto& [k, v] : update.statuses)
153 {
154 skills::SkillExecutionID executionId = k;
155 skills::SkillStatusUpdate statusUpdate = v;
156
157 SkillExecutionTreeWidgetItem* found = nullptr;
158 for (int i = 0; i < this->topLevelItemCount(); ++i)
159 {
160 auto c = dynamic_cast<SkillExecutionTreeWidgetItem*>(topLevelItem(i));
161 if (!c)
162 {
163 // the item is probably not the correct type, skip...
164 continue;
165 }
166
168
169 if (found)
170 {
171 found->updateItem(statusUpdate.status);
172
173 break;
174 }
175 }
176
177 if (!found)
178 {
179 // TODO: Sort to executor!
180 auto item = new SkillExecutionTreeWidgetItem(executionId, memory, this);
181
182 item->updateItem(statusUpdate.status);
183 }
184 }
185 }
186} // namespace armarx::skills::gui
constexpr T c
static const constexpr char * UNKNOWN
Definition SkillID.h:18
std::string skillName
Definition SkillID.h:41
std::shared_ptr< SkillManagerWrapper > memory
static SkillExecutionTreeWidgetItem * SearchRecursiveForMatch(SkillExecutionTreeWidgetItem *haystack, const skills::SkillExecutionID &needle)
void updateGui(SkillManagerWrapper::Snapshot update)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190