WidgetProxy.h
Go to the documentation of this file.
1#pragma once
2
3#include <sstream>
4
5#include <ArmarXGui/interface/RemoteGuiInterface.h>
7
8namespace armarx::RemoteGui
9{
10 class TabProxy;
11
12 template <typename T>
13 class ValueProxy;
14
15 class ButtonProxy;
16
18 {
19 public:
20 TabProxy() = default;
21 TabProxy(RemoteGuiInterfacePrx const& remoteGui, std::string const& tabId);
22
23 void receiveUpdates();
24 void sendUpdates();
25
26 void
28 {
29 remoteGui->removeTab(tabId);
30 }
31
32 template <typename T>
33 ValueProxy<T> getValue(std::string const& name);
34 template <typename T>
35 void getValue(T& val, std::string const& name);
36 template <typename T>
37 void getValue(std::atomic<T>& val, std::string const& name);
38
39 template <typename T>
40 void setValue(const T& val, std::string const& name);
41 template <typename T>
42 void setValue(const std::atomic<T>& val, std::string const& name);
43
44 bool hasValueChanged(std::string const& name);
45
46 ButtonProxy getButton(std::string const& name);
47 bool getButtonClicked(std::string const& name);
48
49 template <typename T>
50 T internalGetValue(std::string const& name) const;
51 template <typename T>
52 void internalSetValue(std::string const& name, T const& value);
53 bool internalButtonClicked(std::string const& name) const;
54 void internalSetHidden(std::string const& name, bool hidden);
55 void internalSetDisabled(std::string const& name, bool disabled);
56
57 RemoteGui::WidgetState& getWidgetState(std::string const& name);
58
59 private:
60 RemoteGuiInterfacePrx remoteGui;
61 std::string tabId;
62 RemoteGui::ValueMap currentValues;
63 RemoteGui::ValueMap oldValues;
64 RemoteGui::ValueMap newValues;
65 RemoteGui::ValueMap dirtyValues;
66
67 RemoteGui::WidgetStateMap currentWidgetStates;
68 RemoteGui::WidgetStateMap oldWidgetStates;
69 RemoteGui::WidgetStateMap newWidgetStates;
70 RemoteGui::WidgetState& getNewWidgetState(std::string const& name);
71
72 bool valuesChanged = false;
73 bool widgetChanged = false;
74 };
75
77 {
78 public:
79 WidgetProxy(TabProxy* tab, std::string const& name);
80
81 void setHidden(bool hidden = true);
82 void setDisabled(bool disabled = true);
83
84 bool isHidden() const;
85 bool isDisabled() const;
86
87 protected:
89 std::string name;
90 };
91
92 template <typename T>
93 class ValueProxy : public WidgetProxy
94 {
95 public:
97
98 T get() const;
99
100 template <class T2>
101 T2 cast_static() const;
102 template <class T2>
103 T2 cast_lexical() const;
104
105 template <typename T1 = T, std::enable_if_t<std::is_trivially_copyable_v<T1>, int> = 0>
106 void set(std::atomic<T1> const& value);
107
108 void set(T const& value);
109 };
110
111 class ButtonProxy : public ValueProxy<int>
112 {
113 public:
114 using ValueProxy::ValueProxy;
115 bool clicked() const;
116 };
117} // namespace armarx::RemoteGui
118
119//TabProxy
120namespace armarx::RemoteGui
121{
122 inline TabProxy::TabProxy(RemoteGuiInterfacePrx const& remoteGui, std::string const& tabId) :
123 remoteGui(remoteGui), tabId(tabId)
124 {
125 currentValues = remoteGui->getValues(tabId);
126 oldValues = currentValues;
127 newValues = currentValues;
128
129 currentWidgetStates = remoteGui->getWidgetStates(tabId);
130 oldWidgetStates = currentWidgetStates;
131 newWidgetStates = currentWidgetStates;
132 }
133
134 inline void
136 {
138 oldValues = currentValues;
139 currentValues = remoteGui->getValues(tabId);
140 newValues = currentValues;
141 valuesChanged = false;
142 dirtyValues.clear();
143
144 currentWidgetStates = remoteGui->getWidgetStates(tabId);
145 oldWidgetStates = currentWidgetStates;
146 newWidgetStates = currentWidgetStates;
147 widgetChanged = false;
148 }
149
150 inline void
152 {
154 if (valuesChanged)
155 {
156 remoteGui->setValues(tabId, dirtyValues);
157 dirtyValues.clear();
158 }
159 if (widgetChanged)
160 {
161 remoteGui->setWidgetStates(tabId, newWidgetStates);
162 }
163 }
164
165 template <typename T>
166 inline ValueProxy<T>
167 TabProxy::getValue(std::string const& name)
168 {
170 return ValueProxy<T>(this, name);
171 }
172
173 template <typename T>
174 inline void
175 TabProxy::getValue(T& val, std::string const& name)
176 {
178 getValueFromMap(val, currentValues, name);
179 }
180
181 template <typename T>
182 inline void
183 TabProxy::getValue(std::atomic<T>& val, std::string const& name)
184 {
186 val.store(getValue<T>(name).get());
187 }
188
189 template <typename T>
190 inline void
191 TabProxy::setValue(const T& val, std::string const& name)
192 {
193 getValue<T>(name).set(val);
194 }
195
196 template <typename T>
197 inline void
198 TabProxy::setValue(const std::atomic<T>& val, std::string const& name)
199 {
200 setValue(val.load(), name);
201 }
202
203 template <typename T>
204 inline T
205 TabProxy::internalGetValue(std::string const& name) const
206 {
208 return RemoteGui::getAndReturnValue<T>(currentValues, name);
209 }
210
211 template <typename T>
212 inline void
213 TabProxy::internalSetValue(std::string const& name, T const& value)
214 {
216 T currentValue = internalGetValue<T>(name);
217 if (currentValue != value)
218 {
219 valuesChanged = true;
220 dirtyValues[name] = makeValue(value);
221 }
222 newValues[name] = makeValue(value);
223 }
224
225 inline bool
226 TabProxy::internalButtonClicked(std::string const& name) const
227 {
228 return RemoteGui::buttonClicked(newValues, oldValues, name);
229 }
230
231 inline void
232 TabProxy::internalSetHidden(std::string const& name, bool hidden)
233 {
234 RemoteGui::WidgetState& state = getNewWidgetState(name);
235 if (state.hidden != hidden)
236 {
237 widgetChanged = true;
238 }
239 state.hidden = hidden;
240 }
241
242 inline void
243 TabProxy::internalSetDisabled(std::string const& name, bool disabled)
244 {
245 RemoteGui::WidgetState& state = getNewWidgetState(name);
246 if (state.disabled != disabled)
247 {
248 widgetChanged = true;
249 }
250 state.disabled = disabled;
251 }
252
253 inline RemoteGui::WidgetState&
254 TabProxy::getWidgetState(std::string const& name)
255 {
256 auto iter = currentWidgetStates.find(name);
257 if (iter == currentWidgetStates.end())
258 {
259 throw LocalException("No widget with name '") << name << "' found";
260 }
261 return iter->second;
262 }
263
264 inline RemoteGui::WidgetState&
265 TabProxy::getNewWidgetState(std::string const& name)
266 {
267 auto iter = newWidgetStates.find(name);
268 if (iter == newWidgetStates.end())
269 {
270 throw LocalException("No widget with name '") << name << "' in NewWidgetStates found";
271 }
272 return iter->second;
273 }
274} // namespace armarx::RemoteGui
275
276//WidgetProxy
277namespace armarx::RemoteGui
278{
279 inline WidgetProxy::WidgetProxy(TabProxy* tab, std::string const& name) : tab(tab), name(name)
280 {
281 }
282
283 inline void
285 {
286 tab->internalSetHidden(name, hidden);
287 }
288
289 inline void
291 {
292 tab->internalSetDisabled(name, disabled);
293 }
294
295 inline bool
297 {
298 return tab->getWidgetState(name).hidden;
299 }
300
301 inline bool
303 {
304 return tab->getWidgetState(name).disabled;
305 }
306} // namespace armarx::RemoteGui
307
308//ValueProxy
309namespace armarx::RemoteGui
310{
311 template <typename T>
312 inline T
314 {
316 return tab->internalGetValue<T>(name);
317 }
318
319 template <typename T>
320 template <class T2>
321 inline T2
323 {
325 return static_cast<T2>(get());
326 }
327
328 template <typename T>
329 template <class T2>
330 inline T2
332 {
334 std::stringstream s;
335 s << get();
336 T2 result;
337 s >> result;
338 return result;
339 }
340
341 template <typename T>
342 inline void
343 ValueProxy<T>::set(T const& value)
344 {
346 tab->internalSetValue<T>(name, value);
347 }
348
349 template <typename T>
350 template <typename T1, typename std::enable_if_t<std::is_trivially_copyable_v<T1>, int>>
351 void
352 ValueProxy<T>::set(const std::atomic<T1>& value)
353 {
354 set(value.load());
355 }
356} // namespace armarx::RemoteGui
357
358//ButtonProxy
359namespace armarx::RemoteGui
360{
361 inline bool
363 {
365 return tab->internalButtonClicked(name);
366 }
367} // namespace armarx::RemoteGui
void setValue(const T &val, std::string const &name)
T internalGetValue(std::string const &name) const
void internalSetDisabled(std::string const &name, bool disabled)
bool hasValueChanged(std::string const &name)
RemoteGui::WidgetState & getWidgetState(std::string const &name)
bool getButtonClicked(std::string const &name)
void internalSetHidden(std::string const &name, bool hidden)
ValueProxy< T > getValue(std::string const &name)
void internalSetValue(std::string const &name, T const &value)
ButtonProxy getButton(std::string const &name)
bool internalButtonClicked(std::string const &name) const
void set(T const &value)
WidgetProxy(TabProxy *tab, std::string const &name)
void set(std::atomic< T1 > const &value)
void setHidden(bool hidden=true)
WidgetProxy(TabProxy *tab, std::string const &name)
void setDisabled(bool disabled=true)
T getAndReturnValue(ValueMap const &values, std::string const &name)
Definition Storage.h:190
ValueVariant makeValue(bool value)
Definition Storage.cpp:144
std::map< std::string, ValueVariant > ValueMap
std::enable_if_t< meta::cfg::gui_definition_enabled_v< T >, void > getValueFromMap(T &cfg, RemoteGui::ValueMap const &values, std::string const &name)
Definition GetValue.h:12
bool buttonClicked(RemoteGui::ValueMap const &newValues, RemoteGui::ValueMap const &oldValues, std::string const &name)
Definition Storage.cpp:7
#define ARMARX_TRACE
Definition trace.h:77