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_; }
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();
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 
283  SignalProxy<void()> sigTreePathChanged();
284 
291  SignalProxy<void()> sigTreePositionChanged();
292 
302  SignalProxy<void(Item* topItem, Item* prevTopParentItem)> sigTreePositionChanged2();
303 
304  [[deprecated("Use sigTreePositionChanged")]]
305  SignalProxy<void()> sigPositionChanged();
306 
310  SignalProxy<void()> sigSubTreeChanged();
311 
312  SignalProxy<void()> sigDisconnectedFromRoot();
313 
314  [[deprecated("Use Item::sigDisconnectedFromRoot.")]]
315  SignalProxy<void()> sigDetachedFromRoot() { return sigDisconnectedFromRoot(); }
316 
317  SignalProxy<void(bool on)> sigSelectionChanged();
318  SignalProxy<void(int checkId, bool on)> sigAnyCheckToggled();
319  SignalProxy<void(bool on)> sigCheckToggled(int checkId = PrimaryCheck);
320 
321  virtual void notifyUpdate();
322  SignalProxy<void()> sigUpdated();
323 
324  bool setAddon(ItemAddon* addon);
325  void removeAddon(ItemAddon* addon);
326  template<class AddonType> AddonType* findAddon(){
327  return static_cast<AddonType*>(findAddon_(typeid(AddonType)));
328  }
329  template<class AddonType> AddonType* getAddon(){
330  return static_cast<AddonType*>(getAddon_(typeid(AddonType)));
331  }
332  std::vector<ItemAddon*> addons();
333 
339  bool load(
340  const std::string& filename, const std::string& format = std::string(),
341  const Mapping* options = nullptr);
342 
348  bool load(
349  const std::string& filename, Item* parent, const std::string& format = std::string(),
350  const Mapping* options = nullptr);
351 
357  bool save(const std::string& filename, const std::string& format = std::string(),
358  const Mapping* options = nullptr);
359 
364  bool overwrite(bool forceOverwrite = false, const std::string& format = std::string());
365 
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;
374 
375  void updateFileInformation(const std::string& filename, const std::string& format, Mapping* options = nullptr);
376  void setConsistentWithFile(bool isConsistent);
377  void suggestFileUpdate();
378 
380  void clearFileInformation();
381 
382  bool reload();
383  bool replace(Item* originalItem);
384  Item* findOriginalItem() const;
385  Item* findReplacementItem() const;
386 
387 #ifdef CNOID_BACKWARD_COMPATIBILITY
388  const std::string& lastAccessedFilePath() const;
389  const std::string& lastAccessedFileFormatId() const;
390 #endif
391 
392  void putProperties(PutPropertyFunction& putProperty);
393 
394  virtual bool store(Archive& archive);
395  virtual bool restore(const Archive& archive);
396 
397 protected:
398 
400  virtual void doAssign(Item* srcItem);
401 
403  virtual Item* doDuplicate() const;
404 
411  virtual Item* doDuplicate(Item* duplicatedParentItem) const;
412 
413  virtual bool onNewTreePositionCheck(bool isManualOperation, std::function<void()>& out_callbackWhenAdded);
414  virtual void onAddedToParent();
415 
422  virtual void onTreePathChanged();
423 
428  virtual void onTreePositionChanged();
429 
434  virtual void onPositionChanged();
435 
440  virtual void onConnectedToRoot();
441 
442  virtual void onRemovedFromParent(Item* parentItem, bool isParentBeingDeleted);
443  virtual void onDisconnectedFromRoot();
444 
452  virtual bool onChildItemAboutToBeAdded(Item* childItem, bool isManualOperation);
453 
459  virtual void doPutProperties(PutPropertyFunction& putProperty);
460 
461 private:
462  class Impl;
463  Impl* impl;
464 
465  mutable int classId_;
466  Item* parent_;
467  ItemPtr firstChild_;
468  ItemPtr nextItem_;
469  Item* prevItem_;
470  Item* lastChild_;
471  int numChildren_;
472  std::string name_;
473  bool isSelected_;
474 
475  static Item* find(const std::string& path, const std::function<bool(Item* item)>& pred);
476  Item* findItem(
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);
482 };
483 
484 #ifndef CNOID_BASE_MVOUT_DECLARED
485 #define CNOID_BASE_MVOUT_DECLARED
486 CNOID_EXPORT std::ostream& mvout(bool doFlush = true);
487 #endif
488 
489 }
490 
491 #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:253
cnoid::Item::getAddon
AddonType * getAddon()
Definition: Item.h:329
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::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:22
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:1289
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:384
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::mvout
CNOID_EXPORT std::ostream & mvout(bool doFlush=true)
Definition: MessageView.cpp:1108
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:315
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:326
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