don't cast buttons, enums and flags to int

This commit is contained in:
KIU Shueng Chuan 2021-01-17 12:23:49 +08:00
parent 3ce7f58384
commit 0fa4557ad6
7 changed files with 32 additions and 31 deletions

View File

@ -165,7 +165,7 @@ class GraphicsScene(QtGui.QGraphicsScene):
## set focus on the topmost focusable item under this click ## set focus on the topmost focusable item under this click
items = self.items(ev.scenePos()) items = self.items(ev.scenePos())
for i in items: for i in items:
if i.isEnabled() and i.isVisible() and int(i.flags() & i.ItemIsFocusable) > 0: if i.isEnabled() and i.isVisible() and (i.flags() & i.ItemIsFocusable):
i.setFocus(QtCore.Qt.MouseFocusReason) i.setFocus(QtCore.Qt.MouseFocusReason)
break break
@ -194,7 +194,7 @@ class GraphicsScene(QtGui.QGraphicsScene):
QtGui.QGraphicsScene.mouseMoveEvent(self, ev) QtGui.QGraphicsScene.mouseMoveEvent(self, ev)
# Next Deliver our own Hover Events # Next Deliver our own Hover Events
self.sendHoverEvents(ev) self.sendHoverEvents(ev)
if int(ev.buttons()) != 0: if ev.buttons():
# button is pressed' send mouseMoveEvents and mouseDragEvents # button is pressed' send mouseMoveEvents and mouseDragEvents
QtGui.QGraphicsScene.mouseMoveEvent(self, ev) QtGui.QGraphicsScene.mouseMoveEvent(self, ev)
if self.mouseGrabberItem() is None: if self.mouseGrabberItem() is None:
@ -202,17 +202,17 @@ class GraphicsScene(QtGui.QGraphicsScene):
init = False init = False
## keep track of which buttons are involved in dragging ## keep track of which buttons are involved in dragging
for btn in [QtCore.Qt.LeftButton, QtCore.Qt.MiddleButton, QtCore.Qt.RightButton]: for btn in [QtCore.Qt.LeftButton, QtCore.Qt.MiddleButton, QtCore.Qt.RightButton]:
if int(ev.buttons() & btn) == 0: if not (ev.buttons() & btn):
continue continue
if int(btn) not in self.dragButtons: ## see if we've dragged far enough yet if btn not in self.dragButtons: ## see if we've dragged far enough yet
cev = [e for e in self.clickEvents if int(e.button()) == int(btn)] cev = [e for e in self.clickEvents if e.button() == btn]
if cev: if cev:
cev = cev[0] cev = cev[0]
dist = Point(ev.scenePos() - cev.scenePos()).length() dist = Point(ev.scenePos() - cev.scenePos()).length()
if dist == 0 or (dist < self._moveDistance and now - cev.time() < self.minDragTime): if dist == 0 or (dist < self._moveDistance and now - cev.time() < self.minDragTime):
continue continue
init = init or (len(self.dragButtons) == 0) ## If this is the first button to be dragged, then init=True init = init or (len(self.dragButtons) == 0) ## If this is the first button to be dragged, then init=True
self.dragButtons.append(int(btn)) self.dragButtons.append(btn)
## if we have dragged buttons, deliver a drag event ## if we have dragged buttons, deliver a drag event
if len(self.dragButtons) > 0: if len(self.dragButtons) > 0:
if self.sendDragEvent(ev, init=init): if self.sendDragEvent(ev, init=init):
@ -235,14 +235,14 @@ class GraphicsScene(QtGui.QGraphicsScene):
ev.accept() ev.accept()
self.dragButtons.remove(ev.button()) self.dragButtons.remove(ev.button())
else: else:
cev = [e for e in self.clickEvents if int(e.button()) == int(ev.button())] cev = [e for e in self.clickEvents if e.button() == ev.button()]
if cev: if cev:
if self.sendClickEvent(cev[0]): if self.sendClickEvent(cev[0]):
#print "sent click event" #print "sent click event"
ev.accept() ev.accept()
self.clickEvents.remove(cev[0]) self.clickEvents.remove(cev[0])
if int(ev.buttons()) == 0: if not ev.buttons():
self.dragItem = None self.dragItem = None
self.dragButtons = [] self.dragButtons = []
self.clickEvents = [] self.clickEvents = []
@ -264,7 +264,7 @@ class GraphicsScene(QtGui.QGraphicsScene):
items = [] items = []
event = HoverEvent(None, acceptable) event = HoverEvent(None, acceptable)
else: else:
acceptable = int(ev.buttons()) == 0 ## if we are in mid-drag, do not allow items to accept the hover event. acceptable = not ev.buttons() ## if we are in mid-drag, do not allow items to accept the hover event.
event = HoverEvent(ev, acceptable) event = HoverEvent(ev, acceptable)
items = self.itemsNearEvent(event, hoverable=True) items = self.itemsNearEvent(event, hoverable=True)
self.sigMouseHover.emit(items) self.sigMouseHover.emit(items)
@ -306,7 +306,7 @@ class GraphicsScene(QtGui.QGraphicsScene):
# item to continue receiving events until the drag is over # item to continue receiving events until the drag is over
# - event is not a mouse event (QEvent.Leave sometimes appears here) # - event is not a mouse event (QEvent.Leave sometimes appears here)
if (ev.type() == ev.GraphicsSceneMousePress or if (ev.type() == ev.GraphicsSceneMousePress or
(ev.type() == ev.GraphicsSceneMouseMove and int(ev.buttons()) == 0)): (ev.type() == ev.GraphicsSceneMouseMove and not ev.buttons())):
self.lastHoverEvent = event ## save this so we can ask about accepted events later. self.lastHoverEvent = event ## save this so we can ask about accepted events later.
def sendDragEvent(self, ev, init=False, final=False): def sendDragEvent(self, ev, init=False, final=False):
@ -344,7 +344,7 @@ class GraphicsScene(QtGui.QGraphicsScene):
if event.isAccepted(): if event.isAccepted():
#print " --> accepted" #print " --> accepted"
self.dragItem = item self.dragItem = item
if int(item.flags() & item.ItemIsFocusable) > 0: if item.flags() & item.ItemIsFocusable:
item.setFocus(QtCore.Qt.MouseFocusReason) item.setFocus(QtCore.Qt.MouseFocusReason)
break break
elif self.dragItem is not None: elif self.dragItem is not None:
@ -389,7 +389,7 @@ class GraphicsScene(QtGui.QGraphicsScene):
debug.printExc("Error sending click event:") debug.printExc("Error sending click event:")
if ev.isAccepted(): if ev.isAccepted():
if int(item.flags() & item.ItemIsFocusable) > 0: if item.flags() & item.ItemIsFocusable:
item.setFocus(QtCore.Qt.MouseFocusReason) item.setFocus(QtCore.Qt.MouseFocusReason)
break break
self.sigMouseClicked.emit(ev) self.sigMouseClicked.emit(ev)

