From 57b5b3f876532296e7a59952a7475a9d1738131e Mon Sep 17 00:00:00 2001 From: Joseph Mirabel <jmirabel@laas.fr> Date: Fri, 24 Nov 2017 14:27:30 +0100 Subject: [PATCH] Add Matplotlib python widget --- pyplugins/CMakeLists.txt | 2 ++ pyplugins/gepetto/gui/matplotlib_example.py | 36 +++++++++++++++++++++ pyplugins/gepetto/gui/matplotlibwidget.py | 24 ++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 pyplugins/gepetto/gui/matplotlib_example.py create mode 100644 pyplugins/gepetto/gui/matplotlibwidget.py diff --git a/pyplugins/CMakeLists.txt b/pyplugins/CMakeLists.txt index 8cabd85..a2d1959 100644 --- a/pyplugins/CMakeLists.txt +++ b/pyplugins/CMakeLists.txt @@ -19,6 +19,8 @@ INSTALL( FILES ${CMAKE_CURRENT_SOURCE_DIR}/gepetto/gui/pythonwidget.py ${CMAKE_CURRENT_SOURCE_DIR}/gepetto/gui/blenderexport.py + ${CMAKE_CURRENT_SOURCE_DIR}/gepetto/gui/matplotlibwidget.py + ${CMAKE_CURRENT_SOURCE_DIR}/gepetto/gui/matplotlib_example.py ${CMAKE_CURRENT_SOURCE_DIR}/gepetto/gui/__init__.py DESTINATION ${PYTHON_SITELIB}/gepetto/gui ) diff --git a/pyplugins/gepetto/gui/matplotlib_example.py b/pyplugins/gepetto/gui/matplotlib_example.py new file mode 100644 index 0000000..b80b29e --- /dev/null +++ b/pyplugins/gepetto/gui/matplotlib_example.py @@ -0,0 +1,36 @@ +from PythonQt import QtGui, Qt +from gepetto.corbaserver import Client +import numpy as np + +from gepetto.gui.matplotlibwidget import MatplotlibWidget + +class Plugin(QtGui.QDockWidget): + """ + Example of plugin using matplotlib + """ + def __init__ (self, mainWindow, flags = None): + if flags is None: + super(Plugin, self).__init__ ("Matplotlib example plugin", mainWindow) + else: + super(Plugin, self).__init__ ("Matplotlib example plugin", mainWindow, flags) + self.setObjectName("Matplotlib example plugin") + self.client = Client() + + # This avoids having a widget bigger than what it needs. It avoids having + # a big dock widget and a small osg widget when creating the main osg widget. + p = Qt.QSizePolicy.Ignored + self.testWidget = MatplotlibWidget(self, True) + self.testWidget.setSizePolicy(Qt.QSizePolicy(p,p)) + self.setWidget (self.testWidget) + + # Plot something + x = np.linspace (0, 10, num=100) + y = np.sin(x) + self.testWidget.figure.gca().plot (x, y) + + ### If present, this function is called when a new OSG Widget is created. + def osgWidget(self, osgWindow): + pass + + def resetConnection(self): + self.client = Client() diff --git a/pyplugins/gepetto/gui/matplotlibwidget.py b/pyplugins/gepetto/gui/matplotlibwidget.py new file mode 100644 index 0000000..1c8c6e7 --- /dev/null +++ b/pyplugins/gepetto/gui/matplotlibwidget.py @@ -0,0 +1,24 @@ +from matplotlib.figure import Figure +import matplotlib.pyplot as plt +from pythonqt.matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg, NavigationToolbar2QT + +from PythonQt import QtGui, Qt +from gepetto.corbaserver import Client + +import numpy as np + +### This class represents one special tab of the new QDockWidget +class MatplotlibWidget (QtGui.QWidget): + def __init__(self, parent, withToolbar = False): + super(MatplotlibWidget, self).__init__ (parent) + + box = QtGui.QVBoxLayout(self) + # Create a figure + self.figure = Figure(figsize=(5,5), dpi=96) + # self.figure = Figure() + self.canvas = FigureCanvasQTAgg (self.figure) + box.addWidget(self.canvas) + + if withToolbar: + self.toolbar = NavigationToolbar2QT(self.canvas, parent, False) + box.addWidget(self.toolbar) -- GitLab