InstanceGroupBoxController.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2025, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package RobotStateComponent::
19 * @author Samet Soenmez (uewtt at student dot kit dot edu)
20 * @date 2025t
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
26
27#include <SimoxUtility/algorithm/get_map_keys_values.h>
28#include <SimoxUtility/algorithm/string/string_tools.h>
29
30#include <QLayout>
31#include <QGroupBox>
32#include <QLabel>
33
37
39{
41 MemoryViewerUIContext& uiContext,
42 std::shared_ptr<armem::gui::model::MemoryViewerModel> model)
43 {
44 _model = std::move(model);
45 _instanceGroupBoxView = new armem::gui::view::InstanceGroupBoxView();
46 armarx::gui::replaceWidget(uiContext.instanceGroupBox, _instanceGroupBoxView, uiContext.instanceGroupBoxParentLayout);
47 _instanceGroupBoxView->instanceView()->setStatusLabel(uiContext.statusLabel);
49
50 connect(_instanceGroupBoxView->instanceView(),
52 this,
54 }
55
56 void
58 {
59 const armem::wm::Memory* data = _model->getSingleMemoryData(selectedID.memoryName);
60 if (data)
61 {
62 if (not selectedID.hasEntityName())
63 {
64 return;
65 }
66 armem::MemoryID id = selectedID;
67 const armem::wm::EntitySnapshot* snapshot = nullptr;
68 if (not id.hasTimestamp())
69 {
70 const armem::wm::Entity& entity = data->getEntity(id);
71 if (entity.empty())
72 {
73 return;
74 }
75 snapshot = &entity.getLatestSnapshot();
76 id.timestamp = snapshot->time();
77 }
78 if (not id.hasInstanceIndex())
79 {
80 if (not snapshot)
81 {
82 try
83 {
84 snapshot = &data->getSnapshot(id);
85 }
86 catch (const armem::error::ArMemError& e)
87 {
88 _statusLabel->setText(e.what());
89 }
90 }
91 if (snapshot && snapshot->size() > 0)
92 {
93 id.instanceIndex = 0;
94 }
95 }
96 if (id.hasInstanceIndex())
97 {
98 _instanceGroupBoxView->instanceView()->update(id, *data);
99 }
100 }
101 else
102 {
103 std::stringstream ss;
104 ss << "Memory name '" << selectedID.memoryName << "' is unknown. Known are: "
105 << simox::alg::join(simox::alg::get_keys(_model->memoryDataCopy()), ", ");
106 _statusLabel->setText(QString::fromStdString(ss.str()));
107 }
108 }
109
110 void
112 {
113 // ARMARX_IMPORTANT << "Resolving memory ID: " << id;
114
115 auto handleError = [this](const std::string& msg)
116 {
117 _statusLabel->setText(QString::fromStdString(msg));
118 ARMARX_WARNING << msg;
119 };
120
121 if (id.memoryName.empty())
122 {
123 handleError("Memory name is empty.");
124 }
125
126 aron::type::ObjectPtr segmentType;
127 std::optional<wm::EntityInstance> instance;
128 try
129 {
130 if (const wm::Memory* data = _model->getSingleMemoryData(id.memoryName))
131 {
132 segmentType = data->getProviderSegment(id).aronType();
133
134 if (id.hasInstanceIndex())
135 {
136 instance = data->getInstance(id);
137 }
138 else if (id.hasTimestamp())
139 {
140 instance = data->getSnapshot(id).getInstance(0);
141 }
142 else
143 {
144 instance = data->getEntity(id).getLatestSnapshot().getInstance(0);
145 }
146 }
147 else
148 {
149 std::stringstream ss;
150 ss << "Memory name '" << id.memoryName << "' is unknown. Known are: "
151 << simox::alg::join(simox::alg::get_keys(_model->memoryDataCopy()), ", ");
152 _statusLabel->setText(QString::fromStdString(ss.str()));
153 }
154 }
155 catch (const armem::error::ArMemError&)
156 {
157 // May be handled by remote lookup
158 }
159
160 if (not instance)
161 {
162 try
163 {
164 // Resolve remotely (may still fail, returns an optional).
165 instance = _model->mns().resolveEntityInstance(id);
166 }
167 catch (const armem::error::ArMemError& e)
168 {
169 ARMARX_WARNING << e.what();
170 _statusLabel->setText(e.what());
171 }
172 }
173
174 if (instance)
175 {
176 auto* view = new InstanceView();
177 _instanceGroupBoxView->instanceView()->addDataView(view);
178 view->update(*instance, segmentType);
179 //instanceGroup->view->addInstanceView(*instance, segmentType);
180 }
181 else
182 {
183 // ToDo: Propagate error back to highlight selected entry in red
184 }
185 }
186
189 {
190 return _instanceGroupBoxView;
191 }
192
193}
#define ARMARX_CHECK_NULL(ptr)
bool hasEntityName() const
Definition MemoryID.h:121
std::string memoryName
Definition MemoryID.h:50
Base class for all exceptions thrown by the armem library.
Definition ArMemError.h:19
InstanceGroupBoxController(MemoryViewerUIContext &uiContext, std::shared_ptr< armem::gui::model::MemoryViewerModel > model)
armem::gui::view::InstanceGroupBoxView * instanceGroupBoxView() const
void memoryIdResolutionRequested(const MemoryID &id)
Client-side working memory entity snapshot.
Client-side working memory entity.
Client-side working memory.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
instance::InstanceView InstanceView
std::shared_ptr< Object > ObjectPtr
Definition Object.h:36
void replaceWidget(WidgetT *&old, QWidget *neu, QLayout *parentLayout)
Definition gui_utils.h:34
auto & getLatestSnapshot(int snapshotIndex=0)
Retrieve the latest entity snapshot.