TypedWidget.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
5 
6 #include "WidgetHandler.h"
7 #include "Storage.h"
8 
9 #include <ArmarXGui/interface/RemoteGuiInterface.h>
11 
12 #include <QWidget>
13 
14 namespace armarx::RemoteGui
15 {
16 
17  template <typename HandlerT>
19  {
20  using RemoteWidgetT = typename HandlerT::RemoteWidgetT;
21  using QWidgetT = typename HandlerT::QWidgetT;
22  static const RemoteGui::ValueVariantType ValueType = HandlerT::ValueType;
23  using ValueT = typename Storage<ValueType>::Type;
24 
25 
26  std::string getHandlerT() const override
27  {
28  return GetTypeString<HandlerT>();
29  }
30 
31  bool isValid(Widget const& desc, std::ostream& out = cnull) const override
32  {
33  if (desc.defaultValue.type != ValueType)
34  {
35  return false;
36  }
37 
38  auto concreteDesc = dynamic_cast<RemoteWidgetT const*>(&desc);
39  ARMARX_CHECK_EXPRESSION(concreteDesc != nullptr);
40 
41  return HandlerT::isValid(*concreteDesc, out);
42  }
43 
44  QWidget* createWidget(Widget const& desc,
45  ValueVariant const& initialValue,
46  CreateWidgetCallback const& createChild,
47  const QObject* stateChangeReceiver,
48  const char* stateChangeSlot) const override
49  {
51  auto concreteDesc = dynamic_cast<RemoteWidgetT const*>(&desc);
52  ARMARX_CHECK_EXPRESSION(concreteDesc != nullptr);
53 
54  ARMARX_CHECK_EXPRESSION(initialValue.type == ValueType)
55  << getVariantTypeName(initialValue.type) << " != " << GetTypeString<ValueT>();
56  ValueT concreteInitialValue = getSingleValue<ValueT>(initialValue);
57 
58  auto* widget = HandlerT::createWidget(*concreteDesc, createChild,
59  stateChangeReceiver, stateChangeSlot);
60 
61  HandlerT::updateGui(*concreteDesc, widget, concreteInitialValue);
62 
63  return widget;
64  }
65 
66  virtual void updateGui(Widget const& desc,
67  QWidget* widget,
68  ValueVariant const& value) const override
69  {
71  auto concreteDesc = dynamic_cast<RemoteWidgetT const*>(&desc);
72  ARMARX_CHECK_EXPRESSION(concreteDesc != nullptr);
73 
74  QWidgetT* concreteWidget = qobject_cast<QWidgetT*>(widget);
75  ARMARX_CHECK_EXPRESSION(concreteWidget != nullptr);
76 
78  << getVariantTypeName(value.type) << " != " << GetTypeString<ValueT>();
79  ValueT concreteValue = getSingleValue<ValueT>(value);
80 
81  HandlerT::updateGui(*concreteDesc, concreteWidget, concreteValue);
82  }
83 
84  virtual ValueVariant handleGuiChange(Widget const& desc,
85  QWidget* widget) const override
86  {
88  ARMARX_CHECK_EXPRESSION(widget != nullptr);
89  const auto dumpParamInfo = ARMARX_STREAM_PRINTER
90  {
91  out << "desc type = "
92  << desc.ice_id()
93  << "\ntarget type = "
94  << GetTypeString<RemoteWidgetT>()
95  << "\nwidget type = "
96  << GetTypeString(*widget)
97  << "\ntarget type = "
98  << GetTypeString<QWidgetT>();
99  };
100  auto concreteDesc = dynamic_cast<RemoteWidgetT const*>(&desc);
101  QWidgetT* concreteWidget = qobject_cast<QWidgetT*>(widget);
102 
103  ARMARX_CHECK_EXPRESSION(concreteDesc != nullptr) << dumpParamInfo;
104  ARMARX_CHECK_EXPRESSION(concreteWidget != nullptr) << dumpParamInfo;
105 
106  ValueVariant currentState = makeValue(HandlerT::handleGuiChange(*concreteDesc, concreteWidget));
107  return currentState;
108  }
109  };
110 
111 
112  template <typename RemoteWidgetT_,
113  typename QWidgetT_,
114  ValueVariantType ValueType_ = RemoteGui::VALUE_VARIANT_EMPTY>
115  struct TypedWidget
116  {
117  using RemoteWidgetT = RemoteWidgetT_;
118  using QWidgetT = QWidgetT_;
119  static const ValueVariantType ValueType = ValueType_;
121 
122  static bool isValid(RemoteWidgetT const&, std::ostream& out)
123  {
124  return true;
125  }
126  };
127 
128  template <typename RemoteWidgetT_,
129  typename QWidgetT_>
130  struct TypedWidget<RemoteWidgetT_, QWidgetT_, RemoteGui::VALUE_VARIANT_EMPTY>
131  {
132  using RemoteWidgetT = RemoteWidgetT_;
133  using QWidgetT = QWidgetT_;
134  static const ValueVariantType ValueType = VALUE_VARIANT_EMPTY;
136 
137  static bool isValid(RemoteWidgetT const&, std::ostream&)
138  {
139  return true;
140  }
141 
142  static void updateGui(RemoteWidgetT const& desc, QWidgetT* widget, ValueT const& value)
143  {
144  // Do nothing
145  }
146 
147  static ValueT handleGuiChange(RemoteWidgetT const& desc, QWidgetT* widget)
148  {
149  // Do nothing
150  return ValueT {};
151  }
152  };
153 
154 }
armarx::RemoteGui::TypedWidgetHandler::isValid
bool isValid(Widget const &desc, std::ostream &out=cnull) const override
Definition: TypedWidget.h:31
armarx::RemoteGui::TypedWidgetHandler::ValueT
typename Storage< ValueType >::Type ValueT
Definition: TypedWidget.h:23
armarx::RemoteGui::Client::ToggleButton
Definition: Widgets.h:139
armarx::RemoteGui::TypedWidgetHandler
Definition: TypedWidget.h:18
armarx::RemoteGui::TypedWidgetHandler::RemoteWidgetT
typename HandlerT::RemoteWidgetT RemoteWidgetT
Definition: TypedWidget.h:20
armarx::RemoteGui
Definition: LightweightRemoteGuiComponentPlugin.h:30
armarx::RemoteGui::TypedWidget
Definition: TypedWidget.h:115
Storage.h
armarx::RemoteGui::TypedWidget< RemoteWidgetT_, QWidgetT_, RemoteGui::VALUE_VARIANT_EMPTY >::isValid
static bool isValid(RemoteWidgetT const &, std::ostream &)
Definition: TypedWidget.h:137
armarx::RemoteGui::TypedWidgetHandler::createWidget
QWidget * createWidget(Widget const &desc, ValueVariant const &initialValue, CreateWidgetCallback const &createChild, const QObject *stateChangeReceiver, const char *stateChangeSlot) const override
Definition: TypedWidget.h:44
armarx::RemoteGui::TypedWidget< RemoteWidgetT_, QWidgetT_, RemoteGui::VALUE_VARIANT_EMPTY >::updateGui
static void updateGui(RemoteWidgetT const &desc, QWidgetT *widget, ValueT const &value)
Definition: TypedWidget.h:142
armarx::RemoteGui::TypedWidget< RemoteWidgetT_, QWidgetT_, RemoteGui::VALUE_VARIANT_EMPTY >::handleGuiChange
static ValueT handleGuiChange(RemoteWidgetT const &desc, QWidgetT *widget)
Definition: TypedWidget.h:147
armarx::RemoteGui::TypedWidget< ToggleButton, QPushButton, VALUE_VARIANT_BOOL >::ValueT
typename Storage< ValueType >::Type ValueT
Definition: TypedWidget.h:120
armarx::RemoteGui::TypedWidgetHandler::QWidgetT
typename HandlerT::QWidgetT QWidgetT
Definition: TypedWidget.h:21
armarx::cnull
std::ostream cnull
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::RemoteGui::Client::Widget
Definition: Widgets.h:21
armarx::RemoteGui::TypedWidget::ValueType
static const ValueVariantType ValueType
Definition: TypedWidget.h:119
armarx::RemoteGui::WidgetHandler
Definition: WidgetHandler.h:22
WidgetHandler.h
armarx::RemoteGui::TypedWidgetHandler::handleGuiChange
virtual ValueVariant handleGuiChange(Widget const &desc, QWidget *widget) const override
Definition: TypedWidget.h:84
armarx::RemoteGui::makeValue
ValueVariant makeValue(bool value)
Definition: Storage.cpp:133
armarx::GetTypeString
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
Definition: GetTypeString.h:36
armarx::RemoteGui::TypedWidget< ToggleButton, QPushButton, VALUE_VARIANT_BOOL >::QWidgetT
QPushButton QWidgetT
Definition: TypedWidget.h:118
LoggingUtil.h
ExpressionException.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::RemoteGui::TypedWidget< RemoteWidgetT_, QWidgetT_, RemoteGui::VALUE_VARIANT_EMPTY >::QWidgetT
QWidgetT_ QWidgetT
Definition: TypedWidget.h:133
armarx::RemoteGui::getVariantTypeName
const char * getVariantTypeName(ValueVariantType type)
Definition: Storage.cpp:109
armarx::RemoteGui::TypedWidgetHandler::getHandlerT
std::string getHandlerT() const override
Definition: TypedWidget.h:26
armarx::RemoteGui::CreateWidgetCallback
std::function< QWidgetPtr(WidgetPtr const &)> CreateWidgetCallback
Definition: WidgetHandler.h:20
armarx::RemoteGui::TypedWidgetHandler::updateGui
virtual void updateGui(Widget const &desc, QWidget *widget, ValueVariant const &value) const override
Definition: TypedWidget.h:66
armarx::RemoteGui::Storage::Type
std::nullptr_t Type
Definition: Storage.h:37
armarx::RemoteGui::TypedWidget::isValid
static bool isValid(RemoteWidgetT const &, std::ostream &out)
Definition: TypedWidget.h:122
armarx::RemoteGui::TypedWidgetHandler::ValueType
static const RemoteGui::ValueVariantType ValueType
Definition: TypedWidget.h:22
armarx::RemoteGui::TypedWidget< RemoteWidgetT_, QWidgetT_, RemoteGui::VALUE_VARIANT_EMPTY >::ValueT
typename Storage< ValueType >::Type ValueT
Definition: TypedWidget.h:135
armarx::RemoteGui::TypedWidget< RemoteWidgetT_, QWidgetT_, RemoteGui::VALUE_VARIANT_EMPTY >::RemoteWidgetT
RemoteWidgetT_ RemoteWidgetT
Definition: TypedWidget.h:132
ARMARX_STREAM_PRINTER
#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:304