5 #ifndef CNOID_BASE_ITEM_H
6 #define CNOID_BASE_ITEM_H
8 #include <cnoid/Referenced>
9 #include <cnoid/Signal>
13 #include "exportdecl.h"
20 template<
class ItemType = Item>
class ItemList;
33 Item(
const std::string& name);
50 Item& operator=(
const Item& rhs) =
delete;
53 if(classId_ < 0) validateClassId();
57 Item* createNewInstance()
const;
60 void assign(
Item* srcItem);
62 Item* duplicate(
Item* duplicatedParentItem =
nullptr)
const;
65 Item* duplicateSubTree()
const;
67 [[deprecated(
"Use Item::duplicateSubTree.")]]
70 const std::string&
name()
const {
return name_; }
72 virtual bool setName(
const std::string& name);
73 virtual std::string displayName()
const;
74 void setDisplayNameModifier(std::function<std::string(
const Item* item)> modifier);
75 SignalProxy<void(
const std::string& oldName)> sigNameChanged();
77 void notifyNameChange();
79 void setAttribute(Attribute attribute);
80 void unsetAttribute(Attribute attribute);
81 bool hasAttribute(Attribute attribute)
const;
83 bool isSubItem()
const;
84 void setSubItemAttributes();
94 bool isTemporal()
const;
96 void setTemporal(
bool on =
true);
99 void setSelected(
bool on,
bool isCurrent =
false);
100 void setSubTreeItemsSelected(
bool on);
106 enum CheckId { LogicalSumOfAllChecks = -1, PrimaryCheck = 0 };
108 bool isChecked(
int checkId = PrimaryCheck)
const;
109 void setChecked(
bool on);
110 void setChecked(
int checkId,
bool on);
111 int numCheckStates()
const;
114 int countDescendantItems()
const;
127 Item* headItem()
const;
130 bool isConnectedToRoot()
const;
131 Item* localRootItem()
const;
133 bool addChildItem(
Item* item,
bool isManualOperation =
false);
135 [[deprecated(
"Use Item::insertChild(Item* position, Item* item, bool isManualOperation).")]]
136 bool insertChildItem(
Item* item,
Item* nextItem,
bool isManualOperation =
false);
138 bool insertChild(
Item* position,
Item* item,
bool isManualOperation =
false);
145 bool addSubItem(
Item* item);
147 [[deprecated(
"Use Item::setSubItemAttributes and Item::insertChild(Item* position, Item* item, bool isManualOperation).")]]
148 bool insertSubItem(
Item* item,
Item* nextItem);
150 void removeFromParentItem();
152 [[deprecated(
"Use Item::removeFromParentItem.")]]
155 void clearChildren();
159 template<
class ItemType>
161 return [](
Item* item) ->
bool {
return dynamic_cast<ItemType*
>(item); };
163 template<
class ItemType>
165 return [pred](
Item* item){
166 if(
auto casted =
dynamic_cast<ItemType*
>(item)){
167 return pred ? pred(casted) :
true;
177 return find(path,
nullptr);
179 template<
class ItemType>
180 ItemType*
find(
const std::string& path =
"") {
181 return static_cast<ItemType*
>(find(path, getItemPredicate<ItemType>()));
188 return findItem(path,
nullptr,
true);
190 template<
class ItemType>
191 ItemType*
findItem(
const std::string& path)
const {
192 return static_cast<ItemType*
>(
193 findItem(path, getItemPredicate<ItemType>(),
true));
195 template<
class ItemType>
196 ItemType*
findItem(std::function<
bool(ItemType* item)> pred =
nullptr)
const {
197 return static_cast<ItemType*
>(
198 findItem(
"", getItemPredicate<ItemType>(pred),
true));
205 return findItem(path, pred,
false);
207 template<
class ItemType>
208 ItemType*
findChildItem(
const std::string& path, std::function<
bool(ItemType* item)> pred =
nullptr)
const {
209 return static_cast<ItemType*
>(
210 findItem(path, getItemPredicate<ItemType>(pred),
false));
212 template<
class ItemType>
213 ItemType*
findChildItem(std::function<
bool(ItemType* item)> pred =
nullptr)
const {
214 return static_cast<ItemType*
>(
215 findItem(
"", getItemPredicate<ItemType>(pred),
false));
221 [[deprecated(
"Use Item::findChildItem with the pred function")]]
223 return findItem(path, [](
Item* item){
return item->
isSubItem(); },
false);
226 template<
class ItemType>
227 [[deprecated(
"Use Item::findChildItem with the pred function")]]
229 return static_cast<ItemType*
>(
232 getItemPredicate<ItemType>([](ItemType* item){
return item->isSubItem(); }),
236 template <
class ItemType> ItemType*
findOwnerItem(
bool includeSelf =
false)
const {
237 Item* parentItem__ = includeSelf ?
const_cast<Item*
>(
this) : parentItem();
239 if(ItemType* ownerItem =
dynamic_cast<ItemType*
>(parentItem__)){
247 bool isOwnedBy(
Item* item)
const;
249 ItemList<> childItems(std::function<
bool(
Item* item)> pred =
nullptr)
const;
251 template <
class ItemType>
253 return getDescendantItems(getItemPredicate<ItemType>(pred),
false);
256 ItemList<> descendantItems(std::function<
bool(
Item* item)> pred =
nullptr)
const;
258 template <
class ItemType>
260 return getDescendantItems(getItemPredicate<ItemType>(pred),
true);
263 ItemList<> selectedDescendantItems(std::function<
bool(
Item* item)> pred =
nullptr)
const;
265 template <
class ItemType>
267 return selectedDescendantItems(getItemPredicate<ItemType>(pred));
270 bool traverse(std::function<
bool(
Item*)> pred);
272 template<
class ItemType>
273 bool traverse(std::function<
bool(ItemType* item)> pred =
nullptr){
304 [[deprecated(
"Use sigTreePositionChanged")]]
314 [[deprecated(
"Use Item::sigDisconnectedFromRoot.")]]
318 SignalProxy<void(
int checkId,
bool on)> sigAnyCheckToggled();
319 SignalProxy<void(
bool on)> sigCheckToggled(
int checkId = PrimaryCheck);
321 virtual void notifyUpdate();
327 return static_cast<AddonType*
>(findAddon_(
typeid(AddonType)));
330 return static_cast<AddonType*
>(getAddon_(
typeid(AddonType)));
332 std::vector<ItemAddon*> addons();
340 const std::string& filename,
const std::string& format = std::string(),
341 const Mapping* options =
nullptr);
349 const std::string& filename,
Item* parent,
const std::string& format = std::string(),
350 const Mapping* options =
nullptr);
357 bool save(
const std::string& filename,
const std::string& format = std::string(),
358 const Mapping* options =
nullptr);
364 bool overwrite(
bool forceOverwrite =
false,
const std::string& format = std::string());
367 const std::string& filePath()
const;
369 std::string fileName()
const;
370 const std::string& fileFormat()
const;
371 const Mapping* fileOptions()
const;
372 std::time_t fileModificationTime()
const;
373 bool isConsistentWithFile()
const;
375 void updateFileInformation(
const std::string& filename,
const std::string& format,
Mapping* options =
nullptr);
376 void setConsistentWithFile(
bool isConsistent);
377 void suggestFileUpdate();
380 void clearFileInformation();
383 bool replace(
Item* originalItem);
384 Item* findOriginalItem()
const;
385 Item* findReplacementItem()
const;
387 #ifdef CNOID_BACKWARD_COMPATIBILITY
388 const std::string& lastAccessedFilePath()
const;
389 const std::string& lastAccessedFileFormatId()
const;
394 virtual bool store(
Archive& archive);
395 virtual bool restore(
const Archive& archive);
400 virtual void doAssign(
Item* srcItem);
403 virtual Item* doDuplicate()
const;
411 virtual Item* doDuplicate(
Item* duplicatedParentItem)
const;
413 virtual bool onNewTreePositionCheck(
bool isManualOperation, std::function<
void()>& out_callbackWhenAdded);
414 virtual void onAddedToParent();
422 virtual void onTreePathChanged();
428 virtual void onTreePositionChanged();
434 virtual void onPositionChanged();
440 virtual void onConnectedToRoot();
442 virtual void onRemovedFromParent(
Item* parentItem,
bool isParentBeingDeleted);
443 virtual void onDisconnectedFromRoot();
452 virtual bool onChildItemAboutToBeAdded(
Item* childItem,
bool isManualOperation);
465 mutable int classId_;
475 static Item* find(
const std::string& path,
const std::function<
bool(
Item* item)>& pred);
477 const std::string& path, std::function<
bool(
Item* item)> pred,
bool isRecursive)
const;
478 ItemList<Item> getDescendantItems(std::function<
bool(
Item* item)> pred,
bool isRecursive)
const;
479 void validateClassId()
const;
480 ItemAddon* findAddon_(
const std::type_info& type);
481 ItemAddon* getAddon_(
const std::type_info& type);
484 #ifndef CNOID_BASE_MVOUT_DECLARED
485 #define CNOID_BASE_MVOUT_DECLARED
486 CNOID_EXPORT std::ostream&
mvout(
bool doFlush =
true);