Skip to content

Commit

Permalink
ENH App menu command: Edit -> Copy as PNG
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbrodbeck committed Feb 3, 2017
1 parent a855c49 commit a7f708f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
9 changes: 9 additions & 0 deletions eelbrain/_wxgui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def OnInit(self):
m.AppendSeparator()
m.Append(wx.ID_CUT, 'Cut \tCtrl+X')
m.Append(wx.ID_COPY, 'Copy \tCtrl+C')
m.Append(ID.COPY_AS_PNG, 'Copy as PNG \tCtrl+Shift+C')
m.Append(wx.ID_PASTE, 'Paste \tCtrl+V')
m.AppendSeparator()
m.Append(wx.ID_CLEAR, 'Cle&ar')
Expand Down Expand Up @@ -109,6 +110,7 @@ def OnInit(self):
self.Bind(wx.EVT_MENU, self.OnClear, id=wx.ID_CLEAR)
self.Bind(wx.EVT_MENU, self.OnCloseWindow, id=wx.ID_CLOSE)
self.Bind(wx.EVT_MENU, self.OnCopy, id=wx.ID_COPY)
self.Bind(wx.EVT_MENU, self.OnCopyAsPNG, id=ID.COPY_AS_PNG)
self.Bind(wx.EVT_MENU, self.OnCut, id=wx.ID_CUT)
self.Bind(wx.EVT_MENU, self.OnOnlineHelp, id=ID.HELP_EELBRAIN)
self.Bind(wx.EVT_MENU, self.OnOnlineHelp, id=ID.HELP_PYTHON)
Expand All @@ -131,6 +133,7 @@ def OnInit(self):
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUIClear, id=wx.ID_CLEAR)
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUIClose, id=wx.ID_CLOSE)
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUICopy, id=wx.ID_COPY)
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUICopyAsPNG, id=ID.COPY_AS_PNG)
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUICut, id=wx.ID_CUT)
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUIDown, id=wx.ID_DOWN)
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUIForward, id=wx.ID_FORWARD)
Expand Down Expand Up @@ -369,6 +372,9 @@ def OnCloseWindow(self, event):
def OnCopy(self, event):
win = wx.Window.FindFocus()
win.Copy()

def OnCopyAsPNG(self, event):
wx.Window.FindFocus().CopyAsPNG()
#
def OnCut(self, event):
win = wx.Window.FindFocus()
Expand Down Expand Up @@ -476,6 +482,9 @@ def OnUpdateUICopy(self, event):
win = wx.Window.FindFocus()
event.Enable(win and hasattr(win, 'CanCopy') and win.CanCopy())

def OnUpdateUICopyAsPNG(self, event):
event.Enable(hasattr(wx.Window.FindFocus(), 'CopyAsPNG'))

def OnUpdateUICut(self, event):
win = wx.Window.FindFocus()
event.Enable(win and hasattr(win, 'CanCut') and win.CanCut())
Expand Down
43 changes: 18 additions & 25 deletions eelbrain/_wxgui/mpl_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
'''

from logging import getLogger
import os
import tempfile
import logging

import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
Expand Down Expand Up @@ -36,7 +36,6 @@ class FigureCanvasPanel(FigureCanvasWxAgg):
Subclass of mpl's Canvas to allow for more interaction with Eelbrain (such
as copying the contents to the clipboard).
"""
_copy_as_pdf = True
def __init__(self, parent, *args, **kwargs):
"""wx.Panel with a matplotlib figure
Expand Down Expand Up @@ -77,29 +76,23 @@ def ChangeCursor(self, event):
"http://matplotlib.sourceforge.net/examples/user_interfaces/wxcursor_demo.html"
self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))

# def OnPaint(self, event):
# self.draw()

def Copy(self):
if self._copy_as_pdf:
try:
if wx.TheClipboard.Open():
# same code in mpl_tools
path = tempfile.mktemp('.pdf') # , text=True)
logging.debug("Temporary file created at: %s" % path)
self.figure.savefig(path)
# copy path
do = wx.FileDataObject()
do.AddFile(path)
wx.TheClipboard.SetData(do)
wx.TheClipboard.Close()
except wx._core.PyAssertionError:
wx.TheClipboard.Close()

else:
self.figure.set_facecolor((1, 1, 1))
self.draw()
self.Copy_to_Clipboard()
# By default, copy PDF
if not wx.TheClipboard.Open():
getLogger('eelbrain').debug("Failed to open clipboard")
return
try:
path = tempfile.mktemp('.pdf')
self.figure.savefig(path)
# copy path
do = wx.FileDataObject()
do.AddFile(path)
wx.TheClipboard.SetData(do)
finally:
wx.TheClipboard.Close()

def CopyAsPNG(self):
self.Copy_to_Clipboard()

def MatplotlibEvent(self, event):
"Create dummy event to check in_axes"
Expand Down
1 change: 1 addition & 0 deletions eelbrain/_wxutils/ID.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
CLEAR_TERMINAL = wx.NewId()
COLOUR_CHOOSER = wx.NewId()
COMMENT = wx.NewId()
COPY_AS_PNG = wx.NewId()

DATASET_ATTACH = wx.NewId()
DATASET_IMPORT = wx.NewId()
Expand Down

0 comments on commit a7f708f

Please sign in to comment.