Choreonoid  1.8
MprProgramViewBase.h
Go to the documentation of this file.
1 #ifndef CNOID_MANIPULATOR_PLUGIN_MPR_PROGRAM_VIEW_BASE_H
2 #define CNOID_MANIPULATOR_PLUGIN_MPR_PROGRAM_VIEW_BASE_H
3 
4 #include "MprStatement.h"
5 #include <cnoid/View>
6 #include <cnoid/Signal>
7 #include <cnoid/Referenced>
8 #include <typeindex>
9 #include "exportdecl.h"
10 
11 namespace cnoid {
12 
13 class Archive;
14 class MprProgramItemBase;
15 class MprProgram;
16 class MprPositionStatement;
17 class MenuManager;
18 class ToolButton;
19 
20 class CNOID_EXPORT MprProgramViewBase : public View
21 {
22 public:
24  virtual ~MprProgramViewBase();
25 
26  virtual void onActivated() override;
27  virtual void onDeactivated() override;
28 
29  MprProgramItemBase* currentProgramItem();
30  bool checkCurrentProgramItem() const;
31 
32  MprStatement* currentStatement();
33  SignalProxy<void(MprStatement* statement)> sigCurrentStatementChanged();
34 
35  const std::vector<MprStatementPtr>& selectedStatements();
36  SignalProxy<void(std::vector<MprStatementPtr>& statements)> sigSelectedStatementsChanged();
37 
38  void updateStatementTree();
39 
40  class CNOID_EXPORT StatementDelegate : public Referenced
41  {
42  public:
45  static constexpr int SpanToLast = -1;
46  virtual void activateStatement(MprStatement* statement) const;
47  virtual int labelSpan(MprStatement* statement, int column) const;
48  virtual QVariant dataOfEditRole(MprStatement* statement, int column) const;
49  virtual void setDataOfEditRole(MprStatement* statement, int column, const QVariant& value) const;
50  virtual QWidget* createEditor(MprStatement* statement, int column, QWidget* parent) const;
51  virtual void setEditorData(MprStatement* statement, int column, QWidget* editor) const;
52  virtual void setStatementData(MprStatement* statement, int column, QWidget* editor) const;
53 
54  class Impl;
55  Impl* impl;
56 
57  protected:
58  // Only calleded from the createEditor function
59  QWidget* createDefaultEditor() const;
60  };
61 
62  template<class StatementType>
64  registerStatementDelegate(typeid(StatementType), delegate);
65  }
66 
67  template<class StatementType>
69  std::function<void(StatementType* statement, MenuManager& menuManager,
70  MprStatementFunctionDispatcher menuFunction)> func){
71  customizeContextMenu_(
72  typeid(StatementType),
73  [func](MprStatement* statement, MenuManager& menuManager,
74  MprStatementFunctionDispatcher menuFunction){
75  func(static_cast<StatementType*>(statement), menuManager, menuFunction);
76  });
77  }
78 
79  enum BodySyncMode { NoBodySync, DirectBodySync, TwoStageBodySync };
80  void setBodySyncMode(BodySyncMode mode);
81  BodySyncMode bodySyncMode() const;
82 
83  class Impl;
84 
85 protected:
86  void addEditButton(ToolButton* button, int row);
87 
88  enum InsertionType { BeforeTargetPosition, AfterTargetPosition };
89  bool insertStatement(
90  MprStatement* statement, int insertionType = AfterTargetPosition);
91 
92  virtual bool onCurrentProgramItemChanged(MprProgramItemBase* item) = 0;
93  virtual void onCurrentStatementChanged(MprStatement* statement);
94  // This function is called when the current statemet is changed or clicked.
95  virtual void onStatementActivated(MprStatement* statement);
96  virtual void onStatementDoubleClicked(MprStatement* statement);
97 
98  virtual void onAttachedMenuRequest(MenuManager& menuManager) override;
99  virtual bool storeState(Archive& archive) override;
100  virtual bool restoreState(const Archive& archive) override;
101 
102 private:
103  void registerStatementDelegate(std::type_index statementType, StatementDelegate* delegate);
104  void registerBaseStatementDelegates();
105 
106  void customizeContextMenu_(
107  const std::type_info& type,
108  std::function<void(MprStatement* statement, MenuManager& menuManager,
109  MprStatementFunctionDispatcher menuFunction)> func);
110 
111  Impl* impl;
112 };
113 
114 }
115 
116 #endif
cnoid::MprProgramViewBase::StatementDelegate
Definition: MprProgramViewBase.h:40
cnoid::MprProgramItemBase
Definition: MprProgramItemBase.h:16
cnoid::View
Definition: View.h:27
cnoid::Archive
Definition: Archive.h:22
cnoid::MprProgramViewBase::StatementDelegate::impl
Impl * impl
Definition: MprProgramViewBase.h:54
cnoid::MenuManager
Definition: MenuManager.h:14
cnoid::MprProgramViewBase
Definition: MprProgramViewBase.h:20
cnoid::MprStatementFunctionDispatcher
PolymorphicMprStatementFunctionSet::Dispatcher MprStatementFunctionDispatcher
Definition: MprStatement.h:78
cnoid::MprProgramViewBase::customizeContextMenu
void customizeContextMenu(std::function< void(StatementType *statement, MenuManager &menuManager, MprStatementFunctionDispatcher menuFunction)> func)
Definition: MprProgramViewBase.h:68
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::MprProgramViewBase::BodySyncMode
BodySyncMode
Definition: MprProgramViewBase.h:79
cnoid::MprProgramViewBase::registerStatementDelegate
void registerStatementDelegate(StatementDelegate *delegate)
Definition: MprProgramViewBase.h:63
cnoid::MprStatement
Definition: MprStatement.h:16
cnoid::Referenced
Definition: Referenced.h:54
cnoid::ToolButton
Definition: Buttons.h:74
cnoid::MprProgramViewBase::InsertionType
InsertionType
Definition: MprProgramViewBase.h:88
cnoid::SignalProxy
Definition: Signal.h:470
MprStatement.h