LayoutWidgets.h
Go to the documentation of this file.
1#pragma once
2
3#include <QGridLayout>
4#include <QGroupBox>
5#include <QHBoxLayout>
6#include <QVBoxLayout>
7
8#include "Basic.h"
9
10namespace armarx::RemoteGui
11{
12 struct GroupBoxHandler : TypedWidget<GroupBox, QGroupBox>
13 {
14 static QWidgetT*
16 CreateWidgetCallback const& createChild,
17 const QObject* stateChangeReceiver,
18 const char* stateChangeSlot)
19 {
21 QGroupBox* widget = new QGroupBox;
22 widget->setTitle(toQString(desc.label));
23 widget->setCheckable(true);
24 widget->setChecked(true);
25
26 //use an arrow as indicator for the checkbox
27 {
28 widget->setStyleSheet(R"(
29 QGroupBox::indicator:checked {
30 image: url(:/icons/TriangleBlackDown.svg);
31 }
32 QGroupBox::indicator:unchecked {
33 image: url(:/icons/TriangleBlackRight.svg);
34 }
35 )");
36 }
37
38 QHBoxLayout* layout = new QHBoxLayout;
39 widget->setLayout(layout);
40 layout->setContentsMargins(0, 0, 0, 0);
41
42 QWidget* holder = new QWidget;
43 layout->addWidget(holder);
44
45 QObject::connect(widget, SIGNAL(clicked(bool)), holder, SLOT(setVisible(bool)));
46
47 if (desc.children.size() > 0)
48 {
49
50 QVBoxLayout* layout = new QVBoxLayout;
51 holder->setLayout(layout);
52 layout->setContentsMargins(9, 0, 0, 0);
53 for (const auto& child : desc.children)
54 {
55 layout->addWidget(createChild(child));
56 }
57 }
58
59 widget->setChecked(!desc.collapsed);
60 holder->setVisible(!desc.collapsed);
61
62 return widget;
63 }
64 };
65
66 struct VBoxLayoutHandler : TypedWidget<VBoxLayout, QWidget>
67 {
68 static QWidgetT*
70 CreateWidgetCallback const& createChild,
71 const QObject* stateChangeReceiver,
72 const char* stateChangeSlot)
73 {
75 QWidgetT* widget = new QWidget();
76
77 QVBoxLayout* layout = new QVBoxLayout(widget);
78 widget->setLayout(layout);
79 layout->setContentsMargins(0, 0, 0, 0);
80 for (const RemoteGui::WidgetPtr& child : desc.children)
81 {
82 QWidget* childWidget = createChild(child);
83 layout->addWidget(childWidget);
84 }
85
86 return widget;
87 }
88 };
89
90 struct HBoxLayoutHandler : TypedWidget<HBoxLayout, QWidget>
91 {
92 static QWidgetT*
94 CreateWidgetCallback const& createChild,
95 const QObject* stateChangeReceiver,
96 const char* stateChangeSlot)
97 {
99 QWidgetT* widget = new QWidget();
100
101 QHBoxLayout* layout = new QHBoxLayout(widget);
102 widget->setLayout(layout);
103 layout->setContentsMargins(0, 0, 0, 0);
104 for (const RemoteGui::WidgetPtr& child : desc.children)
105 {
106 QWidget* childWidget = createChild(child);
107 layout->addWidget(childWidget);
108 }
109
110 return widget;
111 }
112 };
113
115 TypedWidget<SimpleGridLayoutSpanningChild, QWidget>
116 {
117 static QWidgetT*
119 CreateWidgetCallback const& createChild,
120 const QObject* stateChangeReceiver,
121 const char* stateChangeSlot)
122 {
124 ARMARX_CHECK_GREATER(desc.columns, 0);
125 switch (desc.children.size())
126 {
127 case 0:
128 return new QWidget;
129 case 1:
130 return createChild(desc.children.front());
131 default:; //below
132 }
133
134 QWidgetT* widget = new QWidget();
135
136 QHBoxLayout* layout = new QHBoxLayout(widget);
137 widget->setLayout(layout);
138 layout->setContentsMargins(0, 0, 0, 0);
139 for (const RemoteGui::WidgetPtr& child : desc.children)
140 {
141 QWidget* childWidget = createChild(child);
142 layout->addWidget(childWidget);
143 }
144
145 return widget;
146 }
147 };
148
149 struct SimpleGridLayoutHandler : TypedWidget<SimpleGridLayout, QWidget>
150 {
151 static QWidgetT*
153 CreateWidgetCallback const& createChild,
154 const QObject* stateChangeReceiver,
155 const char* stateChangeSlot)
156 {
158 QWidgetT* widget = new QWidget();
159
160 QGridLayout* layout = new QGridLayout(widget);
161 widget->setLayout(layout);
162 layout->setContentsMargins(0, 0, 0, 0);
163 ARMARX_CHECK_GREATER(desc.columns, 0);
164 int col = 0;
165 int row = 0;
166 for (const RemoteGui::WidgetPtr& child : desc.children)
167 {
168 QWidget* childWidget = createChild(child);
169 int colspan = 1;
170 if (auto spc = SimpleGridLayoutSpanningChildPtr::dynamicCast(child); spc)
171 {
172 colspan = std::max(1, spc->columns);
173 }
174 layout->addWidget(childWidget, row, col, 1, colspan);
175
176 if (col + colspan >= desc.columns)
177 {
178 //next row
179 ++row;
180 col = 0;
181 }
182 else
183 {
184 col += colspan;
185 }
186 }
187
188 return widget;
189 }
190 };
191
192 struct GridLayoutHandler : TypedWidget<GridLayout, QWidget>
193 {
194 static QWidgetT*
196 CreateWidgetCallback const& createChild,
197 const QObject* stateChangeReceiver,
198 const char* stateChangeSlot)
199 {
201 QWidgetT* widget = new QWidget();
202
203 QGridLayout* layout = new QGridLayout(widget);
204 widget->setLayout(layout);
205 layout->setContentsMargins(0, 0, 0, 0);
206 int maxrow = 0;
207 for (std::size_t i = 0; i < desc.children.size(); ++i)
208 {
209 const RemoteGui::WidgetPtr& child = desc.children.at(i);
210
211 QWidget* childWidget = createChild(child);
212
213 GridLayoutData l;
214 if (i < desc.childrenLayoutInfo.size())
215 {
216 l = desc.childrenLayoutInfo.at(i);
217 }
218 else
219 {
220 l.col = 0;
221 l.row = maxrow;
222 l.spanCol = 1;
223 l.spanRow = 1;
224 }
225 maxrow = std::max(maxrow, l.row + l.spanRow);
226 layout->addWidget(childWidget, l.row, l.col, l.spanRow, l.spanCol);
227 }
228
229 return widget;
230 }
231 };
232} // namespace armarx::RemoteGui
#define ARMARX_CHECK_GREATER(lhs, rhs)
This macro evaluates whether lhs is greater (>) than rhs and if it turns out to be false it will thro...
std::function< QWidgetPtr(WidgetPtr const &)> CreateWidgetCallback
QString toQString(std::string const &string)
static QWidgetT * createWidget(RemoteWidgetT const &desc, CreateWidgetCallback const &createChild, const QObject *stateChangeReceiver, const char *stateChangeSlot)
static QWidgetT * createWidget(RemoteWidgetT const &desc, CreateWidgetCallback const &createChild, const QObject *stateChangeReceiver, const char *stateChangeSlot)
static QWidgetT * createWidget(RemoteWidgetT const &desc, CreateWidgetCallback const &createChild, const QObject *stateChangeReceiver, const char *stateChangeSlot)
static QWidgetT * createWidget(RemoteWidgetT const &desc, CreateWidgetCallback const &createChild, const QObject *stateChangeReceiver, const char *stateChangeSlot)
static QWidgetT * createWidget(RemoteWidgetT const &desc, CreateWidgetCallback const &createChild, const QObject *stateChangeReceiver, const char *stateChangeSlot)
static QWidgetT * createWidget(RemoteWidgetT const &desc, CreateWidgetCallback const &createChild, const QObject *stateChangeReceiver, const char *stateChangeSlot)
#define ARMARX_TRACE
Definition trace.h:77