Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support forward/backward mouse buttons #12904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2019

Conversation

timhoffm
Copy link
Member

PR Summary

This adds basic support for forward/backward mouse buttons to backend_bases.py and implements it for the Qt5 backend.

Other backends may follow, but I'm not an expert for these. Essentially one has to bind the extra mouse buttons to the new MouseButton.X1 and MouseButton.X2.

This is not yet implemented for ToolManager as I wanted a simple working version first, and I don't think I fully understand the intendend usage / current state of ToolManager.

PR Checklist

  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)

@timhoffm timhoffm force-pushed the mouse-forward-backward branch from 8e7db60 to f5602fa Compare November 29, 2018 18:47
QtCore.Qt.MidButton: MouseButton.MIDDLE,
QtCore.Qt.RightButton: MouseButton.RIGHT,
QtCore.Qt.XButton1: MouseButton.BACK,
QtCore.Qt.XButton2: MouseButton.FORWARD,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Using XButton1 instead of BackButton to keep compatibility with Qt 4.8
http://doc.qt.io/archives/qt-4.8/qt.html#MouseButton-enum

@timhoffm timhoffm added this to the v3.1 milestone Dec 9, 2018
@anntzer
Copy link
Contributor

anntzer commented Dec 9, 2018

I'm mildly dubious about whether this is a good idea mostly because Matplotlib already binds so many keys/buttons by default that it's a bit difficult to find "available" keys when one wants to write one's own interactive app (for example, in mplcursors, I bind moving the cursor to shift-left/right as left/right is already taken by forward/backward).
Especially, in this case, it's not even something that's togglable via rcParams["keymap.foo"].

Perhaps extend rcParams["keymap.forward/back"] to also support MouseButton.FOO? At least this way it's configurable.

@anntzer
Copy link
Contributor

anntzer commented Dec 10, 2018

Also, despite its name, I would consider putting that functionality also in key_press_handler (with a first dispatch on the event type) (possibly rename key_press_handler to event_handler with a property to handle the deprecation, yada yada) to make it easier for someone to disconnect all handlers (as I don't think many will want to disconnect the keypress but not the mousepress handler; and if they really want to have one but not the other they can always edit the rcParams entries).

@timhoffm
Copy link
Member Author

Thanks for the feedback. IMHO you are addressing issues much larger than the scope of the PR.

Configurability

Configurability would be nice, but honestly, I don't see the need. Will someone really confiure other mouse buttons to forward/backward? It's also unlikely that someone wants to use the forward/backward mouse buttons for something else. The semantics is quite unambiguous and you'd have to have a really good case to do it and not surprise your users at the same time. We also don't provide custom bindings for left/right mouse buttons.

If someone has such special needs, they can (as before) do it based on the respective GUI toolkit. Deactivating the effect of the PR is as simple as disconnecting the button press handler.

Changes to the event system

Renaming key_press_handler to event_handler is not a good idea since there are many other events unrelated to keyboard and mouse input. If anything that would be an input_event_handler. However, I think most GUI toolkits handle keyboard and mouse events separately (not that it neccessarily has to be that way, but they had reasons and programmers are used to it). Any backward-incompatible change of the API would have to be thought trough thoroughly.

Also, if the main reason for a single handler is to be able to disconnect easily, we should instead add a disconnect_all() method. Having separate handlers for separate events makes it easier to reimplement them. Say, I want to do my own key press handling, I would have to reimplement the input_event_handler and redo the dispatch logic there to still keep the mouse events working.

Summary

I think, that the current PR is narrow and specific on just implementing the default forward/backward behavior within the current event logic. It doesn't make it more difficult to transition to something else later.

@anntzer
Copy link
Contributor

anntzer commented Dec 10, 2018

Deactivating the effect of the PR is as simple as disconnecting the button press handler.

But this has to be done on a canvas-by-canvas basis, unlike other keybindings that can be disabled globally through the rc system.
The situation with left/right is not really comparable as they don't do anything by default, they are only meaningful when a tool is active.
Regardless of the other changes (which I agree are a bit of a distraction), I think at least you should set

keymap.forward: right, v, MouseButton.FORWARD

(with the corresponding parsing code in rcsetup) and then have button_press_handler lookup the relevant entries in that rcParam.

@timhoffm
Copy link
Member Author

timhoffm commented Dec 10, 2018

Agreed on using rcParams. Wouldn't it be better to define a separate

mousemap.forward:  MouseButton.FORWARD

We could get int trouble with 'up', which is a key, but currently also the name for the wheel-up action (not that it's has a configurable action right now, but we should avoid possible future clashes).

@anntzer
Copy link
Contributor

anntzer commented Dec 10, 2018

That's why it'd "MouseButton.UP", not "up".
Having separate entries for mousemap would work too, I'm just trying to prevent the list of rcParams from growing too much, but no strong opinion there.

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per comments above.

@timhoffm
Copy link
Member Author

Anyone has opinions on rcparms

keymap.forward: right, v, MouseButton.FORWARD

vs.

keymap.forward: right, v
mousemap.forward: MouseButton.FORWARD

?

@timhoffm
Copy link
Member Author

timhoffm commented Jan 6, 2019

Using the keymap.forward/back for configuring also the mouse buttons.

keymap.forward: right, v, MouseButton.FORWARD

@@ -2394,6 +2396,18 @@ def _get_uniform_gridstate(ticks):
a.set_navigate(i == n)


def button_press_handler(event, canvas, toolbar=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mouse_event_handler?

@tacaswell
Copy link
Member

I like folding it into keymap.* configuration but am 50/50 on folding the implementation into the keypress handlers.

Lean slightly towards a separate function as that would also leave a clear place to put 'zoom-on-scroll'.

@anntzer anntzer dismissed their stale review January 6, 2019 22:33

handled

@timhoffm
Copy link
Member Author

timhoffm commented Jan 6, 2019

This is not in the keypress handler. button_press_handler is a separate method analogous to key_press_handler. I've chosen the name according to the event names key_press_event -> key_press_handler and thus button_press_event -> button_press_handler.

'button_press_event',

The question is: should there be one handler per event type? Or do we collect multiple events with a handler (which would then be more generic key_event_handler / mouse_event_handler)?

@timhoffm
Copy link
Member Author

rebased.

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually have a mouse with forward/backward buttons to test this, but it looks reasonable.
A whatsnew entry would be useful too (as other people may legitimately have been binding these (using toolkit-specific code) to other stuff.

@timhoffm timhoffm force-pushed the mouse-forward-backward branch from d03049c to 0ff1924 Compare February 17, 2019 22:53
@timhoffm
Copy link
Member Author

What's new entry added.

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WFM with my mouse.

@QuLogic
Copy link
Member

QuLogic commented Feb 17, 2019

Not sure if @tacaswell has any further comments.

@jklymak jklymak merged commit 86d80aa into matplotlib:master Feb 22, 2019
@jklymak
Copy link
Member

jklymak commented Feb 22, 2019

Merging. If @tacaswell still has comments, we can still update...

@QuLogic
Copy link
Member

QuLogic commented Feb 23, 2019

This actually works out-of-the-box with GTK3 as well. The mouse buttons are just passed through directly and are already 8&9.

@timhoffm timhoffm deleted the mouse-forward-backward branch February 23, 2019 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants