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

Skip to content

Added a TextBox widget #1983

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

Closed
wants to merge 26 commits into from
Closed

Conversation

keflavich
Copy link
Contributor

This PR adds an editable TextBox widget based on the text box on some ancient mailing list threads. It adds some features:

  • movable cursor (with left/right arrow)
  • backspace works
  • restricts values to floats
  • has an activate callback (so you click on it to edit)
  • has an enter callback (pressing enter does something)
  • deactivates other key bindings when active (so typing s doesn't activate the save dialog)

I can add some example usage, but it would be helpful to have some initial feedback. It would certainly be preferable to have a fully-active text box (i.e., one where mouse-highlighting, shift-selection, etc.) can be used to edit the text, but frankly that seems overwhelming at the moment.

The main use-case for this text box is changing numbers directly. I've used it specifically for communication with a slider: changing the slider value & limits via text entry.

I'd also like to see or create a drop-down text menu, but given the difficulty in getting this to work at all (and not very prettily), I think backend-specific approaches may prove better.

@pelson
Copy link
Member

pelson commented May 8, 2013

@keflavich - I've looked at doing something similar in the past, so I'm definitely in favour of this addition.
I wonder if you can come up with a testing strategy for this new Widget? Do you find that the keyboard shortcuts that MPL provides interferes with your ability to type contents into the text box?

I plan to have a good look through this PR in the coming weeks - but I'm hopeful we can squeeze this into v1.3.0.

Thanks for submitting!

@keflavich
Copy link
Contributor Author

@pelson - I'm sure I can come up with some tests.

Yes, MPL shortcuts directly interfere: the s key specifically brings up a save dialog, which would badly interfere with typing. The others, less so.

@pelson
Copy link
Member

pelson commented May 8, 2013

Yes, MPL shortcuts directly interfere: the s key specifically brings up a save dialog, which would badly interfere with typing. The others, less so.

@efiring - I know in the past we've talked about removing non-modified (by ctrl) shortcut keys. Is v1.3 the time to do it? @mdboom?

@mdboom
Copy link
Member

mdboom commented May 8, 2013

What about this as a middle option?

  1. Add "Ctrl+" modifiers for all existing shortcut keys
  2. When a text widget is added to a figure, disable all of the non-Ctrl-modified shortcut keys
  3. Add a note in the docs that the non-Ctrl shortcut keys are going away in the future

@keflavich
Copy link
Contributor Author

I like that approach. How can this be accomplished? i.e., is there any way to know which connections in the callbacks registry correspond to non-Ctrl-modifier keys? I know how to blindly remove all KeyPress connections, or all connections whose numbers I've tracked, but I'm not sure where the cids are stored for the default mpl connections.

@mdboom
Copy link
Member

mdboom commented May 8, 2013

There is a single callback that handles all of the default keys in matplotlib in backend_bases.py:key_press_handler. I had forgotten that loads the keys it uses from the config file, which does somewhat throw a monkey wrench into what I suggested.

We could certainly add Ctrl+ versions of everything to the default rcParams (which live in rcsetup.py, but are duplicated for the user's benefit in matplotlibrc.template) easily enough, but if the user overrode any of those keys in their config file, it will be difficult to do so.

Then, when a TextWidget is added to the canvas, it could set a flag. In key_press_handler, at the top, one would have something like:

if canvas.has_text_widget and not event.key.startswith('ctrl+'):
    return

But all of that is off the top of my head -- we'd have to really try this out, I think, and run it by some users who build applications using these key press callbacks to make sure we're not breaking anything in the process.

@efiring
Copy link
Member

efiring commented May 8, 2013

On 2013/05/08 7:23 AM, Michael Droettboom wrote:

What about this as a middle option?

  1. Add "Ctrl+" modifiers for all existing shortcut keys
  2. When a text widget is added to a figure, disable all of the
    non-Ctrl-modified shortcut keys
  3. Add a note in the docs that the non-Ctrl shortcut keys are going away
    in the future

That sounds reasonable to me.

@mrterry
Copy link
Contributor

mrterry commented Jun 3, 2013

This may be fixed in master, but this doesn't work with the macos backend on v1.2.0 due to the FigureCanvasMac not having a copy_from_bbox() method. It is also calling Text.get_window_extents() in init and failing with a RuntimeError "RuntimeError: Cannot get window extent w/o renderer"

Runs great with tkAgg backend, though.

@keflavich
Copy link
Contributor Author

Should the mac backend have a copy_from_bbox method, or is there a better way to achieve the same result? Sorry, I'm out of my depth on this.

@WeatherGod
Copy link
Member

This has been a long-running issue, related to blitting. With the way
Cocoa works, the event loop doesn't really allow us to have a copy from
bbox. I am not savvy on the details, and there may be changes in the
future to fix this, but in the immediate future, macosx backend has a bunch
of wontfix issues. While it would be nice if TextWidget not require
copy_from_bbox(), it may be sufficient to merely document that it doesn't
work with macosx backend.

ax.set_navigate(False)
self.canvas.draw()

self.region = self.canvas.copy_from_bbox(ax.bbox)
Copy link
Contributor

Choose a reason for hiding this comment

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

self.region is not used by TextBox or Widget. Do other things assume it is there or can it be dropped? Dropping it would help appease macos.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point; I have no idea if other things will assume it. But perhaps a try/except around this would be the safest way to go, not knowing any better?

@mrterry
Copy link
Contributor

mrterry commented Jun 4, 2013

https://gist.github.com/mrterry/5703912

I've done some surgery on your TextBox. I make no promise regarding the quality of my practice : )
https://gist.github.com/mrterry/5703912

This TextBox variant does a few things

  • doesn't throw exceptions for macos (but doesn't work, yet)
  • removes self.region
  • uses a property to delay the creation of the cursor. You need a renderer to get a bbox, but the macos backend doesn't give you a renderer until you render the first frame. I delay creating the cursor until the first click. This makes macos happier.
  • Renamed to FloatTextBox since it is only for floating point text.
  • Inherits from AxesWidget rather than just Widget.
  • Like other mpl.widgets, it uses self.active (via self.ignore(event)) to control whether callbacks are active
  • Changed names to things i think are more descriptive.

There is something buggy in the callback twiddling section when using the macos backend. The callbacks seem to be twiddled correctly (it works for TkAgg). keypress() appears to be in the right dictionaries and can be manually executed, but key_press_events just don't trigger it. Any ideas on what the problem might be? It is very close to working.

@keflavich
Copy link
Contributor Author

@mterry want to submit that as a PR on this PR?

@dmcdougall
Copy link
Member

This also needs an entry in the CHANGELOG and in whats_new.rst.

@mrterry
Copy link
Contributor

mrterry commented Aug 14, 2013

I haven't forgotten about this. All the backends ought to accept backspace key_press_events before merging, so I've been banging on that. It should also support rotation.

@adrn
Copy link
Contributor

adrn commented Oct 27, 2013

Sooo this would be a really awesome feature to have...whats the status?

@tacaswell
Copy link
Member

@mrterry What is the status of making sure everything will take backspace?

@pelson
Copy link
Member

pelson commented Jan 6, 2014

Note: Whilst we can't do too much integration testing on this, it'd be good to get some unit tests for the TextBox class.

@keflavich
Copy link
Contributor Author

@pelson - are there any unit tests for other widgets we could use as a template/starting point? I don't see any in the tests directory now.

@pelson
Copy link
Member

pelson commented Jan 8, 2014

are there any unit tests for other widgets we could use as a template/starting point? I don't see any in the tests directory now.

Afraid not. matplotlib was virtually test free just a few years ago 😒.

@muzhig
Copy link

muzhig commented Jan 12, 2014

Hey, still not merged??? Damn!

@pelson
Copy link
Member

pelson commented Jan 14, 2014

Hey, still not merged??? Damn!

😄 - it just needs a few unit tests and an entry in the "what's new" documentation and it will make it into v1.4...

@tacaswell
Copy link
Member

This has issues with unicode literals and py3k

@tacaswell
Copy link
Member

@keflavich Are you going to have time to work on this in the near future?

@keflavich
Copy link
Contributor Author

Unlikely, sorry. I'll readily accept PRs on it, but you can take over
completely (even start a new PR?) if you prefer.

@tacaswell
Copy link
Member

I am to punting this to 1.5 unless someone volunteers to take it over.

@tacaswell tacaswell modified the milestones: v1.5.x, v1.4.0 Apr 4, 2014
@tacaswell tacaswell modified the milestones: proposed next point release, next point release Feb 19, 2015
@tacaswell tacaswell modified the milestones: next point release, proposed next point release Jun 18, 2015
@tacaswell tacaswell mentioned this pull request Nov 1, 2015
@fariza
Copy link
Member

fariza commented Sep 12, 2016

Replaced by #6988

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

Successfully merging this pull request may close these issues.