View File

@ -19,8 +19,8 @@ class MouseDragEvent(object):
self._buttonDownScenePos = {} self._buttonDownScenePos = {}
self._buttonDownScreenPos = {} self._buttonDownScreenPos = {}
for btn in [QtCore.Qt.LeftButton, QtCore.Qt.MiddleButton, QtCore.Qt.RightButton]: for btn in [QtCore.Qt.LeftButton, QtCore.Qt.MiddleButton, QtCore.Qt.RightButton]:
self._buttonDownScenePos[int(btn)] = moveEvent.buttonDownScenePos(btn) self._buttonDownScenePos[btn] = moveEvent.buttonDownScenePos(btn)
self._buttonDownScreenPos[int(btn)] = moveEvent.buttonDownScreenPos(btn) self._buttonDownScreenPos[btn] = moveEvent.buttonDownScreenPos(btn)
self._scenePos = moveEvent.scenePos() self._scenePos = moveEvent.scenePos()
self._screenPos = moveEvent.screenPos() self._screenPos = moveEvent.screenPos()
if lastEvent is None: if lastEvent is None:
@ -61,7 +61,7 @@ class MouseDragEvent(object):
""" """
if btn is None: if btn is None:
btn = self.button() btn = self.button()
return Point(self._buttonDownScenePos[int(btn)]) return Point(self._buttonDownScenePos[btn])
def buttonDownScreenPos(self, btn=None): def buttonDownScreenPos(self, btn=None):
""" """
@ -70,7 +70,7 @@ class MouseDragEvent(object):
""" """
if btn is None: if btn is None:
btn = self.button() btn = self.button()
return Point(self._buttonDownScreenPos[int(btn)]) return Point(self._buttonDownScreenPos[btn])
def lastScenePos(self): def lastScenePos(self):
""" """
@ -119,7 +119,7 @@ class MouseDragEvent(object):
""" """
if btn is None: if btn is None:
btn = self.button() btn = self.button()
return Point(self.currentItem.mapFromScene(self._buttonDownScenePos[int(btn)])) return Point(self.currentItem.mapFromScene(self._buttonDownScenePos[btn]))
def isStart(self): def isStart(self):
"""Returns True if this event is the first since a drag was initiated.""" """Returns True if this event is the first since a drag was initiated."""
@ -137,7 +137,7 @@ class MouseDragEvent(object):
else: else:
lp = self.lastPos() lp = self.lastPos()
p = self.pos() p = self.pos()
return "<MouseDragEvent (%g,%g)->(%g,%g) buttons=%d start=%s finish=%s>" % (lp.x(), lp.y(), p.x(), p.y(), int(self.buttons()), str(self.isStart()), str(self.isFinish())) return "<MouseDragEvent (%g,%g)->(%g,%g) buttons=%s start=%s finish=%s>" % (lp.x(), lp.y(), p.x(), p.y(), str(self.buttons()), str(self.isStart()), str(self.isFinish()))
def modifiers(self): def modifiers(self):
"""Return any keyboard modifiers currently pressed. """Return any keyboard modifiers currently pressed.
@ -230,9 +230,9 @@ class MouseClickEvent(object):
p = self._scenePos p = self._scenePos
else: else:
p = self.pos() p = self.pos()
return "<MouseClickEvent (%g,%g) button=%d>" % (p.x(), p.y(), int(self.button())) return "<MouseClickEvent (%g,%g) button=%s>" % (p.x(), p.y(), str(self.button()))
except: except:
return "<MouseClickEvent button=%d>" % (int(self.button())) return "<MouseClickEvent button=%s>" % (str(self.button()))
def time(self): def time(self):
return self._time return self._time
@ -362,7 +362,7 @@ class HoverEvent(object):
else: else:
lp = self.lastPos() lp = self.lastPos()
p = self.pos() p = self.pos()
return "<HoverEvent (%g,%g)->(%g,%g) buttons=%d enter=%s exit=%s>" % (lp.x(), lp.y(), p.x(), p.y(), int(self.buttons()), str(self.isEnter()), str(self.isExit())) return "<HoverEvent (%g,%g)->(%g,%g) buttons=%s enter=%s exit=%s>" % (lp.x(), lp.y(), p.x(), p.y(), str(self.buttons()), str(self.isEnter()), str(self.isExit()))
def modifiers(self): def modifiers(self):
"""Return any keyboard modifiers currently pressed. """Return any keyboard modifiers currently pressed.

