Changeset 1036


Ignore:
Timestamp:
12/02/11 21:59:11 (18 months ago)
Author:
tal
Message:
New notifications and improved glyph renaming behavior.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • packages/defcon/branches/ufo3/Lib/defcon/objects/layer.py

    r1031 r1036  
    2222    Layer.GlyphsChanged 
    2323    Layer.GlyphChanged 
     24    Layer.GlyphWillBeAdded 
    2425    Layer.GlyphAdded 
     26    Layer.GlyphWillBeDeleted 
    2527    Layer.GlyphDeleted 
    26     Layer.GlyphWillBeDeleted 
    2728    Layer.GlyphNameChanged 
    2829    Layer.NameChanged 
     
    175176            raise KeyError, "%s not in layer" % name 
    176177        glyph = self.instantiateGlyphObject() 
    177         self.beginSelfGlyphNotificationObservation(glyph) 
    178178        glyph.disableNotifications() 
     179        glyph.name = name 
     180        self._insertGlyph(glyph) 
    179181        pointPen = glyph.getPointPen() 
    180182        self._glyphSet.readGlyph(glyphName=name, glyphObject=glyph, pointPen=pointPen) 
    181183        glyph.dirty = False 
    182184        glyph.enableNotifications() 
    183         self._glyphs[name] = glyph 
    184185        self._stampGlyphDataState(glyph) 
    185186        return glyph 
     
    191192        with the new glyph. 
    192193 
    193         This posts *Layer.GlyphAdded* and *Layer.Changed* notifications. 
    194         """ 
     194        This posts *Layer.GlyphWillBeAdded*, *Layer.GlyphAdded* 
     195        and *Layer.Changed* notifications. 
     196        """ 
     197        self.postNotification("Layer.GlyphWillBeAdded", data=(dict(name=name))) 
    195198        if name in self: 
    196199            self._unicodeData.removeGlyphData(name, self[name].unicodes) 
    197200        glyph = self.instantiateGlyphObject() 
    198         self.beginSelfGlyphNotificationObservation(glyph) 
    199201        glyph.disableNotifications() 
    200202        glyph.name = name 
     203        self._insertGlyph(glyph) 
    201204        glyph.enableNotifications() 
    202         self._glyphs[name] = glyph 
    203         if name in self._scheduledForDeletion: 
    204             del self._scheduledForDeletion[name] 
    205         self._keys.add(name) 
    206205        self.postNotification("Layer.GlyphAdded", data=(dict(name=name))) 
    207206        self.dirty = True 
     
    215214        be replaced with the new glyph. 
    216215 
    217         This posts *Layer.GlyphAdded* and *Layer.Changed* notifications. 
     216        This posts *Layer.GlyphWillBeAdded*, *Layer.GlyphAdded* 
     217        and *Layer.Changed* notifications. 
    218218        """ 
    219219        # DO NOT ACTUALLY INSERT THE GLYPH! 
     
    225225        if name is None: 
    226226            name = source.name 
     227        self.postNotification("Layer.GlyphWillBeAdded", data=(dict(name=name))) 
    227228        self.holdNotifications() 
    228229        self.newGlyph(name) 
    229230        dest = self[name] 
    230         dest.disableNotifications() 
    231231        dest.copyDataFromGlyph(glyph) 
    232         dest.enableNotifications() 
    233         if dest.unicodes: 
    234             self._unicodeData.addGlyphData(name, dest.unicodes) 
    235232        self.releaseHeldNotifications() 
    236233        return dest 
     234 
     235    def _insertGlyph(self, glyph, beginObservations=True): 
     236        name = glyph.name 
     237        self._glyphs[name] = glyph 
     238        if name in self._scheduledForDeletion: 
     239            del self._scheduledForDeletion[name] 
     240        self._keys.add(name) 
     241        if beginObservations: 
     242            self.beginSelfGlyphNotificationObservation(glyph) 
     243        if glyph.unicodes: 
     244            self._unicodeData.addGlyphData(name, glyph.unicodes) 
    237245 
    238246    # ------------- 
     
    256264            raise KeyError, "%s not in layer" % name 
    257265        self.postNotification("Layer.GlyphWillBeDeleted", data=dict(name=name)) 
     266        self._deleteGlyph(name) 
     267        self.postNotification("Layer.GlyphDeleted", data=dict(name=name)) 
     268        self.dirty = True 
     269 
     270    def _deleteGlyph(self, name, endObservations=True): 
    258271        self._unicodeData.removeGlyphData(name, self[name].unicodes) 
    259272        dataOnDiskTimeStamp = None 
     
    261274        if name in self._glyphs: 
    262275            glyph = self._glyphs.pop(name) 
    263             self.endSelfGlyphNotificationObservation(glyph) 
     276            if endObservations: 
     277                self.endSelfGlyphNotificationObservation(glyph) 
    264278            dataOnDiskTimeStamp = glyph._dataOnDiskTimeStamp 
    265279            dataOnDisk = glyph._dataOnDisk 
     
    268282        if self._glyphSet is not None and name in self._glyphSet: 
    269283            self._scheduledForDeletion[name] = dict(dataOnDiskTimeStamp=dataOnDiskTimeStamp, dataOnDisk=dataOnDisk) 
    270         self.postNotification("Layer.GlyphDeleted", data=dict(name=name)) 
    271         self.dirty = True 
    272284 
    273285    def __len__(self): 
     
    707719        newName = data["newValue"] 
    708720        glyph = self._glyphs[oldName] 
    709         self.disableNotifications() 
    710         del self[oldName] # use __del__ because *lots* of things have to be adjusted 
    711         self.enableNotifications() 
    712         self._glyphs[newName] = glyph 
    713         self._keys.add(newName) 
     721        self._deleteGlyph(oldName, endObservations=False) 
    714722        self._unicodeData.removeGlyphData(oldName, glyph.unicodes) 
    715         self._unicodeData.addGlyphData(newName, glyph.unicodes) 
     723        self._insertGlyph(glyph, beginObservations=False) 
    716724        self.postNotification("Layer.GlyphNameChanged", data=dict(oldValue=oldName, newValue=newName)) 
    717725 
Note: See TracChangeset for help on using the changeset viewer.