Exemple #1
0
 def merge_undo(self, undo_item):
     """ Merges two undo items if possible.
     """
     # Discard undo items that are identical to us. This is to eliminate
     # the same undo item being created by multiple listeners monitoring the
     # same list for changes:
     if (
         isinstance(undo_item, self.__class__)
         and (self.object is undo_item.object)
         and (self.name == undo_item.name)
         and (self.index == undo_item.index)
     ):
         added = undo_item.added
         removed = undo_item.removed
         if (len(self.added) == len(added)) and (len(self.removed) == len(removed)):
             for i, item in enumerate(self.added):
                 if item is not added[i]:
                     break
             else:
                 for i, item in enumerate(self.removed):
                     if item is not removed[i]:
                         break
                 else:
                     return True
     return False
Exemple #2
0
 def find ( self, name, stack = None ):
     """ Finds a specified ViewElement within the specified (optional) search
         context.
     """
     # Assume search starts from the beginning the of the search order:
     i = 0
     
     # If a stack was specified, see if there is a matching entry in the 
     # stack already:
     if stack is not None:
         for ssi in stack:
             if name == ssi.id:
                 # Match found, resume search at next ViewElements object
                 # in the search order:
                 i = ssi.context + 1
                 break
                 
     # Search for a matching name starting at the specified ViewElements
     # object in the search order:
     for j, ves in enumerate( self._get_search_order()[i:] ):
         result = ves.content.get( name )
         if result is not None:
             # Match found. If there is a stack, push matching name and
             # ViewElements context onto it:
             if stack is not None:
                 stack[0:0] = [ SearchStackItem( id      = name, 
                                                 context = i + j ) ]
                 
             # Return the ViewElement object that matched the name:
             return result
             
     # Indicate no match was found:
     return None
Exemple #3
0
    def find(self, name, stack=None):
        """ Finds a specified ViewElement within the specified (optional) search
            context.
        """
        # Assume search starts from the beginning the of the search order:
        i = 0

        # If a stack was specified, see if there is a matching entry in the
        # stack already:
        if stack is not None:
            for ssi in stack:
                if name == ssi.id:
                    # Match found, resume search at next ViewElements object
                    # in the search order:
                    i = ssi.context + 1
                    break

        # Search for a matching name starting at the specified ViewElements
        # object in the search order:
        for j, ves in enumerate(self._get_search_order()[i:]):
            result = ves.content.get(name)
            if result is not None:
                # Match found. If there is a stack, push matching name and
                # ViewElements context onto it:
                if stack is not None:
                    stack[0:0] = [SearchStackItem(id=name, context=i + j)]

                # Return the ViewElement object that matched the name:
                return result

        # Indicate no match was found:
        return None
Exemple #4
0
 def merge_undo ( self, undo_item ):
     """ Merges two undo items if possible.
     """
     # Undo items are potentially mergeable only if they are of the same 
     # class and refer to the same object trait, so check that first:
     if (isinstance( undo_item, self.__class__ ) and
        (self.object is undo_item.object) and
        (self.name == undo_item.name)):
         v1 = self.new_value
         v2 = undo_item.new_value
         t1 = type( v1 )
         if t1 is type( v2 ):
             
             if t1 is str:
                 # Merge two undo items if they have new values which are 
                 # strings which only differ by one character (corresponding 
                 # to a single character insertion, deletion or replacement 
                 # operation in a text editor): 
                 n1 = len( v1 )
                 n2 = len( v2 )
                 n  = min( n1, n2 )
                 i  = 0
                 while (i < n) and (v1[i] == v2[i]): 
                     i += 1
                 if v1[i + (n2 <= n1):] == v2[i + (n2 >= n1):]:
                     self.new_value = v2
                     return True
                 
             elif isSequenceType( v1 ):
                 # Merge sequence types only if a single element has changed
                 # from the 'original' value, and the element type is a 
                 # simple Python type:
                 v1 = self.old_value
                 if isSequenceType( v1 ):
                     # Note: wxColour says it's a sequence type, but it
                     # doesn't support 'len', so we handle the exception
                     # just in case other classes have similar behavior:
                     try:
                         if len( v1 ) == len( v2 ):
                             diffs = 0
                             for i, item in enumerate( v1 ):
                                 titem = type( item )
                                 item2 = v2[i]
                                 if ((titem not in SimpleTypes)   or
                                     (titem is not type( item2 )) or
                                     (item != item2)):
                                     diffs += 1
                                     if diffs >= 2:
                                         return False
                             self.new_value = v2
                             return True
                     except:
                         pass
                     
             elif t1 in NumericTypes:
                 # Always merge simple numeric trait changes: 
                 self.new_value = v2
                 return True
     return False
