5 #ifndef CNOID_BASE_ITEM_MANAGER_H
6 #define CNOID_BASE_ITEM_MANAGER_H
15 #include "exportdecl.h"
30 virtual bool initializePanel(
Item* protoItem,
Item* parentItem);
31 virtual bool initializeItem(
Item* protoItem,
Item* parentItem);
34 virtual bool initializePanel(
Item* protoItem);
36 virtual bool initializeItem(
Item* protoItem);
48 virtual bool initializePanel(
Item* protoItem,
Item* parentItem)
override;
49 virtual bool initializeItem(
Item* protoItem,
Item* parentItem)
override;
59 void detachAllManagedTypeItemsFromRoot();
62 template <
class ItemType>
class Factory {
64 virtual Item* operator()() {
return new ItemType(); }
67 class CreationPanelFilterBase
70 virtual ~CreationPanelFilterBase() { }
71 virtual bool operator()(
Item* protoItem,
Item* parentItem) = 0;
74 class OverwritingCheckFunctionBase
77 ~OverwritingCheckFunctionBase();
78 virtual bool operator()(
Item* item) = 0;
86 virtual bool operator()(
Item* item,
const std::string& filename, std::ostream& os,
Item* parentItem) = 0;
89 void bindTextDomain(
const std::string& domain);
91 enum { PRIORITY_CONVERSION = -10, PRIORITY_COMPATIBILITY = 0, PRIORITY_OPTIONAL = 0, PRIORITY_DEFAULT = 10 };
96 typedef std::function<bool(ItemType* protoItem,
Item* parentItem)>
Function;
99 return function(
static_cast<ItemType*
>(protoItem), parentItem);
108 typedef std::function<bool(ItemType* item,
const std::string& filename, std::ostream& os,
Item* parentItem)>
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);
117 template <
class ItemType,
class SuperItemType = Item>
119 registerClass_(className,
typeid(ItemType),
typeid(SuperItemType), Factory<ItemType>(),
nullptr);
124 template <
class ItemType,
class SuperItemType = Item>
126 registerClass_(className,
typeid(ItemType),
typeid(SuperItemType),
nullptr, singletonInstance);
130 template <
class ItemType,
class SuperItemType = Item>
132 registerClass_(
"",
typeid(ItemType),
typeid(SuperItemType),
nullptr,
nullptr);
136 template <
class ItemType>
138 addAlias_(
typeid(ItemType), className, moduleName);
142 static bool getClassIdentifier(
Item* item, std::string& out_moduleName, std::string& out_className);
145 return static_cast<ItemType*
>(getSingletonInstance(
typeid(ItemType)));
149 addCreationPanel_(
typeid(ItemType), panel);
153 template <
class ItemType>
155 addCreationPanelFilter_(
161 template <
class ItemType>
163 addCreationPanelFilter_(
169 template <
class ItemType>
171 registerFileIO_(
typeid(ItemType), fileIO);
175 template <
class ItemType>
177 return getFileIOs(
typeid(ItemType));
180 static std::vector<ItemFileIO*> getFileIOs(
const std::type_info& type);
182 static ItemFileIO* findFileIO(
const std::type_info& type,
const std::string& formatId);
184 template <
class ItemType>
186 const std::string& caption,
const std::string& formatId,
const std::string& extensions,
188 addLoader_(
typeid(ItemType), caption, formatId, [extensions](){
return extensions; },
189 std::make_shared<FileFunction<ItemType>>(
function), priority);
193 template <
class ItemType>
195 const std::string& caption,
const std::string& formatId, std::function<std::string()> getExtensions,
197 addLoader_(
typeid(ItemType), caption, formatId, getExtensions,
202 template<
class ItemType>
204 const std::string& caption,
const std::string& formatId,
const std::string& extensions,
206 addSaver_(
typeid(ItemType), caption, formatId, [extensions](){
return extensions; },
207 std::make_shared<FileFunction<ItemType>>(
function), priority);
211 template<
class ItemType>
213 const std::string& caption,
const std::string& formatId, std::function<std::string()> getExtensions,
215 addSaver_(
typeid(ItemType), caption, formatId, getExtensions,
220 template<
class ItemType>
222 const std::string& extensions,
225 int priority = PRIORITY_DEFAULT){
226 addLoader<ItemType>(caption, formatId, extensions, loadingFunction, priority);
227 addSaver<ItemType>(caption, formatId, extensions, savingFunction, priority);
231 template<
class ItemType>
233 std::function<std::string()> getExtensions,
236 int priority = PRIORITY_DEFAULT){
237 addLoader<ItemType>(caption, formatId, getExtensions, loadingFunction, priority);
238 addSaver<ItemType>(caption, formatId, getExtensions, savingFunction, priority);
242 void addMenuItemToImport(
const std::string& caption, std::function<
void()> slot);
244 static Item* createItem(
const std::string& moduleName,
const std::string& itemClassName);
245 static Item* createItem(
int itemClassId);
256 template <
class ItemType>
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));
265 template <
class ItemType>
267 Item* parentItem,
bool doAddtion =
true,
Item* nextItem =
nullptr){
268 return loadItemsWithDialog_(
typeid(ItemType), parentItem, doAddtion, nextItem);
271 template <
class ItemType>
273 return saveItemWithDialog_(
typeid(ItemType), item);
276 [[deprecated(
"Use Item::reload().")]]
277 static void reloadItems(
const ItemList<>& items);
279 [[deprecated(
"Use Item::findOriginalItem().")]]
280 static Item* findOriginalItemForReloadedItem(
Item* item);
282 template<
class AddonType>
284 registerAddon_(
typeid(AddonType), name, factory ? factory : [](){
return new AddonType; });
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);
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);
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);
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);
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);
308 static Item* getSingletonInstance(
const std::type_info& type);
310 static Item* createItemWithDialog_(
311 const std::type_info& type,
Item* parentItem,
bool doAddition,
Item* nextItem,
312 Item* protoItem,
const std::string& title);
314 const std::type_info& type,
Item* parentItem,
bool doAddtion,
Item* nextItem);
315 static bool saveItemWithDialog_(
const std::type_info& type,
Item* item);
319 Item* item,
const std::string& filename,
Item* parentItem,
const std::string& formatId,
320 const Mapping* options =
nullptr);
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);
326 const std::type_info& type,
const std::string& name,
const std::function<
ItemAddon*(
void)>& factory);
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);