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