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