Event handling callbacks call order

Hello,

I have an application where I let the user place Patches on the canvas
via mouse events. To do so I have a "button_press_event" callback
registered to the canvas. I now would like to allow to drag the drawn
patches to change their position. To do so I register other callbacks on
the canvas like in the examples at
https://matplotlib.org/users/event_handling.html. The problem is that
the callbacks are called in the order they are registered and I haven't
found a way to inhibit execution of callbacks down the stack from
previously called callback. This results in the fact that patches are
drawn on top of the old ones when the user tries to drag those around.

I can see (quite convoluted) ways to avoid this in my code, but I would
think that correctly handling this is a common occurrence and that there
could be a solution built into matplotlib. However, I haven't found it.

I am missing something?

Thank you!

Cheers,
Dan

Hi,
A simple way to do this would be for the first handler to attach e.g. a
"handled" ("accepted", in Qt terminology) attribute to the event and for
later handlers to first check whether that attribute exists. Note that
this can be done independently of anything provided by Matplotlib itself
(Matplotlib could automatically stop propagating events with that attribute
set, but I'm not sure it's really worth it).
Cheers,
Antony

···

On Thu, Feb 14, 2019 at 1:56 AM Daniele Nicolodi <daniele at grinta.net> wrote:

Hello,

I have an application where I let the user place Patches on the canvas
via mouse events. To do so I have a "button_press_event" callback
registered to the canvas. I now would like to allow to drag the drawn
patches to change their position. To do so I register other callbacks on
the canvas like in the examples at
https://matplotlib.org/users/event_handling.html. The problem is that
the callbacks are called in the order they are registered and I haven't
found a way to inhibit execution of callbacks down the stack from
previously called callback. This results in the fact that patches are
drawn on top of the old ones when the user tries to drag those around.

I can see (quite convoluted) ways to avoid this in my code, but I would
think that correctly handling this is a common occurrence and that there
could be a solution built into matplotlib. However, I haven't found it.

I am missing something?

Thank you!

Cheers,
Dan
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel at python.org
Matplotlib-devel Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20190214/b863ef0d/attachment.html&gt;

I used the same Matplotlib examples to implement something similar in widgets.py <https://github.com/rayosborn/nexpy/blob/edit-options/src/nexpy/gui/widgets.py&gt; (https://github.com/rayosborn/nexpy/blob/edit-options/src/nexpy/gui/widgets.py\). There is an NXpatch class, with a variety of subclassed shapes. Once they are created and connected to the plot, they are independently draggable and resizable as promised.

plotview.deactivate()
r=NXrectangle(1650,2000,200,1000,color='g?)
r.connect()
plotview.activate()

I do have to deactivate zoom and pan modes when adding the shapes, but they can be reactivated afterwards and the shapes will still be movable provided the cursor is within them. I don?t know if that is the problem you are encountering. In the code, ?plotview? is the active plotting window - you can find the activate and deactivate functions in https://github.com/rayosborn/nexpy/blob/edit-options/src/nexpy/gui/plotview.py.

Ray

···

On Feb 13, 2019, at 6:46 PM, Daniele Nicolodi <daniele at grinta.net> wrote:

Hello,

I have an application where I let the user place Patches on the canvas
via mouse events. To do so I have a "button_press_event" callback
registered to the canvas. I now would like to allow to drag the drawn
patches to change their position. To do so I register other callbacks on
the canvas like in the examples at
https://matplotlib.org/users/event_handling.html. The problem is that
the callbacks are called in the order they are registered and I haven't
found a way to inhibit execution of callbacks down the stack from
previously called callback. This results in the fact that patches are
drawn on top of the old ones when the user tries to drag those around.

I can see (quite convoluted) ways to avoid this in my code, but I would
think that correctly handling this is a common occurrence and that there
could be a solution built into matplotlib. However, I haven't found it.

I am missing something?

Thank you!

Cheers,
Dan
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel at python.org
Matplotlib-devel Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20190214/a3be7bda/attachment.html&gt;