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

Skip to content

Add "copy to clipboard" to the Toolbar #1987

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
ryggyr opened this issue May 9, 2013 · 24 comments
Closed

Add "copy to clipboard" to the Toolbar #1987

ryggyr opened this issue May 9, 2013 · 24 comments

Comments

@ryggyr
Copy link

ryggyr commented May 9, 2013

Wishlist: a "copy image to clipboard" button in the Toolbar for interactive backends.

For the wx backend, I've had some success with adding such a button by altering the function matplotlib.backends.NavigationToolbar2Wx._init_toolbar() in backend_wx.py so that the last line:

self.Realize()

is replaced by:

    _NTB2_COPY = wx.NewId()
    self.AddSimpleTool(_NTB2_COPY, _load_bitmap('stock_up.xpm'),
                        'Copy', 'Copy plot image to clipboard') 
    bind(self, wx.EVT_TOOL, self.copy, id=_NTB2_COPY) 

    self.Realize()

def copy(self, evt):
    self.canvas.Copy_to_Clipboard(event=evt) # bmp image
@mdboom
Copy link
Member

mdboom commented May 9, 2013

Seems like a nice idea, and should be doable in all of the backends. Not sure we'll get to this before 1.3, but if you want to have a crack at it, I can help as able.

@efiring
Copy link
Member

efiring commented May 9, 2013

I will add my usual curmudgeonly caution: I'm not sure it is a good idea to keep piling things into the toolbars, and adding backend-specific code. I would prefer to see this sort of thing be strictly optional, and ideally not part of core mpl.

@ryggyr
Copy link
Author

ryggyr commented May 10, 2013

@efiring - I'm sympathetic with the philosophy to keep the toolbar uncluttered, but I believe the copy-to-clipboard command is nearly universal across gui programs which handle images, and may be an acceptable exception even to many sharing this philosophy.

I'm interested in the comment that a gui button can be optional. Has that already been implemented? What optional buttons are available that I don't yet know about?

The addition of toolbar buttons of course must be backend-specific, but I'm sure the actual "copy" function could be made backend-independent. Perhaps as a method of Figure?

@pelson
Copy link
Member

pelson commented May 10, 2013

How about adding a keyboard shortcut? ctrl+c is pretty ubiquitous...

It'd be nice if this were a method on the figure (or some other suitable place) which did not need to know about the backends/OS etc. - perhaps https://github.com/gfxmonk/pyperclip might be useful. If not, I propose starting a new repo which provides copy (and paste) capabilities in a cross platform & cross windowing toolkit way which matplotlib can make use of.

@pelson pelson closed this as completed May 10, 2013
@pelson pelson reopened this May 10, 2013
@mdboom
Copy link
Member

mdboom commented May 10, 2013

pyperclip comes close. We would need to add a few backends that we have that it doesn't (tk and wx, for example). It imports GUI frameworks to do what it does, and importing multiple gui frameworks at the same time can cause all kinds of strange conflicts, so we'd need to make sure it never imports something that matplotlib hasn't already imported. It also seems to only deal with setting text to the clipboard, not binary images, but that's probably also fixable.

I think ultimately, the backends are going to have to handle this, though. It might be possible to copy from an Agg backend in a backend-agnostic way, but if the rendering is being handled by the GUI framework, (Gtk, Wx and Mac OS-X) the method used to retrieve the buffer and put it in the clipboard will be different. I think it's cleanest to just put this in the backends. That doesn't mean we can't put a method on the figure that delegates to the backend, though, and from the user's point of view, it's an operation on the figure.

@pelson
Copy link
Member

pelson commented May 13, 2013

I think ultimately, the backends are going to have to handle this, though.

Mmmm. I'm inclined to agree. Thanks for the insight @mdboom.

@tacaswell
Copy link
Member

This is an obvious addition to the Tools library in MEP22.

@fariza
Copy link
Member

fariza commented Jan 22, 2014

Added a CopyTool in the example examples/user_interfaces/navigation.py of MEP22
https://github.com/matplotlib/matplotlib/wiki/Mep22#branches-and-pull-requests (I don't know if it is the right way to do it, but it seems to work).

Does anybody has a proposal to handle default (optional) Tools?

  • rcParams
  • Class methods in the Navigation to modify the default Tools
  • A Tool, to set default tools

@fariza
Copy link
Member

fariza commented Jan 27, 2014

