def bbox(self): """ Return the bounding box of the Path """ b = Bbox() for pl in self._pathlettes: b.union(pl.bbox(self.itoe)) # take into account extent of arrowheads for ar in self.heads: b.union(ar.bbox()) return b
def bbox(self): """ Gather together common bounding box for group Don't use Area's bbox as transformations may mean a tighter bbox (eg a circle) @return: a Bbox() @rtype: Bbox """ # We need to do the calculation in the # external co-ordinates (that's where the # bounding box will be used) # first a null Bbox bbox = Bbox() for obj in self.objects: bbox.union(obj.bbox(), self.itoe) return bbox
class Group(Area): """ Groups together a list of objects """ def __init__(self, *objects, **options): """ Initialisation of Group object @param objects: list of objects to group together @type objects: list @param options: dictionary of options @type options: dict """ self.objects = [] self.objbox = Bbox() if len(objects) == 1 and type(objects[0]) in (TupleType, ListType): apply(self.append, objects[0]) #self.append(objects[0]) else: apply(self.append, objects) #self.append(objects) Area.__init__(self, **options) def __getitem__(self, i): """ Get an item from the list of objects @param i: the index of the item to get @type i: int """ return self.objects[i] # these will break alignment #def __setitem__(self,i,other): # self.objects[i]=other def __getslice__(self, i, j): """ Get a slice of items from the list of objects @param i: the first index of the slice of items to get @type i: int @param j: the last index of the slice of items to get @type j: int """ return self.objects[i:j] #def __setslice__(self,i,j,wert): # self.objects[i:j]=wert def __len__(self): """ Returns the length of the object list """ return len(self.objects) def validate(self, obj): ''' make sure this object can be inserted into group @param obj: object to test for insertability @type obj: object ''' if isinstance(obj, Page) or isinstance(obj, Pages): raise TypeError, "Can't add a Page to %s" % str(self.__class__) def reverse(self): """ Reverse the order of objects in the list of objects in the group """ self.objects.reverse() # for convenience return reference to group return self def insert(self, idx, obj): ''' insert object @param idx: index at which to insert object @type idx: int @param obj: the object to insert @type obj: object ''' self.validate(obj) self.objbox.union(obj.bbox()) self.objects.insert(idx, obj) # for convenience return reference to group return self def append(self, *objs, **options): ''' append object(s) to group @param objs: list of objects to append @type objs: list @param options: dictionary of options @type options: dict ''' for obj in objs: self.validate(obj) self.objbox.union(obj.bbox()) self.objects.append(obj) # update size if self.objbox.is_set(): self.isw = self.objbox.sw self.width = self.objbox.width self.height = self.objbox.height # for convenience return reference to group return self def apply(self, **options): ''' apply attributes to all objects @param options: dictionary of attributes @type options: dict @return: reference to self @rtype: self ''' # do this by attributes since they # might not all get accepted for key, value in options.items(): dict1 = {key:value} for obj in self.objects: if isinstance(obj, Group): # recurse apply(obj.apply, (), options) try: apply(obj, (), dict1) except AttributeError: # skip objects that don't have the attribute pass # we don't know if the sizes where changes so recalculate them self.recalc_size() # for convenience return reference to group return self def recalc_size(self): ''' recalculate internal container size based on objects within ''' self.objbox = Bbox() for obj in self.objects: self.objbox.union(obj.bbox()) if self.objbox.is_set(): self.isw = self.objbox.sw self.width = self.objbox.width self.height = self.objbox.height def clear(self): ''' Clear all the elements and reset group to an empty group ''' self.isw = Area.isw self.width = Area.width self.height = Area.height self.objbox = Bbox() self.objects = [] def body(self): """ Returns the body postscript of the object """ out = cStringIO.StringIO() for obj in self.objects: if obj.bbox().sw is not None: out.write(str(obj)) return out.getvalue() def bbox(self): """ Gather together common bounding box for group Don't use Area's bbox as transformations may mean a tighter bbox (eg a circle) @return: a Bbox() @rtype: Bbox """ # We need to do the calculation in the # external co-ordinates (that's where the # bounding box will be used) # first a null Bbox bbox = Bbox() for obj in self.objects: bbox.union(obj.bbox(), self.itoe) return bbox