OSDN Git Service

fix loading strokemap brushes w/ unknown parents
authorAndrew Chadwick <andrewc-git@piffle.org>
Wed, 21 Sep 2011 00:21:48 +0000 (01:21 +0100)
committerAndrew Chadwick <andrewc-git@piffle.org>
Wed, 21 Sep 2011 00:57:01 +0000 (01:57 +0100)
Fix for a pair of related exceptions raised when trying to load toolbar
previews for brushes whose parent is no longer on disk.

This can happen when loading old files or files from other people whose
strokemaps contain brush settings whose stored parent name is no longer
in the user's collection.

gui/brushmanager.py
gui/toolbar.py
lib/brush.py

index f22938b..3691685 100644 (file)
@@ -19,6 +19,7 @@ from os.path import basename
 import urllib
 import gobject
 from lib.brush import BrushInfo
+from warnings import warn
 
 preview_w = 128
 preview_h = 128
@@ -801,6 +802,10 @@ class ManagedBrush(object):
 
     def load_preview(self):
         """Loads the brush preview as pixbuf into the brush."""
+        if self.name is None:
+            warn("Attempt to load preview for unnamed brush, don't do that.",
+                 RuntimeWarning, 2)
+            return
         prefix = self.get_fileprefix()
 
         filename = prefix + '_prev.png'
index 83efff8..2d0acb0 100644 (file)
@@ -587,9 +587,12 @@ class ManagedBrushPreview (gtk.Image):
     def set_from_managed_brush(self, brush):
         if brush is None:
             return
+        self.pixbuf = None
         if not brush.preview:
-            brush.load_preview()
-        self.pixbuf = brush.preview.copy()
+            if brush.name is not None:   # e.g. one from a strokemap
+                brush.load_preview()
+        if brush.preview:
+            self.pixbuf = brush.preview.copy()
         self.brush_name = brush.get_display_name()
         self._update()
 
@@ -600,23 +603,26 @@ class ManagedBrushPreview (gtk.Image):
             self.image_size = alloc.width, alloc.height
             self._update()
 
+    def _get_scaled_pixbuf(self, size):
+        if self.pixbuf is None:
+            theme = gtk.icon_theme_get_default()
+            return theme.load_icon(gtk.STOCK_MISSING_IMAGE, size, 0)
+        else:
+            return self.pixbuf.scale_simple(size, size, gdk.INTERP_BILINEAR)
 
     def on_query_tooltip(self, widget, x, y, keyboard_mode, tooltip):
-        if not self.pixbuf:
-            return False
         s = self.TOOLTIP_ICON_SIZE
-        scaled_pixbuf = self.pixbuf.scale_simple(s, s, gdk.INTERP_BILINEAR)
+        scaled_pixbuf = self._get_scaled_pixbuf(s)
         tooltip.set_icon(scaled_pixbuf)
         tooltip.set_text(self.brush_name)  # XXX markup and summary of changes
         return True
 
-
     def _update(self):
-        if not (self.pixbuf and self.image_size):
+        if not self.image_size:
             return
         w, h = self.image_size
         s = min(w, h)
-        scaled_pixbuf = self.pixbuf.scale_simple(s, s, gdk.INTERP_BILINEAR)
+        scaled_pixbuf = self._get_scaled_pixbuf(s)
         self.set_from_pixbuf(scaled_pixbuf)
 
 
index 2cdb35d..3bdffc2 100644 (file)
@@ -54,7 +54,7 @@ class BrushInfo:
 
     def __init__(self, string=None):
         """Construct a BrushInfo object, optionally parsing it."""
-        self.settings = None
+        self.settings = {}
         self.cache_str = None
         self.observers = [self.settings_changed_cb]
         self.observers_hidden = []