pyplugins/gepetto/gui/pythonwidget.py

This is an example Python Plugin for hpp::gui::PythonWidget. Two classes are defined: _NodeCreator and Plugin. Two signals are used: mainWindow.refresh() and osgWidget. Add this to your configuration file to load this plugin

[pyplugins]

Then right-click on a tool bar, menu bar or title bar of dock widgets to open your window.

1 from PythonQt import QtGui, Qt
2 from gepetto.corbaserver import Client
3 
4 
5 class _NodeCreator (QtGui.QWidget):
6  def __init__(self, parent, plugin):
7  super(_NodeCreator, self).__init__ (parent)
8  self.plugin = plugin
9  box = QtGui.QVBoxLayout(self)
10 
11  # Name line edit
12  self.nodeName = QtGui.QLineEdit("nodeName")
13  box.addWidget(self.addWidgetsInHBox([QtGui.QLabel("Node name:"), self.nodeName]))
14 
15  # Create group
16  box.addWidget(self.bindFunctionToButton("Create group", self.createGroup))
17 
18  # Add to group
19  self.groupNodes = QtGui.QComboBox(self)
20  self.groupNodes.editable = False
21  box.addWidget(self.addWidgetsInHBox( [
22  self.groupNodes,
23  self.bindFunctionToButton("Add to group", self.addToGroup)
24  ]))
25 
26  # Add mesh
27  box.addWidget(self.bindFunctionToButton("Add mesh", self.addMesh))
28 
29  # Add box
30  box.addWidget(self.bindFunctionToButton("Add box", self.addBox))
31 
32  box.addWidget(self.bindFunctionToButton("Create window", self.createWindow))
33 
34  self.update()
35 
36  def update(self):
37  self.groupNodes.clear()
38  for n in self.plugin.client.gui.getSceneList():
39  self.groupNodes.addItem (n)
40 
41  def addWidgetsInHBox(self, widgets):
42  nameParentW = QtGui.QWidget(self)
43  hboxName = QtGui.QHBoxLayout(nameParentW)
44  for w in widgets:
45  hboxName.addWidget(w)
46  return nameParentW
47 
48  def bindFunctionToButton (self, buttonLabel, func):
49  button = QtGui.QPushButton(self)
50  button.text = buttonLabel
51  button.connect ('clicked()', func)
52  return button
53 
54  def addMesh (self):
55  filename = QtGui.QFileDialog.getOpenFileName (self, "Choose a mesh")
56  self.plugin.client.gui.addMesh(str(self.nodeName.text), str(filename))
57  self.refreshBodyTree()
58 
59  def addBox (self):
60  self.plugin.client.gui.addBox(str(self.nodeName.text), 1, 1, 1, [1, 0, 0, 1])
61  self.refreshBodyTree()
62 
63  def createWindow (self):
64  self.plugin.windowsManager.createWindow(str(self.nodeName.text))
65 
66  def createGroup (self):
67  self.plugin.client.gui.createGroup(str(self.nodeName.text))
68  self.groupNodes.addItem(self.nodeName.text)
69  self.refreshBodyTree()
70 
71  def addToGroup (self):
72  self.plugin.client.gui.addToGroup(str(self.nodeName.text), str(self.groupNodes.currentText))
73  self.refreshBodyTree()
74 
75 
76  def refreshBodyTree(self):
77  self.plugin.main.requestRefresh()
78 
79 class Plugin(QtGui.QDockWidget):
80  """
81  Example of plugin of the Gepetto Viewer GUI. This can interact with
82  PythonWidget C++ class.
83  """
84  def __init__ (self, mainWindow, flags = None):
85  if flags is None:
86  super(Plugin, self).__init__ ("Gepetto Viewer plugin", mainWindow)
87  else:
88  super(Plugin, self).__init__ ("Gepetto Viewer plugin", mainWindow, flags)
89  self.setObjectName("Gepetto Viewer plugin")
90  self.client = Client()
91  # Initialize the widget
92  self.tabWidget = QtGui.QTabWidget(self)
93  # This avoids having a widget bigger than what it needs. It avoids having
94  # a big dock widget and a small osg widget when creating the main osg widget.
95  p = Qt.QSizePolicy.Maximum
96  self.tabWidget.setSizePolicy(Qt.QSizePolicy(p,p))
97  self.setWidget (self.tabWidget)
98  self.nodeCreator = _NodeCreator(self, self)
99  self.tabWidget.addTab (self.nodeCreator, "Node Creator")
100  self.main = mainWindow
101  self.windowsManager = windowsManager
102  mainWindow.connect('refresh()', self.refresh)
103  mainWindow.bodyTree().connect('bodySelected(SelectionEvent*)', self.selected)
104 
105 
106  def osgWidget(self, osgWindow):
107  pass
108 
109  def resetConnection(self):
110  self.client = Client()
111 
112  def refresh(self):
113  self.nodeCreator.update()
114 
115  def selected(self, event):
116  #QtGui.QMessageBox.information(self, "Selected object", event.nodeName() + " " + str(event.point(False)))
117  print "name: ", event.nodeName()
118  if event.hasIntersection():
119  print "point LF: ", event.point(True)
120  print "point WF: ", event.point(False)
121  print "normal LF: ", event.normal(True)
122  print "normal WF: ", event.normal(False)