Improve control over ROI/handle pens (#1285)
* Exposed ability to set pens for handles and hovering for ROIs * Consistent color format for pen creation * Exposed ability to set pens for handles and hovering for ROIs * Consistent color format for pen creation * Add handleHoverPen arg to ROI and rename Handle arg to hoverPen Co-authored-by: nmearl <nchlsearl@gmail.com>
This commit is contained in:
parent
3947d34ae4
commit
3cf2845743
@ -86,6 +86,12 @@ class ROI(GraphicsObject):
|
|||||||
is generally not necessary to specify the parent.
|
is generally not necessary to specify the parent.
|
||||||
pen (QPen or argument to pg.mkPen) The pen to use when drawing
|
pen (QPen or argument to pg.mkPen) The pen to use when drawing
|
||||||
the shape of the ROI.
|
the shape of the ROI.
|
||||||
|
hoverPen (QPen or argument to mkPen) The pen to use while the
|
||||||
|
mouse is hovering over the ROI shape.
|
||||||
|
handlePen (QPen or argument to mkPen) The pen to use when drawing
|
||||||
|
the ROI handles.
|
||||||
|
handleHoverPen (QPen or argument to mkPen) The pen to use while the mouse
|
||||||
|
is hovering over an ROI handle.
|
||||||
movable (bool) If True, the ROI can be moved by dragging anywhere
|
movable (bool) If True, the ROI can be moved by dragging anywhere
|
||||||
inside the ROI. Default is True.
|
inside the ROI. Default is True.
|
||||||
rotatable (bool) If True, the ROI can be rotated by mouse drag + ALT
|
rotatable (bool) If True, the ROI can be rotated by mouse drag + ALT
|
||||||
@ -128,10 +134,11 @@ class ROI(GraphicsObject):
|
|||||||
sigClicked = QtCore.Signal(object, object)
|
sigClicked = QtCore.Signal(object, object)
|
||||||
sigRemoveRequested = QtCore.Signal(object)
|
sigRemoveRequested = QtCore.Signal(object)
|
||||||
|
|
||||||
def __init__(self, pos, size=Point(1, 1), angle=0.0, invertible=False, maxBounds=None,
|
def __init__(self, pos, size=Point(1, 1), angle=0.0, invertible=False,
|
||||||
snapSize=1.0, scaleSnap=False, translateSnap=False, rotateSnap=False,
|
maxBounds=None, snapSize=1.0, scaleSnap=False,
|
||||||
parent=None, pen=None, movable=True, rotatable=True, resizable=True,
|
translateSnap=False, rotateSnap=False, parent=None, pen=None,
|
||||||
removable=False):
|
hoverPen=None, handlePen=None, handleHoverPen=None,
|
||||||
|
movable=True, rotatable=True, resizable=True, removable=False):
|
||||||
GraphicsObject.__init__(self, parent)
|
GraphicsObject.__init__(self, parent)
|
||||||
self.setAcceptedMouseButtons(QtCore.Qt.NoButton)
|
self.setAcceptedMouseButtons(QtCore.Qt.NoButton)
|
||||||
pos = Point(pos)
|
pos = Point(pos)
|
||||||
@ -145,11 +152,20 @@ class ROI(GraphicsObject):
|
|||||||
|
|
||||||
self.freeHandleMoved = False ## keep track of whether free handles have moved since last change signal was emitted.
|
self.freeHandleMoved = False ## keep track of whether free handles have moved since last change signal was emitted.
|
||||||
self.mouseHovering = False
|
self.mouseHovering = False
|
||||||
|
|
||||||
if pen is None:
|
if pen is None:
|
||||||
pen = (255, 255, 255)
|
pen = (255, 255, 255)
|
||||||
self.setPen(pen)
|
self.setPen(pen)
|
||||||
|
if hoverPen is None:
|
||||||
self.handlePen = QtGui.QPen(QtGui.QColor(150, 255, 255))
|
hoverPen = (255, 255, 0)
|
||||||
|
self.hoverPen = fn.mkPen(hoverPen)
|
||||||
|
if handlePen is None:
|
||||||
|
handlePen = (150, 255, 255)
|
||||||
|
self.handlePen = fn.mkPen(handlePen)
|
||||||
|
if handleHoverPen is None:
|
||||||
|
handleHoverPen = (255, 255, 0)
|
||||||
|
self.handleHoverPen = handleHoverPen
|
||||||
|
|
||||||
self.handles = []
|
self.handles = []
|
||||||
self.state = {'pos': Point(0,0), 'size': Point(1,1), 'angle': 0} ## angle is in degrees for ease of Qt integration
|
self.state = {'pos': Point(0,0), 'size': Point(1,1), 'angle': 0} ## angle is in degrees for ease of Qt integration
|
||||||
self.lastState = None
|
self.lastState = None
|
||||||
@ -589,7 +605,8 @@ class ROI(GraphicsObject):
|
|||||||
def addHandle(self, info, index=None):
|
def addHandle(self, info, index=None):
|
||||||
## If a Handle was not supplied, create it now
|
## If a Handle was not supplied, create it now
|
||||||
if 'item' not in info or info['item'] is None:
|
if 'item' not in info or info['item'] is None:
|
||||||
h = Handle(self.handleSize, typ=info['type'], pen=self.handlePen, parent=self)
|
h = Handle(self.handleSize, typ=info['type'], pen=self.handlePen,
|
||||||
|
hoverPen=self.handleHoverPen, parent=self)
|
||||||
info['item'] = h
|
info['item'] = h
|
||||||
else:
|
else:
|
||||||
h = info['item']
|
h = info['item']
|
||||||
@ -735,7 +752,7 @@ class ROI(GraphicsObject):
|
|||||||
def _makePen(self):
|
def _makePen(self):
|
||||||
# Generate the pen color for this ROI based on its current state.
|
# Generate the pen color for this ROI based on its current state.
|
||||||
if self.mouseHovering:
|
if self.mouseHovering:
|
||||||
return fn.mkPen(255, 255, 0)
|
return self.hoverPen
|
||||||
else:
|
else:
|
||||||
return self.pen
|
return self.pen
|
||||||
|
|
||||||
@ -1273,12 +1290,13 @@ class Handle(UIGraphicsItem):
|
|||||||
sigClicked = QtCore.Signal(object, object) # self, event
|
sigClicked = QtCore.Signal(object, object) # self, event
|
||||||
sigRemoveRequested = QtCore.Signal(object) # self
|
sigRemoveRequested = QtCore.Signal(object) # self
|
||||||
|
|
||||||
def __init__(self, radius, typ=None, pen=(200, 200, 220), parent=None, deletable=False, activePen=(255, 255, 0)):
|
def __init__(self, radius, typ=None, pen=(200, 200, 220),
|
||||||
|
hoverPen=(255, 255, 0), parent=None, deletable=False):
|
||||||
self.rois = []
|
self.rois = []
|
||||||
self.radius = radius
|
self.radius = radius
|
||||||
self.typ = typ
|
self.typ = typ
|
||||||
self.pen = fn.mkPen(pen)
|
self.pen = fn.mkPen(pen)
|
||||||
self.activePen = fn.mkPen(activePen)
|
self.hoverPen = fn.mkPen(hoverPen)
|
||||||
self.currentPen = self.pen
|
self.currentPen = self.pen
|
||||||
self.pen.setWidth(0)
|
self.pen.setWidth(0)
|
||||||
self.pen.setCosmetic(True)
|
self.pen.setCosmetic(True)
|
||||||
@ -1308,7 +1326,7 @@ class Handle(UIGraphicsItem):
|
|||||||
self.setAcceptedMouseButtons(self.acceptedMouseButtons() | QtCore.Qt.RightButton)
|
self.setAcceptedMouseButtons(self.acceptedMouseButtons() | QtCore.Qt.RightButton)
|
||||||
else:
|
else:
|
||||||
self.setAcceptedMouseButtons(self.acceptedMouseButtons() & ~QtCore.Qt.RightButton)
|
self.setAcceptedMouseButtons(self.acceptedMouseButtons() & ~QtCore.Qt.RightButton)
|
||||||
|
|
||||||
def removeClicked(self):
|
def removeClicked(self):
|
||||||
self.sigRemoveRequested.emit(self)
|
self.sigRemoveRequested.emit(self)
|
||||||
|
|
||||||
@ -1322,7 +1340,7 @@ class Handle(UIGraphicsItem):
|
|||||||
hover=True
|
hover=True
|
||||||
|
|
||||||
if hover:
|
if hover:
|
||||||
self.currentPen = self.activePen
|
self.currentPen = self.hoverPen
|
||||||
else:
|
else:
|
||||||
self.currentPen = self.pen
|
self.currentPen = self.pen
|
||||||
self.update()
|
self.update()
|
||||||
@ -1383,11 +1401,11 @@ class Handle(UIGraphicsItem):
|
|||||||
self.isMoving = True
|
self.isMoving = True
|
||||||
self.startPos = self.scenePos()
|
self.startPos = self.scenePos()
|
||||||
self.cursorOffset = self.scenePos() - ev.buttonDownScenePos()
|
self.cursorOffset = self.scenePos() - ev.buttonDownScenePos()
|
||||||
self.currentPen = self.activePen
|
self.currentPen = self.hoverPen
|
||||||
|
|
||||||
if self.isMoving: ## note: isMoving may become False in mid-drag due to right-click.
|
if self.isMoving: ## note: isMoving may become False in mid-drag due to right-click.
|
||||||
pos = ev.scenePos() + self.cursorOffset
|
pos = ev.scenePos() + self.cursorOffset
|
||||||
self.currentPen = self.activePen
|
self.currentPen = self.hoverPen
|
||||||
self.movePoint(pos, ev.modifiers(), finish=False)
|
self.movePoint(pos, ev.modifiers(), finish=False)
|
||||||
|
|
||||||
def movePoint(self, pos, modifiers=QtCore.Qt.KeyboardModifier(), finish=True):
|
def movePoint(self, pos, modifiers=QtCore.Qt.KeyboardModifier(), finish=True):
|
||||||
|
Loading…
Reference in New Issue
Block a user