Exemplo n.º 1
0
    def __init__(self, display: CanvasDisplay, page: Page):
        self.display = DisplayDrawHistory(display)
        self.page = page
        self.point_draw_objects: Dict[Point, DrawTransaction] = {}
        self.temp_point = Point(0, 0)

        self.points_iterator = CyclicIterator(
            self.page.get_line_transform().anchors)
        # must be called to init iterators. or else will not work!
        self.reset_iterator()
    def read_pages_from_dir(self, directory_path: Path):
        """
        Reads all page data from binary files and after that, reads all images if they are present

        :param directory_path: directory with all images and pages data
        """
        # read all binary pages
        self.pages = Page.read_pages(directory_path)
        prev_i = self.pages.index
        self.pages = CyclicIterator(self.pages)
        self.pages.select(prev_i)
    def read_images_to_pages(self, search_directory: Path, image_patterns=None):
        if image_patterns is None:
            image_patterns = ["*.png", "*.jpg"]

        for query in image_patterns:
            for path in search_directory.glob(query):
                try:
                    new_page = Page.from_image(path)
                    self.pages.append(new_page)
                except Exception as exc:
                    print(f"cannot open file {str(exc)}")

        prev_i = self.pages.index
        self.pages = CyclicIterator(self.pages)
        self.pages.select(prev_i)
Exemplo n.º 4
0
class SignatureDictionaryIterator:
    def __init__(self, dictionary):
        self.dictionary = dictionary
        self.group_iter = CyclicIterator(self.dictionary.groups)
        self.variant_iter: CyclicIterator = None
        self.update_variant()

    def update_variant(self):
        try:
            self.variant_iter = self.group_iter.get_or_raise(
            ).get_paths_iterator()
        except ObjectNotFound:
            self.variant_iter = EmptyCyclicIterator()

    def next(self):
        if self.variant_iter.is_last():
            # select next group and first variant
            self.group_iter.next()
            self.update_variant()
            self.variant_iter.select(0)
        else:
            self.variant_iter.next()

    def prev(self):
        if self.variant_iter.is_first():
            # select previous group and last variant
            self.group_iter.prev()
            self.update_variant()
            self.variant_iter.select_last()
        else:
            self.variant_iter.prev()

    def get_variant_or_raise(self) -> HandwrittenPath:
        return self.variant_iter.get_or_raise()

    def get_group_or_raise(self) -> PathGroup:
        return self.group_iter.get_or_raise()

    def get_variant_or_none(self) -> Union[HandwrittenPath, None]:
        return self.variant_iter.get_or_none()

    def get_group_or_none(self) -> Union[PathGroup, None]:
        return self.group_iter.get_or_none()

    def select_group(self, group_i: int):
        self.group_iter.select(group_i)
        self.update_variant()

    def select_variant(self, variant_i: int):
        self.variant_iter.select(variant_i)

    def delete_group(self):
        if self.group_iter.empty(): return

        self.dictionary.remove_group(self.group_iter.index)
        self.group_iter.update_index()
        self.update_variant()

    def delete_current_variant(self):
        if self.group_iter.empty(): return
        if self.variant_iter.empty(): return

        new_group, new_variant = self.dictionary.remove_variant(
            self.group_iter.index, self.variant_iter.index)

        # must be called in this order or variants will be taken from previous group
        self.group_iter.select(new_group)
        self.update_variant()
        self.variant_iter.select(new_variant)

    def get_current_labels(self):
        group = self.get_group_or_none()
        variant = self.get_variant_or_none()

        return group.name if group is not None else None, \
                variant.name if variant is not None else None
Exemplo n.º 5
0
 def __init__(self, dictionary):
     self.dictionary = dictionary
     self.group_iter = CyclicIterator(self.dictionary.groups)
     self.variant_iter: CyclicIterator = None
     self.update_variant()
Exemplo n.º 6
0
 def get_groups_iterator(self):
     return CyclicIterator(self.groups)
Exemplo n.º 7
0
 def reset_iterator(self):
     self.points_iterator = CyclicIterator(
         self.page.get_line_transform().anchors)
