##// END OF EJS Templates
Add a WrappedError class
Add a WrappedError class

File last commit:

r13587:210d5e2f
r18895:33abaae6
Show More
svg.py
91 lines | 2.4 KiB | text/x-python | PythonLexer
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 """ Defines utility functions for working with SVG documents in Qt.
"""
# System library imports.
Evan Patterson
Paved the way for PySide support....
r3304 from IPython.external.qt import QtCore, QtGui, QtSvg
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765
Thomas Kluyver
Replace references to unicode and basestring
r13353 # Our own imports
from IPython.utils.py3compat import unicode_type
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765
def save_svg(string, parent=None):
""" Prompts the user to save an SVG document to disk.
Thomas Kluyver
Clean up numpydoc section headers
r13587 Parameters
----------
epatters
PySide fix: PySide's QByteArray constructor does not overload for unicode.
r3335 string : basestring
Evan Patterson
Paved the way for PySide support....
r3304 A Python string containing a SVG document.
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765
parent : QWidget, optional
The parent to use for the file dialog.
Thomas Kluyver
Clean up numpydoc section headers
r13587 Returns
-------
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 The name of the file to which the document was saved, or None if the save
was cancelled.
"""
Thomas Kluyver
Replace references to unicode and basestring
r13353 if isinstance(string, unicode_type):
Martin Spacek
check for unicode before encoding as UTF-8
r3920 string = string.encode('utf-8')
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 dialog = QtGui.QFileDialog(parent, 'Save SVG Document')
dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
dialog.setDefaultSuffix('svg')
dialog.setNameFilter('SVG document (*.svg)')
if dialog.exec_():
filename = dialog.selectedFiles()[0]
Thomas Kluyver
Replace references to unicode and basestring
r13353 f = open(filename, 'wb')
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 try:
Martin Spacek
check for unicode before encoding as UTF-8
r3920 f.write(string)
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 finally:
f.close()
return filename
return None
def svg_to_clipboard(string):
""" Copy a SVG document to the clipboard.
Thomas Kluyver
Clean up numpydoc section headers
r13587 Parameters
----------
epatters
PySide fix: PySide's QByteArray constructor does not overload for unicode.
r3335 string : basestring
Evan Patterson
Paved the way for PySide support....
r3304 A Python string containing a SVG document.
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 """
Thomas Kluyver
Replace references to unicode and basestring
r13353 if isinstance(string, unicode_type):
epatters
PySide fix: PySide's QByteArray constructor does not overload for unicode.
r3335 string = string.encode('utf-8')
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 mime_data = QtCore.QMimeData()
Evan Patterson
Paved the way for PySide support....
r3304 mime_data.setData('image/svg+xml', string)
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 QtGui.QApplication.clipboard().setMimeData(mime_data)
def svg_to_image(string, size=None):
""" Convert a SVG document to a QImage.
Thomas Kluyver
Clean up numpydoc section headers
r13587 Parameters
----------
epatters
PySide fix: PySide's QByteArray constructor does not overload for unicode.
r3335 string : basestring
Evan Patterson
Paved the way for PySide support....
r3304 A Python string containing a SVG document.
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765
size : QSize, optional
The size of the image that is produced. If not specified, the SVG
document's default size is used.
Thomas Kluyver
Clean up numpydoc section headers
r13587 Raises
------
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 ValueError
If an invalid SVG string is provided.
Thomas Kluyver
Clean up numpydoc section headers
r13587 Returns
-------
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 A QImage of format QImage.Format_ARGB32.
"""
Thomas Kluyver
Replace references to unicode and basestring
r13353 if isinstance(string, unicode_type):
epatters
PySide fix: PySide's QByteArray constructor does not overload for unicode.
r3335 string = string.encode('utf-8')
Evan Patterson
Paved the way for PySide support....
r3304 renderer = QtSvg.QSvgRenderer(QtCore.QByteArray(string))
epatters
* Added a custom context menu to the RichIPythonWidget which allows saving plot as an images or SVG documents....
r2765 if not renderer.isValid():
raise ValueError('Invalid SVG data.')
if size is None:
size = renderer.defaultSize()
image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32)
painter = QtGui.QPainter(image)
renderer.render(painter)
return image