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){
303 [[deprecated(
"Use Item::sigDisconnectedFromRoot.")]]
307 SignalProxy<void(
int checkId,
bool on)> sigAnyCheckToggled();
308 SignalProxy<void(
bool on)> sigCheckToggled(
int checkId = PrimaryCheck);
310 virtual void notifyUpdate();
316 return static_cast<AddonType*
>(findAddon_(
typeid(AddonType)));
319 return static_cast<AddonType*
>(getAddon_(
typeid(AddonType)));
321 std::vector<ItemAddon*> addons();
332 const std::string& filename,
const std::string& format = std::string(),
333 const Mapping* options =
nullptr);
340 const std::string& filename,
Item* parent,
const std::string& format = std::string(),
341 const Mapping* options =
nullptr);
351 bool save(
const std::string& filename,
const std::string& format = std::string(),
352 const Mapping* options =
nullptr);
358 bool overwrite(
bool forceOverwrite =
false,
const std::string& format = std::string());
361 const std::string& filePath()
const;
363 std::string fileName()
const;
364 const std::string& fileFormat()
const;
365 const Mapping* fileOptions()
const;
366 std::time_t fileModificationTime()
const;
367 bool isConsistentWithFile()
const;
369 void updateFileInformation(
const std::string& filename,
const std::string& format,
Mapping* options =
nullptr);
370 void setConsistentWithFile(
bool isConsistent);
371 void suggestFileUpdate();
374 void clearFileInformation();
377 bool replace(
Item* originalItem);
378 Item* findOriginalItem()
const;
379 Item* findReplacementItem()
const;
381 #ifdef CNOID_BACKWARD_COMPATIBILITY
382 const std::string& lastAccessedFilePath()
const;
383 const std::string& lastAccessedFileFormatId()
const;
388 virtual bool store(
Archive& archive);
389 virtual bool restore(
const Archive& archive);
394 virtual void doAssign(
Item* srcItem);
397 virtual Item* doDuplicate()
const;
405 virtual Item* doDuplicate(
Item* duplicatedParentItem)
const;
407 virtual bool onNewPositionCheck(
bool isManualOperation, std::function<
void()>& out_callbackWhenAdded);
408 virtual void onAddedToParent();
417 virtual void onPositionChanged();
423 virtual void onConnectedToRoot();
425 virtual void onRemovedFromParent(
Item* parentItem,
bool isParentBeingDeleted);
426 virtual void onDisconnectedFromRoot();
433 virtual bool onChildItemAboutToBeAdded(
Item* childItem,
bool isManualOperation);
447 mutable int classId_;
457 static Item* find(
const std::string& path,
const std::function<
bool(
Item* item)>& pred);
459 const std::string& path, std::function<
bool(
Item* item)> pred,
bool isRecursive)
const;
460 ItemList<Item> getDescendantItems(std::function<
bool(
Item* item)> pred,
bool isRecursive)
const;
461 void validateClassId()
const;
462 ItemAddon* findAddon_(
const std::type_info& type);
463 ItemAddon* getAddon_(
const std::type_info& type);
466 #ifndef CNOID_BASE_MVOUT_DECLARED
467 #define CNOID_BASE_MVOUT_DECLARED
468 CNOID_EXPORT std::ostream&
mvout(
bool doFlush =
false);