Widgets.cpp
Go to the documentation of this file.
1 #include "Widgets.h"
2 
3 #include "Impl.h"
4 
6 {
7 
8  Widget::Widget(void* iceWidget)
9  : impl(new WidgetImpl)
10  {
11  impl->desc = static_cast<RemoteGui::Widget*>(iceWidget);
12 
13  // Set a default name, so the client code does not need to bother
14  static std::atomic<std::uint64_t> counter{0};
15  std::uint64_t uniqueNumber = counter++;
16  setName(std::to_string(uniqueNumber));
17  }
18 
19  void Widget::setName(const std::string& name)
20  {
21  impl->desc->name = name;
22  }
23 
24  const std::string& Widget::getName() const
25  {
26  return impl->desc->name;
27  }
28 
30  : Widget(new RemoteGui::Label)
31  {
32  }
33 
34  Label::Label(const std::string& text)
35  : Label()
36  {
37  setText(text);
38  }
39 
40  void Label::setText(const std::string& text)
41  {
42  impl->setValue(text);
43  }
44 
46  : Widget(new RemoteGui::IntSpinBox)
47  {
48  setValue(0);
49  }
50 
51  void IntSpinBox::setRange(int min, int max)
52  {
53  auto* concrete = static_cast<RemoteGui::IntSpinBox*>(impl->desc.get());
54  concrete->min = min;
55  concrete->max = max;
56  }
57 
58  void IntSpinBox::setValue(int newValue)
59  {
60  impl->setValue(newValue);
61  }
62 
64  {
65  return impl->hasValueChanged();
66  }
67 
69  {
70  return impl->getValue<int>();
71  }
72 
74  : ContainerWidget(new RemoteGui::HBoxLayout)
75  {
76  }
77 
78  HBoxLayout::HBoxLayout(std::initializer_list<Widget> children)
79  : HBoxLayout()
80  {
81  addChildren(children);
82  }
83 
85  : ContainerWidget(new RemoteGui::VBoxLayout)
86  {
87  }
88 
89  VBoxLayout::VBoxLayout(std::initializer_list<Widget> children)
90  : VBoxLayout()
91  {
92  addChildren(children);
93  }
94 
95  void ContainerWidget::addChild(const Widget& child)
96  {
97  impl->addChild(child.impl);
98  }
99 
100  void ContainerWidget::addChildren(std::initializer_list<Widget> children)
101  {
102  for (auto& child : children)
103  {
104  addChild(child);
105  }
106  }
107 
109  : Widget(new RemoteGui::VSpacer)
110  {
111  }
112 
114  : Widget(new RemoteGui::HSpacer)
115  {
116  }
117 
119  : Widget(new RemoteGui::Button)
120  {
121  impl->desc->defaultValue = makeValue(0);
122  }
123 
124  void Button::setLabel(const std::string& label)
125  {
126  auto* concrete = static_cast<RemoteGui::Button*>(impl->desc.get());
127  concrete->label = label;
128  }
129 
130  bool Button::wasClicked() const
131  {
132  return impl->tab->getButton(getName()).clicked();
133  }
134 
136  : Widget(new RemoteGui::LineEdit)
137  {
138  setValue("");
139  }
140 
141  std::string LineEdit::getValue()
142  {
143  return impl->getValue<std::string>();
144  }
145 
146  void LineEdit::setValue(const std::string& text)
147  {
148  auto* concrete = static_cast<RemoteGui::LineEdit*>(impl->desc.get());
149  concrete->defaultValue = makeValue(text);
150  }
151 
153  {
154  return impl->hasValueChanged();
155  }
156 
158  : Widget(new RemoteGui::ComboBox)
159  {
160  }
161 
162  void ComboBox::addOption(const std::string& option)
163  {
164  auto* concrete = static_cast<RemoteGui::ComboBox*>(impl->desc.get());
165  concrete->options.push_back(option);
166 
167  // Set the default value to the first option that is set
168  if (concrete->defaultValue.type == VALUE_VARIANT_EMPTY)
169  {
170  concrete->defaultValue = makeValue(option);
171  }
172  }
173 
174  void ComboBox::addOptions(std::initializer_list<std::string> options)
175  {
176  for (auto& option : options)
177  {
178  addOption(option);
179  }
180  }
181 
182  void ComboBox::setOptions(const std::vector<std::string>& options)
183  {
184  auto* concrete = static_cast<RemoteGui::ComboBox*>(impl->desc.get());
185  concrete->options = options;
186  if (options.size() > 0)
187  {
188  concrete->defaultValue = makeValue(options.front());
189  }
190  }
191 
192  void ComboBox::setValue(const std::string& newValue)
193  {
194  impl->setValue(newValue);
195  }
196 
197  std::string ComboBox::getValue() const
198  {
199  return impl->getValue<std::string>();
200  }
201 
203  {
204  auto* concrete = static_cast<RemoteGui::ComboBox*>(impl->desc.get());
205  if (index < 0 || index >= (int)concrete->options.size())
206  {
207  throw std::runtime_error("ComboBox::setIndex(i) Index i is invalid");
208  }
209  setValue(concrete->options[index]);
210  }
211 
212  int ComboBox::getIndex() const
213  {
214  auto* concrete = static_cast<RemoteGui::ComboBox*>(impl->desc.get());
215  std::string value = getValue();
216  for (int i = 0; i < (int)concrete->options.size(); ++i)
217  {
218  if (concrete->options[i] == value)
219  {
220  return i;
221  }
222  }
223  return -1;
224  }
225 
227  {
228  return impl->hasValueChanged();
229  }
230 
232  : Widget(new RemoteGui::CheckBox)
233  {
234  setValue(false);
235  }
236 
237  bool CheckBox::getValue() const
238  {
239  return impl->getValue<bool>();
240  }
241 
242  void CheckBox::setValue(bool newValue)
243  {
244  impl->setValue(newValue);
245  }
246 
248  {
249  return impl->hasValueChanged();
250  }
251 
253  : Widget(new RemoteGui::ToggleButton)
254  {
255  setValue(false);
256  }
257 
258  void ToggleButton::setLabel(const std::string& label)
259  {
260  auto* concrete = static_cast<RemoteGui::ToggleButton*>(impl->desc.get());
261  concrete->label = label;
262  }
263 
265  {
266  return impl->getValue<bool>();
267  }
268 
269  void ToggleButton::setValue(bool newValue)
270  {
271  impl->setValue(newValue);
272  }
273 
275  {
276  return impl->hasValueChanged();
277  }
278 
280  : Widget(new RemoteGui::IntSlider)
281  {
282  setValue(0);
283  }
284 
285  void IntSlider::setRange(int min, int max)
286  {
287  auto* concrete = static_cast<RemoteGui::IntSlider*>(impl->desc.get());
288  concrete->min = min;
289  concrete->max = max;
290  }
291 
293  {
294  return impl->getValue<int>();
295  }
296 
297  void IntSlider::setValue(int newValue)
298  {
299  impl->setValue(newValue);
300  }
301 
303  {
304  return impl->hasValueChanged();
305  }
306 
308  : Widget(new RemoteGui::FloatSpinBox)
309  {
310  setValue(0.0f);
311  }
312 
313  void FloatSpinBox::setRange(float min, float max)
314  {
315  auto* concrete = static_cast<RemoteGui::FloatSpinBox*>(impl->desc.get());
316  concrete->min = min;
317  concrete->max = max;
318  }
319 
320  void FloatSpinBox::setSteps(int steps)
321  {
322  auto* concrete = static_cast<RemoteGui::FloatSpinBox*>(impl->desc.get());
323  concrete->steps = steps;
324  }
325 
326  void FloatSpinBox::setDecimals(int decimals)
327  {
328  auto* concrete = static_cast<RemoteGui::FloatSpinBox*>(impl->desc.get());
329  concrete->decimals = decimals;
330  }
331 
333  {
334  return impl->getValue<float>();
335  }
336 
337  void FloatSpinBox::setValue(float newValue)
338  {
339  impl->setValue(newValue);
340  }
341 
343  {
344  return impl->hasValueChanged();
345  }
346 
348  : Widget(new RemoteGui::FloatSlider)
349  {
350  setValue(0.0f);
351  }
352 
353  void FloatSlider::setRange(float min, float max)
354  {
355  auto* concrete = static_cast<RemoteGui::FloatSlider*>(impl->desc.get());
356  concrete->min = min;
357  concrete->max = max;
358  }
359 
360  void FloatSlider::setSteps(int steps)
361  {
362  auto* concrete = static_cast<RemoteGui::FloatSlider*>(impl->desc.get());
363  concrete->steps = steps;
364  }
365 
366  float FloatSlider::getValue() const
367  {
368  return impl->getValue<float>();
369  }
370 
371  void FloatSlider::setValue(float newValue)
372  {
373  impl->setValue(newValue);
374  }
375 
377  {
378  return impl->hasValueChanged();
379  }
380 
382  : ContainerWidget(new RemoteGui::GroupBox)
383  {
384  }
385 
386  GroupBox::GroupBox(std::initializer_list<Widget> children)
387  : GroupBox()
388  {
389  for (auto& child : children)
390  {
391  addChild(child);
392  }
393  }
394 
395  void GroupBox::setLabel(const std::string& text)
396  {
397  auto* concrete = static_cast<RemoteGui::GroupBox*>(impl->desc.get());
398  concrete->label = text;
399  }
400 
401  void GroupBox::setCollapsed(bool collapsed)
402  {
403  auto* concrete = static_cast<RemoteGui::GroupBox*>(impl->desc.get());
404  concrete->collapsed = collapsed;
405  }
406 
408  : Widget(new RemoteGui::GridLayout)
409  {
410  }
411 
412  GridLayout& GridLayout::add(const Widget& child, Pos pos, Span span)
413  {
414  impl->addChild(child.impl);
415 
416  auto* concrete = static_cast<RemoteGui::GridLayout*>(impl->desc.get());
417  concrete->childrenLayoutInfo.push_back({pos.row, pos.column, span.rows, span.columns});
418 
419  return *this;
420  }
421 
422 }
armarx::RemoteGui::Client::ComboBox::setIndex
void setIndex(int index)
Definition: Widgets.cpp:202
armarx::RemoteGui::Client::IntSpinBox::setValue
void setValue(int newValue)
Definition: Widgets.cpp:58
armarx::RemoteGui::Client::FloatSpinBox::hasValueChanged
bool hasValueChanged() const
Definition: Widgets.cpp:342
armarx::RemoteGui::Client::FloatSpinBox::getValue
float getValue() const
Definition: Widgets.cpp:332
armarx::RemoteGui::Client::ComboBox::getIndex
int getIndex() const
Definition: Widgets.cpp:212
armarx::RemoteGui::Client::ContainerWidget::addChild
void addChild(Widget const &child)
Definition: Widgets.cpp:95
armarx::RemoteGui::Client::GridLayout::GridLayout
GridLayout()
Definition: Widgets.cpp:407
armarx::RemoteGui::Client::ToggleButton
Definition: Widgets.h:139
armarx::RemoteGui::Client::LineEdit::getValue
std::string getValue()
Definition: Widgets.cpp:141
armarx::RemoteGui::Client::Span::columns
int columns
Definition: Widgets.h:183
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::RemoteGui::Client::ToggleButton::ToggleButton
ToggleButton()
Definition: Widgets.cpp:252
armarx::RemoteGui::Client::VBoxLayout
Definition: Widgets.h:167
armarx::RemoteGui::Client::CheckBox::hasValueChanged
bool hasValueChanged() const
Definition: Widgets.cpp:247
armarx::RemoteGui::Client::IntSlider::IntSlider
IntSlider()
Definition: Widgets.cpp:279
armarx::RemoteGui::Client::ToggleButton::setLabel
void setLabel(std::string const &label)
Definition: Widgets.cpp:258
armarx::RemoteGui::Client::Label
Definition: Widgets.h:31
armarx::RemoteGui::Client::HSpacer
Definition: Widgets.h:209
armarx::RemoteGui::Client::GridLayout::add
GridLayout & add(Widget const &child, Pos pos, Span span=Span{1, 1})
Definition: Widgets.cpp:412
armarx::RemoteGui::Client::FloatSlider::hasValueChanged
bool hasValueChanged() const
Definition: Widgets.cpp:376
armarx::RemoteGui::Client::FloatSpinBox::setValue
void setValue(float newValue)
Definition: Widgets.cpp:337
armarx::RemoteGui::Client::Button::wasClicked
bool wasClicked() const
Definition: Widgets.cpp:130
armarx::RemoteGui::Client::Widget::setName
void setName(std::string const &name)
Definition: Widgets.cpp:19
armarx::RemoteGui::Client::Pos::row
int row
Definition: Widgets.h:176
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
armarx::RemoteGui::Client::VBoxLayout::VBoxLayout
VBoxLayout()
Definition: Widgets.cpp:84
armarx::RemoteGui::Client::FloatSpinBox::setRange
void setRange(float min, float max)
Definition: Widgets.cpp:313
armarx::RemoteGui::Client::ContainerWidget
Definition: Widgets.h:151
armarx::RemoteGui::Client::WidgetImpl
Definition: Impl.h:17
armarx::RemoteGui::Client::GroupBox::GroupBox
GroupBox()
Definition: Widgets.cpp:381
armarx::RemoteGui::Client::FloatSlider::setValue
void setValue(float newValue)
Definition: Widgets.cpp:371
armarx::RemoteGui::Client::IntSpinBox::hasValueChanged
bool hasValueChanged() const
Definition: Widgets.cpp:63
armarx::RemoteGui::Client::VSpacer
Definition: Widgets.h:204
armarx::RemoteGui::Client::Widget::impl
std::shared_ptr< WidgetImpl > impl
Definition: Widgets.h:28
armarx::RemoteGui::Client::IntSlider::setValue
void setValue(int newValue)
Definition: Widgets.cpp:297
armarx::RemoteGui::Client::IntSlider::getValue
int getValue() const
Definition: Widgets.cpp:292
armarx::RemoteGui::Client::CheckBox::getValue
bool getValue() const
Definition: Widgets.cpp:237
armarx::RemoteGui::Client::GridLayout
Definition: Widgets.h:186
armarx::RemoteGui::Client::ContainerWidget::addChildren
void addChildren(std::initializer_list< Widget > children)
Definition: Widgets.cpp:100
armarx::RemoteGui::Client::Pos
Definition: Widgets.h:174
armarx::RemoteGui::Client::Widget::Widget
Widget(void *iceWidget)
Definition: Widgets.cpp:8
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::RemoteGui::Client::Widget
Definition: Widgets.h:21
armarx::RemoteGui::Client::ComboBox::ComboBox
ComboBox()
Definition: Widgets.cpp:157
armarx::RemoteGui::Client::FloatSlider::getValue
float getValue() const
Definition: Widgets.cpp:366
armarx::RemoteGui::Client::FloatSlider
Definition: Widgets.h:107
armarx::RemoteGui::Client::IntSlider::setRange
void setRange(int min, int max)
Definition: Widgets.cpp:285
armarx::RemoteGui::Client::HBoxLayout::HBoxLayout
HBoxLayout()
Definition: Widgets.cpp:73
armarx::RemoteGui::Client::IntSlider
Definition: Widgets.h:81
armarx::RemoteGui::Client::ComboBox::addOption
void addOption(std::string const &option)
Definition: Widgets.cpp:162
armarx::RemoteGui::Client::GroupBox
Definition: Widgets.h:193
armarx::RemoteGui::Client::LineEdit
Definition: Widgets.h:40
armarx::RemoteGui::Client::GroupBox::setCollapsed
void setCollapsed(bool collapsed)
Definition: Widgets.cpp:401
armarx::RemoteGui::Client::ToggleButton::hasValueChanged
bool hasValueChanged() const
Definition: Widgets.cpp:274
armarx::RemoteGui::Client::FloatSpinBox::FloatSpinBox
FloatSpinBox()
Definition: Widgets.cpp:307
armarx::RemoteGui::Client::FloatSpinBox::setDecimals
void setDecimals(int decimals)
Definition: Widgets.cpp:326
armarx::RemoteGui::Client::FloatSlider::FloatSlider
FloatSlider()
Definition: Widgets.cpp:347
armarx::RemoteGui::Client::ComboBox
Definition: Widgets.h:50
armarx::RemoteGui::Client::FloatSpinBox::setSteps
void setSteps(int steps)
Definition: Widgets.cpp:320
armarx::RemoteGui::Client::Span
Definition: Widgets.h:180
armarx::RemoteGui::Client::ComboBox::hasValueChanged
bool hasValueChanged() const
Definition: Widgets.cpp:226
armarx::RemoteGui::Client::Button::setLabel
void setLabel(std::string const &label)
Definition: Widgets.cpp:124
armarx::RemoteGui::Client::Pos::column
int column
Definition: Widgets.h:177
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::RemoteGui::makeValue
ValueVariant makeValue(bool value)
Definition: Storage.cpp:133
armarx::RemoteGui::Client::GroupBox::setLabel
void setLabel(std::string const &text)
Definition: Widgets.cpp:395
armarx::RemoteGui::Client::FloatSlider::setRange
void setRange(float min, float max)
Definition: Widgets.cpp:353
armarx::RemoteGui::Client::HBoxLayout
Definition: Widgets.h:160
option
#define option(type, fn)
armarx::RemoteGui::Client::Label::setText
void setText(std::string const &text)
Definition: Widgets.cpp:40
armarx::RemoteGui::Client::LineEdit::hasValueChanged
bool hasValueChanged() const
Definition: Widgets.cpp:152
armarx::RemoteGui::Client::LineEdit::setValue
void setValue(std::string const &text)
Definition: Widgets.cpp:146
armarx::RemoteGui::Client::ToggleButton::setValue
void setValue(bool newValue)
Definition: Widgets.cpp:269
armarx::RemoteGui::Client::LineEdit::LineEdit
LineEdit()
Definition: Widgets.cpp:135
armarx::RemoteGui::Client::CheckBox::CheckBox
CheckBox()
Definition: Widgets.cpp:231
armarx::RemoteGui::Client::CheckBox::setValue
void setValue(bool newValue)
Definition: Widgets.cpp:242
armarx::RemoteGui::Client::IntSpinBox::setRange
void setRange(int min, int max)
Definition: Widgets.cpp:51
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:294
armarx::RemoteGui::Client::ComboBox::addOptions
void addOptions(std::initializer_list< std::string > options)
Definition: Widgets.cpp:174
armarx::RemoteGui::Client::Span::rows
int rows
Definition: Widgets.h:182
armarx::RemoteGui::Client::IntSpinBox
Definition: Widgets.h:69
armarx::RemoteGui::Client::ComboBox::setOptions
void setOptions(std::vector< std::string > const &options)
Definition: Widgets.cpp:182
armarx::RemoteGui::Client::IntSpinBox::getValue
int getValue() const
Definition: Widgets.cpp:68
armarx::RemoteGui::Client::ComboBox::getValue
std::string getValue() const
Definition: Widgets.cpp:197
armarx::RemoteGui::Client::CheckBox
Definition: Widgets.h:129
Widgets.h
armarx::RemoteGui::Client::Label::Label
Label()
Definition: Widgets.cpp:29
armarx::RemoteGui::Client::Button
Definition: Widgets.h:120
armarx::RemoteGui::Client::IntSlider::hasValueChanged
bool hasValueChanged() const
Definition: Widgets.cpp:302
armarx::RemoteGui::Client::Button::Button
Button()
Definition: Widgets.cpp:118
armarx::RemoteGui::Client
Definition: EigenWidgets.cpp:8
armarx::RemoteGui::Client::IntSpinBox::IntSpinBox
IntSpinBox()
Definition: Widgets.cpp:45
armarx::RemoteGui::Client::FloatSpinBox
Definition: Widgets.h:93
armarx::RemoteGui::Client::HSpacer::HSpacer
HSpacer()
Definition: Widgets.cpp:113
armarx::RemoteGui::Client::ComboBox::setValue
void setValue(std::string const &newValue)
Definition: Widgets.cpp:192
Impl.h
armarx::RemoteGui::Client::VSpacer::VSpacer
VSpacer()
Definition: Widgets.cpp:108
armarx::RemoteGui::Client::Widget::getName
std::string const & getName() const
Definition: Widgets.cpp:24
armarx::RemoteGui::Client::ToggleButton::getValue
bool getValue() const
Definition: Widgets.cpp:264
armarx::RemoteGui::Client::FloatSlider::setSteps
void setSteps(int steps)
Definition: Widgets.cpp:360