Exemple #1
0
def attrib_table_contents(*classes):
    """
    Create an `attr.ib` with the validator and converter to be used
    on `contents` attributes.

    :param classes:
        The accepted classes for this contents.
    """
    def converter(v):
        return tuple(_convert_decoration_content_item(item) for item in v)

    return attr.ib(validator=tuple_of(*classes), converter=converter)
class TableRow:
    """
    Represents the contents of a table's row when inserting or updating a
    table in the graph.

    :ivar str tag:
    :ivar tuple[union[str,TableData]] contents: The row's cells. Normal `str`
        elements will be interpreted as `TableData` elements with all the
        default values and it's contents equal to a tuple of one element (the
        `str` used).
    """
    tag = attr.ib(default='tr', init=False)
    contents = attr.ib(validator=tuple_of(str, TableData), convert=tuple)
class TableData:
    """
    Represents the contents of a table's cell when inserting or updating a
    table in the graph.

    :ivar str tag:
    :ivar tuple[union[str,Image]] contents: The table cell's contents.
    :ivar int colspan: The number of columns the cell should span into.
    :ivar int rowspan: The number of rows the cell should span into.
    :ivar optional[str] style: A inline style for the element.
    """
    tag = attr.ib(default='td', init=False)
    contents = attr.ib(validator=tuple_of(str, Image), convert=tuple)
    colspan = attr.ib(default=1, validator=_is_int)
    rowspan = attr.ib(default=1, validator=_is_int)
    style = attr.ib(default=None, validator=attr.validators.optional(_is_str))
class Table:
    """
    Represents the contents of a table when inserting or updating a table in
    the graph.

    :ivar str tag:
    :ivar tuple[TableRow] contents: The table rows.
    """
    tag = attr.ib(default='table', init=False)
    contents = attr.ib(validator=tuple_of(TableRow), convert=tuple)

    def contents_after(self, caption):
        """
        Useful for testing: truncates the contents after the first row with
        the given caption and return it as a list.

        :rtype: tuple[TableRow]
        """
        seen_captions = []

        def get_caption(row):
            first_row_content = row.contents[0]
            if isinstance(first_row_content, TableData):
                return first_row_content.contents[0]
            return first_row_content

        for index, row in enumerate(self.contents):
            row_caption = get_caption(row)
            if row_caption == caption:
                break
            seen_captions.append(row_caption)
        else:
            __tracebackhide__ = True
            msg = '\nCould not find row with caption "{}" in\n{}'
            assert False, msg.format(caption, seen_captions)
        return tuple(self.contents[index + 1:])
Exemple #5
0
class GraphOptions(object):
    """
    Object that configures features available in graph drawing widget.

    :ivar bool allow_create_target: Defines if clones the source if new
        connection has no target.
    :ivar bool allow_dangling_edges: Defines if it can create edges when
        they don't connect two vertices.
    :ivar bool cells_cloneable: Enables the cells to be cloned pressing
        ctrl and dragging them.
    :ivar bool cells_connectable: Enables new edges between vertices of
        graph.
    :ivar bool cells_movable: Specifies if the graph should allow moving
        of cells.
    :ivar bool cells_resizable: Specifies if vertices in graph can be
        resized.
    :ivar tuple[str, int, int]|None connection_image: If configured,
        this image is shown to user when he hovers a vertex. It can be
        clicked and dragged to create edges. Respectively, formed by
        image path, its width and height.
    :ivar bool enable_cloning: Enables cloning by control-drag.
    :ivar tuple[str]|None font_family: Defines the default family for
        all fonts in graph. It configures a priority list, trying always to
        use the left-most family first.
    :ivar bool multigraph: Allow multiple edges between nodes
    :ivar tuple[str, int, int]|None port_image: Replaces image in
        connection ports. Respectively, formed by image path, its width and
        height.
    :ivar bool show_grid: Show a grid in background to aid users.
    :ivar bool show_highlight: Show highlight when it hovers over a cell
        in graph.
    :ivar bool show_outline: Show outline of graph in a floating window.
    :ivar bool snap_to_grid: Snap to background grid when moving vertices
        in graph.
    """

    allow_create_target = attr.ib(default=False, validator=_is_bool)
    allow_dangling_edges = attr.ib(default=False, validator=_is_bool)
    cells_cloneable = attr.ib(default=True, validator=_is_bool)
    cells_connectable = attr.ib(default=True, validator=_is_bool)
    cells_movable = attr.ib(default=True, validator=_is_bool)
    cells_resizable = attr.ib(default=True, validator=_is_bool)
    connection_image = attr.ib(
        default=None,
        validator=attr.validators.optional(_is_image_configuration),
    )
    enable_cloning = attr.ib(default=True, validator=_is_bool)
    font_family = attr.ib(default=None,
                          validator=attr.validators.optional(tuple_of(str)))
    multigraph = attr.ib(default=False, validator=_is_bool)
    port_image = attr.ib(
        default=None,
        validator=attr.validators.optional(_is_image_configuration),
    )
    show_grid = attr.ib(default=True, validator=_is_bool)
    show_highlight = attr.ib(default=True, validator=_is_bool)
    show_outline = attr.ib(default=False, validator=_is_bool)
    snap_to_grid = attr.ib(default=True, validator=_is_bool)

    def as_dict(self):
        return attr.asdict(self)