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