Exemplo n.º 1
0
class IncrementDecrement(UncheckedContainer):
    def __init__(self, height=40, width=None, padding=None, distance=None):
        super().__init__()

        if width is None:
            # width = (height / 15) * 7
            width = (height / 5) * 3
        if distance is None:
            distance = height / 5
        if padding is None:
            padding = height / 8

        arrow_width = width - 2 * padding
        arrow_height = (height - 2 * padding - distance) / 2
        self.up_arrow = Arrow(arrow_width, arrow_height, direction=Arrow.UP)
        self.down_arrow = Arrow(arrow_width,
                                arrow_height,
                                direction=Arrow.DOWN)

        self.up_arrow.translate(arrow_width / 2 + padding,
                                padding + arrow_height / 2)
        self.down_arrow.translate(arrow_width / 2 + padding,
                                  height - padding - arrow_height / 2)

        self.add(self.up_arrow)
        self.add(self.down_arrow)
        self.shape = DrawableRectangle(Point(0, 0), width, height)

        self.increment_action = lambda: None
        self.decrement_action = lambda: None

        self.__moving_action = None
        self.__skip = 0

    def set_increment_action(self, action: Callable[[], None]):
        self.increment_action = action

    def set_decrement_action(self, action: Callable[[], None]):
        self.decrement_action = action

    def on_draw(self, widget: Widget, context: cairo.Context):
        context.save()
        self.shape.draw_on_context(context)
        context.set_source_rgb(1, 1, 1)
        context.fill_preserve()
        context.set_source_rgb(0, 0, 0)
        context.stroke()
        context.restore()
        super().on_draw(widget, context)

    def is_point_in(self, p: Point, category=MouseEvent.UNKNOWN):
        return self.shape.is_point_in(p)

    def on_mouse_down(self, widget: "Widget", event: MouseEvent):
        # self.__skip = 4
        if event.y <= self.shape.height / 2:
            action = self.increment_action
        else:
            action = self.decrement_action
        self.__moving_action = RepeatedAction(action,
                                              skip=10,
                                              interval=50,
                                              first=True).start()
        return True

    def on_mouse_up(self, widget: "Widget", event: MouseEvent):
        if self.__moving_action is not None:
            self.__moving_action.end = True
        return False
Exemplo n.º 2
0
class IncrementDecrement(UncheckedContainer):

    def __init__(self, height=40, width=None, padding=None, distance=None):
        super().__init__()

        if width is None:
            # width = (height / 15) * 7
            width = (height / 5) * 3
        if distance is None:
            distance = height / 5
        if padding is None:
            padding = height / 8

        arrow_width = width - 2 * padding
        arrow_height = (height - 2 * padding - distance) / 2
        self.up_arrow = Arrow(arrow_width,
                              arrow_height,
                              direction=Arrow.UP)
        self.down_arrow = Arrow(arrow_width,
                                arrow_height,
                                direction=Arrow.DOWN)

        self.up_arrow.translate(arrow_width / 2 + padding,
                                padding + arrow_height / 2)
        self.down_arrow.translate(arrow_width / 2 + padding,
                                  height - padding - arrow_height/2)

        self.add(self.up_arrow)
        self.add(self.down_arrow)
        self.shape = DrawableRectangle(Point(0, 0), width, height)

        self.increment_action = lambda: None
        self.decrement_action = lambda: None

        self.__moving_action = None
        self.__skip = 0

    def set_increment_action(self, action: Callable[[], None]):
        self.increment_action = action

    def set_decrement_action(self, action: Callable[[], None]):
        self.decrement_action = action

    def on_draw(self, widget: Widget, context: cairo.Context):
        context.save()
        self.shape.draw_on_context(context)
        context.set_source_rgb(1, 1, 1)
        context.fill_preserve()
        context.set_source_rgb(0, 0, 0)
        context.stroke()
        context.restore()
        super().on_draw(widget, context)

    def is_point_in(self, p: Point, category=MouseEvent.UNKNOWN):
        return self.shape.is_point_in(p)

    def on_mouse_down(self, widget: "Widget", event: MouseEvent):
        # self.__skip = 4
        if event.y <= self.shape.height / 2:
            action = self.increment_action
        else:
            action = self.decrement_action
        self.__moving_action = RepeatedAction(action,
                                              skip=10,
                                              interval=50,
                                              first=True).start()
        return True

    def on_mouse_up(self, widget: "Widget", event: MouseEvent):
        if self.__moving_action is not None:
            self.__moving_action.end = True
        return False
