Skip to content

Commit

Permalink
Merge pull request #3904 from t20100/update-marker-end-drag
Browse files Browse the repository at this point in the history
silx.gui.plot: Fixed PlotWidget mouse cursor update
  • Loading branch information
t20100 authored Jul 11, 2023
2 parents 577d260 + 385236d commit 95d0d9d
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions src/silx/gui/plot/PlotInteraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,13 @@ def __init__(self, *args, **kw):
super(ItemsInteraction.Idle, self).__init__(*args, **kw)
self._hoverMarker = None

def enterState(self):
widget = self.machine.plot.getWidgetHandle()
if widget is None or not widget.isVisible():
return
position = widget.mapFromGlobal(qt.QCursor.pos())
self.onMove(position.x(), position.y())

def onMove(self, x, y):
marker = self.machine.plot._getMarkerAt(x, y)

Expand All @@ -1112,22 +1119,7 @@ def onMove(self, x, y):

if marker != self._hoverMarker:
self._hoverMarker = marker

if marker is None:
self.machine.plot.setGraphCursorShape()

elif marker.isDraggable():
if isinstance(marker, items.YMarker):
self.machine.plot.setGraphCursorShape(CURSOR_SIZE_VER)
elif isinstance(marker, items.XMarker):
self.machine.plot.setGraphCursorShape(CURSOR_SIZE_HOR)
else:
self.machine.plot.setGraphCursorShape(CURSOR_SIZE_ALL)

elif marker.isSelectable():
self.machine.plot.setGraphCursorShape(CURSOR_POINTING)
else:
self.machine.plot.setGraphCursorShape()
self.machine._setCursorForMarker(marker)

return True

Expand All @@ -1139,6 +1131,27 @@ def __init__(self, plot):
clickButtons=(LEFT_BTN, RIGHT_BTN),
dragButtons=(LEFT_BTN, MIDDLE_BTN))

def _setCursorForMarker(self, marker: Optional[items.MarkerBase] = None):
"""Set mouse cursor for given marker"""
if marker is None:
cursor = None

elif marker.isDraggable():
if isinstance(marker, items.YMarker):
cursor = CURSOR_SIZE_VER
elif isinstance(marker, items.XMarker):
cursor = CURSOR_SIZE_HOR
else:
cursor = CURSOR_SIZE_ALL

elif marker.isSelectable():
cursor = CURSOR_POINTING

else:
cursor = None

self.plot.setGraphCursorShape(cursor)

def click(self, x, y, btn):
"""Handle mouse click
Expand Down Expand Up @@ -1253,9 +1266,9 @@ def _signalMarkerMovingEvent(self, eventType, marker, x, y):
def __isDraggableItem(item):
return isinstance(item, items.DraggableMixIn) and item.isDraggable()

def __terminateDrag(self):
def __terminateDrag(self, x, y):
"""Finalize a drag operation by reseting to initial state"""
self.plot.setGraphCursorShape()
self._setCursorForMarker(self.plot._getMarkerAt(x, y))
self.draggedItemRef = None

def beginDrag(self, x, y, btn):
Expand All @@ -1276,7 +1289,7 @@ def beginDrag(self, x, y, btn):
self.draggedItemRef = None if item is None else weakref.ref(item)

if item is None:
self.__terminateDrag()
self.__terminateDrag(x, y)
return False

if isinstance(item, items.MarkerBase):
Expand Down Expand Up @@ -1325,13 +1338,17 @@ def endDrag(self, startPos, endPos, btn):
self.plot.notify(**eventDict)
item._endDrag()

self.__terminateDrag()
self.__terminateDrag(*endPos)
elif btn == MIDDLE_BTN:
self._pan.endDrag(startPos, endPos, btn)

def cancel(self):
self._pan.cancel()
self.__terminateDrag()
widget = self.plot.getWidgetHandle()
if widget is None or not widget.isVisible():
return
position = widget.mapFromGlobal(qt.QCursor.pos())
self.__terminateDrag(position.x(), position.y())


class ItemsInteractionForCombo(ItemsInteraction):
Expand Down

0 comments on commit 95d0d9d

Please sign in to comment.