cgv
control.h
1 #pragma once
2 
3 #include "view.h"
4 
5 #include <cgv/signal/signal.h>
6 
7 #include "lib_begin.h"
8 
9 namespace cgv {
10  namespace gui {
11 
13 class CGV_API abst_control : public abst_view
14 {
15 public:
17  abst_control(const std::string& name);
19  bool shows(const void* ptr) const;
21  virtual bool controls(const void* ptr) const = 0;
25  virtual void attach_to_check_value(cgv::signal::functor_base* bool_func) = 0;
26 };
27 
30 
31 #if _MSC_VER >= 1400
32 CGV_TEMPLATE template class CGV_API data::ref_ptr<abst_control>;
33 #endif
34 
37 {
39 
41  virtual bool controls(const void* ptr, void* user_data) const = 0;
42 };
43 
50 template <typename T>
52 {
54  virtual void set_value(const T& value, void* user_data) = 0;
56  virtual const T get_value(void* user_data) const = 0;
58  virtual bool controls(const void* ptr, void* user_data) const { return false; }
59 };
60 
80 template <typename T>
81 class control : public abst_control
82 {
83 private:
84  T* value_ptr;
86  T new_value;
87 protected:
88  // protected function for the setter
89  void set_value(const T& v) {
90  if (cp)
91  cp->set_value(v, value_ptr);
92  else {
93  *value_ptr = v;
94  update_views();
95  }
96  }
97  // return pointer to value or 0 if not available
98  const T* get_value_ptr() const { return cp ? 0 : value_ptr; }
99 public:
101  typedef cgv::signal::bool_signal<control<T>&> value_check_signal_type;
103  typedef cgv::signal::signal<control<T>&> value_change_signal_type;
105  control(const std::string& _name, T& _value) : abst_control(_name), value_ptr(&_value), new_value(_value), cp(0) {
106  attach_to_reference(value_ptr);
107  }
109  control(const std::string& _name, T* _value) : abst_control(_name) {
110  value_ptr = _value;
111  new_value = *_value;
112  cp = 0;
113  attach_to_reference(value_ptr);
114  }
118  control(const std::string& _name, abst_control_provider* _cp, void* user_data) : abst_control(_name), cp(0) {
119  if (_cp) {
120  cp = static_cast<control_provider<T>*>(_cp);
121  (void*&)value_ptr = user_data;
122  new_value = cp->get_value(user_data);
123  }
124  else {
125  value_ptr = (T*)user_data;
126  new_value = *value_ptr;
127  attach_to_reference(value_ptr);
128  }
129  }
131 
133  cgv::signal::bool_signal<control<T>&> check_value;
135 
136  cgv::signal::signal<control<T>&> value_change;
138  const T get_value() const { return cp ? cp->get_value(value_ptr) : *value_ptr; }
140  const T& get_new_value() const { return new_value; }
142  void set_new_value(const T& nv) { new_value = nv; }
144  const T& get_old_value() const { return new_value; }
146  bool check_and_set_value(const T& nv) {
147  set_new_value(nv);
148  if (check_value(*this)) {
149  T tmp_value = get_value();
150  set_value(this->get_new_value());
151  set_new_value(tmp_value);
152  value_change(*this);
153  return true;
154  }
155  return false;
156  }
158  bool controls(const void* ptr) const { return cp ? cp->controls(ptr,value_ptr) : (value_ptr == ptr); }
161  value_change.connect_abst(func);
162  }
165  check_value.connect_abst(bool_func);
166  }
167 };
168 
169  }
170 }
171 
172 #include <cgv/config/lib_end.h>
cgv::gui::control::get_old_value
const T & get_old_value() const
return the old value to the callbacks attached to the change_value signal
Definition: control.h:144
cgv::gui::control::value_change
cgv::signal::signal< control< T > & > value_change
this signal is sent after the user triggered a change of value and the check_value succeeded.
Definition: control.h:136
cgv::gui::control_provider::get_value
virtual const T get_value(void *user_data) const =0
overload to get the value
cgv::gui::abst_control::attach_to_value_change
virtual void attach_to_value_change(cgv::signal::functor_base *func)=0
attach a functor to the value change signal
cgv::gui::abst_control_provider
type independent base class of control provider interface
Definition: control.h:37
cgv::gui::control::check_value
cgv::signal::bool_signal< control< T > & > check_value
this signal is sent when the user triggered a change of value in order to check whether the new value...
Definition: control.h:133
cgv::gui::abst_control::controls
virtual bool controls(const void *ptr) const =0
return whether the control controls the value pointed to by ptr
cgv::gui::abst_control
gui and type independent base class of all controls
Definition: control.h:14
cgv::gui::control::controls
bool controls(const void *ptr) const
check whether the value represented by this element is pointing to the passed pointer
Definition: control.h:158
cgv::data::ref_ptr
Definition: ref_ptr.h:19
cgv::gui::control::check_and_set_value
bool check_and_set_value(const T &nv)
set new value only if check_value signal succeeds and send value_change signal. Return true if value ...
Definition: control.h:146
cgv::gui::control::get_new_value
const T & get_new_value() const
return the new value to the callbacks attached to the check_value signal
Definition: control.h:140
cgv::gui::control::value_change_signal_type
cgv::signal::signal< control< T > & > value_change_signal_type
type of the value change signal
Definition: control.h:103
cgv::gui::control::value_check_signal_type
cgv::signal::bool_signal< control< T > & > value_check_signal_type
type of the value check signal
Definition: control.h:101
cgv::gui::control::get_value
const T get_value() const
return a reference to the current value
Definition: control.h:138
cgv::gui::control_provider::set_value
virtual void set_value(const T &value, void *user_data)=0
overload to set the value
cgv::gui::control_ptr
data::ref_ptr< abst_control > control_ptr
ref counted pointer to abst control
Definition: control.h:29
cgv::signal::functor_base
base class for all functor classes which provides access to the tacker interface of an involved insta...
Definition: abst_signal.h:40
cgv::gui::control::control
control(const std::string &_name, T &_value)
construct abstract element from reference to value
Definition: control.h:105
cgv::gui::control_provider
Definition: control.h:52
cgv::gui::abst_control::attach_to_check_value
virtual void attach_to_check_value(cgv::signal::functor_base *bool_func)=0
attach a functor to the value change signal
cgv::gui::control
Definition: control.h:82
cgv::gui::abst_control_provider::controls
virtual bool controls(const void *ptr, void *user_data) const =0
overload to check if ptr points to the controlled value
cgv::gui::abst_view
type independent &base class of all views
Definition: view.h:13
cgv::gui::control::control
control(const std::string &_name, T *_value)
construct abstract element from control_provider
Definition: control.h:109
cgv::gui::control_provider::controls
virtual bool controls(const void *ptr, void *user_data) const
the default implementation compares ptr to &get_value().
Definition: control.h:58
cgv::gui::control::attach_to_value_change
void attach_to_value_change(cgv::signal::functor_base *func)
attach a functor to the value change signal
Definition: control.h:160
cgv::gui::control::control
control(const std::string &_name, abst_control_provider *_cp, void *user_data)
Definition: control.h:118
cgv::gui::control::set_new_value
void set_new_value(const T &nv)
set a different new value from the callbacks attached to the check_value signal
Definition: control.h:142
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::gui::control::attach_to_check_value
void attach_to_check_value(cgv::signal::functor_base *bool_func)
attach a functor to the value change signal
Definition: control.h:164