RemoteGuiProvider.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarXGui::ArmarXObjects::RemoteGuiProvider
17 * @author Fabian Paus ( fabian dot paus at kit dot edu )
18 * @date 2018
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include "RemoteGuiProvider.h"
24
25#include <functional>
26
30
32
33namespace armarx
34{
35 void
37 {
38 topicName = getProperty<std::string>("TopicName").getValue();
39
40 offeringTopic(topicName);
41 }
42
43 void
48
49 void
53
54 void
58
65
66 std::string
68 {
69 return topicName;
70 }
71
72 static void
73 fillRecursively(RemoteGui::WidgetStateMap& widgetStates,
74 RemoteGui::ValueMap& values,
75 RemoteGui::WidgetPtr const& widget,
76 RemoteGui::WidgetPtr const& rootWidget)
77 {
79 const auto dumpInfo = ARMARX_STREAM_PRINTER
80 {
81 out << "\nall state names: " << getMapKeys(widgetStates)
82 << "\nall value names: " << getMapKeys(values) << "\nwidget tree\n";
83
84 std::function<void(RemoteGui::WidgetPtr const&, std::size_t)> dump =
85 [&](RemoteGui::WidgetPtr const& node, std::size_t lvl)
86 {
87 out << std::string(lvl * 4, '-') << " " << node->name << " ["
88 << GetTypeString(*node) << "]\n";
89 for (const auto& c : node->children)
90 {
91 dump(c, lvl + 1);
92 }
93 };
94 dump(rootWidget, 1);
95 };
97 {
98 std::stringstream s;
99 if (!handler.isValid(*widget, s))
100 {
101 throw LocalException()
102 << "Widget is not valid: " << widget->name << ", WidgetT: " << widget->ice_id()
103 << ", HandlerT: " << handler.getHandlerT() << "\nMessage:\n"
104 << s.str() << dumpInfo;
105 }
106 }
107
108 std::string const& name = widget->name;
109 if (!name.empty())
110 {
112 bool nameIsNew = widgetStates.emplace(widget->name, widget->defaultState).second;
113 if (!nameIsNew)
114 {
116 throw LocalException()
117 << "Widget with name '" << widget->name << "' already exists" << dumpInfo;
118 }
119 nameIsNew = values.emplace(widget->name, widget->defaultValue).second;
120 ARMARX_CHECK_EXPRESSION(nameIsNew);
121 }
122
123 for (auto& child : widget->children)
124 {
126 fillRecursively(widgetStates, values, child, rootWidget);
127 }
128 }
129
130 void
131 RemoteGuiProvider::createTab(const std::string& tabId,
132 const RemoteGui::WidgetPtr& rootWidget,
133 const Ice::Current&)
134 {
136 ARMARX_INFO << "Creating remote tab: " << tabId;
137 {
138 std::unique_lock<std::mutex> lock(tabMutex);
139 tabs[tabId] = rootWidget;
140
141 // Clear old state
142 auto& widgetStates = tabWidgetStates[tabId];
143 widgetStates.clear();
144 auto& values = tabStates[tabId];
145 values.clear();
146
147 // Fill default values
148 fillRecursively(tabWidgetStates[tabId], tabStates[tabId], rootWidget, rootWidget);
149 }
150
151 topic->reportTabChanged(tabId);
152 }
153
154 void
155 RemoteGuiProvider::removeTab(const std::string& tabId, const Ice::Current&)
156 {
158 ARMARX_INFO << "Removing remote tab: " << tabId;
159 {
160 std::unique_lock<std::mutex> lock(tabMutex);
161 tabs.erase(tabId);
162 tabWidgetStates.erase(tabId);
163 tabStates.erase(tabId);
164 }
165 topic->reportTabsRemoved();
166 }
167
168 RemoteGui::WidgetMap
169 RemoteGuiProvider::getTabs(const Ice::Current&)
170 {
171 std::unique_lock<std::mutex> lock(tabMutex);
172 return tabs;
173 }
174
175 RemoteGui::TabWidgetStateMap
177 {
178 std::unique_lock<std::mutex> lock(tabMutex);
179 return tabWidgetStates;
180 }
181
182 RemoteGui::TabValueMap
184 {
185 std::unique_lock<std::mutex> lock(tabMutex);
186 return tabStates;
187 }
188
190 RemoteGuiProvider::getValues(const std::string& tabId, const Ice::Current&)
191 {
192 std::unique_lock<std::mutex> lock(tabMutex);
193 return tabStates.at(tabId);
194 }
195
196 void
197 RemoteGuiProvider::setValue(const std::string& tabId,
198 const std::string& widgetName,
199 const RemoteGui::ValueVariant& value,
200 const Ice::Current&)
201 {
203 std::unique_lock<std::mutex> lock(tabMutex);
204 RemoteGui::ValueMap& tabState = tabStates.at(tabId);
205 tabState.at(widgetName) = value;
207 delta[widgetName] = value;
208 topic->reportStateChanged(tabId, delta);
209 }
210
211 void
212 RemoteGuiProvider::setValues(const std::string& tabId,
213 const RemoteGui::ValueMap& values,
214 const Ice::Current&)
215 {
217 std::unique_lock<std::mutex> lock(tabMutex);
218 RemoteGui::ValueMap& merged = tabStates.at(tabId);
219 for (const auto& pair : values)
220 {
221 merged[pair.first] = pair.second;
222 }
223 topic->reportStateChanged(tabId, values);
224 }
225
226 RemoteGui::WidgetStateMap
227 RemoteGuiProvider::getWidgetStates(const std::string& tabId, const Ice::Current&)
228 {
229 std::unique_lock<std::mutex> lock(tabMutex);
230 return tabWidgetStates.at(tabId);
231 }
232
233 void
234 RemoteGuiProvider::setWidgetStates(const std::string& tabId,
235 const RemoteGui::WidgetStateMap& state,
236 const Ice::Current&)
237 {
239 std::unique_lock<std::mutex> lock(tabMutex);
240 RemoteGui::WidgetStateMap& tabWidgetState = tabWidgetStates.at(tabId);
241 tabWidgetState = state;
242 topic->reportWidgetChanged(tabId, tabWidgetState);
243 }
244} // namespace armarx
#define ARMARX_STREAM_PRINTER
use this macro to write output code that is executed when printed and thus not executed if the debug ...
Definition Logging.h:310
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
RemoteGui::TabWidgetStateMap getTabStates(const Ice::Current &) override
RemoteGui::WidgetStateMap getWidgetStates(const std::string &tabId, const Ice::Current &) override
void removeTab(const std::string &tabId, const Ice::Current &) override
void createTab(const std::string &tabId, const RemoteGui::WidgetPtr &rootWidget, const Ice::Current &) override
void onDisconnectComponent() override
void setWidgetStates(const std::string &tabId, const RemoteGui::WidgetStateMap &state, const Ice::Current &) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
RemoteGui::ValueMap getValues(const std::string &tabId, const Ice::Current &) override
RemoteGui::WidgetMap getTabs(const Ice::Current &) override
RemoteGui::TabValueMap getValuesForAllTabs(const Ice::Current &) override
std::string getTopicName(const Ice::Current &) override
void setValues(const std::string &tabId, const RemoteGui::ValueMap &values, const Ice::Current &) override
void setValue(const std::string &tabId, const std::string &widgetName, const RemoteGui::ValueVariant &value, const Ice::Current &) override
void dump(vec_point_2d &vec)
Definition gdiam.cpp:1666
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
std::map< std::string, ValueVariant > ValueMap
const WidgetHandler & getWidgetHandler(WidgetPtr const &desc)
double s(double t, double s0, double v0, double a0, double j)
Definition CtrlUtil.h:33
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void getMapKeys(const MapType &map, OutputIteratorType it)
Definition algorithm.h:173
#define ARMARX_TRACE
Definition trace.h:77