If everybody agrees, at the same time of implementing all the backends for MEP22, I can add this as a default Tool, of course without button, just with the ctrl+c shortcut,

@efiring
Copy link
Member

efiring commented Jan 27, 2014

ctrl-C seems problematic as a universal "copy" shortcut. On the Mac it is cmd-C. In typical linux terminals it is shift-ctrl-C because ctrl-C is of course the classic "abort", or KeyboardInterrupt in python. I suppose you could still use ctrl-C in the figure window, though. The user just has to be sure that window has focus.

@mdboom
Copy link
Member

mdboom commented Jan 27, 2014

Many of the GUI frameworks have ways of getting the default key shortcuts for common operations like this. We should at least use those as the default. For example, in Qt (scroll down to standard shortcuts):

http://qt-project.org/doc/qt-4.8/qkeysequence.html

I recall Gtk having similar facilities, though I can't find the docs atm.

@fariza
Copy link
Member

fariza commented Jan 28, 2014

Ups... I didn't realize the problem.
I guess this will have to wait until we have a method to find this shortcuts for each backend.

I wasn't able to find it for GTK, I posted a question on SO....

@fariza
Copy link
Member

fariza commented Jan 30, 2014

Why rcsetup doesn't have a validation against OS/framework?
As far as I can see, the option so far has been to include everything in the strings, for example 'keymap.quit': [('ctrl+w', 'cmd+w'), validate_stringlist], instead of checking witch one is available for the current OS/framework.
If this is considered an acceptable practice, in that case I would suggest to add
'keymap.copy': [('ctrl+c', 'cmd+c', 'shift+ctrl+c'),validate_stringlist ] to rcParams

@efiring
Copy link
Member

efiring commented Jan 31, 2014

@fariza That makes sense; if we ever decide we need to customize for a given OS, all of these can be customized at once. Probably no need, though.

@fariza
Copy link
Member

fariza commented Apr 7, 2015

For reference, the GTK3 tool that allows to copy the canvas to the clipboard is

class CopyToolGTK3(ToolBase):
    '''Copy canvas to clipboard'''
    default_keymap = 'ctrl+c'
    description = 'Copy canvas'

    def trigger(self, *args, **kwargs):
        clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        window = self.figure.canvas.get_window()
        x, y, width, height = window.get_geometry()
        pb = Gdk.pixbuf_get_from_window(window, x, y, width, height)
        clipboard.set_image(pb)

The idea is to get the same for all the backends, and then include it as default.

@thomasaarholt
Copy link

Could I bring this back from the silence? This would still be useful.

@fariza
Copy link
Member

fariza commented Jan 13, 2016

@thomasaarholt yes indeed this is useful.
The reason I didn't add it as default tool is... (I'm ashamed) is because I wasn't able to get it to work with Tkinter 😊 .

The default tools have to be available in all the backends. At this moment the Toolmanager is working with Gtk3 and Tkinter, so if we want this to be added, we have to make Tkinter's version.

If you can help out and get it to work with Tkinter, then we can add it, and in that way, we force all other backends to have it also (when they are being ported).

@thomasaarholt
Copy link

@fariza, I am only just beginning to get to know Python as a developer, and in the writing-up stage of my PhD. :) (It was due to the last bit that I realised that it might be a useful feature) After I've finished my thesis, I'd love to take a look!

@ghost
Copy link

ghost commented May 25, 2017

If I may cast a vote, as a (former, at least for now) Matlab user I would also find this feature extremely useful.

As of now I right click the toolbar of the Qt4 backend, uncheck the checkbox to hide the toolbar and use ksnapshot to grab a snapshot of the window region. Of course having a button would make everything faster :)

@fariza
Copy link
Member

fariza commented Jun 3, 2017

@marcwell as I said before, I am waiting for volunteers to implement the tool for other backends. So far I have only Gtk3

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@smcinerney
Copy link

@fariza
Copy link
Member

fariza commented Feb 13, 2018

I just started the work on the implementation, I need help for QT and TK... volunteers?

@timhoffm
Copy link
Member

I could do the Qt part at some point. But not before in a month or so. So if someone else is quicker, go for it.

@jklymak
Copy link
Member

jklymak commented Jul 28, 2020

#10446 is merged - I'll close this as done, but feel free to re-open if I'm misunderstanding....

@jklymak jklymak closed this as completed Jul 28, 2020
@story645 story645 removed this from the future releases milestone Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests