Skip to content

fix click bug on removable ROIs; test#1804

Merged
j9ac9k merged 2 commits intopyqtgraph:masterfrom
outofculture:r-o-INT-erest
May 27, 2021
Merged

fix click bug on removable ROIs; test#1804
j9ac9k merged 2 commits intopyqtgraph:masterfrom
outofculture:r-o-INT-erest

Conversation

@outofculture
Copy link
Copy Markdown
Contributor

Looks like in stopped working at some point, but this int version should be safer and work everywhere.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented May 26, 2021

Thanks for the fix @outofculture that's too bad the in operator doesn't work, it feels so right for this use case.

Elsewhere in ROI.py we use ev.button() & self.acceptedMouseButtons() the int version I'm pretty sure was broken on PyQt6 6.0.0, but we don't support that version due to their non-integer alternative to enums.

Should we change the other usage of self.acceptedMouseButtons() or should we do away with the integer casting (or just use two different methods)?

@outofculture
Copy link
Copy Markdown
Contributor Author

Oh, I hadn't seen it used elsewhere. int casting isn't necessary, so I'm fine skipping that. Well, fine except that now my branch name isn't clever, but 🤷🏽

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented May 26, 2021

Well, fine except that now my branch name isn't clever, but 🤷🏽

we can't get everything we want in life....

@j9ac9k j9ac9k merged commit 1735eff into pyqtgraph:master May 27, 2021
@pijyoi
Copy link
Copy Markdown
Contributor

pijyoi commented May 27, 2021

       elif ev.button() & self.acceptedMouseButtons() > 0:

0fa4557 is the commit that added PyQt6 compatibility wrt to enums and flags.
No testing against > 0
I recall that you can't generally test against an integer value, It might end up being False just because you are comparing two different types.
It would be a good idea to physically test out the change with a PyQt6 venv.

@pijyoi
Copy link
Copy Markdown
Contributor

pijyoi commented May 27, 2021

Ran the ImageView example in PyQt6 6.0.3 and 6.1 and did a mouse click within the ROI

[19:54:39]  Error sending click event:

    |==============================>>
    |  Traceback (most recent call last):
    |    File "C:\work\github\pyqtgraph\examples\ImageView.py", line 67, in <module>
    |      pg.exec()
    |    File "C:\work\github\pyqtgraph\pyqtgraph\Qt.py", line 447, in exec_
    |      return app.exec() if hasattr(app, 'exec') else app.exec_()
    |    File "C:\work\github\pyqtgraph\pyqtgraph\widgets\GraphicsView.py", line 355, in mouseReleaseEvent
    |      super().mouseReleaseEvent(ev)
    |    File "C:\work\github\pyqtgraph\pyqtgraph\GraphicsScene\GraphicsScene.py", line 240, in mouseReleaseEvent
    |      if self.sendClickEvent(cev[0]):
    |    File "C:\work\github\pyqtgraph\pyqtgraph\GraphicsScene\GraphicsScene.py", line 389, in sendClickEvent
    |      debug.printExc("Error sending click event:")
    |    --- exception caught here ---
    |    File "C:\work\github\pyqtgraph\pyqtgraph\GraphicsScene\GraphicsScene.py", line 387, in sendClickEvent
    |      item.mouseClickEvent(ev)
    |    File "C:\work\github\pyqtgraph\pyqtgraph\graphicsItems\ROI.py", line 804, in mouseClickEvent
    |      elif ev.button() & self.acceptedMouseButtons() > 0:
    |  TypeError: '>' not supported between instances of 'MouseButtons' and 'int'
    |==============================<<

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented May 28, 2021

@pijyoi I'm going to fix this in #1807 thanks for catching it.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented May 28, 2021

I swear, qt bindings are never happy (PyQt5 5.12 + Python 3.8)

================================================================================= warnings summary ==================================================================================
tests/graphicsItems/test_ROI.py::test_PolyLineROI
  /Users/ogi/Developer/pyqtgraph/pyqtgraph/graphicsItems/ROI.py:804: DeprecationWarning: an integer is required (got type MouseButtons).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
    elif ev.button() & self.acceptedMouseButtons():

-- Docs: https://docs.pytest.org/en/stable/warnings.html

I think I may just suppress this warning.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented May 28, 2021

While the fix was easy, why didn't the new test that was added catch this is the mystery, looking into that now.

@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented May 28, 2021

oh, it did catch it, but it was suppressed; ... that's a problem; you can see the exception when you run pytest -s

EDIT: and here is why an exception wasn't raised...

def sendClickEvent(self, ev):
## if we are in mid-drag, click events may only go to the dragged item.
if self.dragItem is not None and hasattr(self.dragItem, 'mouseClickEvent'):
ev.currentItem = self.dragItem
self.dragItem.mouseClickEvent(ev)
## otherwise, search near the cursor
else:
if self.lastHoverEvent is not None:
acceptedItem = self.lastHoverEvent.clickItems().get(ev.button(), None)
else:
acceptedItem = None
if acceptedItem is not None:
ev.currentItem = acceptedItem
try:
acceptedItem.mouseClickEvent(ev)
except:
debug.printExc("Error sending click event:")
else:
for item in self.itemsNearEvent(ev):
if not item.isVisible() or not item.isEnabled():
continue
if hasattr(item, 'mouseClickEvent'):
ev.currentItem = item
try:
item.mouseClickEvent(ev)
except:
debug.printExc("Error sending click event:")
if ev.isAccepted():
if item.flags() & item.ItemIsFocusable:
item.setFocus(QtCore.Qt.MouseFocusReason)
break
self.sigMouseClicked.emit(ev)
return ev.isAccepted()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants