@@ -172,8 +172,9 @@ def get_next_colour(idx):
172172class ModLegend (pg .LegendItem ):
173173 """
174174 Modified LegendItem to remove the ugly / in the label. Also reduces text size and padding.
175+ Pass offset=None when positioning via anchor() instead.
175176 """
176- def __init__ (self , offset , text_size = '9pt' ):
177+ def __init__ (self , offset = None , text_size = '9pt' ):
177178 self .text_size = text_size
178179 LegendItem .__init__ (self , None , offset )
179180
@@ -216,8 +217,12 @@ def create_help_pltitm():
216217 plt .axes ['left' ]['item' ].hide ()
217218 plt .axes ['bottom' ]['item' ].hide ()
218219
219- plt .legend = ModLegend (offset = ( - 140 , 60 ) )
220+ plt .legend = ModLegend (offset = None )
220221 plt .legend .setParentItem (plt .vb )
222+ # anchor: item point (0.5, 0.5) = centre of legend
223+ # parent point (0.75, 0.75) = centre of lower-right quadrant - but (0.60, 0.45)
224+ # looks better ...
225+ plt .legend .anchor (itemPos = (0.5 , 0.5 ), parentPos = (0.60 , 0.45 ), offset = (0 , 0 ))
221226
222227 for l in get_help_lines ():
223228 plt .plot ([0 ], [0 ], name = l )
@@ -335,13 +340,21 @@ def _init_2dmode(self):
335340 self ._unzoom ()
336341
337342 def unzoom_handler (event ):
338- if event .button () != 2 :
343+ try :
344+ right = QtCore .Qt .MouseButton .RightButton
345+ except AttributeError :
346+ right = QtCore .Qt .RightButton
347+ if event .button () != right :
339348 return
340349 if self .zoomstate == self .ZoomState .ZOOM :
341350 self ._unzoom ()
342-
351+
343352 def zoom_handler (event , item = None , idx = None ):
344- if event .button () != 1 :
353+ try :
354+ left = QtCore .Qt .MouseButton .LeftButton
355+ except AttributeError :
356+ left = QtCore .Qt .LeftButton
357+ if event .button () != left :
345358 return
346359 if self .zoomstate == self .ZoomState .UNZOOM and event .currentItem == item :
347360 self ._zoom (idx )
@@ -406,7 +419,7 @@ def _get_comp_color_pairs(self):
406419 def run_ui (self , instr , rays ):
407420 ''' '''
408421 self ._init_2dmode ()
409- self ._set_and_plot_instr (instr )
422+ self ._set_and_plot_instr (instr , enable_clickable = True )
410423 if not rays == []:
411424 self ._set_rays (rays )
412425 self ._unzoom ()
@@ -461,12 +474,36 @@ def _set_and_plot_instr(self, instr, enable_clickable=False):
461474
462475 # set PlotDataItem click events
463476 if enable_clickable :
477+ self ._comp_curve_pairs = []
464478 for pairs in [comp_plotdataitm_pairs_zy , comp_plotdataitm_pairs_xy , comp_plotdataitm_pairs_zx ]:
465479 for p in pairs :
466480 comp = p [0 ]
467481 itm = p [1 ]
468- itm .curve .setClickable (True )
469- itm .curve .mouseClickEvent = lambda event , comp = comp : self ._handle_comp_clicked (event , comp )
482+ itm .curve .opts ['mouseWidth' ] = 8 # widen hit area to 8px
483+ self ._comp_curve_pairs .append ((comp , itm .curve ))
484+
485+ def _scene_click_handler (event ):
486+ btn = event .button ()
487+ pos = event .scenePos ()
488+ try :
489+ left = QtCore .Qt .MouseButton .LeftButton # PyQt6
490+ except AttributeError :
491+ left = QtCore .Qt .LeftButton # PyQt5
492+ if btn != left :
493+ return
494+ for comp , curve in self ._comp_curve_pairs :
495+ curve ._mouseShape = None
496+ scene_shape = curve .mapToScene (curve .mouseShape ())
497+ if scene_shape .contains (pos ):
498+ self ._handle_comp_clicked (event , comp )
499+ return
500+
501+ scenes_connected = set ()
502+ for plt in [self .plt_zy , self .plt_xy , self .plt_zx ]:
503+ sc = plt .scene ()
504+ if id (sc ) not in scenes_connected :
505+ sc .sigMouseClicked .connect (_scene_click_handler )
506+ scenes_connected .add (id (sc ))
470507
471508 def _handle_comp_clicked (self , event , comp ):
472509 ''' display clicked component info '''
0 commit comments