Skip to content

Commit

Permalink
Merge pull request #368 from mattgibbs/symbol_crash_fix
Browse files Browse the repository at this point in the history
Fix crash in PyDMSymbol when the imageFiles attribute is invalid.
  • Loading branch information
hhslepicka authored Jul 12, 2018
2 parents 60b5060 + e938981 commit fd9a120
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions pydm/widgets/symbol.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import json
import logging
from ..PyQt.QtGui import QApplication, QWidget, QPainter, QPixmap, QStyle, QStyleOption
from ..PyQt.QtCore import pyqtProperty, Qt, QSize, QSizeF, QRectF, qInstallMessageHandler
from ..PyQt.QtSvg import QSvgRenderer
import json
from ..utilities import is_pydm_app
from .base import PyDMWidget

logger = logging.getLogger(__name__)

class PyDMSymbol(QWidget, PyDMWidget):
"""
PyDMSymbol will render an image (symbol) for each value of a channel.
Expand All @@ -19,6 +23,7 @@ def __init__(self, parent=None, init_channel=None):
QWidget.__init__(self, parent)
PyDMWidget.__init__(self, init_channel=init_channel)
self.app = QApplication.instance()
self._state_images_string = ""
self._state_images = {} # Keyed on state values (ints), values are (filename, qpixmap or qsvgrenderer) tuples.
self._aspect_ratio_mode = Qt.KeepAspectRatio
self._sizeHint = self.minimumSizeHint()
Expand All @@ -41,6 +46,8 @@ def imageFiles(self):
-------
str
"""
if not self._state_images:
return self._state_images_string
return json.dumps({str(state): val[0] for (state, val) in self._state_images.items()})

@imageFiles.setter
Expand All @@ -53,13 +60,21 @@ def imageFiles(self, new_files):
----------
new_files : str
"""
new_file_dict = json.loads(str(new_files))
self._state_images_string = str(new_files)
try:
new_file_dict = json.loads(self._state_images_string)
except Exception:
self._state_images = {}
return
self._sizeHint = QSize(0, 0)
for (state, filename) in new_file_dict.items():
try:
file_path = self.app.get_path(filename)
except Exception as e:
print(e)
if is_pydm_app():
try:
file_path = self.app.get_path(filename)
except Exception as e:
logger.exception("Couldn't get file with path %s", filename)
file_path = filename
else:
file_path = filename
# First, lets try SVG. We have to try SVG first, otherwise
# QPixmap will happily load the SVG and turn it into a raster image.
Expand All @@ -83,7 +98,7 @@ def imageFiles(self, new_files):
self._sizeHint = self._sizeHint.expandedTo(image.size())
continue
# If we get this far, the file specified could not be loaded at all.
print("Could not load image: {}".format(filename))
logger.error("Could not load image: {}".format(filename))
self._state_images[int(state)] = (filename, None)

@pyqtProperty(Qt.AspectRatioMode)
Expand Down

0 comments on commit fd9a120

Please sign in to comment.