|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +An example of using the backend |
| 4 | +""" |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from matplotlib_backend_qtquick.backend_qtquick import ( |
| 10 | + NavigationToolbar2QtQuick) |
| 11 | +from matplotlib_backend_qtquick.backend_qtquickagg import ( |
| 12 | + FigureCanvasQtQuickAgg) |
| 13 | +from matplotlib_backend_qtquick.qt_compat import QtGui, QtQml, QtCore |
| 14 | + |
| 15 | + |
| 16 | +class DisplayBridge(QtCore.QObject): |
| 17 | + """ A bridge class to interact with the plot in python |
| 18 | + """ |
| 19 | + coordinatesChanged = QtCore.Signal(str) |
| 20 | + |
| 21 | + def __init__(self, parent=None): |
| 22 | + super().__init__(parent) |
| 23 | + |
| 24 | + # The figure and toolbar |
| 25 | + self.figure = None |
| 26 | + self.toolbar = None |
| 27 | + |
| 28 | + # this is used to display the coordinates of the mouse in the window |
| 29 | + self._coordinates = "" |
| 30 | + |
| 31 | + def updateWithCanvas(self, canvas): |
| 32 | + """ initialize with the canvas for the figure |
| 33 | + """ |
| 34 | + self.figure = canvas.figure |
| 35 | + self.toolbar = NavigationToolbar2QtQuick(canvas=canvas) |
| 36 | + |
| 37 | + # make a small plot |
| 38 | + self.axes = self.figure.add_subplot(111) |
| 39 | + self.axes.grid(True) |
| 40 | + |
| 41 | + x = np.linspace(0, 2*np.pi, 100) |
| 42 | + y = np.sin(x) |
| 43 | + |
| 44 | + self.axes.plot(x, y) |
| 45 | + canvas.draw_idle() |
| 46 | + |
| 47 | + # connect for displaying the coordinates |
| 48 | + self.figure.canvas.mpl_connect('motion_notify_event', self.on_motion) |
| 49 | + |
| 50 | + # define the coordinates property |
| 51 | + # (I have had problems using the @QtCore.Property directy in the past) |
| 52 | + def getCoordinates(self): |
| 53 | + return self._coordinates |
| 54 | + |
| 55 | + def setCoordinates(self, coordinates): |
| 56 | + self._coordinates = coordinates |
| 57 | + self.coordinatesChanged.emit(self._coordinates) |
| 58 | + |
| 59 | + coordinates = QtCore.Property(str, getCoordinates, setCoordinates, |
| 60 | + notify=coordinatesChanged) |
| 61 | + |
| 62 | + # The toolbar commands |
| 63 | + @QtCore.Slot() |
| 64 | + def pan(self, *args): |
| 65 | + """Activate the pan tool.""" |
| 66 | + self.toolbar.pan(*args) |
| 67 | + |
| 68 | + @QtCore.Slot() |
| 69 | + def zoom(self, *args): |
| 70 | + """activate zoom tool.""" |
| 71 | + self.toolbar.zoom(*args) |
| 72 | + |
| 73 | + @QtCore.Slot() |
| 74 | + def home(self, *args): |
| 75 | + self.toolbar.home(*args) |
| 76 | + |
| 77 | + @QtCore.Slot() |
| 78 | + def back(self, *args): |
| 79 | + self.toolbar.back(*args) |
| 80 | + |
| 81 | + @QtCore.Slot() |
| 82 | + def forward(self, *args): |
| 83 | + self.toolbar.forward(*args) |
| 84 | + |
| 85 | + def on_motion(self, event): |
| 86 | + """ |
| 87 | + Update the coordinates on the display |
| 88 | + """ |
| 89 | + if event.inaxes == self.axes: |
| 90 | + self.coordinates = f"({event.xdata:.2f}, {event.ydata:.2f})" |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) |
| 94 | + app = QtGui.QGuiApplication(sys.argv) |
| 95 | + engine = QtQml.QQmlApplicationEngine() |
| 96 | + |
| 97 | + # instantate the display bridge |
| 98 | + displayBridge = DisplayBridge() |
| 99 | + |
| 100 | + # Expose the Python object to QML |
| 101 | + context = engine.rootContext() |
| 102 | + context.setContextProperty("displayBridge", displayBridge) |
| 103 | + |
| 104 | + # matplotlib stuff |
| 105 | + QtQml.qmlRegisterType(FigureCanvasQtQuickAgg, "Backend", 1, 0, "FigureCanvas") |
| 106 | + |
| 107 | + # Load the QML file |
| 108 | + qmlFile = Path(Path.cwd(), Path(__file__).parent, "main.qml") |
| 109 | + engine.load(QtCore.QUrl.fromLocalFile(str(qmlFile))) |
| 110 | + |
| 111 | + win = engine.rootObjects()[0] |
| 112 | + displayBridge.updateWithCanvas(win.findChild(QtCore.QObject, "figure")) |
| 113 | + |
| 114 | + # execute and cleanup |
| 115 | + app.exec_() |
0 commit comments