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