Choreonoid  1.8
ItemTreePanelDialog.h
Go to the documentation of this file.
1 #ifndef CNOID_BASE_ITEM_TREE_PANEL_DIALOG_H
2 #define CNOID_BASE_ITEM_TREE_PANEL_DIALOG_H
3 
4 #include <cnoid/Dialog>
5 #include "exportdecl.h"
6 
7 namespace cnoid {
8 
9 class ItemTreeWidget;
10 class Item;
11 class ItemTreePanelBase;
12 
13 class CNOID_EXPORT ItemTreePanelDialog : public Dialog
14 {
15 public:
17  ItemTreePanelDialog(QWidget* parent, Qt::WindowFlags f= Qt::WindowFlags());
19 
20  enum ModeFlags {
21  PanelOnlyDisplayMode = 1,
22  LastValidPanelKeepingMode = 2,
23  // The dialog is always closed when the operation on a single panel is accepted or rejected
24  SinglePanelSyncMode = 4
25  };
26 
27  void setMode(int flags);
28  int mode() const;
29 
30  ItemTreeWidget* itemTreeWidget();
31 
32  template<class TargetItemType>
33  void registerPanel(ItemTreePanelBase* panel);
34 
35  template<class TargetItemType>
37  std::function<ItemTreePanelBase*(TargetItemType* item)> panelFunction,
38  std::function<QSize()> minimumSizeHintFunction)
39  {
40  registerPanel_(
41  typeid(TargetItemType),
42  [panelFunction](Item* item){ return panelFunction(static_cast<TargetItemType*>(item)); },
43  minimumSizeHintFunction);
44  }
45 
46  void addTopAreaWidget(QWidget* widget);
47  void updateTopAreaLayout();
48 
49  bool setTopItem(Item* topItem, bool isTopVisible = false);
50  void show();
51  bool setCurrentItem(Item* item, bool isNewItem = false);
52 
53 protected:
54  virtual void onCurrentItemChanged(Item* item);
55  virtual void keyPressEvent(QKeyEvent* event) override;
56  virtual void hideEvent(QHideEvent* event) override;
57 
58 private:
59  class Impl;
60  Impl* impl;
61 
62  void registerPanel_(
63  const std::type_info& type,
64  const std::function<ItemTreePanelBase*(Item* item)>& panelFunction,
65  const std::function<QSize()>& minimumSizeHintFunction);
66 
67  friend class ItemTreePanelBase;
68 
69 };
70 
71 class CNOID_EXPORT ItemTreePanelBase : public QWidget
72 {
73 public:
74  ItemTreePanelBase(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
75  bool activate(Item* topItem, Item* targetItem, bool isNewItem, ItemTreePanelDialog* currentDialog);
76  virtual std::string caption() const = 0;
77  virtual bool onActivated(Item* topItem, Item* targetItem, bool isNewItem) = 0;
78  virtual void onDeactivated();
79  ItemTreePanelDialog* dialog(){ return currentDialog; }
80  ItemTreeWidget* itemTreeWidget();
81 
82 protected:
83  void accept();
84  void reject();
85 
86 private:
87  ItemTreePanelDialog* currentDialog;
88 };
89 
90 template<class TopItemType, class TargetItemType>
92 {
93 public:
94  ItemTreePanel(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags())
95  : ItemTreePanelBase(parent, f)
96  { }
97 
98  virtual bool onActivated(Item* topItem, Item* targetItem, bool isNewItem) override final {
99  if(auto derivedTopItem = dynamic_cast<TopItemType*>(topItem)){
100  if(auto derivedTargetItem = dynamic_cast<TargetItemType*>(targetItem)){
101  return onActivated(derivedTopItem, derivedTargetItem, isNewItem);
102  }
103  }
104  return false;
105  }
106 
107  virtual bool onActivated(TopItemType* topItem, TargetItemType* targetItem, bool isNewItem) = 0;
108 };
109 
110 
111 template<class TargetItemType>
113 {
114  registerPanel_(
115  typeid(TargetItemType),
116  [panel](Item*){ return panel; },
117  [panel](){ return panel->minimumSizeHint(); });
118 }
119 
120 
121 }
122 
123 #endif
cnoid::ItemTreePanelDialog::registerPanel
void registerPanel(std::function< ItemTreePanelBase *(TargetItemType *item)> panelFunction, std::function< QSize()> minimumSizeHintFunction)
Definition: ItemTreePanelDialog.h:36
cnoid::ItemTreePanel::onActivated
virtual bool onActivated(Item *topItem, Item *targetItem, bool isNewItem) override final
Definition: ItemTreePanelDialog.h:98
cnoid::Dialog
Definition: Dialog.h:14
cnoid::ItemTreePanelDialog::ModeFlags
ModeFlags
Definition: ItemTreePanelDialog.h:20
cnoid::ItemTreePanel::ItemTreePanel
ItemTreePanel(QWidget *parent=Q_NULLPTR, Qt::WindowFlags f=Qt::WindowFlags())
Definition: ItemTreePanelDialog.h:94
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::ItemTreePanelBase
Definition: ItemTreePanelDialog.h:71
cnoid::ItemTreePanelBase::dialog
ItemTreePanelDialog * dialog()
Definition: ItemTreePanelDialog.h:79
cnoid::ItemTreeWidget
Definition: ItemTreeWidget.h:19
cnoid::ItemTreePanelDialog::registerPanel
void registerPanel(ItemTreePanelBase *panel)
Definition: ItemTreePanelDialog.h:112
cnoid::ItemTreePanel
Definition: ItemTreePanelDialog.h:91
cnoid::ItemTreePanelDialog
Definition: ItemTreePanelDialog.h:13