Exemplo n.º 3
0
class Number(Widget):
    def __init__(self, digits=2, font_size=20, padding=5):
        super().__init__()
        self.digits = digits
        self.font_size = font_size
        self.padding = padding
        self._max_value = None
        self._min_value = 1
        self._value = 1
        self.shape = None

    @property
    def max_value(self) -> int:
        if self._max_value is None:
            return 10**self.digits - 1
        else:
            return max(self._max_value, self._min_value)

    @max_value.setter
    def max_value(self, value: int):
        self._max_value = value
        self._value = min(value, self._value)

    @property
    def min_value(self) -> int:
        return self._min_value

    @min_value.setter
    def min_value(self, value):
        self._min_value = value
        self._value = max(value, self._value)

    @property
    def value(self) -> int:
        return self._value

    @value.setter
    def value(self, value: int):
        self._value = min(self.max_value, max(self.min_value, value))

    def layout(self, context: cairo.Context):
        context.save()
        context.set_font_size(self.font_size)
        generator = (context.text_extents(str(digit) * self.digits)
                     for digit in range(10))
        sizes = [(xa, h) for xb, yb, w, h, xa, ya in generator]
        max_width = 0
        max_height = 0
        for w, h in sizes:
            max_width = max(w, max_width)
            max_height = max(h, max_height)

        width = self.padding * 2 + max_width
        height = self.padding * 2 + max_height

        self.shape = DrawableRectangle(Point(0, 0), width, height)
        context.restore()

    def on_draw(self, widget: Widget, context: cairo.Context):
        shape = self.shape
        if shape is None:
            self.layout(context)
            shape = self.shape
        shape.draw_on_context(context)
        context.set_source_rgb(1, 1, 1)
        context.fill_preserve()
        context.set_source_rgb(0, 0, 0)
        context.stroke()

        text = str(self.value)
        context.set_font_size(self.font_size)
        xb, yb, w, h, xa, ya = context.text_extents(text)
        context.move_to(shape.start.x + shape.width - w - self.padding,
                        shape.start.y + shape.height - self.padding)
        context.show_text(text)

    def is_point_in(self, p: "Point", category=MouseEvent.UNKNOWN):
        if not self.is_shape_set:
            return False
        else:
            return self.shape.is_point_in(p)
Exemplo n.º 4
0
class Number(Widget):

    def __init__(self, digits=2, font_size=20, padding=5):
        super().__init__()
        self.digits = digits
        self.font_size = font_size
        self.padding = padding
        self._max_value = None
        self._min_value = 1
        self._value = 1
        self.shape = None

    @property
    def max_value(self) -> int:
        if self._max_value is None:
            return 10 ** self.digits - 1
        else:
            return max(self._max_value, self._min_value)

    @max_value.setter
    def max_value(self, value: int):
        self._max_value = value
        self._value = min(value, self._value)

    @property
    def min_value(self) -> int:
        return self._min_value

    @min_value.setter
    def min_value(self, value):
        self._min_value = value
        self._value = max(value, self._value)

    @property
    def value(self) -> int:
        return self._value

    @value.setter
    def value(self, value: int):
        self._value = min(self.max_value, max(self.min_value, value))

    def layout(self, context: cairo.Context):
        context.save()
        context.set_font_size(self.font_size)
        generator = (context.text_extents(str(digit) * self.digits)
                     for digit in range(10))
        sizes = [(xa, h) for xb, yb, w, h, xa, ya in generator]
        max_width = 0
        max_height = 0
        for w, h in sizes:
            max_width = max(w, max_width)
            max_height = max(h, max_height)

        width = self.padding * 2 + max_width
        height = self.padding * 2 + max_height

        self.shape = DrawableRectangle(Point(0, 0), width, height)
        context.restore()

    def on_draw(self, widget: Widget, context: cairo.Context):
        shape = self.shape
        if shape is None:
            self.layout(context)
            shape = self.shape
        shape.draw_on_context(context)
        context.set_source_rgb(1, 1, 1)
        context.fill_preserve()
        context.set_source_rgb(0, 0, 0)
        context.stroke()

        text = str(self.value)
        context.set_font_size(self.font_size)
        xb, yb, w, h, xa, ya = context.text_extents(text)
        context.move_to(shape.start.x + shape.width - w - self.padding,
                        shape.start.y + shape.height - self.padding)
        context.show_text(text)

    def is_point_in(self, p: "Point", category=MouseEvent.UNKNOWN):
        if not self.is_shape_set:
            return False
        else:
            return self.shape.is_point_in(p)