Choreonoid  1.8
ViewManager.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_BASE_VIEW_MANAGER_H
6 #define CNOID_BASE_VIEW_MANAGER_H
7 
8 #include "View.h"
9 #include "ExtensionManager.h"
10 #include "Archive.h"
11 #include <cnoid/Signal>
12 #include <set>
13 #include "exportdecl.h"
14 
15 namespace cnoid {
16 
17 class ExtensionManager;
18 
19 class CNOID_EXPORT ViewManager
20 {
21 public:
22  static void initializeClass(ExtensionManager* ext);
23 
25  ~ViewManager();
26 
27  class FactoryBase {
28  public:
29  virtual View* create() = 0;
30  virtual ~FactoryBase() { }
31  };
32 
33  template <class ViewType> class Factory : public FactoryBase {
34  public:
35  virtual View* create() { return new ViewType(); }
36  };
37 
39  Single = 0,
40  Multiple = 1,
41  Default = 2,
42  SINGLE_OPTIONAL = Single,
43  SINGLE_DEFAULT = Single | Default,
44  MULTI_OPTIONAL = Multiple,
45  MULTI_DEFAULT = Multiple | Default
46  };
47 
48  template <class ViewType>
50  const std::string& className, const std::string& defaultInstanceName, int instantiationFlags = Single) {
51  registerClass_(
52  typeid(ViewType), className, defaultInstanceName, instantiationFlags, new Factory<ViewType>());
53  return *this;
54  }
55 
56  ViewManager& registerClassAlias(const std::string& alias, const std::string& orgClassName);
57 
58  static ViewClass* viewClass(const std::type_info& view_type_info);
59 
60  // get or create the primal instance of the specified view type
61  static View* getOrCreateView(const std::string& moduleName, const std::string& className);
62 
63  // get or create the instance of the specified view type and instance name
64  static View* getOrCreateView(
65  const std::string& moduleName, const std::string& className, const std::string& instanceName);
66 
67  static std::vector<View*> allViews();
68  static std::vector<View*> activeViews();
69 
70  template <class ViewType> static ViewType* getOrCreateView(bool doMountCreatedView = false) {
71  return static_cast<ViewType*>(getOrCreateSpecificTypeView(typeid(ViewType), std::string(), doMountCreatedView));
72  }
73 
74  template <class ViewType> static ViewType* getOrCreateView(const std::string& instanceName, bool doMountCreatedView = false) {
75  return static_cast<ViewType*>(getOrCreateSpecificTypeView(typeid(ViewType), instanceName, doMountCreatedView));
76  }
77 
78  template <class ViewType> static ViewType* findView() {
79  return static_cast<ViewType*>(findSpecificTypeView(typeid(ViewType), std::string()));
80  }
81 
82  template <class ViewType> static ViewType* findView(const std::string& instanceName) {
83  return static_cast<ViewType*>(findSpecificTypeView(typeid(ViewType), instanceName));
84  }
85 
86  void deleteView(View* view);
87 
88  static bool isPrimalInstance(View* view);
89 
90  static bool storeViewStates(ArchivePtr archive, const std::string& key);
91 
92  class ViewStateInfo {
93  public:
94  ViewStateInfo();
95  ~ViewStateInfo();
96  operator bool() const { return (data != 0); }
97  private:
98  void* data;
99  friend class ViewManager;
100  };
101 
102  static bool restoreViews(
103  ArchivePtr archive, const std::string& key, ViewStateInfo& out_viewStateInfo,
104  const std::set<std::string>& optionalPlugins);
105  static bool restoreViewStates(ViewStateInfo& info);
106 
107  static SignalProxy<void(View* view)> sigViewCreated();
108  static SignalProxy<void(View* view)> sigViewActivated();
109  static SignalProxy<void(View* view)> sigViewDeactivated();
110  static SignalProxy<void(View* view)> sigViewRemoved();
111 
112  class Impl;
113 
114 private:
115  ViewManager(const ViewManager&) { }
116 
117  void registerClass_(
118  const std::type_info& view_type_info,
119  const std::string& className, const std::string& defaultInstanceName, int instantiationFlags,
120  FactoryBase* factory);
121  static View* getOrCreateSpecificTypeView(
122  const std::type_info& view_type_info, const std::string& instanceName, bool doMountCreatedView);
123  static View* findSpecificTypeView(const std::type_info& view_type_info, const std::string& instanceName);
124 
125  Impl* impl;
126 };
127 
128 }
129 
130 #endif
cnoid::ExtensionManager
Definition: ExtensionManager.h:23
cnoid::ViewManager::findView
static ViewType * findView(const std::string &instanceName)
Definition: ViewManager.h:82
cnoid::View
Definition: View.h:27
cnoid::ViewManager::FactoryBase::~FactoryBase
virtual ~FactoryBase()
Definition: ViewManager.h:30
cnoid::ref_ptr< Archive >
cnoid::ViewManager::ViewStateInfo
Definition: ViewManager.h:92
Archive.h
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::ViewManager::registerClass
ViewManager & registerClass(const std::string &className, const std::string &defaultInstanceName, int instantiationFlags=Single)
Definition: ViewManager.h:49
cnoid::ViewManager::InstantiationFlags
InstantiationFlags
Definition: ViewManager.h:38
cnoid::ViewManager::Factory::create
virtual View * create()
Definition: ViewManager.h:35
cnoid::ViewManager::getOrCreateView
static ViewType * getOrCreateView(bool doMountCreatedView=false)
Definition: ViewManager.h:70
cnoid::ViewManager
Definition: ViewManager.h:19
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::ViewManager::findView
static ViewType * findView()
Definition: ViewManager.h:78
View.h
cnoid::ViewManager::getOrCreateView
static ViewType * getOrCreateView(const std::string &instanceName, bool doMountCreatedView=false)
Definition: ViewManager.h:74
cnoid::ViewManager::FactoryBase
Definition: ViewManager.h:27
ExtensionManager.h
cnoid::ViewManager::Factory
Definition: ViewManager.h:33
cnoid::ViewClass
Definition: View.h:21