diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2258b7a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +*.egg-info +__pycache__ diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..4bf4483 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include README.md \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..48dc94f --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +addcopyfighandler: Adds a Ctrl+C handler to matplotlib figures for copying the figure to the clipboard +====================================================================================================== + +Simply importing this module (after importing matplotlib) will add a handler +so that pressing Ctrl+C with a matplotlib figure window selected will copy +the figure to the clipboard as an image. + +Original concept code taken from: +https://stackoverflow.com/questions/31607458/how-to-add-clipboard-support-to-matplotlib-figures + + +Releases +-------- + +### 1.0: 2017-08-09 + +- Initial release + + +### 1.0.1: 2018-11-27 + +- Improve setup.py: remove need for importing module, add proper installation dependencies +- Change readme from ReST to Markdown \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..7f96e8c --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ +# empty on purpose diff --git a/addcopyfighandler.py b/addcopyfighandler.py new file mode 100644 index 0000000..daf15a5 --- /dev/null +++ b/addcopyfighandler.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +""" +Monkey-patch plt.figure() to support Ctrl+C for copying to clipboard as an image + +@author: Josh Burnett +Modified from code found on Stack Exchange: + https://stackoverflow.com/questions/31607458/how-to-add-clipboard-support-to-matplotlib-figures +""" + + +import matplotlib.pyplot as plt +from win32gui import GetWindowText, GetForegroundWindow + +import io +import qtpy + +__version__ = (1, 0, 1) + +if qtpy.API == 'pyqt4': + from PyQt4.QtGui import QApplication, QImage + clipboard = QApplication.clipboard +elif qtpy.API == 'pyqt5': + from PyQt5.QtGui import QGuiApplication, QImage + clipboard = QGuiApplication.clipboard + +oldfig = plt.figure + + +def copyfig(fig=None): + # store the image in a buffer using savefig(), this has the + # advantage of applying all the default savefig parameters + # such as background color; those would be ignored if you simply + # grab the canvas using Qt + if fig is None: + # find the figure window that has UI focus right now (not necessarily the same as plt.gcf()) + fig_window_text = GetWindowText(GetForegroundWindow()) + for i in plt.get_fignums(): + if plt.figure(i).canvas.get_window_title() == fig_window_text: + fig = plt.figure(i) + break + + buf = io.BytesIO() + fig.savefig(buf) + clipboard().setImage(QImage.fromData(buf.getvalue())) + buf.close() + + +def newfig(*args, **kwargs): + fig = oldfig(*args, **kwargs) + + def clipboard_handler(event): + if event.key == 'ctrl+c': + copyfig() + + fig.canvas.mpl_connect('key_press_event', clipboard_handler) + return fig + + +plt.figure = newfig diff --git a/build/lib/addcopyfighandler.py b/build/lib/addcopyfighandler.py new file mode 100644 index 0000000..daf15a5 --- /dev/null +++ b/build/lib/addcopyfighandler.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +""" +Monkey-patch plt.figure() to support Ctrl+C for copying to clipboard as an image + +@author: Josh Burnett +Modified from code found on Stack Exchange: + https://stackoverflow.com/questions/31607458/how-to-add-clipboard-support-to-matplotlib-figures +""" + + +import matplotlib.pyplot as plt +from win32gui import GetWindowText, GetForegroundWindow + +import io +import qtpy + +__version__ = (1, 0, 1) + +if qtpy.API == 'pyqt4': + from PyQt4.QtGui import QApplication, QImage + clipboard = QApplication.clipboard +elif qtpy.API == 'pyqt5': + from PyQt5.QtGui import QGuiApplication, QImage + clipboard = QGuiApplication.clipboard + +oldfig = plt.figure + + +def copyfig(fig=None): + # store the image in a buffer using savefig(), this has the + # advantage of applying all the default savefig parameters + # such as background color; those would be ignored if you simply + # grab the canvas using Qt + if fig is None: + # find the figure window that has UI focus right now (not necessarily the same as plt.gcf()) + fig_window_text = GetWindowText(GetForegroundWindow()) + for i in plt.get_fignums(): + if plt.figure(i).canvas.get_window_title() == fig_window_text: + fig = plt.figure(i) + break + + buf = io.BytesIO() + fig.savefig(buf) + clipboard().setImage(QImage.fromData(buf.getvalue())) + buf.close() + + +def newfig(*args, **kwargs): + fig = oldfig(*args, **kwargs) + + def clipboard_handler(event): + if event.key == 'ctrl+c': + copyfig() + + fig.canvas.mpl_connect('key_press_event', clipboard_handler) + return fig + + +plt.figure = newfig diff --git a/dist/addcopyfighandler-1.0.1-py2.py3-none-any.whl b/dist/addcopyfighandler-1.0.1-py2.py3-none-any.whl new file mode 100644 index 0000000..618d904 Binary files /dev/null and b/dist/addcopyfighandler-1.0.1-py2.py3-none-any.whl differ diff --git a/dist/addcopyfighandler-1.0.1.tar.gz b/dist/addcopyfighandler-1.0.1.tar.gz new file mode 100644 index 0000000..0e253ef Binary files /dev/null and b/dist/addcopyfighandler-1.0.1.tar.gz differ diff --git a/generate_source_dist.bat b/generate_source_dist.bat new file mode 100644 index 0000000..c19d0b1 --- /dev/null +++ b/generate_source_dist.bat @@ -0,0 +1 @@ +python setup.py sdist --formats=gztar,zip bdist_wheel diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..4f4bb69 --- /dev/null +++ b/setup.py @@ -0,0 +1,55 @@ +import re +from setuptools import setup + +with open('README.md') as readme: + long_description = readme.read() + + +def get_version(filename='addcopyfighandler.py'): + """ Extract version information stored as a tuple in source code """ + version = '' + with open(filename, 'r') as fp: + for line in fp: + m = re.search('__version__ .* \((.*)\)', line) + if m is not None: + version = m.group(1).replace(', ', '.') + break + return version + + +# What packages are required for this module to be executed? +REQUIRED = [ + 'matplotlib', 'pywin32', 'qtpy', +] + +setup( + name = "addcopyfighandler", + version = get_version(), + + py_modules=["addcopyfighandler"], + + install_requires=REQUIRED, + + classifiers=[ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + + 'Programming Language :: Python', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + ], + + # metadata for upload to PyPI + author = "Josh Burnett", + author_email = "github@burnettsonline.org", + description = "Adds a Ctrl+C handler to matplotlib figures for copying the figure to the clipboard", + long_description=long_description, + long_description_content_type="text/markdown", + license = "MIT", + keywords = "addcopyfighandler figure matplotlib handler copy", + url = "https://github.com/joshburnett/addcopyfighandler", + platform="windows", +) \ No newline at end of file