merged SVG fixes from acq4

This commit is contained in:
Luke Campagnola 2012-12-28 16:27:17 -05:00
commit 9b4eb78d12
4 changed files with 37 additions and 16 deletions

View File

@ -83,7 +83,7 @@ class SVGExporter(Exporter):
return bytes(xml)
else:
with open(fileName, 'w') as fh:
fh.write(xml)
fh.write(xml.encode('UTF-8'))
xmlHeader = """\
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
@ -171,8 +171,16 @@ def _generateItemSvg(item, nodes=None, root=None):
doc = xml.parseString(xmlStr)
else:
childs = item.childItems()
tr = itemTransform(item, root)
tr = itemTransform(item, item.scene())
## offset to corner of root item
if isinstance(root, QtGui.QGraphicsScene):
rootPos = QtCore.QPoint(0,0)
else:
rootPos = root.scenePos()
tr2 = QtGui.QTransform()
tr2.translate(-rootPos.x(), -rootPos.y())
tr = tr * tr2
#print item, pg.SRTTransform(tr)
#tr.translate(item.pos().x(), item.pos().y())
@ -239,17 +247,23 @@ def _generateItemSvg(item, nodes=None, root=None):
nodes[name] = g1
g1.setAttribute('id', name)
## If this item clips its children, we need to take car of that.
## If this item clips its children, we need to take care of that.
childGroup = g1 ## add children directly to this node unless we are clipping
if not isinstance(item, QtGui.QGraphicsScene):
## See if this item clips its children
if int(item.flags() & item.ItemClipsChildrenToShape) > 0:
## Generate svg for just the path
if isinstance(root, QtGui.QGraphicsScene):
path = QtGui.QGraphicsPathItem(item.mapToScene(item.shape()))
else:
path = QtGui.QGraphicsPathItem(root.mapToParent(item.mapToItem(root, item.shape())))
pathNode = _generateItemSvg(path, root=root).getElementsByTagName('path')[0]
#if isinstance(root, QtGui.QGraphicsScene):
#path = QtGui.QGraphicsPathItem(item.mapToScene(item.shape()))
#else:
#path = QtGui.QGraphicsPathItem(root.mapToParent(item.mapToItem(root, item.shape())))
path = QtGui.QGraphicsPathItem(item.mapToScene(item.shape()))
item.scene().addItem(path)
try:
pathNode = _generateItemSvg(path, root=root).getElementsByTagName('path')[0]
finally:
item.scene().removeItem(path)
## and for the clipPath element
clip = name + '_clip'
clipNode = g1.ownerDocument.createElement('clipPath')
@ -294,7 +308,10 @@ def correctCoordinates(node, item):
elif ch.tagName == 'path':
removeTransform = True
newCoords = ''
for c in ch.getAttribute('d').strip().split(' '):
oldCoords = ch.getAttribute('d').strip()
if oldCoords == '':
continue
for c in oldCoords.split(' '):
x,y = c.split(',')
if x[0].isalpha():
t = x[0]
@ -317,8 +334,6 @@ def correctCoordinates(node, item):
#fs = c[1]-c[2]
#fs = (fs**2).sum()**0.5
#ch.setAttribute('font-size', str(fs))
else:
print('warning: export not implemented for SVG tag %s (from item %s)' % (ch.tagName, item))
## correct line widths if needed
if removeTransform and ch.getAttribute('vector-effect') != 'non-scaling-stroke':

View File

@ -54,3 +54,8 @@ class ArrowItem(QtGui.QGraphicsPathItem):
def paint(self, p, *args):
p.setRenderHint(QtGui.QPainter.Antialiasing)
QtGui.QGraphicsPathItem.paint(self, p, *args)
def shape(self):
#if not self.opts['pxMode']:
#return QtGui.QGraphicsPathItem.shape(self)
return self.path

View File

@ -496,4 +496,4 @@ class GraphicsItem(object):
#self._exportOpts['antialias'] = True
else:
self._exportOpts = False

View File

@ -115,9 +115,10 @@ class TextItem(UIGraphicsItem):
self.viewRangeChanged()
self.lastTransform = tr
p.setPen(self.border)
p.setBrush(self.fill)
p.setRenderHint(p.Antialiasing, True)
p.drawPolygon(self.textItem.mapToParent(self.textItem.boundingRect()))
if self.border.style() != QtCore.Qt.NoPen or self.fill.style() != QtCore.Qt.NoBrush:
p.setPen(self.border)
p.setBrush(self.fill)
p.setRenderHint(p.Antialiasing, True)
p.drawPolygon(self.textItem.mapToParent(self.textItem.boundingRect()))