Choreonoid  1.8
Item.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_BASE_ITEM_H
6 #define CNOID_BASE_ITEM_H
7 
8 #include <cnoid/Referenced>
9 #include <cnoid/Signal>
10 #include <string>
11 #include <vector>
12 #include <ctime>
13 #include "exportdecl.h"
14 
15 namespace cnoid {
16 
17 class Item;
18 typedef ref_ptr<Item> ItemPtr;
19 
20 template<class ItemType = Item> class ItemList;
21 
22 class ItemAddon;
23 class RootItem;
24 class Archive;
25 class Mapping;
26 class ExtensionManager;
28 
29 class CNOID_EXPORT Item : public Referenced
30 {
31 protected:
32  Item();
33  Item(const std::string& name);
34  Item(const Item& item);
35 
36 public:
37  enum Attribute {
43  SUB_ITEM = SubItem,
44  TEMPORAL = Temporal,
45  LOAD_ONLY = LoadOnly
46  };
47 
48  virtual ~Item();
49 
50  Item& operator=(const Item& rhs) = delete;
51 
52  int classId() const {
53  if(classId_ < 0) validateClassId();
54  return classId_;
55  }
56 
57  Item* createNewInstance() const;
58 
60  void assign(Item* srcItem);
61 
62  Item* duplicate(Item* duplicatedParentItem = nullptr) const;
63 
65  Item* duplicateSubTree() const;
66 
67  [[deprecated("Use Item::duplicateSubTree.")]]
68  Item* duplicateAll() const { return duplicateSubTree(); }
69 
70  const std::string& name() const { return name_; }
71  // Return true if the name is successfully updated or the item originally has the same 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();
76  // This is used to notify the system of a displayName change
77  void notifyNameChange();
78 
79  void setAttribute(Attribute attribute);
80  void unsetAttribute(Attribute attribute);
81  bool hasAttribute(Attribute attribute) const;
82 
83  bool isSubItem() const;
84  void setSubItemAttributes();
85 
94  bool isTemporal() const;
95 
96  void setTemporal(bool on = true);
97 
98  bool isSelected() const { return isSelected_; }
99  void setSelected(bool on, bool isCurrent = false);
100  void setSubTreeItemsSelected(bool on);
101 
106  enum CheckId { LogicalSumOfAllChecks = -1, PrimaryCheck = 0 };
107 
108  bool isChecked(int checkId = PrimaryCheck) const;
109  void setChecked(bool on); // for PrimaryCheck
110  void setChecked(int checkId, bool on);
111  int numCheckStates() const;
112 
113  int numChildren() const { return numChildren_; }
114  int countDescendantItems() const;
115 
116  Item* childItem() const { return firstChild_; }
117  Item* prevItem() const { return prevItem_; }
118  Item* nextItem() const { return nextItem_; }
119  Item* lastChildItem() const { return lastChild_; }
120  Item* parentItem() const { return parent_; }
121 
127  Item* headItem() const;
128 
129  RootItem* findRootItem() const;
130  bool isConnectedToRoot() const;
131  Item* localRootItem() const;
132 
133  bool addChildItem(Item* item, bool isManualOperation = false);
134 
135  [[deprecated("Use Item::insertChild(Item* position, Item* item, bool isManualOperation).")]]
136  bool insertChildItem(Item* item, Item* nextItem, bool isManualOperation = false);
137 
138  bool insertChild(Item* position, Item* item, bool isManualOperation = false);
139 
145  bool addSubItem(Item* item);
146 
147  [[deprecated("Use Item::setSubItemAttributes and Item::insertChild(Item* position, Item* item, bool isManualOperation).")]]
148  bool insertSubItem(Item* item, Item* nextItem);
149 
150  void removeFromParentItem();
151 
152  [[deprecated("Use Item::removeFromParentItem.")]]
153  void detachFromParentItem() { removeFromParentItem(); }
154 
155  void clearChildren();
156 
157  typedef std::function<bool(Item* item)> ItemPredicate;
158 
159  template<class ItemType>
161  return [](Item* item) -> bool { return dynamic_cast<ItemType*>(item); };
162  }
163  template<class ItemType>
164  static ItemPredicate getItemPredicate(std::function<bool(ItemType* item)> pred) {
165  return [pred](Item* item){
166  if(auto casted = dynamic_cast<ItemType*>(item)){
167  return pred ? pred(casted) : true;
168  }
169  return false;
170  };
171  }
172 
176  static Item* find(const std::string& path) {
177  return find(path, nullptr);
178  }
179  template<class ItemType>
180  ItemType* find(const std::string& path = "") {
181  return static_cast<ItemType*>(find(path, getItemPredicate<ItemType>()));
182  }
183 
187  Item* findItem(const std::string& path) const {
188  return findItem(path, nullptr, true);
189  }
190  template<class ItemType>
191  ItemType* findItem(const std::string& path) const {
192  return static_cast<ItemType*>(
193  findItem(path, getItemPredicate<ItemType>(), true));
194  }
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));
199  }
200 
204  Item* findChildItem(const std::string& path, std::function<bool(Item* item)> pred = nullptr) const {
205  return findItem(path, pred, false);
206  }
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));
211  }
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));
216  }
217 
221  [[deprecated("Use Item::findChildItem with the pred function")]]
222  Item* findSubItem(const std::string& path) const {
223  return findItem(path, [](Item* item){ return item->isSubItem(); }, false);
224  }
225 
226  template<class ItemType>
227  [[deprecated("Use Item::findChildItem with the pred function")]]
228  ItemType* findSubItem(const std::string& path = "") const {
229  return static_cast<ItemType*>(
230  findItem(
231  path,
232  getItemPredicate<ItemType>([](ItemType* item){ return item->isSubItem(); }),
233  false));
234  }
235 
236  template <class ItemType> ItemType* findOwnerItem(bool includeSelf = false) const {
237  Item* parentItem__ = includeSelf ? const_cast<Item*>(this) : parentItem();
238  while(parentItem__){
239  if(ItemType* ownerItem = dynamic_cast<ItemType*>(parentItem__)){
240  return ownerItem;
241  }
242  parentItem__ = parentItem__->parentItem();
243  }
244  return nullptr;
245  }
246 
247  bool isOwnedBy(Item* item) const;
248 
249  ItemList<> childItems(std::function<bool(Item* item)> pred = nullptr) const;
250 
251  template <class ItemType>
252  ItemList<ItemType> childItems(std::function<bool(ItemType* item)> pred = nullptr) const {
253  return getDescendantItems(getItemPredicate<ItemType>(pred), false);
254  }
255 
256  ItemList<> descendantItems(std::function<bool(Item* item)> pred = nullptr) const;
257 
258  template <class ItemType>
259  ItemList<ItemType> descendantItems(std::function<bool(ItemType* item)> pred = nullptr) const {
260  return getDescendantItems(getItemPredicate<ItemType>(pred), true);
261  }
262 
263  ItemList<> selectedDescendantItems(std::function<bool(Item* item)> pred = nullptr) const;
264 
265  template <class ItemType>
266  ItemList<ItemType> selectedDescendantItems(std::function<bool(Item* item)> pred = nullptr) const {
267  return selectedDescendantItems(getItemPredicate<ItemType>(pred));
268  }
269 
270  bool traverse(std::function<bool(Item*)> pred);
271 
272  template<class ItemType>
273  bool traverse(std::function<bool(ItemType* item)> pred = nullptr){
274  return Item::traverse(getItemPredicate<ItemType>(pred));
275  }
276 
285  SignalProxy<void()> sigPositionChanged();
286 
296  SignalProxy<void(Item* topItem, Item* prevTopParentItem)> sigPositionChanged2();
297 
298  // This signal that is emitted when the structure of the sub tree changes.
299  SignalProxy<void()> sigSubTreeChanged();
300 
301  SignalProxy<void()> sigDisconnectedFromRoot();
302 
303  [[deprecated("Use Item::sigDisconnectedFromRoot.")]]
304  SignalProxy<void()> sigDetachedFromRoot() { return sigDisconnectedFromRoot(); }
305 
306  SignalProxy<void(bool on)> sigSelectionChanged();
307  SignalProxy<void(int checkId, bool on)> sigAnyCheckToggled();
308  SignalProxy<void(bool on)> sigCheckToggled(int checkId = PrimaryCheck);
309 
310  virtual void notifyUpdate();
311  SignalProxy<void()> sigUpdated();
312 
313  bool setAddon(ItemAddon* addon);
314  void removeAddon(ItemAddon* addon);
315  template<class AddonType> AddonType* findAddon(){
316  return static_cast<AddonType*>(findAddon_(typeid(AddonType)));
317  }
318  template<class AddonType> AddonType* getAddon(){
319  return static_cast<AddonType*>(getAddon_(typeid(AddonType)));
320  }
321  std::vector<ItemAddon*> addons();
322 
331  bool load(
332  const std::string& filename, const std::string& format = std::string(),
333  const Mapping* options = nullptr);
334 
339  bool load(
340  const std::string& filename, Item* parent, const std::string& format = std::string(),
341  const Mapping* options = nullptr);
342 
351  bool save(const std::string& filename, const std::string& format = std::string(),
352  const Mapping* options = nullptr);
353 
358  bool overwrite(bool forceOverwrite = false, const std::string& format = std::string());
359 
360  // full path file name
361  const std::string& filePath() const;
362  // file name without the directory
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;
368 
369  void updateFileInformation(const std::string& filename, const std::string& format, Mapping* options = nullptr);
370  void setConsistentWithFile(bool isConsistent);
371  void suggestFileUpdate();
372 
374  void clearFileInformation();
375 
376  bool reload();
377  bool replace(Item* originalItem);
378  Item* findOriginalItem() const;
379  Item* findReplacementItem() const;
380 
381 #ifdef CNOID_BACKWARD_COMPATIBILITY
382  const std::string& lastAccessedFilePath() const;
383  const std::string& lastAccessedFileFormatId() const;
384 #endif
385 
386  void putProperties(PutPropertyFunction& putProperty);
387 
388  virtual bool store(Archive& archive);
389  virtual bool restore(const Archive& archive);
390 
391 protected:
392 
394  virtual void doAssign(Item* srcItem);
395 
397  virtual Item* doDuplicate() const;
398 
405  virtual Item* doDuplicate(Item* duplicatedParentItem) const;
406 
407  virtual bool onNewPositionCheck(bool isManualOperation, std::function<void()>& out_callbackWhenAdded);
408  virtual void onAddedToParent();
409 
417  virtual void onPositionChanged();
418 
423  virtual void onConnectedToRoot();
424 
425  virtual void onRemovedFromParent(Item* parentItem, bool isParentBeingDeleted);
426  virtual void onDisconnectedFromRoot();
427 
433  virtual bool onChildItemAboutToBeAdded(Item* childItem, bool isManualOperation);
434 
441  virtual void doPutProperties(PutPropertyFunction& putProperty);
442 
443 private:
444  class Impl;
445  Impl* impl;
446 
447  mutable int classId_;
448  Item* parent_;
449  ItemPtr firstChild_;
450  ItemPtr nextItem_;
451  Item* prevItem_;
452  Item* lastChild_;
453  int numChildren_;
454  std::string name_;
455  bool isSelected_;
456 
457  static Item* find(const std::string& path, const std::function<bool(Item* item)>& pred);
458  Item* findItem(
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);
464 };
465 
466 #ifndef CNOID_BASE_MVOUT_DECLARED
467 #define CNOID_BASE_MVOUT_DECLARED
468 CNOID_EXPORT std::ostream& mvout(bool doFlush = false);
469 #endif
470 
471 }
472 
473 #endif
cnoid::Item::detachFromParentItem
void detachFromParentItem()
Definition: Item.h:153
cnoid::Item::ItemPredicate
std::function< bool(Item *item)> ItemPredicate
Definition: Item.h:157
cnoid::Mapping
Definition: ValueTree.h:251
cnoid::Item::getAddon
AddonType * getAddon()
Definition: Item.h:318
cnoid::Item::findItem
Item * findItem(const std::string &path) const
Definition: Item.h:187
cnoid::Item::findSubItem
ItemType * findSubItem(const std::string &path="") const
Definition: Item.h:228
cnoid::mvout
CNOID_EXPORT std::ostream & mvout(bool doFlush=false)
Definition: MessageView.cpp:1086
cnoid::ExtensionManager
Definition: ExtensionManager.h:23
cnoid::Item::findItem
ItemType * findItem(const std::string &path) const
Definition: Item.h:191
cnoid::Item::duplicateAll
Item * duplicateAll() const
Definition: Item.h:68
cnoid::RootItem
Definition: RootItem.h:16
cnoid::PutPropertyFunction
Definition: PutPropertyFunction.h:51
cnoid::Item::findItem
ItemType * findItem(std::function< bool(ItemType *item)> pred=nullptr) const
Definition: Item.h:196
cnoid::Archive
Definition: Archive.h:23
cnoid::Item::lastChildItem
Item * lastChildItem() const
Definition: Item.h:119
cnoid::Item::childItems
ItemList< ItemType > childItems(std::function< bool(ItemType *item)> pred=nullptr) const
Definition: Item.h:252
cnoid::ItemPtr
ref_ptr< Item > ItemPtr
Definition: Item.h:17
cnoid::Item::nextItem
Item * nextItem() const
Definition: Item.h:118
cnoid::Item::Attribute
Attribute
Definition: Item.h:37
cnoid::Item::numChildren
int numChildren() const
Definition: Item.h:113
cnoid::ItemAddon
Definition: ItemAddon.h:13
cnoid::Item::getItemPredicate
static ItemPredicate getItemPredicate()
Definition: Item.h:160
cnoid::Item::NumAttributes
@ NumAttributes
Definition: Item.h:42
cnoid::ref_ptr< Item >
cnoid::Item::traverse
bool traverse(std::function< bool(Item *)> pred)
Definition: Item.cpp:1251
cnoid::Item::findChildItem
Item * findChildItem(const std::string &path, std::function< bool(Item *item)> pred=nullptr) const
Definition: Item.h:204
cnoid::Item::isSelected
bool isSelected() const
Definition: Item.h:98
cnoid::Item::LoadOnly
@ LoadOnly
Definition: Item.h:41
cnoid::Item::find
static Item * find(const std::string &path)
Definition: Item.h:176
cnoid::Item::isSubItem
bool isSubItem() const
Definition: Item.cpp:382
cnoid::Item::findOwnerItem
ItemType * findOwnerItem(bool includeSelf=false) const
Definition: Item.h:236
cnoid::Item::getItemPredicate
static ItemPredicate getItemPredicate(std::function< bool(ItemType *item)> pred)
Definition: Item.h:164
cnoid::Item::findChildItem
ItemType * findChildItem(std::function< bool(ItemType *item)> pred=nullptr) const
Definition: Item.h:213
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::ItemList
Definition: Item.h:20
cnoid::Item::childItem
Item * childItem() const
Definition: Item.h:116
cnoid::Item::find
ItemType * find(const std::string &path="")
Definition: Item.h:180
cnoid::Item::name
const std::string & name() const
Definition: Item.h:70
cnoid::Referenced
Definition: Referenced.h:54
cnoid::Item::traverse
bool traverse(std::function< bool(ItemType *item)> pred=nullptr)
Definition: Item.h:273
cnoid::Item::selectedDescendantItems
ItemList< ItemType > selectedDescendantItems(std::function< bool(Item *item)> pred=nullptr) const
Definition: Item.h:266
cnoid::Item::sigDetachedFromRoot
SignalProxy< void()> sigDetachedFromRoot()
Definition: Item.h:304
cnoid::Item::descendantItems
ItemList< ItemType > descendantItems(std::function< bool(ItemType *item)> pred=nullptr) const
Definition: Item.h:259
cnoid::Item::findAddon
AddonType * findAddon()
Definition: Item.h:315
cnoid::Item::SubItem
@ SubItem
Definition: Item.h:38
cnoid::Item::findChildItem
ItemType * findChildItem(const std::string &path, std::function< bool(ItemType *item)> pred=nullptr) const
Definition: Item.h:208
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::Item::Temporal
@ Temporal
Definition: Item.h:40
cnoid::Item::classId
int classId() const
Definition: Item.h:52
cnoid::Item::parentItem
Item * parentItem() const
Definition: Item.h:120
cnoid::Item::Attached
@ Attached
Definition: Item.h:39
cnoid::Item::findSubItem
Item * findSubItem(const std::string &path) const
Definition: Item.h:222
cnoid::Item::CheckId
CheckId
Definition: Item.h:106
cnoid::Item::prevItem
Item * prevItem() const
Definition: Item.h:117