Choreonoid  1.8
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 <QWidget>
11 #include <string>
12 #include <typeinfo>
13 #include <iosfwd>
14 #include <memory>
15 #include "exportdecl.h"
16 
17 namespace cnoid {
18 
19 class MenuManager;
20 class Item;
21 class ItemFileIO;
22 class ItemAddon;
23 class Mapping;
24 
25 class CNOID_EXPORT ItemCreationPanel : public QWidget
26 {
27 public:
29  virtual bool initializeCreation(Item* protoItem, Item* parentItem) = 0;
30  virtual bool updateItem(Item* protoItem, Item* parentItem) = 0;
31 };
32 
33 template<class ItemType>
35 {
36 protected:
38  virtual bool initializeCreation(ItemType* protoItem, Item* parentItem) = 0;
39  virtual bool updateItem(ItemType* protoItem, Item* parentItem) = 0;
40 
41 private:
42  virtual bool initializeCreation(Item* protoItem, Item* parentItem) override final {
43  return initializeCreation(dynamic_cast<ItemType*>(protoItem), parentItem);
44  }
45  virtual bool updateItem(Item* protoItem, Item* parentItem) override final {
46  return updateItem(dynamic_cast<ItemType*>(protoItem), parentItem);
47  }
48 };
49 
50 class CNOID_EXPORT DefaultItemCreationPanel : public ItemCreationPanel
51 {
52  QWidget* nameEntry;
53 public:
55  virtual bool initializeCreation(Item* protoItem, Item* parentItem) override;
56  virtual bool updateItem(Item* protoItem, Item* parentItem) override;
57 };
58 
59 class CNOID_EXPORT ItemManager
60 {
61 public:
62  ItemManager(const std::string& moduleName, MenuManager& menuManager);
63  ~ItemManager();
64 
65  void detachAllManagedTypeItemsFromRoot();
66 
67 private:
68  template <class ItemType> class Factory {
69  public:
70  virtual Item* operator()() { return new ItemType(); }
71  };
72 
73  class OverwritingCheckFunctionBase
74  {
75  public:
76  ~OverwritingCheckFunctionBase();
77  virtual bool operator()(Item* item) = 0;
78  };
79 
80 public:
82  {
83  public:
84  virtual ~FileFunctionBase() { }
85  virtual bool operator()(Item* item, const std::string& filename, std::ostream& os, Item* parentItem) = 0;
86  };
87 
88  void bindTextDomain(const std::string& domain);
89 
90  enum IoUsageType {
91  Standard = 10,
92  Conversion = 0,
93  Internal = -10,
94 
95  // deprecated
96  PRIORITY_DEFAULT = Standard,
97  PRIORITY_COMPATIBILITY = Internal,
98  PRIORITY_OPTIONAL = Internal,
99  PRIORITY_CONVERSION = Conversion
100  };
101 
102  template <class ItemType> class FileFunction : public FileFunctionBase
103  {
104  public:
105  typedef std::function<bool(ItemType* item, const std::string& filename, std::ostream& os, Item* parentItem)> Function;
106  FileFunction(Function function) : function(function) { }
107  virtual bool operator()(Item* item, const std::string& filename, std::ostream& os, Item* parentItem){
108  return function(static_cast<ItemType*>(item), filename, os, parentItem);
109  }
110  private:
111  Function function;
112  };
113 
114  template <class ItemType, class SuperItemType = Item>
115  ItemManager& registerClass(const std::string& className) {
116  registerClass_(className, typeid(ItemType), typeid(SuperItemType), Factory<ItemType>(), nullptr);
117  return *this;
118  }
119 
121  template <class ItemType, class SuperItemType = Item>
122  ItemManager& registerClass(const std::string& className, ItemType* singletonInstance){
123  registerClass_(className, typeid(ItemType), typeid(SuperItemType), nullptr, singletonInstance);
124  return *this;
125  }
126 
127  template <class ItemType, class SuperItemType = Item>
129  registerClass_("", typeid(ItemType), typeid(SuperItemType), nullptr, nullptr);
130  return *this;
131  }
132 
133  template <class ItemType>
134  ItemManager& addAlias(const std::string& className, const std::string& moduleName){
135  addAlias_(typeid(ItemType), className, moduleName);
136  return *this;
137  }
138 
139  static bool getClassIdentifier(Item* item, std::string& out_moduleName, std::string& out_className);
140 
141  template <class ItemType> static ItemType* singletonInstance() {
142  return static_cast<ItemType*>(getSingletonInstance(typeid(ItemType)));
143  }
144 
145  template <class ItemType> ItemManager& addCreationPanel(ItemCreationPanel* panel = nullptr) {
146  addCreationPanel_(typeid(ItemType), panel);
147  return *this;
148  }
149 
150  template <class ItemType>
152  registerFileIO_(typeid(ItemType), fileIO);
153  return *this;
154  }
155 
157  template <class ItemType>
158  [[deprecated("Use addFileIO")]]
160  return addFileIO<ItemType>(fileIO);
161  }
162 
163  template <class ItemType>
164  static std::vector<ItemFileIO*> getFileIOs(){
165  return getFileIOs(typeid(ItemType));
166  }
167 
168  static std::vector<ItemFileIO*> getFileIOs(const std::type_info& type);
169 
170  static ItemFileIO* findFileIO(const std::type_info& type, const std::string& format);
171 
172  template <class ItemType>
174  const std::string& caption, const std::string& format, const std::string& extensions,
175  typename FileFunction<ItemType>::Function function, int usage = Standard)
176  {
177  addLoader_(typeid(ItemType), caption, format, [extensions](){ return extensions; },
178  std::make_shared<FileFunction<ItemType>>(function), usage);
179  return *this;
180  }
181 
182  template <class ItemType>
184  const std::string& caption, const std::string& format, std::function<std::string()> getExtensions,
185  typename FileFunction<ItemType>::Function function, int usage = Standard)
186  {
187  addLoader_(typeid(ItemType), caption, format, getExtensions,
188  std::make_shared<FileFunction<ItemType>>(function), usage);
189  return *this;
190  }
191 
192  template<class ItemType>
194  const std::string& caption, const std::string& format, const std::string& extensions,
195  typename FileFunction<ItemType>::Function function, int usage = Standard)
196  {
197  addSaver_(typeid(ItemType), caption, format, [extensions](){ return extensions; },
198  std::make_shared<FileFunction<ItemType>>(function), usage);
199  return *this;
200  }
201 
202  template<class ItemType>
204  const std::string& caption, const std::string& format, std::function<std::string()> getExtensions,
205  typename FileFunction<ItemType>::Function function, int usage = Standard)
206  {
207  addSaver_(typeid(ItemType), caption, format, getExtensions,
208  std::make_shared<FileFunction<ItemType>>(function), usage);
209  return *this;
210  }
211 
212  template<class ItemType>
214  const std::string& caption, const std::string& format, const std::string& extensions,
215  typename FileFunction<ItemType>::Function loaderFunction,
216  typename FileFunction<ItemType>::Function saverFunction,
217  int usage = Standard)
218  {
219  addLoader<ItemType>(caption, format, extensions, loaderFunction, usage);
220  addSaver<ItemType>(caption, format, extensions, saverFunction, usage);
221  return *this;
222  }
223 
224  template<class ItemType>
226  const std::string& caption, const std::string& format, std::function<std::string()> getExtensions,
227  typename FileFunction<ItemType>::Function loaderFunction,
228  typename FileFunction<ItemType>::Function saverFunction,
229  int usage = Standard)
230  {
231  addLoader<ItemType>(caption, format, getExtensions, loaderFunction, usage);
232  addSaver<ItemType>(caption, format, getExtensions, saverFunction, usage);
233  return *this;
234  }
235 
236  void addMenuItemToImport(const std::string& caption, std::function<void()> slot);
237 
238  static Item* createItem(const std::string& moduleName, const std::string& itemClassName);
239  static Item* createItem(int itemClassId);
240 
250  template <class ItemType>
251  static ItemType* createItemWithDialog(
252  Item* parentItem, bool doAddition = true, Item* nextItem = nullptr,
253  Item* protoItem = nullptr, const std::string& title = std::string())
254  {
255  return static_cast<ItemType*>(
256  createItemWithDialog_(
257  typeid(ItemType), parentItem, doAddition, nextItem, protoItem, title));
258  }
259 
260  template <class ItemType>
262  Item* parentItem, bool doAddtion = true, Item* nextItem = nullptr)
263  {
264  return loadItemsWithDialog_(typeid(ItemType), parentItem, doAddtion, nextItem);
265  }
266 
267  template <class ItemType>
268  static bool saveItemWithDialog(ItemType* item){
269  return saveItemWithDialog_(typeid(ItemType), item);
270  }
271 
272  [[deprecated("Use Item::reload().")]]
273  static void reloadItems(const ItemList<>& items);
274 
275  [[deprecated("Use Item::findOriginalItem().")]]
276  static Item* findOriginalItemForReloadedItem(Item* item);
277 
278  template<class AddonType>
279  void registerAddon(const std::string& name, std::function<ItemAddon*(void)> factory = nullptr){
280  registerAddon_(typeid(AddonType), name, factory ? factory : [](){ return new AddonType; });
281  }
282  static ItemAddon* createAddon(const std::type_info& type);
283  static ItemAddon* createAddon(const std::string& moduleName, const std::string& addonName);
284  static bool getAddonIdentifier(ItemAddon* addon, std::string& out_moduleName, std::string& out_addonName);
285 
286  class Impl;
287 
288 private:
289  void registerClass_(
290  const std::string& className, const std::type_info& type, const std::type_info& superType,
291  std::function<Item*()> factory, Item* singletonInstance);
292  void addAlias_(const std::type_info& type, const std::string& className, const std::string& moduleName);
293  void addCreationPanel_(const std::type_info& type, ItemCreationPanel* panel);
294  void registerFileIO_(const std::type_info& type, ItemFileIO* fileIO);
295  void addLoader_(
296  const std::type_info& type, const std::string& caption, const std::string& format,
297  std::function<std::string()> getExtensions, std::shared_ptr<FileFunctionBase> function, int usage);
298  void addSaver_(
299  const std::type_info& type, const std::string& caption, const std::string& format,
300  std::function<std::string()> getExtensions, std::shared_ptr<FileFunctionBase> function, int usage);
301 
302  static Item* getSingletonInstance(const std::type_info& type);
303 
304  static Item* createItemWithDialog_(
305  const std::type_info& type, Item* parentItem, bool doAddition, Item* nextItem,
306  Item* protoItem, const std::string& title);
307  static ItemList<Item> loadItemsWithDialog_(
308  const std::type_info& type, Item* parentItem, bool doAddtion, Item* nextItem);
309  static bool saveItemWithDialog_(const std::type_info& type, Item* item);
310 
311  // The following static functions are called from functions in the Item class
312  static bool load(
313  Item* item, const std::string& filename, Item* parentItem, const std::string& format,
314  const Mapping* options = nullptr);
315  static bool save(
316  Item* item, const std::string& filename, const std::string& format, const Mapping* options = nullptr);
317  static bool overwrite(Item* item, bool forceOverwrite, const std::string& format);
318 
319  void registerAddon_(
320  const std::type_info& type, const std::string& name, const std::function<ItemAddon*(void)>& factory);
321 
322  friend class Item;
323 
324  Impl* impl;
325 };
326 
327 CNOID_EXPORT std::string getOpenFileName(const std::string& caption, const std::string& extensions);
328 CNOID_EXPORT std::vector<std::string> getOpenFileNames(const std::string& caption, const std::string& extensions);
329 
330 }
331 
332 #endif
cnoid::ItemManager::loadItemsWithDialog
static ItemList< ItemType > loadItemsWithDialog(Item *parentItem, bool doAddtion=true, Item *nextItem=nullptr)
Definition: ItemManager.h:261
cnoid::ItemCreationPanel
Definition: ItemManager.h:25
cnoid::ItemFileIO
Definition: ItemFileIO.h:19
cnoid::ItemManager::IoUsageType
IoUsageType
Definition: ItemManager.h:90
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::ItemManager::addLoader
ItemManager & addLoader(const std::string &caption, const std::string &format, const std::string &extensions, typename FileFunction< ItemType >::Function function, int usage=Standard)
Definition: ItemManager.h:173
cnoid::ItemManager::saveItemWithDialog
static bool saveItemWithDialog(ItemType *item)
Definition: ItemManager.h:268
cnoid::ItemManager::addSaver
ItemManager & addSaver(const std::string &caption, const std::string &format, std::function< std::string()> getExtensions, typename FileFunction< ItemType >::Function function, int usage=Standard)
Definition: ItemManager.h:203
cnoid::ItemManager::registerClass
ItemManager & registerClass(const std::string &className, ItemType *singletonInstance)
This function registers a singleton item class.
Definition: ItemManager.h:122
cnoid::ItemManager::FileFunction
Definition: ItemManager.h:102
cnoid::ItemManager::addFileIO
ItemManager & addFileIO(ItemFileIO *fileIO)
Definition: ItemManager.h:151
cnoid::ItemManager::addSaver
ItemManager & addSaver(const std::string &caption, const std::string &format, const std::string &extensions, typename FileFunction< ItemType >::Function function, int usage=Standard)
Definition: ItemManager.h:193
cnoid::getOpenFileNames
vector< string > getOpenFileNames(const string &caption, const string &extensions)
Definition: ItemManager.cpp:1179
cnoid::ItemManager::FileFunctionBase::~FileFunctionBase
virtual ~FileFunctionBase()
Definition: ItemManager.h:84
cnoid::ItemManager::FileFunction::FileFunction
FileFunction(Function function)
Definition: ItemManager.h:106
cnoid::ItemManager::registerAddon
void registerAddon(const std::string &name, std::function< ItemAddon *(void)> factory=nullptr)
Definition: ItemManager.h:279
cnoid::MenuManager
Definition: MenuManager.h:14
cnoid::ItemManager::addLoaderAndSaver
ItemManager & addLoaderAndSaver(const std::string &caption, const std::string &format, std::function< std::string()> getExtensions, typename FileFunction< ItemType >::Function loaderFunction, typename FileFunction< ItemType >::Function saverFunction, int usage=Standard)
Definition: ItemManager.h:225
cnoid::ItemAddon
Definition: ItemAddon.h:13
cnoid::ItemManager::singletonInstance
static ItemType * singletonInstance()
Definition: ItemManager.h:141
cnoid::ItemManager::addAlias
ItemManager & addAlias(const std::string &className, const std::string &moduleName)
Definition: ItemManager.h:134
cnoid::ItemManager::addLoaderAndSaver
ItemManager & addLoaderAndSaver(const std::string &caption, const std::string &format, const std::string &extensions, typename FileFunction< ItemType >::Function loaderFunction, typename FileFunction< ItemType >::Function saverFunction, int usage=Standard)
Definition: ItemManager.h:213
cnoid::ItemManager::registerAbstractClass
ItemManager & registerAbstractClass()
Definition: ItemManager.h:128
cnoid::ItemManager::registerFileIO
ItemManager & registerFileIO(ItemFileIO *fileIO)
Definition: ItemManager.h:159
cnoid::ItemManager::createItemWithDialog
static ItemType * createItemWithDialog(Item *parentItem, bool doAddition=true, Item *nextItem=nullptr, Item *protoItem=nullptr, const std::string &title=std::string())
Definition: ItemManager.h:251
cnoid::ItemManager::addCreationPanel
ItemManager & addCreationPanel(ItemCreationPanel *panel=nullptr)
Definition: ItemManager.h:145
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::ItemList
Definition: Item.h:20
cnoid::ItemManager::addLoader
ItemManager & addLoader(const std::string &caption, const std::string &format, std::function< std::string()> getExtensions, typename FileFunction< ItemType >::Function function, int usage=Standard)
Definition: ItemManager.h:183
cnoid::ItemCreationPanelBase::updateItem
virtual bool updateItem(ItemType *protoItem, Item *parentItem)=0
ItemList.h
cnoid::ItemManager::FileFunction::operator()
virtual bool operator()(Item *item, const std::string &filename, std::ostream &os, Item *parentItem)
Definition: ItemManager.h:107
cnoid::ItemCreationPanelBase::initializeCreation
virtual bool initializeCreation(ItemType *protoItem, Item *parentItem)=0
cnoid::ItemCreationPanelBase::ItemCreationPanelBase
ItemCreationPanelBase()
Definition: ItemManager.h:37
cnoid::ItemManager::FileFunction::Function
std::function< bool(ItemType *item, const std::string &filename, std::ostream &os, Item *parentItem)> Function
Definition: ItemManager.h:105
cnoid::ItemManager::getFileIOs
static std::vector< ItemFileIO * > getFileIOs()
Definition: ItemManager.h:164
cnoid::ItemManager::FileFunctionBase
Definition: ItemManager.h:81
cnoid::ItemManager::registerClass
ItemManager & registerClass(const std::string &className)
Definition: ItemManager.h:115
cnoid::ItemCreationPanelBase
Definition: ItemManager.h:34
cnoid::ItemManager
Definition: ItemManager.h:59
ExtensionManager.h
cnoid::DefaultItemCreationPanel
Definition: ItemManager.h:50
cnoid::getOpenFileName
string getOpenFileName(const string &caption, const string &extensions)
Definition: ItemManager.cpp:1164