View File

@ -128,7 +128,8 @@ class Exporter(object):
while len(childs) > 0: while len(childs) > 0:
ch = childs.pop(0) ch = childs.pop(0)
tree = self.getPaintItems(ch) tree = self.getPaintItems(ch)
if int(ch.flags() & ch.ItemStacksBehindParent) > 0 or (ch.zValue() < 0 and int(ch.flags() & ch.ItemNegativeZStacksBehindParent) > 0): if (ch.flags() & ch.ItemStacksBehindParent) or \
(ch.zValue() < 0 and (ch.flags() & ch.ItemNegativeZStacksBehindParent)):
preItems.extend(tree) preItems.extend(tree)
else: else:
postItems.extend(tree) postItems.extend(tree)

View File

@ -251,7 +251,7 @@ def _generateItemSvg(item, nodes=None, root=None, options={}):
childGroup = g1 ## add children directly to this node unless we are clipping childGroup = g1 ## add children directly to this node unless we are clipping
if not isinstance(item, QtGui.QGraphicsScene): if not isinstance(item, QtGui.QGraphicsScene):
## See if this item clips its children ## See if this item clips its children
if int(item.flags() & item.ItemClipsChildrenToShape) > 0: if item.flags() & item.ItemClipsChildrenToShape:
## Generate svg for just the path ## Generate svg for just the path
path = QtGui.QGraphicsPathItem(item.mapToScene(item.shape())) path = QtGui.QGraphicsPathItem(item.mapToScene(item.shape()))
item.scene().addItem(path) item.scene().addItem(path)
@ -414,7 +414,7 @@ def itemTransform(item, root):
return tr return tr
if int(item.flags() & item.ItemIgnoresTransformations) > 0: if item.flags() & item.ItemIgnoresTransformations:
pos = item.pos() pos = item.pos()
parent = item.parentItem() parent = item.parentItem()
if parent is not None: if parent is not None:
@ -431,7 +431,7 @@ def itemTransform(item, root):
if nextRoot is None: if nextRoot is None:
nextRoot = root nextRoot = root
break break
if nextRoot is root or int(nextRoot.flags() & nextRoot.ItemIgnoresTransformations) > 0: if nextRoot is root or (nextRoot.flags() & nextRoot.ItemIgnoresTransformations):
break break
if isinstance(nextRoot, QtGui.QGraphicsScene): if isinstance(nextRoot, QtGui.QGraphicsScene):