Exemplo n.º 8
0
class PointCollectionRedactor:
    """
    Iterates through list of points, updates their positions using input from
    """
    def __init__(self, display: CanvasDisplay, page: Page):
        self.display = DisplayDrawHistory(display)
        self.page = page
        self.point_draw_objects: Dict[Point, DrawTransaction] = {}
        self.temp_point = Point(0, 0)

        self.points_iterator = CyclicIterator(
            self.page.get_line_transform().anchors)
        # must be called to init iterators. or else will not work!
        self.reset_iterator()

    def reset_iterator(self):
        self.points_iterator = CyclicIterator(
            self.page.get_line_transform().anchors)

    def save_page_points(self):
        self.page.lines_points = [it.objects for it in self.points_iterator]
        self.reset_iterator()

    def draw_all(self):
        self.display.delete_all()
        self.redraw_all_points()

    def redraw_all_points(self):
        for point in self.points_iterator.objects:
            if not point.empty():
                self.redraw_point(point)

    def delete_current_canvas_objects(self):
        try:
            current_point = self.line_iterator.get_or_raise(
            ).get_variant_or_raise()
            self.delete_point_canvas_objects(current_point)
        except ObjectNotFound:
            pass

    def redraw_pointer_point(self, new_position: Point):
        self.redraw_temp_point(new_position)
        try:
            cur_point = self.line_iterator.get_or_raise().get_variant_or_raise(
            )
            self.redraw_point(cur_point, "blue")
        except ObjectNotFound:
            pass

    def redraw_temp_point(self, pos: Point):
        self.temp_point.x = pos.x
        self.temp_point.y = pos.y
        self.redraw_point(self.temp_point, "red")

    @staticmethod
    def draw_point_scope(draw: DrawTransaction, point: Point, color="black"):
        r = 10
        draw.create_oval((point.x - r, point.y - r, point.x + r, point.y + r),
                         outline=color)
        draw.create_line(point.x,
                         point.y - r,
                         point.x,
                         point.y + r,
                         fill=color)
        draw.create_line(point.x - r,
                         point.y,
                         point.x + r,
                         point.y,
                         fill=color)

    def redraw_point(self, point: Point, color="black"):
        last_draw = self.point_draw_objects.get(point)
        if last_draw is not None: self.display.delete(last_draw)
        self.point_draw_objects[point] = self.display.draw(
            self.draw_point_scope, point, color)

    def redraw_current_point_black(self):
        self.redraw_point(self.get_current_point())

    def move_left(self):
        self.redraw_current_point_black()
        self.points_iterator.prev()

    def move_right(self):
        self.redraw_current_point_black()
        self.points_iterator.next()

    def update_current(self, point: Point):
        self.delete_current_canvas_objects()
        self.points_iterator.get_or_raise().set_current(point)
        self.redraw_current_point_black()

    def get_current_point(self):
        return self.points_iterator.get_or_raise().get_variant_or_raise()
class PageIterator:
    """
    class allows to store pages and iterate through them
    """

    def __init__(self):
        self.pages = CyclicIterator([])

    def get_page(self) -> Page:
        try:
            return self.pages.get_or_raise()
        except ObjectNotFound:
            return self.create_empty_page()

    def next_page(self):
        self.pages.next()

    def previous_page(self):
        self.pages.prev()

    def delete_current_page(self):
        self.pages.remove_current()
        self.pages.update_index()

    def select_page(self, index):
        self.pages.select(index)

    # read pages from files
    def read_pages_from_dir(self, directory_path: Path):
        """
        Reads all page data from binary files and after that, reads all images if they are present

        :param directory_path: directory with all images and pages data
        """
        # read all binary pages
        self.pages = Page.read_pages(directory_path)
        prev_i = self.pages.index
        self.pages = CyclicIterator(self.pages)
        self.pages.select(prev_i)

    def read_images_to_pages(self, search_directory: Path, image_patterns=None):
        if image_patterns is None:
            image_patterns = ["*.png", "*.jpg"]

        for query in image_patterns:
            for path in search_directory.glob(query):
                try:
                    new_page = Page.from_image(path)
                    self.pages.append(new_page)
                except Exception as exc:
                    print(f"cannot open file {str(exc)}")

        prev_i = self.pages.index
        self.pages = CyclicIterator(self.pages)
        self.pages.select(prev_i)

    def page_exists(self):
        return len(self.pages) > 0

    def create_empty_page(self):
        page = Page.empty()
        self.pages.append(page)
        return page
Exemplo n.º 10
0
 def __init__(self):
     self.pages = CyclicIterator([])