Prev Up Next
The class GraphicsObject The Document and Graphics Classes The class HierarchyNode

The class Bounded

In Sketch, each graphics object has a bounding rectangle and a coordinate rectangle. The sides of both rectangles are parallel to the coordinate axes.

The bounding rectangle is a rectangle that contains the entire object. Any marks the object or its children leave on the paper when printed lie in it. Ideally it should be the smallest such rectangle, but in practice it is sometimes difficult and indeed not always desirable to get exactly the smallest one. In any case, the bounding rectangle must contain the entire object.

Sketch uses the bounding rect for three purposes: to decide, which parts of the screen to redraw, as an aid when deciding which objects to select and to compute the BoundingBox of an EPS-File.

The coordinate rectangle is the smallest rectangle that contains the entire object without taking the line width into account. Sketch uses this rectangle for layout purposes and as the reference for interactive resize- and transformation operations.

The coordinate rectangle of an object should always lie completely inside of the bounding rectangle. Both rectangles may be (and for many object types they are) identical.

Since Bounded's variables are used for layout calculations, it provides access to and default behavior for the layout point. The layout point is the point of an object that should lie exactly on the grid or on other special points.

By default, the layout point is the lower left corner of the coordinate rectangle. For images this is the lower left corner of the image which is not identical to the default if the image was transformed. For text objects with default justification, the layout point is the origin of the first letter, which is on the baseline, whereas the coordinate rectangle takes the descender into account.

Instance Variables

Bounded provides the following instance variables

bounding_rect:

the bounding rectangle

coord_rect:

the coordinate rectangle

These instance variables are public attributes. Sketch uses bounding_rect to speed up the search for the object a mouse click has hit (when the point the user clicked on is outside of the bounding rect, the object cannot be hit). This test is significantly faster when the bounding rectangle is read directly from an attribute instead of accessed through a method call.

Methods

Bounded implements lazy evaluation for its public instance variables (bounding_rect and coord_rect). When one of these variables is not defined, Bounded's __getattr__ method invokes the method update_rects to compute the rectangles and set the variables. To force recomputation when these variables are read next, a derived class may call del_lazy_attrs.

This lazy evaluation mechanism is not limited to the instance variables mentioned above. __getattr__ consults the dictionary self._lazy_attrs to determine, whether the requested attribute is a lazy attribute: if self._lazy_attrs has the attribute as a key the attribute is treated as a lazy attribute. The value associated with that key must be the name of the method to call to recompute the value of the attribute. __getattr__ calls this method and expects that self has that attribute after the method has run.

_lazy_attrs is a class variable of Bounded. If a derived class needs additional lazy attributes, it should create its own class variable _lazy_attrs as a copy of its base-class' _lazy_attrs and add the appropriate keys:

 
class MyObject(GraphicsObject):

    _lazy_attrs = GraphicsObject._lazy_attrs.copy()
    _lazy_attrs['my_lazy_attribute'] = 'update_my_lazy_attr'

    # somewhere in the class definition:
    def update_my_lazy_attr(self):
	# compute the new value of my_lazy_attr...
	# ... and finally:
	self.my_lazy_attribute = new_value

Thus Bounded's methods are:

del_lazy_attrs()

Delete the `lazy' instance variables. Any attempt to access a lazy attribute later triggers its recomputation via the __getattr__ method.

update_rects()

Bounded.__getattr__ invokes this method whenever one of the attributes bounding_rect or coord_rect is not set.

This method must be supplied by some derived class and it must compute both rectangles.

__getattr__(attr)

If self._lazy_attrs.has_key(attr) is true, invoke getattr(self, self._lazy_attrs[attr])() to recompute the attribute.

LayoutPoint()

Return the layout point of self as a point object. Default is the lower left corner of self.coord_rect.

GetSnapPoints()

Return a list of point objects indicating the `hot spots' of the graphics object. These points are used when `Snapping to Objects' is active. The default implementation returns an empty list.


The class GraphicsObject The Document and Graphics Classes The class HierarchyNode
Prev Up Next