Choreonoid  1.8
ItemTreeWidget.h
Go to the documentation of this file.
1 #ifndef CNOID_BASE_ITEM_TREE_WIDGET_H
2 #define CNOID_BASE_ITEM_TREE_WIDGET_H
3 
4 #include "ItemList.h"
6 #include <QWidget>
7 #include <functional>
8 #include "exportdecl.h"
9 
10 class QTreeWidgetItem;
11 
12 namespace cnoid {
13 
14 class Item;
15 class RootItem;
16 class MenuManager;
17 class Archive;
18 
19 class CNOID_EXPORT ItemTreeWidget : public QWidget
20 {
21 public:
22  class Impl;
23 
24  ItemTreeWidget(QWidget* parent = nullptr);
25  ~ItemTreeWidget();
26 
27  RootItem* projectRootItem();
28 
29  /*
30  The following root item is the top item of a sub tree the item tree widget covers.
31  The item is not necessarily the project root item of the RootItem type but it may
32  be any item in the project item tree.
33  */
34  Item* findRootItem();
35  template<class ItemType>
36  ItemType* findRootItem(){ return dynamic_cast<ItemType*>(findRootItem()); }
37  Item* findOrCreateRootItem();
38  template<class ItemType>
39  ItemType* findOrCreateRootItem(){ return dynamic_cast<ItemType*>(findRootItem()); }
40 
41  void setRootItem(Item* item);
42  void setRootItemUpdateFunction(std::function<Item*(bool doCreate)> func);
43 
45  void setRootItemVisible(bool on);
46  bool isRootItemVisible() const;
47 
48  void setDragDropEnabled(bool on);
49  void setCheckColumnShown(bool on);
50 
51  template<class ItemType>
52  void customizeVisibility(std::function<bool(ItemType* item, bool isTopLevelItemCandidate)> func){
53  customizeVisibility_(
54  typeid(ItemType),
55  [func](Item* item, bool isTopLevelItemCandidate){
56  return func(static_cast<ItemType*>(item), isTopLevelItemCandidate);
57  });
58  }
59 
60  class CNOID_EXPORT Display {
61  public:
62  QBrush foreground() const;
63  void setForeground(const QBrush& brush);
64  QBrush background() const;
65  void setBackground(const QBrush& brush);
66  QFont font() const;
67  void setFont(const QFont& font);
68  QIcon icon() const;
69  void setIcon(const QIcon& icon);
70  void setToolTip(const std::string& toolTip);
71  void setStatusTip(const std::string& statusTip);
72  void setNameEditable(bool on);
73  private:
74  QTreeWidgetItem* item;
75  friend class Impl;
76  };
77 
78  template<class ItemType>
79  void customizeDisplay(std::function<void(ItemType* item, Display& display)> func){
80  customizeDisplay_(
81  typeid(ItemType),
82  [func](Item* item, Display& display){
83  return func(static_cast<ItemType*>(item), display);
84  });
85  }
86 
87  template<class ItemType>
89  std::function<bool(ItemType* item, Item* parentItem)> func){
90  customizePositionAcceptance_(
91  typeid(ItemType),
92  [func](Item* item, Item* parentItem){
93  return func(static_cast<ItemType*>(item), parentItem);
94  });
95  }
96 
97  bool checkPositionAcceptance(Item* item, Item* parentItem) const;
98 
99  void customizeRootContextMenu(std::function<void(MenuManager& menuManager)> func);
100 
101  template<class ItemType>
103  std::function<void(ItemType* item, MenuManager& menuManager, ItemFunctionDispatcher menuFunction)> func){
104  customizeContextMenu_(
105  typeid(ItemType),
106  [func](Item* item, MenuManager& menuManager, ItemFunctionDispatcher menuFunction){
107  func(static_cast<ItemType*>(item), menuManager, menuFunction);
108  });
109  }
110 
111  void updateTreeWidgetItems();
112  void setExpanded(Item* item, bool on = true);
113  void editItemName(Item* item);
114 
115  ItemList<> getItems() const;
116  template <class ItemType> ItemList<ItemType> getItems() {
117  return getItems();
118  }
119 
120  void setSelectionSyncGroup(const std::string& id);
121 
122  SignalProxy<void(const ItemList<>&)> sigSelectionChanged();
123 
124  ItemList<> getSelectedItems() const;
125  template <class ItemType> ItemList<ItemType> getSelectedItems() {
126  return getSelectedItems();
127  }
128 
130  bool selectOnly(Item* item);
131 
132  void selectAllItems();
133  void clearSelection();
134  void setSelectedItemsChecked(bool on);
135  void toggleSelectedItemChecks();
136  void cutSelectedItems();
137  void copySelectedItems();
138  void copySelectedItemsWithSubTrees();
139  bool pasteItems(bool doCheckPositionAcceptance = true);
140  bool checkPastable(Item* pasteParentItem) const;
141 
142  bool storeState(Archive& archive);
143  bool restoreState(const Archive& archive);
144 
145 private:
146  void customizeVisibility_(
147  const std::type_info& type, std::function<bool(Item* item, bool isTopLevelItemCandidate)> func);
148  void customizeDisplay_(
149  const std::type_info& type, std::function<void(Item* item, Display& display)> func);
150  void customizePositionAcceptance_(
151  const std::type_info& type, std::function<bool(Item* item, Item* parentItem)> func);
152  void customizeContextMenu_(
153  const std::type_info& type,
154  std::function<void(Item* item, MenuManager& menuManager, ItemFunctionDispatcher menuFunction)> func);
155 
156  Impl* impl;
157 };
158 
159 }
160 
161 #endif
cnoid::ItemTreeWidget::customizeDisplay
void customizeDisplay(std::function< void(ItemType *item, Display &display)> func)
Definition: ItemTreeWidget.h:79
cnoid::RootItem
Definition: RootItem.h:16
cnoid::Archive
Definition: Archive.h:22
cnoid::MenuManager
Definition: MenuManager.h:14
cnoid::ItemTreeWidget::customizePositionAcceptance
void customizePositionAcceptance(std::function< bool(ItemType *item, Item *parentItem)> func)
Definition: ItemTreeWidget.h:88
cnoid::ItemTreeWidget::Display
Definition: ItemTreeWidget.h:60
cnoid::ItemTreeWidget::customizeVisibility
void customizeVisibility(std::function< bool(ItemType *item, bool isTopLevelItemCandidate)> func)
Definition: ItemTreeWidget.h:52
cnoid::ItemTreeWidget::getSelectedItems
ItemList< ItemType > getSelectedItems()
Definition: ItemTreeWidget.h:125
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::ItemTreeWidget::getItems
ItemList< ItemType > getItems()
Definition: ItemTreeWidget.h:116
cnoid::ItemList
Definition: Item.h:20
cnoid::ItemTreeWidget::customizeContextMenu
void customizeContextMenu(std::function< void(ItemType *item, MenuManager &menuManager, ItemFunctionDispatcher menuFunction)> func)
Definition: ItemTreeWidget.h:102
cnoid::ItemTreeWidget
Definition: ItemTreeWidget.h:19
ItemList.h
PolymorphicItemFunctionSet.h
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::ItemTreeWidget::findOrCreateRootItem
ItemType * findOrCreateRootItem()
Definition: ItemTreeWidget.h:39
cnoid::ItemTreeWidget::findRootItem
ItemType * findRootItem()
Definition: ItemTreeWidget.h:36
cnoid::ItemFunctionDispatcher
PolymorphicItemFunctionSet::Dispatcher ItemFunctionDispatcher
Definition: PolymorphicItemFunctionSet.h:16