SkillDetailsTreeWidget.cpp
Go to the documentation of this file.
2 
3 #include <QApplication>
4 #include <QClipboard>
5 #include <QHeaderView>
6 #include <QResizeEvent>
7 #include <QVBoxLayout>
8 
10 
11 namespace armarx::skills::gui
12 {
13  SkillDetailsTreeWidget::SkillDetailsTreeWidget(std::shared_ptr<SkillManagerWrapper> _memory,
14  QWidget* parent) :
15  QTreeWidget(parent), MemoryCommunicatorBase(_memory)
16  {
17  setupUi();
18  }
19 
20  std::optional<SkillID>
22  {
23  if (shownSkill.has_value())
24  {
25  return shownSkill.value().skillId;
26  }
27  else
28  {
29  return std::nullopt;
30  }
31  }
32 
33  void
35  {
36  // dont touch the widget if the skill id didn't change
37  // note: this is only relevant when periodic updates are enabled
38  /*
39  if (shownSkill.has_value() && skillId == shownSkill.value().skillId)
40  return;
41  */
42 
43  // check the parameters: did they change?
44  if (shownSkill.has_value())
45  {
46  auto remDesc = shownSkill.value().descr;
47  if (descr.rootProfileDefaults != remDesc.rootProfileDefaults)
48  {
49  // TODO: ask the user if they want to reset to defaults
50  // for now, we just overwrite without asking... (and do nothing)
51  }
52  // other cases: if the parameter types change, we *have* to reset; else the skill
53  // cannot be started with the parameters.
54  // same goes for the result type.
55  }
56 
57  this->resetWidget();
58 
59  auto aron_args = descr.parametersType;
60  auto default_args_of_profile = descr.rootProfileDefaults;
61  // ideally we want to use the invisible root item, but aron expects a parent...
62  QTreeWidgetItem* aronTreeWidgetItem = new QTreeWidgetItem(this);
63  aronTreeWidgetItem->setText(0, QString::fromStdString("Parameters"));
64 
65  aronTreeWidgetController = std::make_shared<AronTreeWidgetController>(
66  this, aronTreeWidgetItem, aron_args, default_args_of_profile);
67 
68 
69  this->expandAll();
71 
72  // update the ShownSkill
73  shownSkill = {skillId, descr};
74  }
75 
76  void
78  {
79  this->aronTreeWidgetController = nullptr;
80  }
81 
82  void
84  {
85  if (!shownSkill.has_value())
86  return;
87  skills::SkillID sid = shownSkill.value().skillId;
88 
89  // we assume the id to be fully specified, as it is checked while constructing
90  // sanity check
92 
93  // maybe the search is empty?
94  auto skillsMap = memory->getSkills();
95  if (skillsMap.count(sid.providerId.value()) == 0 ||
96  skillsMap.at(sid.providerId.value()).count(sid) == 0)
97  {
98  this->resetWidget();
99  return;
100  }
101 
102  auto descr = memory->getSkills().at(sid.providerId.value()).at(sid);
103 
104  this->updateContents(sid, descr);
105  }
106 
107  void
108  SkillDetailsTreeWidget::setupUi()
109  {
110  this->setColumnCount(3);
111 
112  this->setContextMenuPolicy(Qt::CustomContextMenu);
113  this->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed);
114 
115  QTreeWidgetItem* qtreewidgetitem2 = this->headerItem();
116  qtreewidgetitem2->setText(3, "defaultValue (hidden in GUI)");
117  qtreewidgetitem2->setText(2, "Type");
118  qtreewidgetitem2->setText(1, "Value");
119  qtreewidgetitem2->setText(0, "Key");
120 
121  setColumnHidden(3, true);
122  }
123 
124  /**
125  * Problem: columns 0 and 1 have arbitrary size; so we want to limit their size, to make sure
126  * everything is visible without scrolling. Column 2 is limited by design, as it only contains
127  * type information.
128  */
129  void
131  {
132  // take remainder of width (which we want to assign to dynamic columns)
133  const int widthRemainder = this->width() - typeWidth;
134 
135  // we want to assign half of it to each dynamic column
136  const int dynamicColumnSize = widthRemainder / 2;
137 
138  // set width...
139 
140  this->setColumnWidth(0, dynamicColumnSize);
141 
142  this->setColumnWidth(1, dynamicColumnSize);
143  }
144 
147  {
148  // create argument aron (if there is an accepted type set)
149  if (aronTreeWidgetController)
150  {
151  return aronTreeWidgetController->convertToAron();
152  }
153  return nullptr;
154  }
155 
156  void
158  {
159  auto data = getConfigAsAron();
160  if (!data)
161  {
162  return;
163  }
164 
166  QClipboard* clipboard = QApplication::clipboard();
167  clipboard->setText(QString::fromStdString(json.dump(2)));
168  }
169 
170  void
172  {
173  QClipboard* clipboard = QApplication::clipboard();
174  std::string s = clipboard->text().toStdString();
175  nlohmann::json json = nlohmann::json::parse(s);
176  auto data =
178 
179  if (!aronTreeWidgetController)
180  {
181  return;
182  }
183 
184  aronTreeWidgetController->setFromAron(data);
185  }
186 
187  void
189  {
190  // this will always reset the args to the root profile
191  // good while there is only the root, not good when profiles are properly implemented
192 
193 
194  if (!shownSkill.has_value())
195  {
196  return;
197  }
198  skills::SkillID& skillId = shownSkill.value().skillId;
199  const auto skills = memory->getSkills();
201 
202  // find description
203  // did the provider die?
204  if (skills.count(skillId.providerId.value()) == 0 ||
205  skills.at(skillId.providerId.value()).count(skillId) == 0)
206  return;
207  skills::SkillDescription descr = skills.at(skillId.providerId.value()).at(skillId);
208  this->updateContents(skillId, descr);
209  }
210 
211  void
213  {
214  this->clear();
215  aronTreeWidgetController = nullptr;
216  }
217 
218 
219 } // namespace armarx::skills::gui
SkillDetailsTreeWidget.h
skills
This file is part of ArmarX.
armarx::skills::gui::SkillDetailsTreeWidget::resetCurrentConfig
void resetCurrentConfig()
Definition: SkillDetailsTreeWidget.cpp:188
armarx::skills::SkillDescription
Definition: SkillDescription.h:18
armarx::aron::data::converter::AronNlohmannJSONConverter::ConvertToNlohmannJSON
static nlohmann::json ConvertToNlohmannJSON(const data::VariantPtr &)
Definition: NLohmannJSONConverter.cpp:10
armarx::skills::gui::SkillDetailsTreeWidget::SkillDetailsTreeWidget
SkillDetailsTreeWidget(std::shared_ptr< SkillManagerWrapper > _memory, QWidget *parent=nullptr)
Definition: SkillDetailsTreeWidget.cpp:13
armarx::aron::data::converter::AronNlohmannJSONConverter::ConvertFromNlohmannJSONObject
static data::DictPtr ConvertFromNlohmannJSONObject(const nlohmann::json &, const armarx::aron::Path &p={})
Definition: NLohmannJSONConverter.cpp:25
armarx::skills::SkillID::providerId
std::optional< ProviderID > providerId
Definition: SkillID.h:59
armarx::skills::gui::SkillDetailsTreeWidget::resizeContents
void resizeContents()
Problem: columns 0 and 1 have arbitrary size; so we want to limit their size, to make sure everything...
Definition: SkillDetailsTreeWidget.cpp:130
armarx::skills::gui::SkillDetailsTreeWidget::updateGui
void updateGui()
Definition: SkillDetailsTreeWidget.cpp:83
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::isProviderSpecified
bool isProviderSpecified() const
Definition: SkillID.h:41
NLohmannJSONConverter.h
armarx::skills::gui::SkillDetailsTreeWidget::updateContents
void updateContents(skills::SkillID const &skillId, skills::SkillDescription const &descr)
Definition: SkillDetailsTreeWidget.cpp:34
armarx::skills::gui::SkillDetailsTreeWidget::getShownId
std::optional< skills::SkillID > getShownId()
Definition: SkillDetailsTreeWidget.cpp:21
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::skills::gui::SkillDetailsTreeWidget::copyCurrentConfig
void copyCurrentConfig()
Definition: SkillDetailsTreeWidget.cpp:157
armarx::skills::gui
Definition: PeriodicUpdateWidget.cpp:11
armarx::skills::gui::SkillDetailsTreeWidget::getConfigAsAron
aron::data::DictPtr getConfigAsAron()
Definition: SkillDetailsTreeWidget.cpp:146
armarx::skills::SkillDescription::rootProfileDefaults
aron::data::DictPtr rootProfileDefaults
Definition: SkillDescription.h:22
armarx::skills::gui::SkillDetailsTreeWidget::disconnectGui
void disconnectGui()
Definition: SkillDetailsTreeWidget.cpp:77
armarx::skills::gui::SkillDetailsTreeWidget::resetWidget
void resetWidget()
Definition: SkillDetailsTreeWidget.cpp:212
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::skills::SkillDescription::parametersType
aron::type::ObjectPtr parametersType
Definition: SkillDescription.h:24
armarx::skills::SkillID
Definition: SkillID.h:17
armarx::skills::SkillID::isFullySpecified
bool isFullySpecified() const
Definition: SkillID.h:29
armarx::skills::gui::SkillDetailsTreeWidget::pasteCurrentConfig
void pasteCurrentConfig()
Definition: SkillDetailsTreeWidget.cpp:171
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::skills::gui::MemoryCommunicatorBase
Definition: MemoryCommunicatorBase.h:14