Fix to ensure that items are given the keyboard focus if they are clicked

This commit is contained in:
Luke Campagnola 2012-04-15 10:19:30 -04:00
parent 7287771577
commit 355472271b

View File

@ -124,6 +124,13 @@ class GraphicsScene(QtGui.QGraphicsScene):
#print "mouseGrabberItem: ", self.mouseGrabberItem() #print "mouseGrabberItem: ", self.mouseGrabberItem()
if self.mouseGrabberItem() is None: ## nobody claimed press; we are free to generate drag/click events if self.mouseGrabberItem() is None: ## nobody claimed press; we are free to generate drag/click events
self.clickEvents.append(MouseClickEvent(ev)) self.clickEvents.append(MouseClickEvent(ev))
## set focus on the topmost focusable item under this click
items = self.items(ev.scenePos())
for i in items:
if i.isEnabled() and i.isVisible() and int(i.flags() & i.ItemIsFocusable) > 0:
i.setFocus(QtCore.Qt.MouseFocusReason)
break
#else: #else:
#addr = sip.unwrapinstance(sip.cast(self.mouseGrabberItem(), QtGui.QGraphicsItem)) #addr = sip.unwrapinstance(sip.cast(self.mouseGrabberItem(), QtGui.QGraphicsItem))
#item = GraphicsScene._addressCache.get(addr, self.mouseGrabberItem()) #item = GraphicsScene._addressCache.get(addr, self.mouseGrabberItem())
@ -134,12 +141,10 @@ class GraphicsScene(QtGui.QGraphicsScene):
## First allow QGraphicsScene to deliver hoverEnter/Move/ExitEvents ## First allow QGraphicsScene to deliver hoverEnter/Move/ExitEvents
QtGui.QGraphicsScene.mouseMoveEvent(self, ev) QtGui.QGraphicsScene.mouseMoveEvent(self, ev)
## Next deliver our own HoverEvents ## Next deliver our own HoverEvents
self.sendHoverEvents(ev) self.sendHoverEvents(ev)
if int(ev.buttons()) != 0: ## button is pressed; send mouseMoveEvents and mouseDragEvents if int(ev.buttons()) != 0: ## 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:
@ -266,6 +271,8 @@ class GraphicsScene(QtGui.QGraphicsScene):
#print "drag -> new item" #print "drag -> new item"
for item in self.itemsNearEvent(event): for item in self.itemsNearEvent(event):
#print "check item:", item #print "check item:", item
if not item.isVisible() or not item.isEnabled():
continue
if hasattr(item, 'mouseDragEvent'): if hasattr(item, 'mouseDragEvent'):
event.currentItem = item event.currentItem = item
try: try:
@ -275,6 +282,8 @@ 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:
item.setFocus(QtCore.Qt.MouseFocusReason)
break break
elif self.dragItem is not None: elif self.dragItem is not None:
event.currentItem = self.dragItem event.currentItem = self.dragItem
@ -309,6 +318,8 @@ class GraphicsScene(QtGui.QGraphicsScene):
debug.printExc("Error sending click event:") debug.printExc("Error sending click event:")
else: else:
for item in self.itemsNearEvent(ev): for item in self.itemsNearEvent(ev):
if not item.isVisible() or not item.isEnabled():
continue
if hasattr(item, 'mouseClickEvent'): if hasattr(item, 'mouseClickEvent'):
ev.currentItem = item ev.currentItem = item
try: try:
@ -317,6 +328,8 @@ 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:
item.setFocus(QtCore.Qt.MouseFocusReason)
break break
if not ev.isAccepted() and ev.button() is QtCore.Qt.RightButton: if not ev.isAccepted() and ev.button() is QtCore.Qt.RightButton:
#print "GraphicsScene emitting sigSceneContextMenu" #print "GraphicsScene emitting sigSceneContextMenu"