HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UI_Value.h
Go to the documentation of this file.
1 #ifndef __UI_Value__
2 #define __UI_Value__
3 
4 #include "UI_API.h"
5 #include "UI_Object.h"
6 #include <UT/UT_PtrProxy.h>
7 #include <UT/UT_UniquePtr.h>
8 #include <SYS/SYS_Inline.h>
9 #include <SYS/SYS_Types.h>
10 #include <iosfwd>
11 #include <deque>
12 
13 /*
14  * A UI_Value is a utility class which maintains a single value of
15  * variable type and provides conversions between types.
16  */
17 
26 };
27 UI_API size_t format(char *buffer, size_t buffer_size, const UI_ValueType &v);
28 
29 enum UI_Reason {
30  UI_NO_REASON, // event was likely explicitly generated
31  UI_VALUE_CHANGED, // mouse up
32  UI_VALUE_ACTIVE, // mouse drag
33  UI_VALUE_START, // mouse down
34  UI_VALUE_NEW, // dependent added to UI_Value
35  UI_VALUE_RANGE_CHANGED, // UI_ScrollValue range changed
36  UI_VALUE_PICKED, // quick mouse down and release without drag
37  UI_VALUE_LOCATED, // mouse hover (only if wantsLocates() is true)
38  UI_VALUE_ITEMS_CHANGED // only generated by UI_StringList (and subclasses)
39 };
40 UI_API size_t format(char *buffer, size_t buffer_size, const UI_Reason &v);
41 
42 #define UI_STEP_BIG 4
43 
49 };
50 UI_API size_t format(char *buffer, size_t buffer_size, const UI_StepType &v);
51 
52 class UI_Event;
53 class UT_Color;
54 class UT_String;
55 class UT_StringArray;
56 class UT_StringHolder;
57 class UT_StringRef;
58 class UT_IStream;
59 class ui_Client;
60 
61 //====================================================================
62 
63 class UI_API UI_Value : public UI_Object
64 {
65 public:
66  UI_Value();
67  explicit UI_Value(int32 i);
68  explicit UI_Value(int64 i);
69  explicit UI_Value(fpreal32 f);
70  explicit UI_Value(fpreal64 f);
71  explicit UI_Value(fpreal32 fp[], exint n);
72  explicit UI_Value(fpreal64 dp[], exint n);
73  explicit UI_Value(int32 ip[], exint n);
74  explicit UI_Value(int64 ip[], exint n);
75  explicit UI_Value(const UT_StringArray &ssp);
76  explicit UI_Value(const char *s);
77  explicit UI_Value(const UI_Value &v);
78 
79  ~UI_Value() override;
80 
81  virtual void setValue(int32 i);
82  virtual void setValue(int64 i);
83  virtual void setValue(fpreal64 f);
84  virtual void setValue(fpreal64 f, exint n);
85  virtual void setValue(int32 i, exint n);
86  virtual void setValue(int64 i, exint n);
87  virtual void setValue(const fpreal32 fp[], exint n);
88  virtual void setValue(const fpreal64 dp[], exint n);
89  virtual void setValue(const int32 ip[], exint n);
90  virtual void setValue(const int64 ip[], exint n);
91  virtual void setValue(const UT_StringArray &ssp);
92  virtual void setValue(const char *s);
93  virtual void setValue(const UI_Value &v);
94  void setValue(const UT_Color &c);
95  void setPointerValue(void *p);
96  void stringPrintf(const char *fmt, ...);
97 
98  virtual void changed(UI_Object *by,
99  UI_Reason reason = UI_VALUE_CHANGED,
100  UI_DeviceEvent *state = 0);
101  // This method is similar to "changed" but it triggers the callback events
102  // immediately (bypassing the event queue). It also only notifies the
103  // objects that have added interests in this value. Not the immediate
104  // dependents. **This should only be used in special circumstances.**
105  void triggerImmediate(UI_Object *by,
106  UI_Reason reason = UI_VALUE_CHANGED,
107  UI_DeviceEvent *state = 0);
108  // This method is similar to "triggerImmediate" but it notifies the
109  // immediate dependents rather than the objects that have added interests
110  // in this value. **This should only be used in special circumstances.**
111  void triggerDependents(UI_Object *by,
112  UI_Reason reason = UI_VALUE_CHANGED,
113  UI_DeviceEvent *state = 0);
114 
115  virtual void enable(bool state); // Sends enable event to dependents
116  virtual void visible(bool state); // Sends visible event to dependents
117  virtual void override(bool state, int);// Sends override event to dependents
118  virtual void open(bool state); // Sends open/close event to dependents
119  virtual void refresh(); // Sends redraw in place events
120 
121  // This ugly hack attempts to determine the open state of UI_Feel clients
122  // which are open. It returns true only if *any* of them are open.
123  bool areAnyFeelClientsOpen() const;
124 
125  void handleEvent(UI_Event *event) override;
126  void deleteReferences(UI_Object *to_whom) override;
127 
128  virtual void setSteps(fpreal mysmall, fpreal large);
129  virtual void setRange(fpreal min, fpreal max);
130  virtual void getRange(fpreal *min, fpreal *max) const;
131 
132  virtual void rangeCheck();
133  virtual int step(UI_StepType how);
134 
135  int save(std::ostream &os) const;
136  bool load(UT_IStream &is);
137 
138  void setFormat(const char *fmt) { myFormat = fmt; }
139  const char *getFormat() const { return myFormat; }
140 
141  void getValue(int32 *ip) const;
142  void getValue(int64 *ip) const;
143  void getValue(fpreal32 *fp) const;
144  void getValue(fpreal64 *fp) const;
145  void getValue(fpreal32 *fp, exint n) const;
146  void getValue(fpreal64 *dp, exint n) const;
147  void getValue(int32 *ip, exint n) const;
148  void getValue(int64 *ip, exint n) const;
149  void getValue(char *sp, exint bufsize) const;
150  void getValue(char *sp, exint bufsize, const char *format) const;
151  void getValue(UT_String &s) const;
152  void getValue(UT_StringHolder &s) const;
153  void getValue(UT_StringArray &ssp, exint n) const;
154  void getValue(UI_Value &v) const;
155  void getValue(UT_Color &color) const;
156  void getValue(void **pp) const;
157 
158  void getArrayValue(fpreal *d, exint index) const;
159  void getArrayValue(int32 *d, exint index) const;
160  void getArrayValue(int64 *d, exint index) const;
161  exint getArrayLength() const;
162 
163  SYS_FORCE_INLINE UI_ValueType getType() const { return myType; }
164  virtual int isVector() const { return 0; }
165 
166  void addInterest(UI_Object *client, UI_EventMethod method,
167  bool check_dup = false);
168  void addPriorityInterest(UI_Object *client, UI_EventMethod method,
169  bool check_dup = false);
170  bool hasInterest(UI_Object *in, UI_EventMethod method);
171  bool hasInterest(UI_Object *obj);
172  void removeInterest(UI_Object *in, UI_EventMethod method);
173  void removeInterest(UI_Object *client);
174  void removeAllInterests();
175 
176  // Note: setString sets the char * value whereas setValue(char *) does
177  // a strdup. (ie UI_Value will own the string you set with setString)
178  const char *getString() const; // Short cuts for string manipulation.
179  void setString(char *sp);
180  char *stealString(); // Take the string away from the value.
181 
182  // Expand environment variables and, on OSX and Linux, the ~ character.
183  void expandEnvironment();
184 
185  operator const char *() const;
186  operator bool() const;
187  operator int32() const;
188  operator int64() const;
189  operator fpreal32() const;
190  operator fpreal64() const;
191  operator void *() const;
192 
193  void operator=(int32 i) { setValue(i); }
194  void operator=(int64 i) { setValue(i); }
195  void operator=(fpreal32 f) { setValue(f); }
196  void operator=(fpreal64 f) { setValue(f); }
197  void operator=(const char *s) { setValue(s); }
198  void operator=(const UI_Value &v) { setValue(v); }
199  void operator=(const UT_Color &c) { setValue(c); }
200 
201  bool operator==(const UI_Value &v) const;
202  bool operator==(const fpreal32 fp[]) const;
203  bool operator==(const fpreal64 fp[]) const;
204  bool operator==(const int32 ip[]) const;
205  bool operator==(const int64 ip[]) const;
206  bool operator==(const UT_StringArray &ssp) const;
207  bool operator==(fpreal64 f) const;
208  bool operator==(fpreal32 f) const;
209  bool operator==(int32 i) const;
210  bool operator==(int64 i) const;
211  bool operator==(const char *s) const;
212 
213  const char *className() const override { return "UI_Value"; }
214 
215  // Return the number of interests expressed in this value
216  int getNumInterested() const;
217  UI_Object *getInterest(int index) const;
218 
219  // Utility method for debugging
220  void dumpInterests(std::ostream &os,
221  const char *prefix = 0) const;
222 
223  // Set to true to track
225  {
228  TL_VALUE
229  };
230 
231 
232  bool isTraced() const { return myTraceLevel != TL_NONE; }
233  virtual void setTraceLevel(TraceLevel tl) { myTraceLevel = tl; }
234  TraceLevel getTraceLevel() const { return myTraceLevel; }
235 
236 protected:
237 
238  // Clamps value to given range.
239  // NB: Only valid for float, int, {int,float} arrays
240  void clampValue(fpreal min_value, fpreal max_value);
241 
242  void showTrace();
243 
244 private:
245  void zap();
246 
247  void addMyInterest(UI_Object *client,
248  UI_EventMethod method,
249  bool check_dup,
250  bool priority);
251  void sendEventToInterests(UI_Object *by, UI_Event &event,
252  bool immediate);
253 
254  friend std::ostream &operator<<(std::ostream &os, const UI_Value &v)
255  {
256  v.save(os);
257  return os;
258  }
259 private:
260  union ValueType
261  {
262  exint i;
263  fpreal d;
264  fpreal *a;
265  exint *j;
266  char *s;
267  char **ss;
268  void *p;
269  };
270 
271  class ui_Client
272  {
273  public:
274  ui_Client(UI_Object *who, UI_EventMethod func)
275  : myClientId(who->getProxyId())
276  , myMethod(func)
277  {
278  }
279 
280  // Not copyable
281  ui_Client(const ui_Client& copy) = delete;
282  ui_Client& operator=(const ui_Client& copy) = delete;
283  // But movable
284  ui_Client(ui_Client&& src) = default;
285  ui_Client& operator=(ui_Client&& src) = default;
286 
287  bool isEqual(int id) { return myClientId == id; }
288  int getClientId() { return myClientId; }
289  UI_Object * getClient() { return myClientId; }
290  UI_EventMethod getMethod() { return myMethod; }
291 
292  private:
293  UT_RefProxy<UI_Object> myClientId;
294  UI_EventMethod myMethod;
295  };
296 
297  using ui_ClientList = std::deque<ui_Client>;
298  using ui_ClientListPtr = UT_UniquePtr<ui_ClientList>;
299  using ui_ClientListIter = ui_ClientList::iterator;
300 
302  ui_ClientListIter ibegin() { return myInterests->begin(); }
304  ui_ClientListIter iend() { return myInterests->end(); }
305 
306  ValueType myValue;
307  UI_ValueType myType:4;
308  TraceLevel myTraceLevel:2;
309  ui_ClientListPtr myInterests;
310  const char* myFormat;
311 };
312 
313 UI_API size_t format(char *buffer, size_t buffer_size, const UI_Value &v);
314 
315 inline int
317 {
318  return myInterests ? int(myInterests->size()) : 0;
319 }
320 
321 inline UI_Object *
323 {
324  if (!myInterests || i < 0 || i >= myInterests->size())
325  return nullptr;
326 
327  return (*myInterests)[i].getClient();
328 }
329 
330 inline void
332 {
333  if (min_value > max_value)
334  return;
335 
336  switch (myType)
337  {
338  case UI_VALUE_FLOAT:
339  if (myValue.d < min_value)
340  myValue.d = min_value;
341  else if (myValue.d > max_value)
342  myValue.d = max_value;
343  break;
344 
345  case UI_VALUE_INTEGER:
346  if (myValue.i < min_value)
347  myValue.i = (int)min_value;
348  else if (myValue.i > max_value)
349  myValue.i = (int)max_value;
350  break;
351 
353  for (exint i = 1, n = myValue.a[0]; i < n; ++i)
354  {
355  if (myValue.a[n] < min_value)
356  myValue.a[n] = min_value;
357  else if (myValue.a[n] > max_value)
358  myValue.a[n] = max_value;
359  }
360  break;
361 
362  case UI_VALUE_INT_ARRAY:
363  for (exint i = 1, n = myValue.a[0]; i < n; ++i)
364  {
365  if (myValue.j[n] < (int)min_value)
366  myValue.j[n] = (int)min_value;
367  else if (myValue.j[n] > (int)max_value)
368  myValue.j[n] = (int)max_value;
369  }
370  break;
371 
372  default: break;
373  }
374 }
375 
376 #endif
friend std::ostream & operator<<(std::ostream &os, const UI_Value &v)
Definition: UI_Value.h:254
GLboolean enable
Definition: glew.h:2750
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1817
int int32
Definition: SYS_Types.h:39
UI_API size_t format(char *buffer, size_t buffer_size, const UI_ValueType &v)
bool isTraced() const
Definition: UI_Value.h:232
void operator=(int32 i)
Definition: UI_Value.h:193
void operator=(const UI_Value &v)
Definition: UI_Value.h:198
UI_Object * getInterest(int index) const
Definition: UI_Value.h:322
virtual void setTraceLevel(TraceLevel tl)
Definition: UI_Value.h:233
const char * getFormat() const
Definition: UI_Value.h:139
void setFormat(const char *fmt)
Definition: UI_Value.h:138
GLuint color
Definition: glcorearb.h:1260
const GLfloat * c
Definition: glew.h:16631
int64 exint
Definition: SYS_Types.h:125
void operator=(fpreal64 f)
Definition: UI_Value.h:196
TraceLevel getTraceLevel() const
Definition: UI_Value.h:234
GLenum src
Definition: glcorearb.h:1792
float fpreal32
Definition: SYS_Types.h:200
GLuint buffer
Definition: glcorearb.h:659
void operator=(fpreal32 f)
Definition: UI_Value.h:195
GLuint in
Definition: glew.h:11552
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
struct _cl_event * event
Definition: glcorearb.h:2960
double fpreal64
Definition: SYS_Types.h:201
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
virtual void handleEvent(UI_Event *event)
void operator=(const char *s)
Definition: UI_Value.h:197
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
int open(float queuesize) override
virtual int isVector() const
Definition: UI_Value.h:164
const GLdouble * v
Definition: glcorearb.h:836
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLhandleARB obj
Definition: glew.h:6266
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
long long int64
Definition: SYS_Types.h:116
GLfloat GLfloat p
Definition: glew.h:16656
GLuint id
Definition: glcorearb.h:654
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
int save(std::ostream &os) const
GLdouble n
Definition: glcorearb.h:2007
UI_Reason
Definition: UI_Value.h:29
SYS_FORCE_INLINE UI_ValueType getType() const
Definition: UI_Value.h:163
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107
void clampValue(fpreal min_value, fpreal max_value)
Definition: UI_Value.h:331
GLenum func
Definition: glcorearb.h:782
const char * className() const override
Definition: UI_Value.h:213
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:785
int getNumInterested() const
Definition: UI_Value.h:316
OIIO_API bool copy(string_view from, string_view to, std::string &err)
virtual void deleteReferences(UI_Object *to_whom)
GLfloat f
Definition: glcorearb.h:1925
void(UI_Object::* UI_EventMethod)(UI_Event *)
Definition: UI_Object.h:36
void operator=(const UT_Color &c)
Definition: UI_Value.h:199
#define UI_API
Definition: UI_API.h:10
UI_StepType
Definition: UI_Value.h:44
#define UI_STEP_BIG
Definition: UI_Value.h:42
void operator=(int64 i)
Definition: UI_Value.h:194
GLdouble s
Definition: glew.h:1395
constexpr T max_value()
Definition: format.h:322
UI_ValueType
Definition: UI_Value.h:18