def add_converter(context, from_type, converter): """Register `converter` for converting `from_type` in `context`""" # XXX if converters cached by record types, add hooks here for updating gf = get_converter(context) if not get_converter.has_object(context): gf = generic(gf) # extend base function get_converter.when_object(context)(lambda context: gf) gf.when_type(from_type)(converter)
self.item = item def horizontal(self): """ Return horizontal edges (on y axis) """ return () def vertical(self): """ Return vertical edges (on x axis) """ return () Guide = generic(ItemGuide) @Guide.when_type(Element) class ElementGuide(ItemGuide): def horizontal(self): y = self.item.height return (0, y/2, y) def vertical(self): x = self.item.width return (0, x/2, x) @Guide.when_type(Line) class LineGuide(ItemGuide):
from gaphas.item import Item, Element class ItemFinder(object): """ Find an item on the canvas. """ def __init__(self, view): self.view = view def get_item_at_point(self, pos): item, handle = self.view.get_handle_at_point(pos) return item or self.view.get_item_at_point(pos) Finder = generic(ItemFinder) class ItemSelection(object): """ A role for items. When dealing with selection. Behaviour can be overridden by applying the @aspect decorator to a subclass. """ def __init__(self, item, view): self.item = item self.view = view def select(self): """
assert 0 <= segment < len(item.ports()) - 1 before = handles[handle_index - 1] after = handles[handle_index + 1] d, p = distance_line_point(before.pos, after.pos, handle.pos) # Checks how far the waypoint is from an imaginary line connecting the previous and next way/end point # If it is close, the two segments are merged to one merge_distance = item.line_width * 4 if d < merge_distance: assert len(self.view.canvas.solver._marked_cons) == 0 Segment(item, self.view).merge_segment(segment) if handle: item.request_update() class ItemPaintHovered(object): """ Paints on top of all items, just for the hovered item (see painter.HoveredItemPainter) """ def __init__(self, item, view): self.item = item self.view = view def paint(self, context, selected): pass PaintHovered = generic(ItemPaintHovered)
class ItemFinder(object): """ Find an item on the canvas. """ def __init__(self, view): self.view = view def get_item_at_point(self, pos): item, handle = self.view.get_handle_at_point(pos) return item or self.view.get_item_at_point(pos) Finder = generic(ItemFinder) class ItemSelection(object): """ A role for items. When dealing with selection. Behaviour can be overridden by applying the @aspect decorator to a subclass. """ def __init__(self, item, view): self.item = item self.view = view def select(self):
self.item = item def horizontal(self): """ Return horizontal edges (on y axis) """ return () def vertical(self): """ Return vertical edges (on x axis) """ return () Guide = generic(ItemGuide) @Guide.when_type(Element) class ElementGuide(ItemGuide): """Guide to align Element items. """ def horizontal(self): y = self.item.height return (0, y / 2, y) def vertical(self): x = self.item.width return (0, x / 2, x)
def subtype(typeinfo, *args, **kw): """XXX""" newti = typeinfo_for(typeinfo).clone(*args, **kw) gf = generic(get_converter(typeinfo)) get_converter.when_object(newti)(lambda ctx:gf) return newti
def create_default_converter(t): converter = generic(default_converter) converter.when_object(NoChange)(lambda val: val) converter.when_object(Inherit)(lambda val: val) converter.when_object(None)(lambda val: val) get_converter.when_object(t)(lambda ctx: converter)
def subtype(typeinfo, *args, **kw): """XXX""" newti = typeinfo_for(typeinfo).clone(*args, **kw) gf = generic(get_converter(typeinfo)) get_converter.when_object(newti)(lambda ctx: gf) return newti