vispy_example.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. from PyQt5.QtGui import QPalette
  2. from PyQt5 import QtCore, QtWidgets
  3. import vispy.scene as scene
  4. from vispy.scene.visuals import Rectangle, Text
  5. from vispy.color import Color
  6. import sys
  7. class VisPyCanvas(scene.SceneCanvas):
  8. def __init__(self, config=None):
  9. super().__init__(config=config, keys=None)
  10. self.unfreeze()
  11. # Colors used by the Scene
  12. theme_color = Color('#FFFFFF')
  13. tick_color = Color('#000000')
  14. back_color = str(QPalette().color(QPalette.Window).name())
  15. # Central Widget Colors
  16. self.central_widget.bgcolor = back_color
  17. self.central_widget.border_color = back_color
  18. self.grid_widget = self.central_widget.add_grid(margin=10)
  19. self.grid_widget.spacing = 0
  20. # TOP Padding
  21. top_padding = self.grid_widget.add_widget(row=0, col=0, col_span=2)
  22. top_padding.height_max = 0
  23. # RIGHT Padding
  24. right_padding = self.grid_widget.add_widget(row=0, col=2, row_span=2)
  25. right_padding.width_max = 0
  26. # X Axis
  27. self.xaxis = scene.AxisWidget(
  28. orientation='bottom', axis_color=tick_color, text_color=tick_color,
  29. font_size=8, axis_width=1,
  30. anchors=['center', 'bottom']
  31. )
  32. self.xaxis.height_max = 30
  33. self.grid_widget.add_widget(self.xaxis, row=2, col=1)
  34. # Y Axis
  35. self.yaxis = scene.AxisWidget(
  36. orientation='left', axis_color=tick_color, text_color=tick_color,
  37. font_size=8, axis_width=1
  38. )
  39. self.yaxis.width_max = 55
  40. self.grid_widget.add_widget(self.yaxis, row=1, col=0)
  41. # View & Camera
  42. self.view = self.grid_widget.add_view(row=1, col=1, border_color=tick_color,
  43. bgcolor=theme_color)
  44. self.view.camera = scene.PanZoomCamera(aspect=1, rect=(-25, -25, 150, 150))
  45. self.xaxis.link_view(self.view)
  46. self.yaxis.link_view(self.view)
  47. self.grid = scene.GridLines(parent=self.view.scene, color='dimgray')
  48. self.grid.set_gl_state(depth_test=False)
  49. self.rect = Rectangle(center=(65,30), color=Color('#0000FF10'), border_color=Color('#0000FF10'),
  50. width=120, height=50, radius=[5, 5, 5, 5], parent=self.view)
  51. self.rect.set_gl_state(depth_test=False)
  52. self.text = Text('', parent=self.view, color='black', pos=(5, 30), method='gpu', anchor_x='left')
  53. self.text.font_size = 8
  54. self.text.text = 'Coordinates:\nX: %s\nY: %s' % ('0.0000', '0.0000')
  55. self.freeze()
  56. # self.measure_fps()
  57. class PlotCanvas(QtCore.QObject):
  58. def __init__(self, container, my_app):
  59. """
  60. The constructor configures the VisPy figure that
  61. will contain all plots, creates the base axes and connects
  62. events to the plotting area.
  63. :param container: The parent container in which to draw plots.
  64. :rtype: PlotCanvas
  65. """
  66. super().__init__()
  67. # VisPyCanvas instance
  68. self.vispy_canvas = VisPyCanvas()
  69. self.vispy_canvas.unfreeze()
  70. self.my_app = my_app
  71. # Parent container
  72. self.container = container
  73. # <VisPyCanvas>
  74. self.vispy_canvas.create_native()
  75. self.vispy_canvas.native.setParent(self.my_app.ui)
  76. # <QtCore.QObject>
  77. self.container.addWidget(self.vispy_canvas.native)
  78. # add two Infinite Lines to act as markers for the X,Y axis
  79. self.v_line = scene.visuals.InfiniteLine(
  80. pos=0, color=(0.0, 0.0, 1.0, 0.3), vertical=True,
  81. parent=self.vispy_canvas.view.scene)
  82. self.h_line = scene.visuals.InfiniteLine(
  83. pos=0, color=(0.00, 0.0, 1.0, 0.3), vertical=False,
  84. parent=self.vispy_canvas.view.scene)
  85. self.vispy_canvas.freeze()
  86. def event_connect(self, event, callback):
  87. getattr(self.vispy_canvas.events, event).connect(callback)
  88. def event_disconnect(self, event, callback):
  89. getattr(self.vispy_canvas.events, event).disconnect(callback)
  90. def translate_coords(self, pos):
  91. """
  92. Translate pixels to canvas units.
  93. """
  94. tr = self.vispy_canvas.grid.get_transform('canvas', 'visual')
  95. return tr.map(pos)
  96. class MyGui(QtWidgets.QMainWindow):
  97. def __init__(self):
  98. super().__init__()
  99. self.setWindowTitle("VisPy Test")
  100. # add Menubar
  101. self.menu = self.menuBar()
  102. self.menufile = self.menu.addMenu("File")
  103. self.menuedit = self.menu.addMenu("Edit")
  104. self.menufhelp = self.menu.addMenu("Help")
  105. # add a Toolbar
  106. self.file_toolbar = QtWidgets.QToolBar("File Toolbar")
  107. self.addToolBar(self.file_toolbar)
  108. self.button = self.file_toolbar.addAction("Open")
  109. # add Central Widget
  110. self.c_widget = QtWidgets.QWidget()
  111. self.central_layout = QtWidgets.QVBoxLayout()
  112. self.c_widget.setLayout(self.central_layout)
  113. self.setCentralWidget(self.c_widget)
  114. # add InfoBar
  115. # self.infobar = self.statusBar()
  116. # self.position_label = QtWidgets.QLabel("Position: X: 0.0000\tY: 0.0000")
  117. # self.infobar.addWidget(self.position_label)
  118. class MyApp(QtCore.QObject):
  119. def __init__(self):
  120. super().__init__()
  121. self.ui = MyGui()
  122. self.plot = PlotCanvas(container=self.ui.central_layout, my_app=self)
  123. self.ui.show()
  124. self.plot.event_connect(event="mouse_move", callback=self.on_mouse_move)
  125. def on_mouse_move(self, event):
  126. cursor_pos = event.pos
  127. pos_canvas = self.plot.translate_coords(cursor_pos)
  128. # we don't need all the info in the tuple returned by the translate_coords()
  129. # only first 2 elements
  130. pos_canvas = [pos_canvas[0], pos_canvas[1]]
  131. self.ui.position_label.setText("Position: X: %.4f\tY: %.4f" % (pos_canvas[0], pos_canvas[1]))
  132. # pos_text = 'Coordinates: \nX: {:<7.4f}\nY: {:<7.4f}'.format(pos_canvas[0], pos_canvas[1])
  133. pos_text = 'Coordinates: \nX: {:<.4f}\nY: {:<.4f}'.format(pos_canvas[0], pos_canvas[1])
  134. self.plot.vispy_canvas.text.text = pos_text
  135. if __name__ == '__main__':
  136. app = QtWidgets.QApplication(sys.argv)
  137. m_app = MyApp()
  138. sys.exit(app.exec_())