SkillTreeWidget.cpp
Go to the documentation of this file.
1 #include "SkillTreeWidget.h"
2 
4 
5 namespace armarx::skills::gui
6 {
7  void
8  SkillTreeWidget::skillSelectionChanged(QTreeWidgetItem* current, int)
9  {
10  if (!current)
11  {
12  // gui has died?
13  return;
14  }
15 
16  auto c = dynamic_cast<SkillTreeWidgetItem*>(current);
17  if (!c)
18  {
19  // we don't know what to do with the item. Abort...
20  return;
21  }
22 
23  selectedSkill.skillId = c->skillId;
24  emit updateSkillDetails(selectedSkill.skillId);
25  }
26 
27  void
29  {
30  setSortingEnabled(false);
31 
32  // update tree view. Remove non-existing elements
33  int i = 0;
34  while (i < this->topLevelItemCount())
35  {
36  auto* providerItem = this->topLevelItem(i);
37  auto providerName = providerItem->text(0).toStdString();
38  skills::ProviderID providerId{.providerName = providerName};
39 
40  if (update.find(providerId) == update.end())
41  {
42  providerItem = nullptr; // reset
43  auto remove = this->takeTopLevelItem(i);
44  delete remove;
45  continue;
46  }
47 
48  ++i;
49 
50  // sanity check
51  ARMARX_CHECK(update.count(providerId) > 0);
52  auto& providedSkills = update.at(providerId);
53 
54  int j = 0;
55  while (j < providerItem->childCount())
56  {
57  auto* skillItem = providerItem->child(j);
58  auto skillName = skillItem->text(0).toStdString();
59 
60  skills::SkillID skillId{.providerId =
61  skills::ProviderID{.providerName = providerName},
62  .skillName = skillName};
63 
64  if (providedSkills.find(skillId) == providedSkills.end())
65  {
66  skillItem = nullptr;
67  auto remove = providerItem->takeChild(j);
68  delete remove;
69  continue;
70  }
71 
72  ++j;
73  }
74  }
75 
76  // update tree view. Add new elements
77  for (const auto& [providerId, providedSkills] : update)
78  {
79  bool newProvider = false;
80  QTreeWidgetItem* providerItem = nullptr;
81  for (int i = 0; i < this->topLevelItemCount(); ++i)
82  {
83  auto el = this->topLevelItem(i);
84  auto providerName = el->text(0).toStdString();
85  skills::ProviderID elProviderId{.providerName = providerName};
86 
87  if (providerId == elProviderId)
88  {
89  providerItem = el;
90  break;
91  }
92  }
93 
94  if (!providerItem)
95  {
96  providerItem = new QTreeWidgetItem(this);
97  providerItem->setText(0, QString::fromStdString(providerId.providerName));
98  // expand new items
99  newProvider = true;
100  }
101 
102  for (const auto& [skillId, skill] : providedSkills)
103  {
104  QTreeWidgetItem* skillItem = nullptr;
105  for (int i = 0; i < providerItem->childCount(); ++i)
106  {
107  auto el = providerItem->child(i);
108  auto skillName = el->text(0).toStdString();
109  skills::SkillID elSkillId{providerId, skillName};
110 
111  if (skillId == elSkillId)
112  {
113  skillItem = el;
114  break;
115  }
116  }
117 
118  if (!skillItem)
119  {
120  skillItem = new SkillTreeWidgetItem(skillId, providerItem);
121  skillItem->setText(0, QString::fromStdString(skillId.skillName));
122  }
123  }
124 
125  if (newProvider && PROVIDER_EXPANDED_DEFAULT)
126  {
127  providerItem->setExpanded(true);
128  }
129  }
130 
131  setSortingEnabled(true);
132  sortByColumn(0, Qt::AscendingOrder);
133  sortByColumn(1, Qt::AscendingOrder);
134  }
135 
136  void
138  {
139  this->clear();
140  selectedSkill = SelectedSkill();
141  }
142 
143  void
144  SkillTreeWidget::setupUi()
145  {
146  this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
147 
148  QTreeWidgetItem* qtreewidgetitem = this->headerItem();
149 
150  qtreewidgetitem->setText(0, "Skill");
151 
152  connectSignals();
153  }
154 
155  void
156  SkillTreeWidget::connectSignals()
157  {
158  // The selectionChanged signal does not trigger when clicking the active item
159  // => we use itemClicked
160  connect(this, &QTreeWidget::itemClicked, this, &SkillTreeWidget::skillSelectionChanged);
161  }
162 
163 
164 } // namespace armarx::skills::gui
SkillTreeWidgetItem.h
armarx::skills::gui::SkillTreeWidget::SelectedSkill::skillId
skills::SkillID skillId
Definition: SkillTreeWidget.h:27
armarx::skills::ProviderID::providerName
std::string providerName
Definition: ProviderID.h:31
armarx::skills::SkillID::providerId
std::optional< ProviderID > providerId
Definition: SkillID.h:59
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::skills::gui::SkillTreeWidget::updateSkillDetails
void updateSkillDetails(skills::SkillID &skillId)
Notify the skill description widget of an updated selection.
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::skills::gui::SkillTreeWidgetItem
Definition: SkillTreeWidgetItem.h:10
armarx::skills::ProviderID
Definition: ProviderID.h:15
armarx::skills::gui::SkillMap
std::map< skills::ProviderID, std::map< skills::SkillID, skills::SkillDescription > > SkillMap
Definition: SkillManagerWrapper.cpp:11
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::SkillTreeWidget::disconnectGui
void disconnectGui()
Definition: SkillTreeWidget.cpp:137
armarx::skills::gui::SkillTreeWidget::updateGui
void updateGui(SkillMap update)
Definition: SkillTreeWidget.cpp:28
SkillTreeWidget.h
armarx::skills::SkillID
Definition: SkillID.h:17
armarx::skills::gui::SkillTreeWidget::SelectedSkill
Definition: SkillTreeWidget.h:25