Choreonoid  1.5
ItemManager.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_BASE_ITEM_MANAGER_H
6 #define CNOID_BASE_ITEM_MANAGER_H
7 
8 #include "ExtensionManager.h"
9 #include "ItemList.h"
10 #include <string>
11 #include <typeinfo>
12 #include <iosfwd>
13 #include <boost/function.hpp>
14 #include <QWidget>
15 #include "exportdecl.h"
16 
17 namespace cnoid {
18 
19 class MenuManager;
20 
21 class Item;
22 typedef ref_ptr<Item> ItemPtr;
23 
24 class ItemManagerImpl;
25 
26 class ItemCreationPanel : public QWidget
27 {
28 public:
29  ItemCreationPanel(QWidget* parent = 0) : QWidget(parent) { }
30 
31  virtual bool initializePanel(Item* protoItem) = 0;
32  virtual bool initializeItem(Item* protoItem) = 0;
33 
34 protected:
35  ItemCreationPanel* findPanelOnTheSameDialog(const std::string& name);
36 };
37 
38 
40 {
41 public:
42 
43  ItemManager(const std::string& moduleName, MenuManager& menuManager);
44  ~ItemManager();
45 
46  void detachAllManagedTypeItemsFromRoot();
47 
48 private:
49 
50  template <class ItemType> class Factory {
51  public:
52  virtual Item* operator()() { return new ItemType(); }
53  };
54 
55  class CreationPanelFilterBase
56  {
57  public:
58  virtual ~CreationPanelFilterBase() { }
59  virtual bool operator()(Item* protoItem, Item* parentItem) = 0;
60  };
61  typedef boost::shared_ptr<CreationPanelFilterBase> CreationPanelFilterBasePtr;
62 
63 
64  class FileFunctionBase
65  {
66  public:
67  virtual ~FileFunctionBase() { }
68  virtual bool operator()(Item* item, const std::string& filename, std::ostream& os, Item* parentItem) = 0;
69  };
70  typedef boost::shared_ptr<FileFunctionBase> FileFunctionBasePtr;
71 
72 
73  class OverwritingCheckFunctionBase
74  {
75  public:
76  ~OverwritingCheckFunctionBase();
77  virtual bool operator()(Item* item) = 0;
78  };
79  typedef boost::shared_ptr<OverwritingCheckFunctionBase> OverwritingCheckFunctionBasePtr;
80 
81 
82 public:
83 
84  void bindTextDomain(const std::string& domain);
85 
86  enum { PRIORITY_CONVERSION = -10, PRIORITY_OPTIONAL = 0, PRIORITY_DEFAULT = 10, PRIORITY_FORCE = 20 };
87 
88  template <class ItemType> class CreationPanelFilter : public CreationPanelFilterBase
89  {
90  public:
91  typedef boost::function<bool(ItemType* protoItem, Item* parentItem)> Function;
92  CreationPanelFilter(Function function) : function(function) { }
93  virtual bool operator()(Item* protoItem, Item* parentItem){
94  return function(static_cast<ItemType*>(protoItem), parentItem);
95  }
96  private:
97  Function function;
98  };
99 
100  template <class ItemType> class FileFunction : public FileFunctionBase
101  {
102  public:
103  typedef boost::function<bool(ItemType* item, const std::string& filename, std::ostream& os, Item* parentItem)> Function;
104  FileFunction(Function function) : function(function) { }
105  virtual bool operator()(Item* item, const std::string& filename, std::ostream& os, Item* parentItem){
106  return function(static_cast<ItemType*>(item), filename, os, parentItem);
107  }
108  private:
109  Function function;
110  };
111 
112  template <class ItemType> ItemManager& registerClass(const std::string& className) {
113  registerClassSub(Factory<ItemType>(), 0, typeid(ItemType).name(), className);
114  return *this;
115  }
116 
118  template <class ItemType> ItemManager& registerClass(const std::string& className, ItemType* singletonInstance){
119  registerClassSub(0, singletonInstance, typeid(ItemType).name(), className);
120  return *this;
121  }
122 
123  template <class ItemType, class BaseType>
124  void registerDerivedClass(const std::string& className) {
125  // registerClassSub(new Factory<ItemType>(), typeid(ItemType).name(), className);
126  }
127 
128  static bool getClassIdentifier(ItemPtr item, std::string& out_moduleName, std::string& out_className);
129 
130  template <class ItemType> static ItemType* singletonInstance() {
131  return static_cast<ItemType*>(getSingletonInstance(typeid(ItemType).name()));
132  }
133 
134  static ItemPtr create(const std::string& moduleName, const std::string& itemClassName);
135 
136  template <class ItemType> ItemManager& addCreationPanel(ItemCreationPanel* panel = 0) {
137  addCreationPanelSub(typeid(ItemType).name(), panel);
138  return *this;
139  }
140 
141  template <class ItemType>
143  addCreationPanelFilterSub(typeid(ItemType).name(),
144  CreationPanelFilterBasePtr(new CreationPanelFilter<ItemType>(filter)),
145  false);
146  }
147 
148  template <class ItemType>
150  addCreationPanelFilterSub(typeid(ItemType).name(),
151  CreationPanelFilterBasePtr(new CreationPanelFilter<ItemType>(filter)),
152  true);
153  }
154 
155  template <class ItemType>
156  ItemManager& addLoader(const std::string& caption, const std::string& formatId, const std::string& extensions,
157  const typename FileFunction<ItemType>::Function& function, int priority = PRIORITY_DEFAULT) {
158  addLoaderSub(typeid(ItemType).name(), caption, formatId, extensions,
159  FileFunctionBasePtr(new FileFunction<ItemType>(function)), priority);
160  return *this;
161  }
162 
163  template<class ItemType>
164  ItemManager& addSaver(const std::string& caption, const std::string& formatId, const std::string& extensions,
165  const typename FileFunction<ItemType>::Function& function, int priority = PRIORITY_DEFAULT){
166  addSaverSub(typeid(ItemType).name(), caption, formatId, extensions,
167  FileFunctionBasePtr(new FileFunction<ItemType>(function)), priority);
168  return *this;
169  }
170 
171  template<class ItemType>
172  ItemManager& addLoaderAndSaver(const std::string& caption, const std::string& formatId,
173  const std::string& extensions,
174  const typename FileFunction<ItemType>::Function& loadingFunction,
175  const typename FileFunction<ItemType>::Function& savingFunction,
176  int priority = PRIORITY_DEFAULT){
177  addLoader<ItemType>(caption, formatId, extensions, loadingFunction, priority);
178  addSaver<ItemType>(caption, formatId, extensions, savingFunction, priority);
179  return *this;
180  }
181 
182  void addMenuItemToImport(const std::string& caption, boost::function<void()> slot);
183 
184  static void reloadItems(const ItemList<>& items);
185 
186 private:
187 
188  void registerClassSub(
189  boost::function<Item*()> factory, Item* singletonInstance, const std::string& typeId, const std::string& className);
190  void addCreationPanelSub(const std::string& typeId, ItemCreationPanel* panel);
191  void addCreationPanelFilterSub(
192  const std::string& typeId, CreationPanelFilterBasePtr filter, bool afterInitializionByPanels);
193  void addLoaderSub(const std::string& typeId, const std::string& caption, const std::string& formatId,
194  const std::string& extensions, FileFunctionBasePtr function, int priority);
195  void addSaverSub(const std::string& typeId, const std::string& caption, const std::string& formatId,
196  const std::string& extensions, FileFunctionBasePtr function, int priority);
197 
198  static Item* getSingletonInstance(const std::string& typeId);
199 
200  // The following static functions are called from functions in the Item class
201  static bool load(Item* item, const std::string& filename, Item* parentItem, const std::string& formatId);
202  static bool save(Item* item, const std::string& filename, const std::string& formatId);
203  static bool overwrite(Item* item, bool forceOverwrite, const std::string& formatId); // overwrite
204 
205  friend class Item;
206  friend class ItemManagerImpl;
207  ItemManagerImpl* impl;
208 };
209 
210 CNOID_EXPORT std::string getOpenFileName(const std::string& caption, const std::string& extensions);
211 CNOID_EXPORT std::vector<std::string> getOpenFileNames(const std::string& caption, const std::string& extensions);
212 }
213 
214 #endif
Definition: ItemList.h:14
std::vector< std::string > getOpenFileNames(const std::string &caption, const std::string &extensions)
Definition: ItemManager.cpp:1437
FileFunction(Function function)
Definition: ItemManager.h:104
Definition: ItemManager.h:88
virtual bool initializeItem(Item *protoItem)=0
Definition: ItemManager.h:100
boost::function< bool(ItemType *protoItem, Item *parentItem)> Function
Definition: ItemManager.h:91
ItemManager & addLoader(const std::string &caption, const std::string &formatId, const std::string &extensions, const typename FileFunction< ItemType >::Function &function, int priority=PRIORITY_DEFAULT)
Definition: ItemManager.h:156
virtual bool initializePanel(Item *protoItem)=0
void registerDerivedClass(const std::string &className)
Definition: ItemManager.h:124
ItemManager & registerClass(const std::string &className, ItemType *singletonInstance)
This function registers a singleton item class.
Definition: ItemManager.h:118
virtual bool operator()(Item *protoItem, Item *parentItem)
Definition: ItemManager.h:93
Definition: MenuManager.h:23
Definition: Item.h:38
ItemCreationPanel * findPanelOnTheSameDialog(const std::string &name)
Definition: ItemManager.cpp:695
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
static ItemType * singletonInstance()
Definition: ItemManager.h:130
ItemManager & registerClass(const std::string &className)
Definition: ItemManager.h:112
boost::function< bool(ItemType *item, const std::string &filename, std::ostream &os, Item *parentItem)> Function
Definition: ItemManager.h:103
std::string getOpenFileName(const std::string &caption, const std::string &extensions)
Definition: ItemManager.cpp:1417
ItemCreationPanel(QWidget *parent=0)
Definition: ItemManager.h:29
CreationPanelFilter(Function function)
Definition: ItemManager.h:92
#define CNOID_EXPORT
Definition: Util/exportdecl.h:37
ItemManager & addCreationPanel(ItemCreationPanel *panel=0)
Definition: ItemManager.h:136
void addCreationPanelPreFilter(const typename CreationPanelFilter< ItemType >::Function &filter)
Definition: ItemManager.h:142
Definition: ItemManager.h:26
void addCreationPanelPostFilter(const typename CreationPanelFilter< ItemType >::Function &filter)
Definition: ItemManager.h:149
Definition: ItemManager.h:39
ref_ptr< Item > ItemPtr
Definition: Item.h:20
ItemManager & addSaver(const std::string &caption, const std::string &formatId, const std::string &extensions, const typename FileFunction< ItemType >::Function &function, int priority=PRIORITY_DEFAULT)
Definition: ItemManager.h:164
virtual bool operator()(Item *item, const std::string &filename, std::ostream &os, Item *parentItem)
Definition: ItemManager.h:105
ItemManager & addLoaderAndSaver(const std::string &caption, const std::string &formatId, const std::string &extensions, const typename FileFunction< ItemType >::Function &loadingFunction, const typename FileFunction< ItemType >::Function &savingFunction, int priority=PRIORITY_DEFAULT)
Definition: ItemManager.h:172