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 ItemFileDialog;
17 class Mapping;
18 
19 class CNOID_EXPORT ItemFileIO : public Referenced
20 {
21 public:
22  enum API {
23  Load = 1 << 0,
24  Options = 1 << 1,
25  OptionPanelForLoading = 1 << 2,
26  Save = 1 << 3,
27  OptionPanelForSaving = 1 << 4,
28  };
29  enum InterfaceLevel { Standard, Conversion, Internal };
30  enum InvocationType { Direct, Dialog, DragAndDrop };
31  class Impl;
32 
33  ~ItemFileIO();
34 
35  bool isFormat(const std::string& format) const;
36  int api() const;
37  bool hasApi(int api) const;
38  const std::string& caption() const;
39  const std::string& fileTypeCaption() const;
40  void addFormatAlias(const std::string& format);
41  [[deprecated("Use addFormatAlias")]]
42  void addFormatIdAlias(const std::string& format){
43  addFormatAlias(format);
44  }
45 
46  // deprecated. This is internally used for specifing SceneItem's extensions dynamically.
47  // The dynamic extension specification should be achieved by a signal to update the
48  // extensions and usual the registerExtensions function.
49  void setExtensionFunction(std::function<std::string()> func);
50 
51  std::vector<std::string> extensions() const;
52 
53  int interfaceLevel() const;
54 
55  // Set the invocation type before calling the loadItem or saveItem function
56  // if the invocation type is not "Direct".
57  void setCurrentInvocationType(int type);
58 
59  // Load API
60  Item* loadItem(
61  const std::string& filename,
62  Item* parentItem = nullptr, bool doAddition = true, Item* nextItem = nullptr,
63  const Mapping* options = nullptr);
64 
65  bool loadItem(
66  Item* item, const std::string& filename,
67  Item* parentItem = nullptr, bool doAddition = true, Item* nextItem = nullptr,
68  const Mapping* options = nullptr);
69 
70  // Save API
71  bool saveItem(Item* item, const std::string& filename, const Mapping* options = nullptr);
72 
73  // Options API
74  virtual void resetOptions();
75  virtual void storeOptions(Mapping* options);
76  virtual bool restoreOptions(const Mapping* options);
77 
78  // OptionPanelForLoading API
79  virtual QWidget* getOptionPanelForLoading();
80  virtual void fetchOptionPanelForLoading();
81 
82  // OptionPanelForPostLoading API (pending)
83  //virtual QWidget* getOptionPanelForPostLoading(Item* item);
84  //virtual void fetchOptionPanelForPostLoading(Item* item);
85  //virtual bool doPostProcessForLoading(Item* item);
86 
87  // OptionPanelForSaving API
88  virtual QWidget* getOptionPanelForSaving(Item* item);
89  virtual void fetchOptionPanelForSaving();
90 
91  Item* parentItem();
92  int currentInvocationType() const;
93 
94  bool isRegisteredForSingletonItem() const;
95  Item* findSingletonItemInstance() const;
96 
97  void setItemClassInfo(Referenced* info);
98  const Referenced* itemClassInfo() const;
99 
100  static std::vector<std::string> separateExtensions(const std::string& multiExtString);
101 
102 protected:
103  ItemFileIO(const std::string& format, int api);
104  ItemFileIO(const ItemFileIO& org);
105  ItemFileIO();
106  void copyFrom(const ItemFileIO& org);
107 
108  void setApi(int apiSet);
109  void setCaption(const std::string& caption);
110  void setFileTypeCaption(const std::string& caption);
111  void setExtension(const std::string& extension);
112  void setExtensions(const std::vector<std::string>& extensions);
113  void setInterfaceLevel(InterfaceLevel level);
114 
115  // Load API
116  virtual bool load(Item* item, const std::string& filename);
117  virtual Item* createItem();
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;
136  friend class ItemFileDialog;
137 };
138 
140 
141 template<class ItemType>
143 {
144 public:
145  ItemFileIoBase(const std::string& format, int api)
146  : ItemFileIO(format, api) {
147  }
148  virtual Item* createItem() override {
150  return findSingletonItemInstance();
151  }
152  return new ItemType;
153  }
154  virtual bool load(Item* item, const std::string& filename) override final {
155  if(auto derived = dynamic_cast<ItemType*>(item)){
156  return load(derived, filename);
157  }
158  return false;
159  }
160  virtual bool load(ItemType* /* item */, const std::string& /* filename */) {
161  return false;
162  }
163  virtual bool save(Item* item, const std::string& filename) override final {
164  if(auto derived = dynamic_cast<ItemType*>(item)){
165  return save(derived, filename);
166  }
167  return false;
168  }
169  virtual bool save(ItemType* /* item */, const std::string& /* filename */) {
170  return false;
171  }
172  virtual QWidget* getOptionPanelForSaving(Item* item) override final {
173  return getOptionPanelForSaving(static_cast<ItemType*>(item));
174  }
175  virtual QWidget* getOptionPanelForSaving(ItemType* item) {
176  return nullptr;
177  }
178 };
179 
180 }
181 
182 #endif
cnoid::ItemFileIO
Definition: ItemFileIO.h:19
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::ItemFileIoBase
Definition: ItemFileIO.h:142
cnoid::Dialog
Definition: Dialog.h:14
cnoid::ItemFileIO::InterfaceLevel
InterfaceLevel
Definition: ItemFileIO.h:29
cnoid::ItemFileIO::isRegisteredForSingletonItem
bool isRegisteredForSingletonItem() const
Definition: ItemManager.cpp:382
cnoid::ItemFileIO::findSingletonItemInstance
Item * findSingletonItemInstance() const
Definition: ItemManager.cpp:391
cnoid::ItemFileIoBase::getOptionPanelForSaving
virtual QWidget * getOptionPanelForSaving(ItemType *item)
Definition: ItemFileIO.h:175
cnoid::ItemFileIoBase::ItemFileIoBase
ItemFileIoBase(const std::string &format, int api)
Definition: ItemFileIO.h:145
cnoid::ItemFileIoBase::load
virtual bool load(Item *item, const std::string &filename) override final
Definition: ItemFileIO.h:154
cnoid::ItemFileDialog
Definition: ItemFileDialog.h:11
cnoid::ref_ptr
Definition: Referenced.h:103
cnoid::ItemFileIoBase::createItem
virtual Item * createItem() override
Definition: ItemFileIO.h:148
cnoid::ItemFileIO::addFormatIdAlias
void addFormatIdAlias(const std::string &format)
Definition: ItemFileIO.h:42
cnoid::ItemFileIoBase::save
virtual bool save(Item *item, const std::string &filename) override final
Definition: ItemFileIO.h:163
cnoid::ItemFileIoBase::getOptionPanelForSaving
virtual QWidget * getOptionPanelForSaving(Item *item) override final
Definition: ItemFileIO.h:172
cnoid::ItemFileIO::API
API
Definition: ItemFileIO.h:22
cnoid::ItemFileIoBase::load
virtual bool load(ItemType *, const std::string &)
Definition: ItemFileIO.h:160
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::ItemFileIoBase::save
virtual bool save(ItemType *, const std::string &)
Definition: ItemFileIO.h:169
cnoid::ItemFileIO::Standard
@ Standard
Definition: ItemFileIO.h:29
cnoid::Referenced
Definition: Referenced.h:54
ItemList.h
cnoid::ItemFileIOPtr
ref_ptr< ItemFileIO > ItemFileIOPtr
Definition: ItemFileIO.h:139
cnoid::ItemManager
Definition: ItemManager.h:59
cnoid::ItemFileIO::api
int api() const
Definition: ItemFileIO.cpp:129
cnoid::ItemFileIO::InvocationType
InvocationType
Definition: ItemFileIO.h:30