Exemple #5
0
    def merge_undo(self, undo_item):
        """ Merges two undo items if possible.
        """
        # Undo items are potentially mergeable only if they are of the same
        # class and refer to the same object trait, so check that first:
        if (
            isinstance(undo_item, self.__class__)
            and (self.object is undo_item.object)
            and (self.name == undo_item.name)
        ):
            v1 = self.new_value
            v2 = undo_item.new_value
            t1 = type(v1)
            if t1 is type(v2):

                if t1 is str:
                    # Merge two undo items if they have new values which are
                    # strings which only differ by one character (corresponding
                    # to a single character insertion, deletion or replacement
                    # operation in a text editor):
                    n1 = len(v1)
                    n2 = len(v2)
                    n = min(n1, n2)
                    i = 0
                    while (i < n) and (v1[i] == v2[i]):
                        i += 1
                    if v1[i + (n2 <= n1) :] == v2[i + (n2 >= n1) :]:
                        self.new_value = v2
                        return True

                elif isSequenceType(v1):
                    # Merge sequence types only if a single element has changed
                    # from the 'original' value, and the element type is a
                    # simple Python type:
                    v1 = self.old_value
                    if isSequenceType(v1):
                        # Note: wxColour says it's a sequence type, but it
                        # doesn't support 'len', so we handle the exception
                        # just in case other classes have similar behavior:
                        try:
                            if len(v1) == len(v2):
                                diffs = 0
                                for i, item in enumerate(v1):
                                    titem = type(item)
                                    item2 = v2[i]
                                    if (titem not in SimpleTypes) or (titem is not type(item2)) or (item != item2):
                                        diffs += 1
                                        if diffs >= 2:
                                            return False
                                self.new_value = v2
                                return True
                        except:
                            pass

                elif t1 in NumericTypes:
                    # Always merge simple numeric trait changes:
                    self.new_value = v2
                    return True
        return False
Exemple #6
0
 def replace_include(self, view_elements):
     """ Replaces any items which have an 'id' with an Include object with 
         the same 'id', and puts the object with the 'id' into the specified 
         ViewElements object.
     """
     for i, item in enumerate(self.content):
         if item.is_includable():
             id = item.id
             if id in view_elements.content:
                 raise TraitError, \
                       "Duplicate definition for view element '%s'" % id
             self.content[i] = Include(id)
             view_elements.content[id] = item
         item.replace_include(view_elements)
Exemple #7
0
 def replace_include ( self, view_elements ):
     """ Replaces any items which have an 'id' with an Include object with 
         the same 'id', and puts the object with the 'id' into the specified 
         ViewElements object.
     """
     for i, item in enumerate( self.content ):
         if item.is_includable():
             id = item.id
             if id in view_elements.content:
                 raise TraitError, \
                       "Duplicate definition for view element '%s'" % id
             self.content[ i ] = Include( id )
             view_elements.content[ id ] = item
         item.replace_include( view_elements )
Exemple #8
0
 def merge_undo ( self, undo_item ):
     """ Merges two undo items if possible.
     """
     # Discard undo items that are identical to us. This is to eliminate
     # the same undo item being created by multiple listeners monitoring the
     # same list for changes:
     if (isinstance( undo_item, self.__class__ )        and
        (self.object is undo_item.object)               and
        (self.name  == undo_item.name)                  and
        (self.index == undo_item.index)):
         added   = undo_item.added
         removed = undo_item.removed
         if ((len( self.added )   == len( added )) and
             (len( self.removed ) == len( removed ))):
             for i, item in enumerate( self.added ):
                 if item is not added[i]:
                     break
             else:
                 for i, item in enumerate( self.removed ):
                     if item is not removed[i]:
                         break
                 else:
                     return True
     return False