HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PI_ResourceManager.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: PI_ResourceManager.h ( Direct Manipulation Library, C++)
7  *
8  * COMMENTS:
9  * This class maintains a table of all states, handles, op-handle links,
10  * and input selectors. All state, handle, and op-handle link objects
11  * are created and deleted through a PI_ResourceManager. The active one
12  * is obtained via the global function BMgetResourceManager().
13  */
14 
15 #ifndef __PI_ResourceManager_H__
16 #define __PI_ResourceManager_H__
17 
18 #include "PI_API.h"
19 #include "PI_ResourceTemplate.h"
20 #include "PI_SettingList.h"
21 #include "PI_PythonStateTypes.h"
22 
23 #include <OP/OP_Error.h>
24 #include <OP/OP_OperatorTable.h>
25 #include <UT/UT_Array.h>
26 #include <UT/UT_Pair.h>
27 #include <UT/UT_String.h>
28 #include <UT/UT_StringMap.h>
29 #include <UT/UT_SymbolTable.h>
30 #include <UT/UT_ValArray.h>
31 #include <SYS/SYS_Hash.h>
32 
33 class CPP_Manager;
34 class PRM_Template;
35 class OP_Operator;
36 class OP_OperatorTable;
37 class OP_Node;
38 class CMD_Args;
39 class PI_OpHandleLink;
40 class pi_OpHandleBinding;
41 class pi_OpSelectorBinding;
42 
43 namespace PI_PythonState
44 {
45  class Template;
46 }
47 
48 /// Error codes retured by PI_ResourceManager bind methods
49 /// @{
50 #define PI_BIND_OK 1
51 #define PI_NO_TEMPLATE_FOR_HANDLE -2
52 #define PI_INVALID_OP_PARM -3
53 #define PI_INVALID_PI_PARM -4
54 #define PI_BINDING_EXISTS -5
55 #define PI_INVALID_OP_FOR_PI -6
56 /// @}
57 
58 typedef void (*PI_SaveCallback)(void *data);
59 
61 {
62 public:
64 
65  // Note this is the channel name: the receiver should do a
66  // PRM_ParmList::getParmPtrFromChannel to get the parm.
67  const UT_String &getOpParm() const
68  { return myOpParm; }
69  const UT_String &getPIParm() const
70  { return myPIParm; }
71  int operator==(const PI_BindingParmInfo &i) const
72  { return (this == &i); }
73 
74 private:
75  UT_String myOpParm;
76  UT_String myPIParm;
77  friend class pi_OpHandleBinding;
78 };
79 
84 
86 {
87 public:
89 
90  const UT_String &getPIName() const
91  { return myPIName; }
92  const UT_String &getPIDescription() const
93  { return myPIDescription; }
95  { return myDefaultSettings; }
96  int getId() const
97  { return myId; }
99  { return myBindings; }
100  int operator==(const PI_BindingHandleInfo &i) const
101  { return (this == &i); }
102 
103 private:
104  UT_String myPIName;
105  UT_String myPIDescription;
106  PI_SettingList myDefaultSettings;
107  int myId;
108  PI_BindingParmArray myBindings;
109  friend class pi_OpHandleBinding;
110 };
112 
113 // See the bindSelector function for a description of all the data
114 // members of this class. They map directly to the bindSelector parameters.
116 {
117 public:
119 
120  const UT_String &getName() const
121  { return myName; }
122  const UT_String &getDescription() const
123  { return myDescription; }
124  const UT_String &getPrompt() const
125  { return myPrompt; }
126  const UT_String &getOpParm() const
127  { return myOpParm; }
128  const UT_String &getMenu() const
129  { return myMenu; }
130  const UT_String &getPrimMask() const
131  { return myPrimMask; }
132  int getOpInput() const
133  { return myOpInput; }
134  bool getOpInputReq() const
135  { return myOpInputReq; }
136  bool getAllowDrag() const
137  { return myAllowDrag; }
138  bool getAstSelAll() const
139  { return myAstSelAll; }
140  const UT_String &getExtraInfo() const
141  { return myExtraInfo; }
142  int operator==(const PI_BindingSelectorInfo &i) const
143  { return (this == &i); }
144 
145 private:
146  UT_String myName;
147  UT_String myDescription;
148  UT_String myPrompt;
149  UT_String myOpParm;
150  UT_String myMenu;
151  UT_String myPrimMask;
152  int myOpInput;
153  bool myOpInputReq;
154  bool myAllowDrag;
155  bool myAstSelAll;
156  UT_String myExtraInfo;
157  friend class pi_OpSelectorBinding;
158 };
160 
163 {
164 public:
166  virtual ~PI_ResourceManager();
167 
168  // Return 1 on success and 0 if the state/handle/op-handle link
169  // is already registered.
170  virtual int registerState(PI_StateTemplate *type);
171  virtual int registerHandle(PI_HandleTemplate *type);
172  virtual int registerOpHandleLink(PI_PITemplate *type);
173  virtual int registerSelector(PI_SelectorTemplate *type);
174 
175  bool containsState(const char *name,
176  PI_ViewerType viewer_type,
177  unsigned netmask) const;
178  bool containsHandle(const char *name) const;
179  bool containsOpHandleLink(const char *name) const;
180  bool containsSelector(const char *name) const;
181 
182  // Get all the bound handles and parms for a given op type.
184  *getBindingHandles(OP_Operator *op) const;
185  // Get all the bound selectors for a given op type.
187  *getBindingSelectors(OP_Operator *op) const;
188 
189  // Return the list of states that can be used in a viewer for the
190  // specified network type.
192  &stateList(PI_ViewerType viewer_type,
193  const char *network_type) const;
195  &stateList(PI_ViewerType viewer_type,
196  PI_NetworkType net_type) const;
197  PI_StateTemplate *getStateTemplate(const char *name,
198  PI_ViewerType viewer_type,
199  const char *type) const;
200  PI_StateTemplate *getStateTemplate(OP_Operator *op,
201  PI_ViewerType viewer_type,
202  const char *type) const;
203 
204  // Viewer state handling
205  // Return the registration status of a state template.
206  virtual PI_PythonState::RegisterStatus getViewerStateRegisterStatus(
207  const char *state_name,
208  PI_ViewerType viewer_type,
209  const char *type) const;
210 
211  // Fires a register viewer state event callback
212  virtual void fireViewerStateEvent(
214  PI_PythonState::EventMessage::Data const& event_data);
215 
216  // return true if the state name is valid
217  bool validStateTemplateName(const char* name) const;
218 
219  // unregister a python viewer state
220  virtual PI_PythonState::Error unregisterViewerState(const char* name);
221 
222  // return true if a viewer state is registered
223  virtual bool isRegisteredViewerState(const char* name) const;
224 
225  // return a viewer state given a name
226  virtual PI_PythonState::Template const *
227  viewerStateTemplate(const char * state_name) const;
228 
229  // These are functions which are called from commands that allow
230  // for dynamic binding of handles to parameters
231  int unbind(OP_Operator *op, const char *pidescription,
232  const UT_String &opparm);
233  int bind(OP_Operator *op, const char *piname,
234  const char *pidescription,
235  const UT_String &opparm,
236  const UT_String &piparm,
237  const char *default_settings);
238  // this API is typically used for handle late-binding e.g. python states
239  // would provide functions for binding handle parms
240  int bind(OP_Operator *op, const char *piname,
241  const char *pidescription,
242  const char *default_settings);
243  int bindScriptOp(OP_Operator *op, const char *piname,
244  const char *pidescription,
245  const UT_String &opparm,
246  const UT_String &piparm,
247  const char *default_settings);
248 
249  // Used for binding node operators to registered viewer states (i..e. python states)
250  // returns true if the operation succeeded
251  virtual bool bindOperatorToViewerState(const char* state_name, OP_Operator* op);
252 
253  // Returns true if a state is expected to exist for an operator of a given
254  // network type. By design, management operators are not expected to operate
255  // outside their own network, this function will return false in this case.
256  //
257  // op_name: operator name
258  // op_type: operator type e.g. DOP
259  bool operatorStateMandatory(const char* op_name, const char* op_type) const;
260 
261  /// Unbind the given selector from an operator. Uses the short description
262  /// when bound using bindSelector()
263  int unbindSelector(OP_Operator *op,
264  const char *seldescription);
265 
266  /// Dynamic binding of a selector to an operator
267  ///
268  /// @param op Operator type that we want to bind a selector to
269  /// @param sname Selector type
270  /// @param seldescription Short description that identifies this selector
271  /// in the context of a one operator (used to
272  /// unbind the selector).
273  /// @param prompt Text in the status area when the selector is
274  /// active.
275  /// @param opparm Operator parameter where the selection string
276  /// is put.
277  /// @param opinput Wire input index where the op owning the
278  /// selected geometry is attached to our op.
279  /// @param opinputreq Is an input is required?
280  /// @param primmask Mask of which primitive types can be in the
281  /// selection. This uses the *_FILEID enum in
282  /// GEO_PrimTypeCompat.h. A regular
283  /// GEO_PrimTypeCompat mask can be used if you
284  /// first convert it using
285  /// GEO_PrimTypeCompat::convertToFileFromMask()
286  /// @param allowdrag Determines if click-and-drag operations are
287  /// allowed.
288  /// @param menu Operator menu parameter where we choose the
289  /// selection type.
290  /// @param astselall If an asterisk is required in the selection
291  /// string to indicate selecting all the input.
292  /// Otherwise we assume an empty string indicates
293  /// that the whole geometry is selected.
294  /// @param extrainfo String that is passed to the selector,
295  /// particular selectors may need specific
296  /// parameters that can be passed through this
297  /// mechanism.
298  /// @param fromScriptOp Used by our dialog script parser to avoid
299  /// infinite recursion. Setting this flag decreases
300  /// the error checking we do.
301  ///
302  /// @return PI_BIND_OK upon success, else see of one of the other potential
303  /// error codes at the top of PI_ResourceManager.h
304  int bindSelector(OP_Operator *op, const char *sname,
305  const char *seldescription,
306  const char *prompt, const char *opparm,
307  int opinput, int opinputreq,
308  const char *primmask, int allowdrag,
309  const char *menu, int astselall,
310  const char *extrainfo, bool fromScriptOp);
311 
312  //Helper functions for the above
313  PRM_Template *getTemplateAndVecId(const UT_String &,
314  OP_Operator *,
315  int &) const;
316 
317  // This is for converting old style "t 2" into new style "tz". It
318  // only works if the operator has the template already, else it
319  // assumes channel name == parmname. Thus, it won't work on
320  // multiparms that only exist with OP_Node, not OP_Operator.
321  void getChannelNameFromParmAndVec(UT_String &chan,
322  OP_Operator *op, const char *parmname,
323  int vecidx);
324 
325  int checkForPIName( const char *pi_name,
326  const char *pi_parm ) const;
327 
328  OP_Operator *findOperator(PI_NetworkType net_type,
329  const char *op_name,
330  bool display_warning = true) const;
331  OP_Operator *findOperator(const char *opbindname,
332  UT_String *optype = 0) const;
333 
334  virtual void findVolatileKeys(const char *volatile_hotkey_name,
335  UT_Array<int> &volatile_keys,
336  UT_Array<int> &keys_modifiers)
337  const;
338  virtual void updateViewports(int id, const char *opname,
339  bool open_only = false) const;
340 
341  //These methods are used in PI_ResourceManagerCommand.C.
342  const PI_OpHandleLinkMap &getOpHandleLinks() const { return myOpHandleLinks; }
343  PI_PITemplate *getOpHandleLinkTemplate(const char *pi_name) const;
344  const PI_SelectorTemplateMap &getSelectors() const { return mySelectors; }
345  PI_SelectorTemplate *getSelectorTemplate(const char *selector_name) const;
346 
347  // The following method is used by builddialogs
348  const PI_HandleTemplateMap &getHandles() const { return myHandles; }
349 
350  // Tell us if the pi with the given name is an invisible PI by nature.
351  // Optionally return the type of the pi in isfloat (int or float assumed)
352  bool isPIEthereal(const char *piname,
353  bool *isfloat = 0) const;
354 
355  // Because the order of callbacks for OP_UpdateTableSink is
356  // undefined, some callbacks rely on the resource manager's table of states
357  // to be accurate. Since the resource manager's callback can happen
358  // after the other callbacks, they can call these methods to ensure the
359  // resource manager's table is accurate.
360  void ensureStatesExistForAllOperators(const char *net_type_name);
361  virtual void ensureStatesExistForAllOperators(PI_NetworkType net_type);
362 
363  // Map a viewer type token to a PI_ViewerType, or vice-versa.
364  static bool getViewerType(const char *viewer_type_token,
365  PI_ViewerType &type);
366  static void getViewerTypeToken(PI_ViewerType type,
367  UT_String &viewer_type_token);
368 
369  // Map the network name returned by OP_Node::getOpType() to a
370  // PI_NetworkType, or vice-versa.
371  static bool getNetworkType(const char *network_type_name,
372  PI_NetworkType &type);
373  static const UT_StringHolder &getNetworkTypeName(PI_NetworkType type);
374  static OP_OperatorTable *getOpTableFromOperator(const OP_Operator *op);
375 
376  // Choose the first network type from a mask that contains multiple ones.
377  static PI_NetworkType getFirstNetworkType(unsigned int network_mask);
378 
379  static PRM_Template *findParmTemplate(OP_Operator &op,
380  const char *op_Parm_name,
381  int vec_idx, int *parm_idx);
382 
383  // Write out our bindings information to a file.
384  int writeBindingsFile(const char *fname) const;
385 
386  // Load/save/remove the settings for a particular PI and a node that's
387  // linked to it.
388  void loadPISettingsFromTables(PI_OpHandleLink &pi,
389  bool okay_to_use_last_settings) const;
390  void savePISettingsToTables(PI_OpHandleLink &pi);
391  virtual void removePISettingsFromTables(OP_Operator &op_type);
392  void removePISettingsFromTables(const PI_OpHandleLink &pi);
393 
394  // Record settings for a particular PI. This method should only be
395  // called by the omparm command.
396  void recordPISettings(const char *pi_description,
397  const char *pi_type,
398  const OP_Node &op_node,
399  const char *settings);
400 
401  // Objects may add callbacks that are called just before the resource
402  // manager saves its data to the hip file. These callbacks update
403  // the resource manager to make sure the data it saves is current.
404  void addSaveCallback(PI_SaveCallback callback, void *data);
405  void removeSaveCallback(PI_SaveCallback callback,
406  void *data);
407 
408  void registerOpHandleBindings();
409  virtual void removeAllBindings(OP_Operator *op);
410 
411  // Returns the name of the state being created
412  char const* stateToCreate() const;
413 
414 protected:
415  static void startParmsCache();
416  static void stopParmsCache();
417  static void getParmsCache(UT_ValArray<OP_Node *> *&ops,
418  UT_ValArray<PRM_Parm *> *&parms,
419  UT_IntArray *&vec_indices);
420  static void getParmsCacheBehaviour( bool &set_keys,
421  bool &do_scope,
422  bool &append_scope );
423 
424  // Save the name of the state being created.
425  void saveStateToCreate(char const* state);
426 public:
427  static int cacheParms();
428  static void appendToParmsCache(OP_Node *op, PRM_Parm *parm,
429  int vi);
430  static void setParmsCacheBehaviour( bool set_keys, bool do_scope,
431  bool append_scope );
432 
434  { return theHideLockedHandleParts; }
435  static void setHideLockedHandleParts( bool hide )
436  { theHideLockedHandleParts = hide; }
437  static bool getScopeChannelsOnKey()
438  { return theScopeChannelsOnKey; }
439  static void setScopeChannelsOnKey( bool scope )
440  { theScopeChannelsOnKey = scope; }
441  static bool getAddScopedChannels()
442  { return theAddScopedChannels; }
443  static void setAddScopedChannels( bool add )
444  { theAddScopedChannels = add; }
446  { return theOpenChannelEditorOnScope; }
447  static void setOpenChannelEditorOnScope( bool do_open )
448  { theOpenChannelEditorOnScope = do_open; }
449  static void getChannelScopeCommandPrefix( UT_String &str );
450 
451 
452 protected:
453  // Classes:
454 
456  {
457  public:
458  PI_OpPIKey(const char *pi_name_in_setting_table, const char *pi_type,
459  int op_id) :
460  myPINameInSettingTable(pi_name_in_setting_table),
461  myPIType(pi_type),
462  myOpId(op_id) {}
463 
464  bool operator==(const PI_OpPIKey &o) const
465  {
466  // myPIType is not used.
467  return myPINameInSettingTable == o.myPINameInSettingTable &&
468  myOpId == o.myOpId;
469  }
470 
472  {
473  // myPIType is not used.
474  SYS_HashType hash = myPINameInSettingTable.hash();
475  SYShashCombine(hash, myOpId);
476  return hash;
477  }
478 
479  UT_StringHolder piNameInSettingTable() const { return myPINameInSettingTable; }
480  UT_StringHolder piType() const { return myPIType; }
481  int opId() const { return myOpId; }
482 
483  struct Hasher
484  {
485  size_t operator()(const PI_OpPIKey &pk) const { return size_t(pk.hash()); }
486  };
487 
488  private:
489  UT_StringHolder myPINameInSettingTable;
490  UT_StringHolder myPIType;
491  int myOpId;
492  };
493 
494  // Methods:
495 
496  void installCommands();
497  void registerOpHandleLinks();
498  void registerCopHandleLinks();
499  void registerOpHandleConstructors();
500  void registerSelectors();
501  void registerSelectorConstructors();
502 
503  // Functions used by registerOpHandleLinks for loading the OPbindings file.
504  bool isValidPiParmName( const UT_String &pi_name,
505  const UT_String &pi_parm);
506  void loadHandle(CPP_Manager &cppman, OP_Operator *op,
507  pi_OpHandleBinding *binding);
508  void loadSelector(CPP_Manager &cppman,
509  const UT_String &selname,
510  const UT_String &seldescription,
511  pi_OpSelectorBinding *binding);
512  void loadOperator(CPP_Manager &cppman,
513  const UT_String &oppath);
514  void loadOperator(CPP_Manager &cppman,
515  const UT_String &optype,
516  OP_Operator *op,
517  pi_OpHandleBinding *hbinding,
518  pi_OpSelectorBinding *sbinding);
519 
520  virtual void addAutomaticStates(PI_NetworkType net_type);
521  virtual void tableUpdated(OP_OperatorTable *table);
522  virtual void tableDeleted(OP_OperatorTable *table);
523  virtual void templatesUpdated(OP_Operator *op);
524  virtual void operatorDeleted(OP_Operator *op);
525  void ensureTableInterestExists(PI_NetworkType net_type,
527 
528  PI_HandleTemplate *getHandleTemplate(const char *handle_name) const;
529 
530  // Save all the PI settings to a stream.
531  static OP_ERROR savePIResourceManagerCallback(std::ostream &os,
532  void *data);
533  OP_ERROR saveSettings(std::ostream &os) const;
534 
535  // We need the non-const version of getDefaultPISettings() because we
536  // might need to sort the default settings.
537  const PI_SettingList *getDefaultPISettings(OP_Operator &op_type,
538  const char *type_name,
539  const char *description) const;
540  PI_SettingList *getDefaultPISettings(OP_Operator &op_type,
541  const char *type_name,
542  const char *description);
543 
544 
545  // Data:
552 
555 
558 
562 
564 
565  static bool ourSetKeyOnParms;
566  static bool ourScopeParmChannels;
567  static bool ourAppendToScope;
568  static int ourCacheParms;
572 
575  static bool theAddScopedChannels;
577 };
578 
580 PI_API extern void PIcreateResourceManager(bool verbose=true);
581 PI_API extern bool PIresourceManagerExists();
582 
583 #endif
int operator==(const PI_BindingSelectorInfo &i) const
int operator==(const PI_BindingHandleInfo &i) const
const UT_String & getPrompt() const
UT_StringHolder piNameInSettingTable() const
GLuint const GLchar * name
Definition: glew.h:1814
Unsorted map container.
Definition: UT_Map.h:83
const PI_OpHandleLinkMap & getOpHandleLinks() const
UT_Array< PI_SaveCallbackInfo > mySaveCallbacks
PI_HandleTemplateMap myHandles
static bool theOpenChannelEditorOnScope
const PI_SelectorTemplateMap & getSelectors() const
static UT_ValArray< OP_Node * > ourParmNodes
UT_SymbolMap< pi_OpHandleBinding * > myOpHandleBindings
PI_API void PIcreateResourceManager(bool verbose=true)
static void setScopeChannelsOnKey(bool scope)
PI_OpHandleLinkMap myOpHandleLinks
PI_NetworkType
static bool getAddScopedChannels()
GLenum GLsizei GLenum GLenum const void * table
Definition: glew.h:4940
PI_ViewerType
PI_OpPIKey(const char *pi_name_in_setting_table, const char *pi_type, int op_id)
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
UT_ErrorSeverity
Definition: UT_Error.h:25
const PI_SettingList & getDefaultSettings() const
#define PI_API
Definition: PI_API.h:10
static void setOpenChannelEditorOnScope(bool do_open)
PI_API bool PIresourceManagerExists()
UT_StringHolder piType() const
void(* PI_SaveCallback)(void *data)
PI_SelectorTemplateMap mySelectors
const UT_String & getPIDescription() const
static bool theHideLockedHandleParts
bool operator==(const PI_OpPIKey &o) const
static bool theScopeChannelsOnKey
UT_SymbolMap< PI_NetworkType > myNetworkTypeTable
static UT_IntArray ourParmVecIndices
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
static bool ourScopeParmChannels
const UT_String & getOpParm() const
size_t operator()(const PI_OpPIKey &pk) const
void
Definition: png.h:1083
static bool getHideLockedHandleParts()
const UT_String & getPrimMask() const
static bool getOpenChannelEditorOnScope()
static void setAddScopedChannels(bool add)
const UT_String & getExtraInfo() const
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
UT_Map< PI_OpPIKey, PI_SettingList *, PI_OpPIKey::Hasher > myPISettingsForOps
static bool getScopeChannelsOnKey()
PI_API PI_ResourceManager * PIgetResourceManager()
static void setHideLockedHandleParts(bool hide)
static bool theAddScopedChannels
const UT_String & getPIName() const
const PI_HandleTemplateMap & getHandles() const
const PI_BindingParmArray & getBindings() const
const UT_String & getMenu() const
static bool ourSetKeyOnParms
UT_StringHolder myStateBeingCreated
const UT_String & getDescription() const
UT_SymbolMap< pi_OpSelectorBinding * > myOpSelectorBindings
static UT_ValArray< PRM_Parm * > ourParms
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
int operator==(const PI_BindingParmInfo &i) const
static bool ourAppendToScope
const UT_String & getName() const
UT_StringMap< PI_SettingList * > myLastPISettings
const UT_String & getPIParm() const
const UT_String & getOpParm() const