Choreonoid  1.8
TargetItemPicker.h
Go to the documentation of this file.
1 #ifndef CNOID_BASE_TARGET_ITEM_PICKER_H
2 #define CNOID_BASE_TARGET_ITEM_PICKER_H
3 
4 #include "Item.h"
5 #include "ItemList.h"
6 #include "exportdecl.h"
7 
8 namespace cnoid {
9 
10 class View;
11 
12 class CNOID_EXPORT TargetItemPickerBase
13 {
14 public:
15  TargetItemPickerBase(View* view = nullptr);
17 
18  void setEnabled(bool on);
19  void clearTargetItem();
20  void refresh();
21  void storeTargetItem(Archive& archive, const std::string& key);
22  void restoreTargetItem(const Archive& archive, const std::string& key);
23  void restoreTargetItemLater(const Archive& archive, const std::string& key);
24 
25 protected:
26  Item* getTargetItem();
27  virtual Item* extractTargetItemCandidates(ItemList<>& io_items, Item* preferred, bool selectionChanged) = 0;
28  virtual bool targetPredicate(Item* item) = 0;
29  virtual void onTargetItemSpecified(Item* item, bool isChanged) = 0;
30  virtual void onDeactivated() = 0;
31 
32 private:
33  class Impl;
34  Impl* impl;
35 
36 };
37 
38 template<class ItemType>
40 {
41 public:
43 
46 
47  void setTargetPredicate(std::function<bool(ItemType* item)> predicate){ targetPredicate_ = predicate; }
48 
49  template<class Interface>
51  targetPredicate_ = [](ItemType* item)->bool { return dynamic_cast<Interface*>(item) != nullptr; };
52  }
53 
54  ItemType* currentItem(){ return static_cast<ItemType*>(getTargetItem()); }
55  const ItemList<ItemType>& selectedItems(){ return selectedItems_; }
56 
57  SignalProxy<void(ItemType* targetItem)> sigTargetItemSpecified(){
58  return sigTargetItemSpecified_;
59  }
60  SignalProxy<void(ItemType* targetItem)> sigTargetItemChanged(){
61  return sigTargetItemChanged_;
62  }
64  return sigSelectedItemsChanged_;
65  }
66 
67 protected:
69  (ItemList<>& io_items, Item* preferred, bool selectionChanged) override
70  {
71  Item* candidate = nullptr;
72 
73  auto iter = io_items.begin();
74  while(iter != io_items.end()){
75  auto item = dynamic_cast<ItemType*>(iter->get());
76  if(item && targetPredicate_){
77  if(!targetPredicate_(item)){
78  item = nullptr;
79  }
80  }
81  if(!item){
82  iter = io_items.erase(iter);
83  } else {
84  if(!candidate){
85  candidate = item;
86  } else if(item == preferred){
87  candidate = preferred;
88  }
89  ++iter;
90  }
91  }
92 
93  if(selectionChanged){
94  if(io_items != selectedItems_){
95  selectedItems_ = io_items;
96  sigSelectedItemsChanged_(selectedItems_);
97  }
98  }
99 
100  return candidate;
101  }
102 
103  virtual bool targetPredicate(Item* item) override
104  {
105  if(targetPredicate_){
106  return targetPredicate_(static_cast<ItemType*>(item));
107  }
108  return true;
109  }
110 
111  virtual void onTargetItemSpecified(Item* item, bool isChanged) override
112  {
113  auto targetItem = static_cast<ItemType*>(item);
114  sigTargetItemSpecified_(targetItem);
115  if(isChanged){
116  sigTargetItemChanged_(targetItem);
117  }
118  }
119 
120  virtual void onDeactivated() override
121  {
122  selectedItems_.clear();
123  }
124 
125 private:
126  std::function<bool(ItemType* item)> targetPredicate_;
127  Signal<void(ItemType* targetItem)> sigTargetItemSpecified_;
128  Signal<void(ItemType* targetItem)> sigTargetItemChanged_;
129  Signal<void(const ItemList<ItemType>& selected)> sigSelectedItemsChanged_;
130  ItemList<ItemType> selectedItems_;
131 };
132 
133 }
134 
135 #endif
cnoid::TargetItemPicker::ItemTypePtr
ref_ptr< ItemType > ItemTypePtr
Definition: TargetItemPicker.h:42
Item.h
cnoid::TargetItemPicker::onDeactivated
virtual void onDeactivated() override
Definition: TargetItemPicker.h:120
cnoid::TargetItemPicker::sigTargetItemSpecified
SignalProxy< void(ItemType *targetItem)> sigTargetItemSpecified()
Definition: TargetItemPicker.h:57
cnoid::TargetItemPicker::setTargetInterface
void setTargetInterface()
Definition: TargetItemPicker.h:50
cnoid::View
Definition: View.h:27
cnoid::Archive
Definition: Archive.h:22
cnoid::ref_ptr
Definition: Referenced.h:103
cnoid::TargetItemPicker::sigTargetItemChanged
SignalProxy< void(ItemType *targetItem)> sigTargetItemChanged()
Definition: TargetItemPicker.h:60
cnoid::TargetItemPicker::sigSelectedItemsChanged
SignalProxy< void(const ItemList< ItemType > &selected)> sigSelectedItemsChanged()
Definition: TargetItemPicker.h:63
cnoid::TargetItemPicker::onTargetItemSpecified
virtual void onTargetItemSpecified(Item *item, bool isChanged) override
Definition: TargetItemPicker.h:111
cnoid::TargetItemPicker::extractTargetItemCandidates
virtual Item * extractTargetItemCandidates(ItemList<> &io_items, Item *preferred, bool selectionChanged) override
Definition: TargetItemPicker.h:69
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::ItemList
Definition: Item.h:20
cnoid::TargetItemPicker
Definition: TargetItemPicker.h:39
cnoid::TargetItemPicker::setTargetPredicate
void setTargetPredicate(std::function< bool(ItemType *item)> predicate)
Definition: TargetItemPicker.h:47
ItemList.h
cnoid::Signal
Definition: Signal.h:165
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::TargetItemPicker::selectedItems
const ItemList< ItemType > & selectedItems()
Definition: TargetItemPicker.h:55
cnoid::TargetItemPicker::targetPredicate
virtual bool targetPredicate(Item *item) override
Definition: TargetItemPicker.h:103
cnoid::TargetItemPicker::currentItem
ItemType * currentItem()
Definition: TargetItemPicker.h:54
cnoid::TargetItemPicker::TargetItemPicker
TargetItemPicker(View *view)
Definition: TargetItemPicker.h:45
cnoid::TargetItemPicker::TargetItemPicker
TargetItemPicker()
Definition: TargetItemPicker.h:44
cnoid::TargetItemPickerBase
Definition: TargetItemPicker.h:12
cnoid::TargetItemPickerBase::getTargetItem
Item * getTargetItem()
Definition: TargetItemPicker.cpp:136