I am trying to capture mouse drag events as the user moves a selectable line shape. I want this so I can do some image intensity profiles of a line shape as it is dragged around the screen. Similar to the discussion here and the example code here
I am using the
mouse_drag_callbacks.append decorator as follows. Where
self.shapeLayer is returned from napari.Viewer.add_shapes and
self.myMouseDrag_Shape is a member function of my class
@self.shapeLayer.mouse_drag_callbacks.append def shape_mouse_drag_callback(layer, event): self.myMouseDrag_Shape(layer, event)
With this, I always receive an
event.type=='mouse_press' of type(event)==
But I never receive any additional events while the mouse is still down and the user is dragging the line shape around?
Not sure if this is related. As I am dragging the line shape, I do get this warning. Presumably for each mouse move/drag:
/Users/cudmore/anaconda3/lib/python3.7/site-packages/vispy/visuals/markers.py:564: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. ('a_edgewidth', np.float32, 1)])
Here is a minimal working example I would like to get working. I would like to get mouse move events as the user drags a line shape. Any clues as to what I am doing wrong? In particular, I don’t understand the docstring with ‘“dragging” on its own line followed by yield’?
import napari class bNapari: def __init__(self, title): self.viewer = napari.Viewer(title=title) # # shapes layer with 2x lines line1 = np.array([[11, 13], [111, 113]]) line2 = np.array([[200, 200], [400, 300]]) lines = [line1, line2] self.shapeLayer = self.viewer.add_shapes(lines, shape_type='line', edge_width = 5, edge_color = 'coral', face_color = 'royalblue') self.shapeLayer.mode = 'select' print('type(self.shapeLayer.data):', type(self.shapeLayer.data)) print('self.shapeLayer.data:', self.shapeLayer.data) # see: # https://github.com/napari/napari/pull/544# make a callback for all mouse moves """ # this works fine but as discussed does not get called durnig dragging @self.shapeLayer.mouse_move_callbacks.append def shape_mouse_move_callback(viewer, event): self.myMouseMove_Shape(viewer, event) """ @self.shapeLayer.mouse_drag_callbacks.append def shape_mouse_drag_callback(layer, event): self.myMouseDrag_Shape(layer, event) ''' # this works fine but as discussed does not get called durnig dragging def myMouseMove_Shape(self, layer, event): """ event is type vispy.app.canvas.MouseEvent see: http://api.vispy.org/en/v0.1.0-0/event.html """ print('myMouseMove_Shape() layer:', layer, 'event:', event, type(event), 'type:', event.type, 'button', event.button) ind_x, ind_y = np.round(layer.coordinates).astype('int') print(' myMouseMove_Shape()', ind_x, ind_y) ''' def myMouseDrag_Shape(self, layer, event): """ event is type napari.util.misc.ReadOnlyWrapper """ ind_x, ind_y = np.round(layer.coordinates).astype('int') print('******', 'myMouseDrag_Shape() layer:', layer, 'event:', event, 'type(event):', type(event), 'event.type:', event.type, 'event.button', event.button) print(' x:', ind_x, 'y:', ind_y) # This is from the docstring and the discussion # see: https://github.com/napari/napari/pull/544 # I don't understand the sytax where you have a string sitting along on a line, e.g. "dragging" ''' "dragging" yield # on move while event.type == 'mouse_move': print(event.pos) yield # on release print('goodbye world ;(') ''' if __name__ == '__main__': from PyQt5 import QtGui, QtCore, QtWidgets app = QtWidgets.QApplication(sys.argv) mn = bNapari('drag line shape and get mouse drag position') sys.exit(app.exec_())
os: macOS 10.12.6
Thanks in advance and a huge thanks for making an amazing viewer. I am dedicated to using Napari.