View File

@ -245,7 +245,7 @@ class LinearRegionItem(GraphicsObject):
self.sigRegionChangeFinished.emit(self) self.sigRegionChangeFinished.emit(self)
def mouseDragEvent(self, ev): def mouseDragEvent(self, ev):
if not self.movable or int(ev.button() & QtCore.Qt.LeftButton) == 0: if not self.movable or ev.button() != QtCore.Qt.LeftButton:
return return
ev.accept() ev.accept()

View File

@ -722,7 +722,7 @@ class ROI(GraphicsObject):
hover=True hover=True
for btn in [QtCore.Qt.LeftButton, QtCore.Qt.RightButton, QtCore.Qt.MiddleButton]: for btn in [QtCore.Qt.LeftButton, QtCore.Qt.RightButton, QtCore.Qt.MiddleButton]:
if int(self.acceptedMouseButtons() & btn) > 0 and ev.acceptClicks(btn): if (self.acceptedMouseButtons() & btn) and ev.acceptClicks(btn):
hover=True hover=True
if self.contextMenuEnabled(): if self.contextMenuEnabled():
ev.acceptClicks(QtCore.Qt.RightButton) ev.acceptClicks(QtCore.Qt.RightButton)
@ -794,7 +794,7 @@ class ROI(GraphicsObject):
if ev.button() == QtCore.Qt.RightButton and self.contextMenuEnabled(): if ev.button() == QtCore.Qt.RightButton and self.contextMenuEnabled():
self.raiseContextMenu(ev) self.raiseContextMenu(ev)
ev.accept() ev.accept()
elif int(ev.button() & self.acceptedMouseButtons()) > 0: elif ev.button() & self.acceptedMouseButtons():
ev.accept() ev.accept()
self.sigClicked.emit(self, ev) self.sigClicked.emit(self, ev)
else: else:
@ -1343,7 +1343,7 @@ class Handle(UIGraphicsItem):
if ev.acceptDrags(QtCore.Qt.LeftButton): if ev.acceptDrags(QtCore.Qt.LeftButton):
hover=True hover=True
for btn in [QtCore.Qt.LeftButton, QtCore.Qt.RightButton, QtCore.Qt.MiddleButton]: for btn in [QtCore.Qt.LeftButton, QtCore.Qt.RightButton, QtCore.Qt.MiddleButton]:
if int(self.acceptedMouseButtons() & btn) > 0 and ev.acceptClicks(btn): if (self.acceptedMouseButtons() & btn) and ev.acceptClicks(btn):
hover=True hover=True
if hover: if hover:
@ -1358,7 +1358,7 @@ class Handle(UIGraphicsItem):
self.isMoving = False ## prevents any further motion self.isMoving = False ## prevents any further motion
self.movePoint(self.startPos, finish=True) self.movePoint(self.startPos, finish=True)
ev.accept() ev.accept()
elif int(ev.button() & self.acceptedMouseButtons()) > 0: elif ev.button() & self.acceptedMouseButtons():
ev.accept() ev.accept()
if ev.button() == QtCore.Qt.RightButton and self.deletable: if ev.button() == QtCore.Qt.RightButton and self.deletable:
self.raiseContextMenu(ev) self.raiseContextMenu(ev)

View File

@ -1397,7 +1397,7 @@ class ViewBox(GraphicsWidget):
itemBounds.append((bounds, useX, useY, pxPad)) itemBounds.append((bounds, useX, useY, pxPad))
else: else:
if int(item.flags() & item.ItemHasNoContents) > 0: if item.flags() & item.ItemHasNoContents:
continue continue
bounds = self.mapFromItemToView(item, item.boundingRect()).boundingRect() bounds = self.mapFromItemToView(item, item.boundingRect()).boundingRect()
itemBounds.append((bounds, True, True, 0)) itemBounds.append((bounds, True, True, 0))