6 #include <RobotAPI/interface/armem/actions.h>
8 #include <SimoxUtility/algorithm/string.h>
10 #include <QHeaderView>
23 void TreeWidget::initWidget()
27 columns.insert(
int(Columns::KEY),
"Key");
28 columns.insert(
int(Columns::SIZE),
"#");
29 columns.insert(
int(Columns::TYPE),
"Type");
30 columns.insert(
int(Columns::LEVEL),
"Level");
31 columns.insert(
int(Columns::ID),
"Memory ID");
32 setColumnCount(columns.size());
33 setHeaderLabels(columns);
35 header()->setMinimumSectionSize(25);
36 header()->resizeSection(
int(Columns::KEY), 250);
37 header()->resizeSection(
int(Columns::SIZE), 40);
38 header()->setTextElideMode(Qt::TextElideMode::ElideRight);
42 connect(
this, &QTreeWidget::currentItemChanged,
this, &This::handleSelection);
51 setContextMenuPolicy(Qt::CustomContextMenu);
52 connect(
this, &QTreeWidget::customContextMenuRequested,
this, &This::prepareTreeContextMenu);
55 void TreeWidget::initBuilders()
57 memoryBuilder.setExpand(
true);
58 memoryBuilder.setMakeItemFn([
this](
const std::string & name,
const wm::Memory *
memory)
60 return makeItem(name, *
memory);
62 memoryBuilder.setUpdateItemFn([
this](
const std::string&,
const wm::Memory *
memory, QTreeWidgetItem * memoryItem)
64 updateContainerItem(*
memory, memoryItem);
67 updateChildren(*
memory, memoryItem);
72 auto nameFn = [](
const auto & element)
74 return element.name();
77 coreSegmentBuilder.setExpand(
true);
78 coreSegmentBuilder.setNameFn(nameFn,
int(Columns::KEY));
79 coreSegmentBuilder.setMakeItemFn([
this](
const wm::CoreSegment & coreSeg)
81 return makeItem(coreSeg.name(), coreSeg);
83 coreSegmentBuilder.setUpdateItemFn([
this](
const wm::CoreSegment & coreSeg, QTreeWidgetItem * coreSegItem)
85 updateContainerItem(coreSeg, coreSegItem);
86 updateChildren(coreSeg, coreSegItem);
90 provSegmentBuilder.setExpand(
true);
91 provSegmentBuilder.setNameFn(nameFn,
int(Columns::KEY));
92 provSegmentBuilder.setMakeItemFn([
this](
const wm::ProviderSegment & provSeg)
94 return makeItem(provSeg.name(), provSeg);
96 provSegmentBuilder.setUpdateItemFn([
this](
const wm::ProviderSegment & provSeg, QTreeWidgetItem * provSegItem)
98 updateContainerItem(provSeg, provSegItem);
99 updateChildren(provSeg, provSegItem);
104 entityBuilder.setNameFn(nameFn,
int(Columns::KEY));
105 entityBuilder.setMakeItemFn([
this](
const wm::Entity & entity)
107 return makeItem(entity.name(), entity);
109 entityBuilder.setUpdateItemFn([
this](
const wm::Entity & entity, QTreeWidgetItem * entityItem)
111 updateContainerItem(entity, entityItem);
112 updateChildren(entity, entityItem);
119 item->setData(
int(Columns::KEY), Qt::ItemDataRole::UserRole, QVariant(
static_cast<qlonglong
>(snapshot.time().toMicroSecondsSinceEpoch())));
122 snapshotBuilder.setCompareFn([](
const wm::EntitySnapshot & snapshot, QTreeWidgetItem * item)
125 item->data(
int(Columns::KEY), Qt::ItemDataRole::UserRole).toLongLong());
127 snapshotBuilder.setUpdateItemFn([
this](
const wm::EntitySnapshot & snapshot, QTreeWidgetItem * snapshotItem)
129 updateContainerItem(snapshot, snapshotItem);
130 updateChildren(snapshot, snapshotItem);
136 QTreeWidgetItem* item = makeItem(
"", instance);
139 instanceBuilder.setCompareFn([](
const wm::EntityInstance & lhs, QTreeWidgetItem * rhsItem)
143 instanceBuilder.setUpdateItemFn([
this](
const wm::EntityInstance & instance, QTreeWidgetItem * instanceItem)
145 updateItemItem(instance, instanceItem);
146 updateChildren(instance, instanceItem);
156 handleSelections =
false;
157 updateChildren(
memory,
this);
158 handleSelections =
true;
164 handleSelections =
false;
165 updateChildren(memories,
this);
166 handleSelections =
true;
175 void TreeWidget::handleSelection()
177 if (!handleSelections)
181 QTreeWidgetItem* item = this->currentItem();
187 MemoryID id(item->data(
int(Columns::ID), Qt::UserRole).toString().toStdString());
190 if (!_selectedID ||
id != *_selectedID)
194 const std::string levelName = item->data(
int(Columns::LEVEL), Qt::UserRole).toString().toStdString();
225 template <
class ContainerT>
227 auto makeIteratorFn(
const ContainerT& container)
229 return [&container](
auto&& elementFn)
231 container.forEachChild(elementFn);
236 void TreeWidget::updateChildren(
const armem::wm::Memory& memory, QTreeWidget* tree)
238 updateChildren(std::map<std::string, const armem::wm::Memory*> {{memory.name(), &memory}}, tree);
241 void TreeWidget::updateChildren(
const std::map<std::string, const armem::wm::Memory*>& memories, QTreeWidget* tree)
243 memoryBuilder.updateTree(tree, memories);
247 void TreeWidget::updateChildren(
const armem::wm::Memory& memory, QTreeWidgetItem* memoryItem)
249 coreSegmentBuilder.updateTreeWithIterator(memoryItem, makeIteratorFn(memory));
252 void TreeWidget::updateChildren(
const armem::wm::CoreSegment& coreSeg, QTreeWidgetItem* coreSegItem)
254 provSegmentBuilder.updateTreeWithIterator(coreSegItem, makeIteratorFn(coreSeg));
257 void TreeWidget::updateChildren(
const armem::wm::ProviderSegment& provSeg, QTreeWidgetItem* provSegItem)
259 entityBuilder.updateTreeWithIterator(provSegItem, makeIteratorFn(provSeg));
262 void TreeWidget::updateChildren(
const armem::wm::Entity& entity, QTreeWidgetItem* entityItem)
264 snapshotBuilder.updateTreeWithIterator(entityItem, makeIteratorFn(entity));
269 instanceBuilder.updateTreeWithIterator(snapshotItem, makeIteratorFn(snapshot));
274 (void)
data, (
void) dataItem;
277 void TreeWidget::prepareTreeContextMenu(
const QPoint& pos)
279 const QTreeWidgetItem* item = this->itemAt(pos);
285 MemoryID memoryID(item->data(
int(Columns::ID), Qt::UserRole).toString().toStdString());
289 template <
class MemoryItemT>
290 QTreeWidgetItem* TreeWidget::makeItem(
const std::string& key,
const MemoryItemT& memoryItem)
293 return makeItem(memoryItem.getKeyString(), MemoryItemT::getLevelName(), memoryItem.id());
296 QTreeWidgetItem* TreeWidget::makeItem(
const std::string& key,
const std::string& levelName,
const MemoryID&
id)
299 columns.insert(
int(Columns::KEY), QString::fromStdString(key));
300 columns.insert(
int(Columns::SIZE),
"");
301 columns.insert(
int(Columns::TYPE),
"");
302 columns.insert(
int(Columns::LEVEL), QString::fromStdString(simox::alg::capitalize_words(levelName)));
303 columns.insert(
int(Columns::ID), QString::fromStdString(
id.
str()));
305 QTreeWidgetItem* item =
new QTreeWidgetItem(columns);
306 item->setData(
int(Columns::LEVEL), Qt::UserRole, QString::fromStdString(levelName));
307 item->setData(
int(Columns::ID), Qt::UserRole, QString::fromStdString(
id.
str()));
308 item->setTextAlignment(
int(Columns::SIZE), Qt::AlignRight);
312 void TreeWidget::updateItemItem(
const armem::base::detail::MemoryItem& level, QTreeWidgetItem* item)
314 (void) level, (
void) item;
317 template <
class ContainerT>
318 void TreeWidget::updateContainerItem(
const ContainerT& container, QTreeWidgetItem* item)
320 updateItemItem(container, item);
321 item->setText(
int(Columns::SIZE), QString::number(container.size()));
324 if constexpr(std::is_base_of_v<base::detail::AronTyped, ContainerT>)
326 const base::detail::AronTyped&
cast =
static_cast<const base::detail::AronTyped&
>(container);
327 std::string typeName;
330 typeName =
cast.aronType()->getFullName();
335 typeName =
"(no Aron type)";
337 item->setText(
int(Columns::TYPE), QString::fromStdString(typeName));