cgv
provider.h
1 #pragma once
2 
3 #include <cgv/base/base.h>
4 #include <cgv/type/info/type_name.h>
5 #include <cgv/signal/rebind.h>
6 #include <cgv/utils/convert.h>
7 #include "gui_group.h"
8 #include "shortcut.h"
9 #include "gui_creator.h"
10 
11 #include "lib_begin.h"
12 
13 namespace cgv {
14  namespace gui {
15 
16 
18 template <typename T>
20 {
21  const T& value;
22  int index;
23  with_index_struct(const T& _value, int _index) : value(_value), index(_index) {}
24 };
25 
27 template <typename T>
28 with_index_struct<T> with_index(const T& _value, int _index)
29 {
30  return with_index_struct<T>(_value, _index);
31 }
32 
33 
34 template <typename T>
35 struct with_index_traits
36 {
37  typedef const T* pointer_type;
38  static pointer_type get_value_ptr(const T& value) { return &value; }
39  static int get_index(const T& value) { return -1; }
40 };
41 
42 template <typename T>
43 struct with_index_traits<with_index_struct<T> >
44 {
45  typedef const T* pointer_type;
46  static pointer_type get_value_ptr(const with_index_struct<T>& value) { return &value.value; }
47  static int get_index(const with_index_struct<T>& value) { return value.index; }
48 };
49 
50 template <typename T>
51 typename with_index_traits<T>::pointer_type wi_get_value_ptr(const T& value)
52 {
53  return with_index_traits<T>::get_value_ptr(value);
54 }
55 
56 template <typename T>
57 int wi_get_index(const T& value)
58 {
59  return with_index_traits<T>::get_index(value);
60 }
61 
63 class CGV_API provider : virtual public cgv::signal::tacker
64 {
65 protected:
68  gui_group_ptr parent_group;
71  provider* parent_provider;
73  void set_parent(gui_group_ptr);
75  void update_parent();
77  friend class gui_group;
79 public:
81  gui_group_ptr get_parent_group() const { return parent_group; }
83  view_ptr add_view_void(const std::string& label, const void* value_ptr, const std::string& value_type, const std::string& gui_type, const std::string& options, const std::string& align);
85  control_ptr add_control_void(const std::string& label, void* value_ptr, abst_control_provider* acp, const std::string& value_type, const std::string& gui_type, const std::string& options, const std::string& align, void* user_data);
88  void align(const std::string& _align);
91  // gui_group_ptr add_group(const std::string& label, const std::string& group_type, const std::string& options, const std::string& align);
93 
96  std::string concat_enum_def(const std::vector<std::string>& names, const std::string& additional_first_name = "", const std::string& additional_last_name = "");
98 
99  gui_group_ptr add_object_gui(base_ptr object, const std::string& label, const std::string& group_type, const std::string& options, const std::string& align);
101  void inline_object_gui(base_ptr object);
103  gui_group_ptr add_group(const std::string& label, const std::string& group_type, const std::string& options = "", const std::string& align = "\n");
105  base_ptr add_decorator(const std::string& label, const std::string& decorator_type, const std::string& options = "", const std::string& align = "\n");
107  button_ptr add_button(const std::string& label, const std::string& options = "", const std::string& align = "\n");
109  template <typename T>
110  data::ref_ptr<view<T> > add_view(const std::string& label, const T& value, const std::string& gui_type = "", const std::string& options = "", const std::string& align = "\n") {
111  if (parent_group.empty())
112  return data::ref_ptr<view<T> >();
113  return parent_group->add_view(label, value, gui_type, options, align);
114  }
116  template <typename T>
117  data::ref_ptr<control<T> > add_control(const std::string& label, T& value, const std::string& gui_type = "", const std::string& options = "", const std::string& align = "\n") {
118  if (parent_group.empty())
119  return data::ref_ptr<control<T> >();
120  return parent_group->add_control(label, value, gui_type, options,align);
121  }
123  template <typename T>
124  data::ref_ptr<control<T> > add_control(const std::string& label,
125  control_provider<T>* provider, const std::string& gui_type = "",
126  const std::string& options = "", const std::string& align = "\n", void* user_data = 0) {
127  if (parent_group.empty())
128  return data::ref_ptr<control<T> >();
129  return parent_group->add_control(label, provider, gui_type, options,align,user_data);
130  }
132 
134  template <typename T>
135  data::ref_ptr<control<T> > add_member_control(cgv::base::base* base_ptr, const std::string& label, T& value, const std::string& gui_type = "", const std::string& options = "", const std::string& align = "\n") {
136  data::ref_ptr<control<T> > cp = add_control(label, value, gui_type, options, align);
137  if (cp)
138  connect_copy(cp->value_change,
139  cgv::signal::rebind(base_ptr, &cgv::base::base::on_set, &value));
140  return cp;
141  }
143 
163  bool add_tree_node(const std::string& label, bool& toggle, int level, const std::string& a= "\n", gui_group_ptr ggp = gui_group_ptr());
165 
209  template <typename T>
210  bool begin_tree_node(const std::string& label, const T& value, bool initial_visibility = false, const std::string& options = "", gui_group_ptr ggp = gui_group_ptr()) { return begin_tree_node_void(label, wi_get_value_ptr(value), wi_get_index(value), initial_visibility, options, ggp); }
212 
214  template <typename T>
215  static bool& ref_tree_node_visible_flag(const T& value) { return ref_tree_node_visible_flag_void(wi_get_value_ptr(value), wi_get_index(value)); }
218 
219  template <typename T>
220  void end_tree_node(const T& value) { end_tree_node_void(wi_get_value_ptr(value), wi_get_index(value)); }
222  template <typename T>
223  bool is_tree_node_visible(const T& value) const { return is_tree_node_visible_void(wi_get_value_ptr(value), wi_get_index(value)); }
225  template <typename T>
226  void set_tree_node_visibility(const T& value, bool is_visible) { set_tree_node_visibility_void(wi_get_value_ptr(value), wi_get_index(value), is_visible); }
228  bool begin_tree_node_void(const std::string& label, const void* value_ptr, int index = -1, bool initial_visibility = false, const std::string& options = "", gui_group_ptr ggp = gui_group_ptr());
230  static bool& ref_tree_node_visible_flag_void(const void* value_ptr, int index = -1);
232  void end_tree_node_void(const void* value_ptr, int index = -1);
234  bool is_tree_node_visible_void(const void* value_ptr, int index) const;
236  void set_tree_node_visibility_void(const void* value_ptr, int index, bool is_visible);
237 
239 
244  template <typename T>
245  bool add_gui(const std::string& label, T& value, const std::string& gui_type = "", const std::string& options = "")
246  {
247  return cgv::gui::create_gui(this, label, &value, cgv::type::info::type_name<T>::get_name(), gui_type, options, 0);
248  }
250 protected:
253  virtual void on_select();
256  virtual void on_deselect();
258  virtual void selection_change_cb(cgv::base::base_ptr new_child, bool selected);
260 public:
262  provider();
264  ~provider();
266 
275  virtual std::string get_gui_name() const;
277 
279  virtual std::string get_parent_type() const;
281  virtual bool ensure_selected_in_tab_group_parent();
283  virtual void update_member(void* member_ptr);
285  virtual void update_all_members();
287  virtual std::string get_menu_path() const;
289  virtual shortcut get_shortcut() const;
291  virtual void create_gui() = 0;
293 
299  virtual void recreate_gui();
301 
303  virtual void post_recreate_gui();
304 
307  void remove_element(base_ptr);
310  void remove_all_elements();
312  base_ptr find_element(const std::string& name);
314 
317  template <typename T>
318  data::ref_ptr<view<T> > find_view(const T& value, int* idx_ptr=0) {
319  if (parent_group.empty())
320  return data::ref_ptr<view<T> >();
321  return parent_group->find_view(value,idx_ptr);
322  }
324 
327  template <typename T>
328  data::ref_ptr<control<T> > find_control(T& value, int* idx_ptr=0) {
329  if (parent_group.empty())
330  return data::ref_ptr<control<T> >();
331  return parent_group->find_control(value,idx_ptr);
332  }
334  control_ptr find_control_void(void* value_ptr, int* idx_ptr);
336  view_ptr find_view_void(void* value_ptr, int* idx_ptr);
338 };
339 
340 
341 
342  }
343 }
344 
345 #include <cgv/config/lib_end.h>
cgv::gui::provider::add_control
data::ref_ptr< control< T > > add_control(const std::string &label, control_provider< T > *provider, const std::string &gui_type="", const std::string &options="", const std::string &align="\n", void *user_data=0)
use this to add a new control to the gui, where the control is implemented with a control provider cl...
Definition: provider.h:124
cgv::gui::provider::find_control
data::ref_ptr< control< T > > find_control(T &value, int *idx_ptr=0)
find a control of a given class member
Definition: provider.h:328
cgv::gui::abst_control_provider
type independent base class of control provider interface
Definition: control.h:37
cgv::gui::provider::end_tree_node
void end_tree_node(const T &value)
Definition: provider.h:220
cgv::gui::provider::add_gui
bool add_gui(const std::string &label, T &value, const std::string &gui_type="", const std::string &options="")
Add a composed gui of the given gui_type for the given value.
Definition: provider.h:245
cgv::gui::with_index
with_index_struct< T > with_index(const T &_value, int _index)
helper function to support value references as well as value references with index for the tree_node ...
Definition: provider.h:28
cgv::gui::provider::add_control
data::ref_ptr< control< T > > add_control(const std::string &label, T &value, const std::string &gui_type="", const std::string &options="", const std::string &align="\n")
use this to add a new control to the gui with a given value type, gui type and init options
Definition: provider.h:117
cgv::data::ref_ptr< gui_group, true >
cgv::signal::tacker
Definition: abst_signal.h:63
cgv::gui::provider::is_tree_node_visible
bool is_tree_node_visible(const T &value) const
return whether the sub tree attached to a value is visible
Definition: provider.h:223
cgv::gui::create_gui
bool create_gui(provider *p, const std::string &label, void *value_ptr, const std::string &value_type, const std::string &gui_type, const std::string &options, bool *toggles)
create the gui for a composed structure
Definition: gui_creator.cxx:21
cgv::gui::provider
derive from this class to provide a gui to the current viewer
Definition: provider.h:64
cgv::data::ref_ptr::empty
bool empty() const
check if pointer is not yet set
Definition: ref_ptr.h:230
cgv::gui::provider::create_gui
virtual void create_gui()=0
you must overload this for gui creation
cgv::gui::control_provider
Definition: control.h:52
cgv::gui::provider::add_view
data::ref_ptr< view< T > > add_view(const std::string &label, const T &value, const std::string &gui_type="", const std::string &options="", const std::string &align="\n")
use this to add a new view to the gui with a given value type, gui type and init options
Definition: provider.h:110
cgv::base::base::on_set
virtual void on_set(void *member_ptr)
this callback is called when the set_void method has changed a member and can be overloaded in derive...
Definition: base.cxx:210
cgv::gui::provider::ref_tree_node_visible_flag
static bool & ref_tree_node_visible_flag(const T &value)
return a reference to the boolean flag, that tells whether the tree node for the passed value is visi...
Definition: provider.h:215
cgv::gui::with_index_struct
helper struct to support value references as well as value references with index
Definition: provider.h:20
cgv::base::base
Definition: base.h:57
cgv::gui::gui_group_ptr
data::ref_ptr< gui_group, true > gui_group_ptr
ref counted pointer to a gui group
Definition: gui_group.h:33
cgv::gui::provider::get_parent_group
gui_group_ptr get_parent_group() const
use the parent group to append to be managed elements that should be destroyed in a post_recreate_gui...
Definition: provider.h:81
cgv::gui::provider::add_member_control
data::ref_ptr< control< T > > add_member_control(cgv::base::base *base_ptr, const std::string &label, T &value, const std::string &gui_type="", const std::string &options="", const std::string &align="\n")
add control with callback to cgv::base::on_set method on cgv::gui::control::value_change
Definition: provider.h:135
cgv::gui::provider::set_tree_node_visibility
void set_tree_node_visibility(const T &value, bool is_visible)
set the visibility status of sub tree attached to a value. This calls the post_recreate method if nee...
Definition: provider.h:226
cgv::gui::provider::find_view
data::ref_ptr< view< T > > find_view(const T &value, int *idx_ptr=0)
find a view of a given class member
Definition: provider.h:318
cgv::gui::provider::begin_tree_node
bool begin_tree_node(const std::string &label, const T &value, bool initial_visibility=false, const std::string &options="", gui_group_ptr ggp=gui_group_ptr())
Begin a sub tree of a tree structured gui.
Definition: provider.h:210
cgv::gui::shortcut
the shortcut class encapsulates a key with modifiers
Definition: shortcut.h:84
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::gui::gui_group
gui independent group class which is a container for gui elements
Definition: gui_group.h:40
cgv::type::info::type_name
Definition: type_name.h:54