WidgetProxy.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sstream>
4 
5 #include <ArmarXGui/interface/RemoteGuiInterface.h>
7 
8 namespace armarx::RemoteGui
9 {
10  class TabProxy;
11 
12  template <typename T>
13  class ValueProxy;
14 
15  class ButtonProxy;
16 
17  class TabProxy
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
120 namespace 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  {
137  ARMARX_TRACE;
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  {
153  ARMARX_TRACE;
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  {
169  ARMARX_TRACE;
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  {
177  ARMARX_TRACE;
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  {
185  ARMARX_TRACE;
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  {
207  ARMARX_TRACE;
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  {
215  ARMARX_TRACE;
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
277 namespace armarx::RemoteGui
278 {
279  inline WidgetProxy::WidgetProxy(TabProxy* tab, std::string const& name) : tab(tab), name(name)
280  {
281  }
282 
283  inline void
284  WidgetProxy::setHidden(bool hidden)
285  {
286  tab->internalSetHidden(name, hidden);
287  }
288 
289  inline void
290  WidgetProxy::setDisabled(bool disabled)
291  {
292  tab->internalSetDisabled(name, disabled);
293  }
294 
295  inline bool
296  WidgetProxy::isHidden() const
297  {
298  return tab->getWidgetState(name).hidden;
299  }
300 
301  inline bool
302  WidgetProxy::isDisabled() const
303  {
304  return tab->getWidgetState(name).disabled;
305  }
306 } // namespace armarx::RemoteGui
307 
308 //ValueProxy
309 namespace armarx::RemoteGui
310 {
311  template <typename T>
312  inline T
314  {
315  ARMARX_TRACE;
316  return tab->internalGetValue<T>(name);
317  }
318 
319  template <typename T>
320  template <class T2>
321  inline T2
323  {
324  ARMARX_TRACE;
325  return static_cast<T2>(get());
326  }
327 
328  template <typename T>
329  template <class T2>
330  inline T2
332  {
333  ARMARX_TRACE;
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
344  {
345  ARMARX_TRACE;
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
359 namespace armarx::RemoteGui
360 {
361  inline bool
363  {
364  ARMARX_TRACE;
365  return tab->internalButtonClicked(name);
366  }
367 } // namespace armarx::RemoteGui
armarx::RemoteGui::WidgetProxy::WidgetProxy
WidgetProxy(TabProxy *tab, std::string const &name)
Definition: WidgetProxy.h:279
armarx::RemoteGui::WidgetProxy
Definition: WidgetProxy.h:76
armarx::RemoteGui::TabProxy::hasValueChanged
bool hasValueChanged(std::string const &name)
Definition: WidgetProxy.cpp:18
armarx::RemoteGui::WidgetProxy::setHidden
void setHidden(bool hidden=true)
Definition: WidgetProxy.h:284
armarx::RemoteGui
Definition: LightweightRemoteGuiComponentPlugin.h:30
armarx::RemoteGui::WidgetProxy::isHidden
bool isHidden() const
Definition: WidgetProxy.h:296
armarx::RemoteGui::TabProxy::internalSetValue
void internalSetValue(std::string const &name, T const &value)
Definition: WidgetProxy.h:213
Storage.h
armarx::RemoteGui::ValueProxy::get
T get() const
Definition: WidgetProxy.h:313
armarx::RemoteGui::ValueMap
std::map< std::string, ValueVariant > ValueMap
Definition: RemoteGuiVisitors.h:41
armarx::RemoteGui::TabProxy::remove
void remove()
Definition: WidgetProxy.h:27
armarx::RemoteGui::ButtonProxy
Definition: WidgetProxy.h:111
armarx::RemoteGui::TabProxy::internalSetDisabled
void internalSetDisabled(std::string const &name, bool disabled)
Definition: WidgetProxy.h:243
armarx::RemoteGui::TabProxy::setValue
void setValue(const T &val, std::string const &name)
Definition: WidgetProxy.h:191
armarx::RemoteGui::TabProxy
Definition: WidgetProxy.h:17
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::RemoteGui::TabProxy::getButton
ButtonProxy getButton(std::string const &name)
Definition: WidgetProxy.cpp:6
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::RemoteGui::TabProxy::receiveUpdates
void receiveUpdates()
Definition: WidgetProxy.h:135
armarx::RemoteGui::WidgetProxy::isDisabled
bool isDisabled() const
Definition: WidgetProxy.h:302
armarx::RemoteGui::ValueProxy::cast_lexical
T2 cast_lexical() const
Definition: WidgetProxy.h:331
armarx::RemoteGui::ButtonProxy::clicked
bool clicked() const
Definition: WidgetProxy.h:362
armarx::RemoteGui::makeValue
ValueVariant makeValue(bool value)
Definition: Storage.cpp:144
armarx::RemoteGui::TabProxy::getWidgetState
RemoteGui::WidgetState & getWidgetState(std::string const &name)
Definition: WidgetProxy.h:254
armarx::RemoteGui::TabProxy::internalSetHidden
void internalSetHidden(std::string const &name, bool hidden)
Definition: WidgetProxy.h:232
armarx::RemoteGui::TabProxy::getValue
ValueProxy< T > getValue(std::string const &name)
Definition: WidgetProxy.h:167
armarx::RemoteGui::ValueProxy::set
void set(std::atomic< T1 > const &value)
Definition: WidgetProxy.h:352
armarx::RemoteGui::WidgetProxy::setDisabled
void setDisabled(bool disabled=true)
Definition: WidgetProxy.h:290
armarx::RemoteGui::TabProxy::getButtonClicked
bool getButtonClicked(std::string const &name)
Definition: WidgetProxy.cpp:12
set
set(LIBS ArmarXCoreInterfaces ${CMAKE_THREAD_LIBS_INIT} ${dl_LIBRARIES} ${rt_LIBRARIES} ${QT_LIBRARIES} ${Boost_LIBRARIES} BoostAssertionHandler ArmarXCPPUtility SimoxUtility) set(LIB_FILES ArmarXManager.cpp ArmarXMultipleObjectsScheduler.cpp ArmarXObjectScheduler.cpp ManagedIceObject.cpp ManagedIceObjectPlugin.cpp Component.cpp ComponentPlugin.cpp IceGridAdmin.cpp ArmarXObjectObserver.cpp IceManager.cpp PackagePath.cpp RemoteReferenceCount.cpp logging/LoggingUtil.cpp logging/Logging.cpp logging/LogSender.cpp logging/ArmarXLogBuf.cpp system/ArmarXDataPath.cpp system/DynamicLibrary.cpp system/ProcessWatcher.cpp system/FactoryCollectionBase.cpp system/cmake/CMakePackageFinder.cpp system/cmake/CMakePackageFinderCache.cpp system/cmake/ArmarXPackageToolInterface.cpp system/RemoteObjectNode.cpp services/sharedmemory/HardwareId.cpp services/tasks/RunningTask.cpp services/tasks/ThreadList.cpp services/tasks/ThreadPool.cpp services/profiler/Profiler.cpp services/profiler/FileLoggingStrategy.cpp services/profiler/IceLoggingStrategy.cpp application/Application.cpp application/ApplicationOptions.cpp application/ApplicationProcessFacet.cpp application/ApplicationNetworkStats.cpp application/properties/PropertyUser.cpp application/properties/Property.cpp application/properties/PropertyDefinition.cpp application/properties/PropertyDefinitionContainer.cpp application/properties/PropertyDefinitionHelpFormatter.cpp application/properties/PropertyDefinitionConfigFormatter.cpp application/properties/PropertyDefinitionBriefHelpFormatter.cpp application/properties/PropertyDefinitionXmlFormatter.cpp application/properties/PropertyDefinitionDoxygenFormatter.cpp application/properties/PropertyDefinitionDoxygenComponentPagesFormatter.cpp application/properties/PropertyDefinitionContainerBriefHelpFormatter.cpp application/properties/IceProperties.cpp exceptions/Exception.cpp exceptions/local/UnexpectedEnumValueException.cpp util/FileSystemPathBuilder.cpp util/StringHelpers.cpp util/IceReportSkipper.cpp util/Throttler.cpp util/distributed/AMDCallbackCollection.cpp util/distributed/RemoteHandle/ClientSideRemoteHandleControlBlock.cpp util/distributed/RemoteHandle/RemoteHandle.cpp util/distributed/RemoteHandle/RemoteHandleControlBlock.cpp time/ice_conversions.cpp time/json_conversions.cpp time/CallbackWaitLock.cpp time/Clock.cpp time/ClockType.cpp time/ClockTypeNames.cpp time/CycleUtil.cpp time/DateTime.cpp time/Duration.cpp time/Frequency.cpp time/LocalTimeServer.cpp time/Metronome.cpp time/ScopedStopWatch.cpp time/StopWatch.cpp time/Timer.cpp time/TimeKeeper.cpp time/TimeUtil.cpp csv/CsvWriter.cpp csv/CsvReader.cpp eigen/conversions.cpp eigen/ice_conversions.cpp) set(LIB_HEADERS ArmarXManager.h ArmarXDummyManager.h ArmarXMultipleObjectsScheduler.h ArmarXObjectObserver.h ArmarXObjectScheduler.h ArmarXFwd.h Component.h ComponentPlugin.h ComponentFactories.h CoreObjectFactories.h IceGridAdmin.h IceManager.h IceManagerImpl.h json_conversions.h ManagedIceObject.h ManagedIceObjectPlugin.h ManagedIceObjectImpl.h ManagedIceObjectDependency.h ManagedIceObjectRegistryInterface.h PackagePath.h RemoteReferenceCount.h system/ImportExport.h system/ImportExportComponent.h system/AbstractFactoryMethod.h system/FactoryCollectionBase.h system/Synchronization.h system/ArmarXDataPath.h system/DynamicLibrary.h system/ProcessWatcher.h system/ConditionSynchronization.h system/cmake/CMakePackageFinder.h system/cmake/CMakePackageFinderCache.h system/cmake/FindPackageX.cmake system/cmake/ArmarXPackageToolInterface.h system/RemoteObjectNode.h logging/LoggingUtil.h logging/LogSender.h logging/Logging.h logging/ArmarXLogBuf.h logging/SpamFilterData.h services/tasks/RunningTask.h services/tasks/PeriodicTask.h services/tasks/ThreadList.h services/tasks/TaskUtil.h services/tasks/ThreadPool.h services/sharedmemory/SharedMemoryProvider.h services/sharedmemory/SharedMemoryConsumer.h services/sharedmemory/IceSharedMemoryProvider.h services/sharedmemory/IceSharedMemoryConsumer.h services/sharedmemory/HardwareIdentifierProvider.h services/sharedmemory/HardwareId.h services/sharedmemory/exceptions/SharedMemoryExceptions.h services/profiler/Profiler.h services/profiler/LoggingStrategy.h services/profiler/FileLoggingStrategy.h services/profiler/IceLoggingStrategy.h application/Application.h application/ApplicationOptions.h application/ApplicationProcessFacet.h application/ApplicationNetworkStats.h application/properties/forward_declarations.h application/properties/Properties.h application/properties/Property.h application/properties/PluginEigen.h application/properties/PluginEnumNames.h application/properties/PluginCfgStruct.h application/properties/PluginAll.h application/properties/PropertyUser.h application/properties/PropertyDefinition.h application/properties/PropertyDefinition.hpp application/properties/PropertyDefinitionInterface.h application/properties/PropertyDefinitionContainer.h application/properties/PropertyDefinitionFormatter.h application/properties/PropertyDefinitionContainerFormatter.h application/properties/PropertyDefinitionConfigFormatter.h application/properties/PropertyDefinitionHelpFormatter.h application/properties/PropertyDefinitionBriefHelpFormatter.h application/properties/PropertyDefinitionXmlFormatter.h application/properties/PropertyDefinitionDoxygenFormatter.h application/properties/PropertyDefinitionDoxygenComponentPagesFormatter.h application/properties/PropertyDefinitionContainerBriefHelpFormatter.h application/properties/ProxyPropertyDefinition.h application/properties/IceProperties.h exceptions/Exception.h exceptions/LocalException.h exceptions/local/DynamicLibraryException.h exceptions/local/ExpressionException.h exceptions/local/FileIOException.h exceptions/local/InvalidPropertyValueException.h exceptions/local/MissingRequiredPropertyException.h exceptions/local/PropertyInheritanceCycleException.h exceptions/local/ProxyNotInitializedException.h exceptions/local/UnexpectedEnumValueException.h exceptions/local/UnmappedValueException.h exceptions/local/ValueRangeExceededException.h exceptions/user/NotImplementedYetException.h rapidxml/rapidxml.hpp rapidxml/rapidxml_print.hpp rapidxml/rapidxml_iterators.hpp rapidxml/rapidxml_utils.hpp rapidxml/wrapper/RapidXmlReader.h rapidxml/wrapper/RapidXmlWriter.h rapidxml/wrapper/DefaultRapidXmlReader.h rapidxml/wrapper/MultiNodeRapidXMLReader.h util/IceBlobToObject.h util/ObjectToIceBlob.h util/FileSystemPathBuilder.h util/FiniteStateMachine.h util/StringHelpers.h util/StringHelperTemplates.h util/algorithm.h util/OnScopeExit.h util/Predicates.h util/Preprocessor.h util/PropagateConst.h util/Registrar.h util/TemplateMetaProgramming.h util/TripleBuffer.h util/IceReportSkipper.h util/Throttler.h util/distributed/AMDCallbackCollection.h util/distributed/RemoteHandle/ClientSideRemoteHandleControlBlock.h util/distributed/RemoteHandle/RemoteHandle.h util/distributed/RemoteHandle/RemoteHandleControlBlock.h util/SimpleStatemachine.h time.h time_minimal.h time/forward_declarations.h time/ice_conversions.h time/json_conversions.h time/CallbackWaitLock.h time/Clock.h time/ClockType.h time/ClockTypeNames.h time/CycleUtil.h time/DateTime.h time/Duration.h time/Frequency.h time/LocalTimeServer.h time/Metronome.h time/ScopedStopWatch.h time/StopWatch.h time/Timer.h time/TimeUtil.h time/TimeKeeper.h csv/CsvWriter.h csv/CsvReader.h eigen/conversions.h eigen/ice_conversions.h ice_conversions.h ice_conversions/ice_conversions_boost_templates.h ice_conversions/ice_conversions_templates.h ice_conversions/ice_conversions_templates.tpp $
Definition: CMakeLists.txt:12
armarx::RemoteGui::WidgetProxy::name
std::string name
Definition: WidgetProxy.h:89
armarx::RemoteGui::TabProxy::sendUpdates
void sendUpdates()
Definition: WidgetProxy.h:151
armarx::RemoteGui::TabProxy::internalGetValue
T internalGetValue(std::string const &name) const
Definition: WidgetProxy.h:205
armarx::RemoteGui::WidgetProxy::tab
TabProxy * tab
Definition: WidgetProxy.h:88
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
armarx::RemoteGui::buttonClicked
bool buttonClicked(RemoteGui::ValueMap const &newValues, RemoteGui::ValueMap const &oldValues, std::string const &name)
Definition: Storage.cpp:7
armarx::RemoteGui::ValueProxy
Definition: WidgetProxy.h:13
armarx::RemoteGui::ValueProxy::cast_static
T2 cast_static() const
Definition: WidgetProxy.h:322
armarx::RemoteGui::TabProxy::internalButtonClicked
bool internalButtonClicked(std::string const &name) const
Definition: WidgetProxy.h:226
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::RemoteGui::getValueFromMap
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
armarx::RemoteGui::TabProxy::TabProxy
TabProxy()=default