##// END OF EJS Templates
check for unicode before encoding as UTF-8
Martin Spacek -
Show More
@@ -1,86 +1,89 b''
1 """ Defines utility functions for working with SVG documents in Qt.
1 """ Defines utility functions for working with SVG documents in Qt.
2 """
2 """
3
3
4 # System library imports.
4 # System library imports.
5 from IPython.external.qt import QtCore, QtGui, QtSvg
5 from IPython.external.qt import QtCore, QtGui, QtSvg
6
6
7
7
8 def save_svg(string, parent=None):
8 def save_svg(string, parent=None):
9 """ Prompts the user to save an SVG document to disk.
9 """ Prompts the user to save an SVG document to disk.
10
10
11 Parameters:
11 Parameters:
12 -----------
12 -----------
13 string : basestring
13 string : basestring
14 A Python string containing a SVG document.
14 A Python string containing a SVG document.
15
15
16 parent : QWidget, optional
16 parent : QWidget, optional
17 The parent to use for the file dialog.
17 The parent to use for the file dialog.
18
18
19 Returns:
19 Returns:
20 --------
20 --------
21 The name of the file to which the document was saved, or None if the save
21 The name of the file to which the document was saved, or None if the save
22 was cancelled.
22 was cancelled.
23 """
23 """
24 if isinstance(string, unicode):
25 string = string.encode('utf-8')
26
24 dialog = QtGui.QFileDialog(parent, 'Save SVG Document')
27 dialog = QtGui.QFileDialog(parent, 'Save SVG Document')
25 dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
28 dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
26 dialog.setDefaultSuffix('svg')
29 dialog.setDefaultSuffix('svg')
27 dialog.setNameFilter('SVG document (*.svg)')
30 dialog.setNameFilter('SVG document (*.svg)')
28 if dialog.exec_():
31 if dialog.exec_():
29 filename = dialog.selectedFiles()[0]
32 filename = dialog.selectedFiles()[0]
30 f = open(filename, 'w')
33 f = open(filename, 'w')
31 try:
34 try:
32 f.write(string.encode('UTF-8'))
35 f.write(string)
33 finally:
36 finally:
34 f.close()
37 f.close()
35 return filename
38 return filename
36 return None
39 return None
37
40
38 def svg_to_clipboard(string):
41 def svg_to_clipboard(string):
39 """ Copy a SVG document to the clipboard.
42 """ Copy a SVG document to the clipboard.
40
43
41 Parameters:
44 Parameters:
42 -----------
45 -----------
43 string : basestring
46 string : basestring
44 A Python string containing a SVG document.
47 A Python string containing a SVG document.
45 """
48 """
46 if isinstance(string, unicode):
49 if isinstance(string, unicode):
47 string = string.encode('utf-8')
50 string = string.encode('utf-8')
48
51
49 mime_data = QtCore.QMimeData()
52 mime_data = QtCore.QMimeData()
50 mime_data.setData('image/svg+xml', string)
53 mime_data.setData('image/svg+xml', string)
51 QtGui.QApplication.clipboard().setMimeData(mime_data)
54 QtGui.QApplication.clipboard().setMimeData(mime_data)
52
55
53 def svg_to_image(string, size=None):
56 def svg_to_image(string, size=None):
54 """ Convert a SVG document to a QImage.
57 """ Convert a SVG document to a QImage.
55
58
56 Parameters:
59 Parameters:
57 -----------
60 -----------
58 string : basestring
61 string : basestring
59 A Python string containing a SVG document.
62 A Python string containing a SVG document.
60
63
61 size : QSize, optional
64 size : QSize, optional
62 The size of the image that is produced. If not specified, the SVG
65 The size of the image that is produced. If not specified, the SVG
63 document's default size is used.
66 document's default size is used.
64
67
65 Raises:
68 Raises:
66 -------
69 -------
67 ValueError
70 ValueError
68 If an invalid SVG string is provided.
71 If an invalid SVG string is provided.
69
72
70 Returns:
73 Returns:
71 --------
74 --------
72 A QImage of format QImage.Format_ARGB32.
75 A QImage of format QImage.Format_ARGB32.
73 """
76 """
74 if isinstance(string, unicode):
77 if isinstance(string, unicode):
75 string = string.encode('utf-8')
78 string = string.encode('utf-8')
76
79
77 renderer = QtSvg.QSvgRenderer(QtCore.QByteArray(string))
80 renderer = QtSvg.QSvgRenderer(QtCore.QByteArray(string))
78 if not renderer.isValid():
81 if not renderer.isValid():
79 raise ValueError('Invalid SVG data.')
82 raise ValueError('Invalid SVG data.')
80
83
81 if size is None:
84 if size is None:
82 size = renderer.defaultSize()
85 size = renderer.defaultSize()
83 image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32)
86 image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32)
84 painter = QtGui.QPainter(image)
87 painter = QtGui.QPainter(image)
85 renderer.render(painter)
88 renderer.render(painter)
86 return image
89 return image
General Comments 0
You need to be logged in to leave comments. Login now