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
15namespace 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;
25
26 std::string
27 getHandlerT() const override
28 {
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>
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
149 {
150 // Do nothing
151 return ValueT{};
152 }
153 };
154
155} // namespace armarx::RemoteGui
#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
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
T getSingleValue(ValueVariant const &value, const std::string &name="")
std::function< QWidgetPtr(WidgetPtr const &)> CreateWidgetCallback
ValueVariant makeValue(bool value)
Definition Storage.cpp:144
const char * getVariantTypeName(ValueVariantType type)
Definition Storage.cpp:120
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
std::ostream cnull
std::nullptr_t Type
Definition Storage.h:37
QWidget * createWidget(Widget const &desc, ValueVariant const &initialValue, CreateWidgetCallback const &createChild, const QObject *stateChangeReceiver, const char *stateChangeSlot) const override
Definition TypedWidget.h:47
typename HandlerT::RemoteWidgetT RemoteWidgetT
Definition TypedWidget.h:21
virtual ValueVariant handleGuiChange(Widget const &desc, QWidget *widget) const override
Definition TypedWidget.h:87
static const RemoteGui::ValueVariantType ValueType
Definition TypedWidget.h:23
typename HandlerT::QWidgetT QWidgetT
Definition TypedWidget.h:22
virtual void updateGui(Widget const &desc, QWidget *widget, ValueVariant const &value) const override
Definition TypedWidget.h:70
std::string getHandlerT() const override
Definition TypedWidget.h:27
bool isValid(Widget const &desc, std::ostream &out=cnull) const override
Definition TypedWidget.h:33
typename Storage< ValueType >::Type ValueT
Definition TypedWidget.h:24
static void updateGui(RemoteWidgetT const &desc, QWidgetT *widget, ValueT const &value)
static ValueT handleGuiChange(RemoteWidgetT const &desc, QWidgetT *widget)
static const ValueVariantType ValueType
static bool isValid(RemoteWidgetT const &, std::ostream &out)
typename Storage< ValueType >::Type ValueT
#define ARMARX_TRACE
Definition trace.h:77