Choreonoid  1.8
ItemFileIO.h
Go to the documentation of this file.
1 #ifndef CNOID_BASE_ITEM_FILE_IO_H
2 #define CNOID_BASE_ITEM_FILE_IO_H
3 
4 #include "ItemList.h"
5 #include <cnoid/Referenced>
6 #include <string>
7 #include <vector>
8 #include "exportdecl.h"
9 
10 class QWidget;
11 
12 namespace cnoid {
13 
14 class Item;
15 class ItemManager;
16 class ItemFileIOExtenderBase;
17 class ItemFileDialog;
18 class Mapping;
19 
20 class CNOID_EXPORT ItemFileIO : public Referenced
21 {
22 public:
23  enum API {
24  Load = 1 << 0,
25  Options = 1 << 1,
26  OptionPanelForLoading = 1 << 2,
27  Save = 1 << 3,
28  OptionPanelForSaving = 1 << 4,
29  };
30  enum InterfaceLevel { Standard, Conversion, Internal };
31  enum InvocationType { Direct, Dialog, DragAndDrop };
32  class Impl;
33 
34 public:
35  ~ItemFileIO();
36 
37  bool isFormat(const std::string& id) const;
38  int api() const;
39  void setApi(int apiSet);
40  bool hasApi(int api) const;
41  void setCaption(const std::string& caption);
42  const std::string& caption() const;
43  void setFileTypeCaption(const std::string& caption);
44  const std::string& fileTypeCaption() const;
45  void addFormatIdAlias(const std::string& formatId);
46  void addExtensions(const std::vector<std::string>& extensions);
47 
48  // deprecated. This is internally used for specifing SceneItem's extensions dynamically.
49  // The dynamic extension specification should be achieved by a signal to update the
50  // extensions and usual the registerExtensions function.
51  void setExtensionFunction(std::function<std::string()> func);
52 
53  std::vector<std::string> extensions() const;
54 
55  void setInterfaceLevel(InterfaceLevel level);
56  int interfaceLevel() const;
57 
58  virtual Item* createItem() = 0;
59 
60  // Set the invocation type before calling the loadItem or saveItem function
61  // if the invocation type is not "Direct".
62  void setInvocationType(int type);
63 
64  // Load API
65  Item* loadItem(
66  const std::string& filename,
67  Item* parentItem = nullptr, bool doAddition = true, Item* nextItem = nullptr,
68  const Mapping* options = nullptr);
69 
70  bool loadItem(
71  Item* item, const std::string& filename,
72  Item* parentItem = nullptr, bool doAddition = true, Item* nextItem = nullptr,
73  const Mapping* options = nullptr);
74 
75  // Save API
76  bool saveItem(Item* item, const std::string& filename, const Mapping* options = nullptr);
77 
78  // Options API
79  virtual void resetOptions();
80  virtual void storeOptions(Mapping* options);
81  virtual bool restoreOptions(const Mapping* options);
82 
83  // OptionPanelForLoading API
84  virtual QWidget* getOptionPanelForLoading();
85  virtual void fetchOptionPanelForLoading();
86 
87  // OptionPanelForPostLoading API (pending)
88  //virtual QWidget* getOptionPanelForPostLoading(Item* item);
89  //virtual void fetchOptionPanelForPostLoading(Item* item);
90  //virtual bool doPostProcessForLoading(Item* item);
91 
92  // OptionPanelForSaving API
93  virtual QWidget* getOptionPanelForSaving(Item* item);
94  virtual void fetchOptionPanelForSaving();
95 
96  Item* parentItem();
97  int invocationType() const;
98 
99  bool isRegisteredForSingletonItem() const;
100  Item* findSingletonItemInstance() const;
101 
102  void setItemClassInfo(Referenced* info);
103  const Referenced* itemClassInfo() const;
104 
105  static std::vector<std::string> separateExtensions(const std::string& multiExtString);
106 
107 protected:
108  ItemFileIO(const std::string& formatId, int api);
109  ItemFileIO(const ItemFileIO& org);
110  ItemFileIO();
111  void copyFrom(const ItemFileIO& org);
112 
113  void setExtension(const std::string& extension);
114  void setExtensions(const std::vector<std::string>& extensions);
115 
116  // Load API
117  virtual bool load(Item* item, const std::string& filename);
118 
119  // Save API
120  virtual bool save(Item* item, const std::string& filename);
121 
122  std::ostream& os();
123  void putWarning(const std::string& message);
124  void putError(const std::string& message);
125 
131  void setActuallyLoadedItem(Item* item);
132 
133 private:
134  Impl* impl;
135  friend class ItemManager;
137  friend class ItemFileDialog;
138 };
139 
141 
142 template<class ItemType>
144 {
145 public:
146  ItemFileIoBase(const std::string& formatId, int api)
147  : ItemFileIO(formatId, api) {
148  }
149  virtual Item* createItem() override {
150  return new ItemType;
151  }
152  virtual bool load(Item* item, const std::string& filename) override final {
153  if(auto derived = dynamic_cast<ItemType*>(item)){
154  return load(derived, filename);
155  }
156  return false;
157  }
158  virtual bool load(ItemType* /* item */, const std::string& /* filename */) {
159  return false;
160  }
161  virtual bool save(Item* item, const std::string& filename) override final {
162  if(auto derived = dynamic_cast<ItemType*>(item)){
163  return save(derived, filename);
164  }
165  return false;
166  }
167  virtual bool save(ItemType* /* item */, const std::string& /* filename */) {
168  return false;
169  }
170  virtual QWidget* getOptionPanelForSaving(Item* item) override final {
171  return getOptionPanelForSaving(static_cast<ItemType*>(item));
172  }
173  virtual QWidget* getOptionPanelForSaving(ItemType* item) {
174  return nullptr;
175  }
176 };
177 
178 
179 class CNOID_EXPORT ItemFileIoExtenderBase : public ItemFileIO
180 {
181  ItemFileIOPtr baseFileIO;
182 
183 protected:
184  ItemFileIoExtenderBase(const std::type_info& type, const std::string& formatId);
185 
186 public:
187  bool isAvailable() const;
188  virtual bool load(Item* item, const std::string& filename) override final;
189  virtual void resetOptions() override;
190  virtual void storeOptions(Mapping* options) override;
191  virtual bool restoreOptions(const Mapping* options) override;
192  virtual QWidget* getOptionPanelForLoading() override;
193  virtual void fetchOptionPanelForLoading() override;
194  virtual bool save(Item* item, const std::string& filename) override final;
195  virtual QWidget* getOptionPanelForSaving(Item* item) override final;
196  virtual void fetchOptionPanelForSaving() override;
197 };
198 
199 
200 template<class ItemType, class BaseItemType = ItemType>
202 {
203 public:
204  ItemFileIoExtender(const std::string& formatId = "")
205  : ItemFileIoExtenderBase(typeid(BaseItemType), formatId)
206  { }
207 
208  ItemType* loadItem(
209  const std::string& filename,
210  Item* parentItem = nullptr, bool doAddition = true, Item* nextItem = nullptr,
211  const Mapping* options = nullptr) {
212  return static_cast<ItemType*>(
213  ItemFileIO::loadItem(filename, parentItem, doAddition, nextItem, options));
214  }
215 
216 protected:
217  virtual Item* createItem() override {
218  return new ItemType;
219  }
220  virtual bool load(ItemType* item, const std::string& filename) {
221  return ItemFileIoExtenderBase::load(item, filename);
222  }
223  virtual bool save(ItemType* item, const std::string& filename) {
224  return ItemFileIoExtenderBase::save(item, filename);
225  }
226  virtual QWidget* getOptionPanelForSaving(ItemType* item) {
228  }
229 };
230 
231 }
232 
233 #endif
cnoid::ItemFileIO
Definition: ItemFileIO.h:20
cnoid::ItemFileIoExtender::getOptionPanelForSaving
virtual QWidget * getOptionPanelForSaving(ItemType *item)
Definition: ItemFileIO.h:226
cnoid::ItemFileIoExtenderBase::save
virtual bool save(Item *item, const std::string &filename) override final
Definition: ItemFileIO.cpp:586
cnoid::Mapping
Definition: ValueTree.h:251
cnoid::ItemFileIoBase
Definition: ItemFileIO.h:143
cnoid::Dialog
Definition: Dialog.h:14
cnoid::ItemFileIO::InterfaceLevel
InterfaceLevel
Definition: ItemFileIO.h:30
cnoid::ItemFileIoExtender::ItemFileIoExtender
ItemFileIoExtender(const std::string &formatId="")
Definition: ItemFileIO.h:204
cnoid::ItemFileIoBase::getOptionPanelForSaving
virtual QWidget * getOptionPanelForSaving(ItemType *item)
Definition: ItemFileIO.h:173
cnoid::ItemFileIoExtenderBase::load
virtual bool load(Item *item, const std::string &filename) override final
Definition: ItemFileIO.cpp:530
cnoid::ItemFileIoExtenderBase
Definition: ItemFileIO.h:179
cnoid::ItemFileIoExtenderBase::getOptionPanelForSaving
virtual QWidget * getOptionPanelForSaving(Item *item) override final
Definition: ItemFileIO.cpp:572
cnoid::ItemFileIoBase::load
virtual bool load(Item *item, const std::string &filename) override final
Definition: ItemFileIO.h:152
cnoid::ItemFileDialog
Definition: ItemFileDialog.h:11
cnoid::ItemFileIoExtender::save
virtual bool save(ItemType *item, const std::string &filename)
Definition: ItemFileIO.h:223
cnoid::ref_ptr< ItemFileIO >
cnoid::ItemFileIoBase::createItem
virtual Item * createItem() override
Definition: ItemFileIO.h:149
cnoid::ItemFileIoBase::save
virtual bool save(Item *item, const std::string &filename) override final
Definition: ItemFileIO.h:161
cnoid::ItemFileIoBase::getOptionPanelForSaving
virtual QWidget * getOptionPanelForSaving(Item *item) override final
Definition: ItemFileIO.h:170
cnoid::ItemFileIO::API
API
Definition: ItemFileIO.h:23
cnoid::ItemFileIoBase::load
virtual bool load(ItemType *, const std::string &)
Definition: ItemFileIO.h:158
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::ItemFileIoBase::ItemFileIoBase
ItemFileIoBase(const std::string &formatId, int api)
Definition: ItemFileIO.h:146
cnoid::ItemFileIoBase::save
virtual bool save(ItemType *, const std::string &)
Definition: ItemFileIO.h:167
cnoid::ItemFileIO::Standard
@ Standard
Definition: ItemFileIO.h:30
cnoid::Referenced
Definition: Referenced.h:54
ItemList.h
cnoid::ItemFileIO::parentItem
Item * parentItem()
Definition: ItemFileIO.cpp:473
cnoid::ItemFileIoExtender
Definition: ItemFileIO.h:201
cnoid::ItemFileIoExtender::load
virtual bool load(ItemType *item, const std::string &filename)
Definition: ItemFileIO.h:220
cnoid::ItemFileIoExtender::loadItem
ItemType * loadItem(const std::string &filename, Item *parentItem=nullptr, bool doAddition=true, Item *nextItem=nullptr, const Mapping *options=nullptr)
Definition: ItemFileIO.h:208
cnoid::ItemFileIOPtr
ref_ptr< ItemFileIO > ItemFileIOPtr
Definition: ItemFileIO.h:140
cnoid::ItemManager
Definition: ItemManager.h:53
cnoid::ItemFileIO::api
int api() const
Definition: ItemFileIO.cpp:131
cnoid::ItemFileIO::InvocationType
InvocationType
Definition: ItemFileIO.h:31
cnoid::ItemFileIO::loadItem
Item * loadItem(const std::string &filename, Item *parentItem=nullptr, bool doAddition=true, Item *nextItem=nullptr, const Mapping *options=nullptr)
Definition: ItemFileIO.cpp:275
cnoid::ItemFileIoExtender::createItem
virtual Item * createItem() override
Definition: ItemFileIO.h:217