Choreonoid  1.8
LinkDeviceTreeWidget.h
Go to the documentation of this file.
1 #ifndef CNOID_BODY_PLUGIN_LINK_DEVICE_TREE_WIDGET_H
2 #define CNOID_BODY_PLUGIN_LINK_DEVICE_TREE_WIDGET_H
3 
4 #include <cnoid/TreeWidget>
5 #include <cnoid/Signal>
6 #include <cnoid/Link>
7 #include <cnoid/Device>
8 #include <vector>
9 #include "exportdecl.h"
10 
11 namespace cnoid {
12 
13 class LinkDeviceTreeItem;
14 class BodyItem;
15 class Link;
16 class Device;
17 class LinkGroup;
18 class MenuManager;
19 class Archive;
20 
21 class CNOID_EXPORT LinkDeviceTreeWidget : public TreeWidget
22 {
23 public:
24  LinkDeviceTreeWidget(QWidget* parent = 0);
25  virtual ~LinkDeviceTreeWidget();
26 
27  enum ListingMode { List, Tree, GroupedTree };
28  void setListingMode(int mode);
29  int listingMode() const;
30  void setLinkItemVisible(bool on);
31  bool isLinkItemVisible() const;
32  void setVisibleLinkPredicate(std::function<bool(Link* link)> pred);
33  void setJointItemVisible(bool on);
34  bool isJointItemVisible() const;
35  void setDeviceItemVisible(bool on);
36  bool isDeviceItemVisible() const;
37  enum NumberColumnMode { Index, Identifier };
38  void setNumberColumnMode(int mode);
39  int numberColumnMode() const;
40  void setDefaultExpansionLevel(int level);
41  void setCacheEnabled(bool on);
42  bool isCacheEnabled() const;
43 
44  int nameColumn() const;
45  int numberColumn() const;
46  int setNumColumns(int n);
47  int addColumn();
48  int addColumn(const QString& headerText);
49  void setColumnStretchResizeMode(int column);
50  void setColumnInteractiveResizeMode(int column);
51  void setColumnResizeToContentsMode(int column);
52  void setNameColumnMarginEnabled(bool on);
53  void moveVisualColumnIndex(int column, int visualIndex);
54 
55  typedef std::function<QVariant(const LinkDeviceTreeItem* item, int role)> ColumnDataGetter;
56  typedef std::function<void(const LinkDeviceTreeItem* item, int role, const QVariant& value)> ColumnDataSetter;
57  typedef std::function<QWidget*(const LinkDeviceTreeItem* item)> ColumnWidgetFunction;
58 
59  void setColumnDataGetter(int column, ColumnDataGetter getter);
60  void setColumnDataSetter(int column, ColumnDataSetter func);
61  void setColumnWidgetFunction(int column, ColumnWidgetFunction func);
62 
63  void setAlignedItemWidget(
64  LinkDeviceTreeItem* item, int column, QWidget* widget, Qt::Alignment alignment = Qt::AlignCenter);
65  QWidget* alignedItemWidget(LinkDeviceTreeItem* item, int column);
66 
67  void setBodyItem(BodyItem* bodyItem, bool forceTreeUpdate = false);
68  BodyItem* bodyItem();
69  void updateTreeItems();
70  void addCustomRow(LinkDeviceTreeItem* treeItem);
71  SignalProxy<void(bool isInitialCreation)> sigUpdateRequest();
72  LinkDeviceTreeItem* itemOfLink(int linkIndex);
73  int numLinkDeviceTreeItems();
74 
75  SignalProxy<void(LinkDeviceTreeItem* item, int column)> sigTreeItemChanged();
76 
77  SignalProxy<void()> sigLinkSelectionChanged();
78  const std::vector<bool>& linkSelection() const;
79  const std::vector<int>& selectedLinkIndices() const;
80 
81  // The following functions are available when the cache is enabled
82  SignalProxy<void()> sigSelectionChanged(BodyItem* bodyItem);
83  const std::vector<bool>& linkSelection(BodyItem* bodyItem);
84  const std::vector<int>& selectedLinkIndices(BodyItem* bodyItem);
85 
86  // This funtions does not emit sigSelectionChanged.
87  void setLinkSelection(BodyItem* bodyItem, const std::vector<bool>& selection);
88 
89  MenuManager& popupMenuManager();
90 
91  bool storeState(Archive& archive);
92 
93  // This function must be called after all the items are restored
94  bool restoreState(const Archive& archive);
95 
96 protected:
97  virtual void changeEvent(QEvent* event) override;
98 
99 private:
100  class Impl;
101  Impl* impl;
102 
103  friend class LinkDeviceTreeItem;
104 };
105 
106 class CNOID_EXPORT LinkDeviceTreeItem : public QTreeWidgetItem
107 {
108 public:
109  LinkDeviceTreeItem(const std::string& name, LinkDeviceTreeWidget::Impl* treeImpl = nullptr);
110  int rowIndex() const { return rowIndex_; }
111  const std::string& name() const { return name_; }
112  const QString& nameText() const { return nameText_; }
113  Link* link() { return link_; }
114  const Link* link() const { return link_; }
115  const Device* device() const { return device_; }
116  bool isLinkGroup() const { return isLinkGroup_; }
117  int numberColumnMode() const;
118 
119  virtual QVariant data(int column, int role) const override;
120  virtual void setData(int column, int role, const QVariant& value) override;
121 
122 private:
123  int rowIndex_;
124  std::string name_;
125  QString nameText_;
126  LinkPtr link_;
127  DevicePtr device_;
128  LinkDeviceTreeWidget::Impl* treeImpl;
129  bool isLinkGroup_;
130 
131  LinkDeviceTreeItem(Link* link, LinkDeviceTreeWidget::Impl* treeImpl);
132  LinkDeviceTreeItem(Device* device, LinkDeviceTreeWidget::Impl* treeImpl);
133  LinkDeviceTreeItem(LinkGroup* linkGroup, LinkDeviceTreeWidget::Impl* treeImpl);
134 
135  friend class LinkDeviceTreeWidget;
136  friend class LinkDeviceTreeWidget::Impl;
137 };
138 
139 }
140 
141 #endif
cnoid::LinkDeviceTreeItem::rowIndex
int rowIndex() const
Definition: LinkDeviceTreeWidget.h:110
cnoid::LinkDeviceTreeItem::nameText
const QString & nameText() const
Definition: LinkDeviceTreeWidget.h:112
cnoid::LinkDeviceTreeItem::link
Link * link()
Definition: LinkDeviceTreeWidget.h:113
cnoid::LinkDeviceTreeWidget::NumberColumnMode
NumberColumnMode
Definition: LinkDeviceTreeWidget.h:37
cnoid::Archive
Definition: Archive.h:22
cnoid::LinkGroup
Definition: LinkGroup.h:21
cnoid::MenuManager
Definition: MenuManager.h:14
cnoid::LinkDeviceTreeWidget
Definition: LinkDeviceTreeWidget.h:21
cnoid::ref_ptr< Link >
cnoid::BodyItem
Definition: BodyItem.h:28
cnoid::LinkDeviceTreeWidget::Tree
@ Tree
Definition: LinkDeviceTreeWidget.h:27
cnoid::LinkDeviceTreeItem::isLinkGroup
bool isLinkGroup() const
Definition: LinkDeviceTreeWidget.h:116
cnoid::LinkDeviceTreeWidget::ColumnDataGetter
std::function< QVariant(const LinkDeviceTreeItem *item, int role)> ColumnDataGetter
Definition: LinkDeviceTreeWidget.h:55
cnoid::LinkDeviceTreeItem::link
const Link * link() const
Definition: LinkDeviceTreeWidget.h:114
cnoid::LinkDeviceTreeWidget::ListingMode
ListingMode
Definition: LinkDeviceTreeWidget.h:27
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::LinkDeviceTreeItem::name
const std::string & name() const
Definition: LinkDeviceTreeWidget.h:111
cnoid::LinkDeviceTreeWidget::ColumnDataSetter
std::function< void(const LinkDeviceTreeItem *item, int role, const QVariant &value)> ColumnDataSetter
Definition: LinkDeviceTreeWidget.h:56
cnoid::TreeWidget
Definition: TreeWidget.h:15
cnoid::Device
Definition: Device.h:53
cnoid::LinkDeviceTreeItem::device
const Device * device() const
Definition: LinkDeviceTreeWidget.h:115
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::LinkDeviceTreeItem
Definition: LinkDeviceTreeWidget.h:106
cnoid::LinkDeviceTreeWidget::ColumnWidgetFunction
std::function< QWidget *(const LinkDeviceTreeItem *item)> ColumnWidgetFunction
Definition: LinkDeviceTreeWidget.h:57