Skip to content

Commit

Permalink
Show a default symbol in case of corrupted file
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-duponchelle committed May 24, 2017
1 parent 073665a commit 8ed8a2c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gns3/items/node_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def symbol(self):

@qslot
def _symbolLoadedCallback(self, path, *args):
renderer = QImageSvgRenderer(path)
renderer = QImageSvgRenderer(path, fallback=":/icons/cancel.svg")
renderer.setObjectName(path)
self.setSharedRenderer(renderer)
if self._node.settings().get("symbol") != self._symbol:
Expand Down
28 changes: 21 additions & 7 deletions gns3/qt/qimage_svg_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@
from . import QtSvg
from . import QtGui

import logging
log = logging.getLogger(__name__)


class QImageSvgRenderer(QtSvg.QSvgRenderer):
"""
Renderer pixmap and svg to SVG item
:param path_or_data: Svg element of path to a SVG
:param fallback: Image to display if the image is not working
"""

def __init__(self, path_or_data=None):
def __init__(self, path_or_data=None, fallback=None):
super().__init__()
self._fallback = fallback
self._svg = """<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{width}" height="{height}"></svg>"""
self.load(path_or_data)

Expand Down Expand Up @@ -61,12 +66,21 @@ def load(self, path_or_data):
data = QtCore.QByteArray()
buf = QtCore.QBuffer(data)
image.save(buf, 'PNG')
self._svg = """<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{width}" height="{height}">
<image width="{width}" height="{height}" xlink:href="data:image/png;base64,{data}"/>
</svg>""".format(data=bytes(data.toBase64()).decode(),
width=image.rect().width(),
height=image.rect().height())
return super().load(self._svg.encode())
if image.rect().width() > 0:
self._svg = """<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{width}" height="{height}">
<image width="{width}" height="{height}" xlink:href="data:image/png;base64,{data}"/>
</svg>""".format(data=bytes(data.toBase64()).decode(),
width=image.rect().width(),
height=image.rect().height())
res = super().load(self._svg.encode())
elif self._fallback:
log.error("Invalid or corrupted image file")
res = super().load(self._fallback)
else:
self._svg = """<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
</svg>"""
res = super().load(self._svg.encode())
return res

def svg(self):
"""
Expand Down

0 comments on commit 8ed8a2c

Please sign in to comment.