lyne / lyne / gui / line.py

class Line(object):
    ''' Abstract line class.
    '''
    def __init__(self, canvas, (x0, y0, x1, y1)):
        self._canvas = canvas
        self._coords = (x0, y0, x1, y1)

    def _update_line(self):
        raise NotImplementedError, 'You have to subclass from Line and ' \
            'implement this method.'

    def delete(self):
        raise NotImplementedError, 'You have to subclass from Line and ' \
            'implement this method.'

    def set_coords(self, (x0, y0, x1, y1)):
        self._coords = (x0, y0, x1, y1)
        self._update_line()

    def get_coords(self):
        return self._coords

    coords = property(get_coords, set_coords)


class LineWArrow(Line):
    ''' A straight line w/ an arrow at one end.
    '''
    def __init__(self, canvas, (x0, y0, x1, y1), *args, **kwargs):
        Line.__init__(self, canvas, (x0, y0, x1, y1))

        self._line = self._canvas.create_line(
            (x0, y0, x1, y1), 
            arrow='last', 
            arrowshape=(10, 14, 5),
            *args, 
            **kwargs
            )

    def configure(self, *args, **kwargs):
        self._canvas.itemconfig(self._line, *args, **kwargs)

    def delete(self):
        self._canvas.delete(self._line)

    def _update_line(self):
        (x0, y0, x1, y1) = self.coords
        self._canvas.coords(self._line, x0, y0, x1, y1)


# XXX spline lines are not used currently

class SplineLine(Line):
    ''' A spline consisting of multiple parts.
    '''
    def __init__(self, canvas, (x0, y0, x1, y1), *args, **kwargs):
        Line.__init__(self, canvas, (x0, y0, x1, y1))

        self._coords_to_series()

        self._line = self._canvas.create_line(
            self._point_series,
            smooth=True,
            *args, 
            **kwargs
            )

    def _coords_to_series(self):
        (x0, y0, x1, y1) = self._coords
        self._point_series = (
            x0, y0,
            x0, (y0+y1)/2.0,
            (x0+x1)/2.0, (y0+y1)/2.0,
            x1, (y0+y1)/2.0,
            x1, y1
        )

    def delete(self):
        self._canvas.delete(self._line)

    def _update_line(self):
        self._coords_to_series()
        self._canvas.coords(self._line, self._point_series)


class SplineLineWArrow(Line):
    ''' A spline consisting of multiple parts w/
        an arrow at one end.
    '''
    def __init__(self, canvas, (x0, y0, x1, y1), *args, **kwargs):
        Line.__init__(self, canvas, (x0, y0, x1, y1))

        self._no_of_points = 5

        self._coords_to_series()

        self._line = self._canvas.create_line(
            self._point_series,
            smooth=True,
            arrow='last',
            arrowshape=(10, 14, 5),
            *args, 
            **kwargs
            )

    def set_no_of_points(self, no_of_points):
        if no_of_points not in (3, 5):
            raise ValueError, 'no_of_points must be 3 or 5, ' \
                'not %r', (no_of_points,)
        self._no_of_points = no_of_points

    def _coords_to_series(self):
        (x0, y0, x1, y1) = self._coords
        if self._no_of_points == 3:
            self._point_series = (
                x0, y0,
                x0, y1,
                x1, y1
            )
        else:
            self._point_series = (
                x0, y0,
                x0, (y0+y1)/2.0,
                (x0+x1)/2.0, (y0+y1)/2.0,
                x1, (y0+y1)/2.0,
                x1, y1
            )

    def delete(self):
        self._canvas.delete(self._line)

    def _update_line(self):
        self._coords_to_series()
        self._canvas.coords(self._line, self._point_series)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.