Ejemplo n.º 1
0
    def draw_on_canvas(self, circle=False):
        """
        Draw graph to canvas, edges first so nodes cover them up.
        """
        # show name in top left corner
        name = self.canvas.create_text(5,
                                       5,
                                       text=self.name,
                                       anchor="nw",
                                       font=default_font())

        # if circle set to True, then
        # pick smaller of width/height
        if circle:
            cell_w = cell_h = min(self.cell_w, self.cell_h)
        else:
            cell_w = self.cell_w
            cell_h = self.cell_h

        # draw edges first
        for u, v in self.graph.edges:
            x0 = u.x * cell_w
            y0 = u.y * cell_h
            x1 = v.x * cell_w
            y1 = v.y * cell_h
            center_offsets = [cell_w / 2, cell_h / 2] * 2

            pts = [x0, y0, x1, y1]
            centers = [pt + off for pt, off in zip(pts, center_offsets)]

            color = "black"
            edge = self.canvas.create_line(*centers, fill=color, width=2)

        for node in self.graph.nodes:
            x0 = node.x * cell_w
            y0 = node.y * cell_h

            # draw nodes at 50% size as to not block
            # drawing of edges
            x0_n, y0_n = [x0 + 4 * cell_w / 10, y0 + 4 * cell_h / 10]

            node.tk_id = self.canvas.create_oval(x0_n,
                                                 y0_n,
                                                 x0_n + 2 * cell_w / 10,
                                                 y0_n + 2 * cell_h / 10,
                                                 fill=node.color)

            node_text = node.value
            val_text = self.canvas.create_text(x0 + cell_w / 2,
                                               y0 + cell_h / 2,
                                               text=node_text,
                                               font=default_font())
Ejemplo n.º 2
0
    def __init__(self, control, *args, **kwargs):
        """
        Initializes width and height assuming that
        kwargs['width'/'height'] are the screen size
        :param ds: working data structure
        :param args: args for tk.Frame
        :param kwargs: kwargs for tk.Frame
        """
        self.control = control

        self.screen_width = kwargs["width"]
        self.screen_height = kwargs["height"]
        self.width = self.screen_width * 0.75
        self.height = self.screen_height * 0.75
        super().__init__(*args, **kwargs)

        # resizing frame to 0.75 x 0.75 size
        # (could have changed args before calling super()
        # to accomplish this, but then the frame would have
        # a maximum size of 75% x 75% and couldn't fill the entire screen)
        self.master.geometry("%ix%i" % (self.width, self.height))
        self.pack()
        self.bind("<Configure>", self.on_resize)

        # one annotation mode for entire application
        self.annotation_mode = None
        self.annotation_buttons = {}

        self.mono_font = default_font()
        self.init_components()
Ejemplo n.º 3
0
    def preprocess(self):
        """
        Determine the size of each cell in the array.
        Leave some room on either side between edge of canvas
        and edge of array.
        """
        # give 1/25 space on either edge of canvas
        self.side_space = self.canvas.width / 25

        self.cell_w = (self.canvas.width - self.side_space * 2) / len(
            self.array)
        self.cell_h = self.canvas.height / 4

        # check if values will fit inside cell nicely
        if default_font().measure("000") > self.cell_w or self._force_compress:
            # resize cells and only draw ends of array
            num_shown = min(3, self.array.size // 2)

            # denominator = num_shown * 2 + size of (...) (2)
            self.cell_w = (self.canvas.width -
                           self.side_space * 2) / (num_shown * 2 + 2)
            self.cell_h = self.canvas.height / 4
            self._compressed = True
        else:
            self._compressed = False

        self.cell_w = self.cell_h = min(self.cell_w, self.cell_h)
Ejemplo n.º 4
0
    def __init__(self, parent, x_0, y_0, x_1, y_1, **kwargs):
        """
        Basic textbox class
        :param parent: parent canvas
        :param x_0: top left x
        :param y_0: top left y
        :param x_1: bottom right x
        :param y_1: bottom right y

        background, borderwidth, cursor,
            exportselection, font, foreground,
            highlightbackground, highlightcolor,
            highlightthickness, insertbackground,
            insertborderwidth, insertofftime,
            insertontime, insertwidth, padx, pady,
            relief, selectbackground,
            selectborderwidth, selectforeground,
            setgrid, takefocus,
            xscrollcommand, yscrollcommand,
        """
        self.font = default_font()
        kwargs["font"] = self.font

        super().__init__(parent, **kwargs)
        self.parent = parent

        # setting wrap to word level
        self.configure(wrap=WORD)

        # creating uniform border
        self.configure(highlightthickness=1)
        self.configure(highlightbackground=TEXTBOX_BORDER)
        self.configure(borderwidth=0)

        self.width = (x_1 - x_0)
        self.height = (y_1 - y_0)

        # determine relative placement to parent canvas
        self.relwidth = self.width / parent.width
        self.relheight = self.height / parent.height

        self.x = x_0
        self.y = y_0

        self.place(relwidth=self.relwidth,
                   relheight=self.relheight,
                   x=self.x,
                   y=self.y)

        self.bind("<Button-1>", self.clicked)
        self.bind("<Control-B1-Motion>", self.move_text_box)
        self.bind("<B3-Motion>", self.resize_text_box)

        self.start_x = 0
        self.start_y = 0
Ejemplo n.º 5
0
    def draw_on_canvas(self, circle=False):
        """
               Determines size of each cell to be drawn and
               iterates through tree, drawing edges first, then
               nodes in an inorder traversal.
               :param circle: if True, draw nodes as circles
               """
        # show name in top left corner
        name = self.canvas.create_text(5,
                                       5,
                                       text=self.name,
                                       anchor="nw",
                                       font=default_font())

        # if circle set to True, then
        # pick smaller of width/height
        if circle:
            cell_w = cell_h = min(self.cell_w, self.cell_h)
        else:
            cell_w = self.cell_w
            cell_h = self.cell_h

        # traverse tree in preorder so lines get drawn
        # first and nodes are placed on top
        for node in self.tree.preorder():
            x0 = node.x * cell_w
            y0 = node.y * cell_h
            for c in node.children():
                x1 = c.x * cell_w
                y1 = c.y * cell_h
                center_offsets = [cell_w / 2, cell_h / 2] * 2

                pts = [x0, y0, x1, y1]
                centers = [pt + off for pt, off in zip(pts, center_offsets)]

                # show color for bst property
                color = "blue" if c.value <= node.value else "red"
                color = "green" if c.value == node.value else color
                edge = self.canvas.create_line(*centers, fill=color, width=2)

            # draw nodes at 50% size as to not block
            # drawing of edges
            x0_n, y0_n = [x0 + cell_w / 4, y0 + cell_h / 4]

            # self.model.logger.debug("Drawing Node(%s) at %.2f, %.2f" % (node.value, x0_n, y0_n))

            oval = self.canvas.create_oval(x0_n,
                                           y0_n,
                                           x0_n + cell_w / 2,
                                           y0_n + cell_h / 2,
                                           fill=node.color)
            # node_text = ("%sCC:\n%i, %i\ns:%i, d:%i"
            #                               % (node, node.x, node.y,
            #                                  node.get_size(), node.depth))
            # node_text = ("%s\nd: %s; s: %s" % ("   " + str(node), node.depth, node.get_size()))
            # node_text = ("%s\nxl: %s, xr: %s\nd:%s; s:%s" % (node.value, node.get_xleft().value, node.get_xright().value, node.depth, node.get_size()))
            node_text = node.value
            # node_text = ""

            val_text = self.canvas.create_text(x0 + cell_w / 2,
                                               y0 + cell_h / 2,
                                               text=node_text,
                                               font=default_font())
Ejemplo n.º 6
0
    def draw_on_canvas(self):
        """
        Draw array from left to right at center of canvas.
        """
        # show name in top left corner
        name = self.canvas.create_text(5,
                                       5,
                                       text=self.name,
                                       anchor="nw",
                                       font=default_font())

        # draw entire array
        if not self._compressed:
            for index, arr_node in enumerate(self.array):
                # draw rectangles
                x_0 = self.side_space + index * self.cell_w
                y_0 = (self.canvas.height - self.cell_h) / 2
                x_1 = x_0 + self.cell_w
                y_1 = y_0 + self.cell_h

                # tags used for animations
                element_tag = self.name + "_" + str(index)

                rect = self.canvas.create_rectangle(x_0,
                                                    y_0,
                                                    x_1,
                                                    y_1,
                                                    fill=arr_node.color,
                                                    tag=element_tag)

                # draw text for value
                if not self._hide_values:
                    val_text = self.canvas.create_text(x_0 + self.cell_w / 2,
                                                       y_0 + self.cell_h / 2,
                                                       text=arr_node.value,
                                                       font=default_font(),
                                                       tag=element_tag)

                # draw indices
                if not self._hide_indices:
                    ind = self.canvas.create_text(x_0 + self.cell_w / 2,
                                                  y_0 - self.cell_h / 2,
                                                  text=index,
                                                  font=default_font())
        else:
            # draw first 3 elements ... last 3 elements (or less if array is < 6)
            num_shown = min(3, self.array.size // 2)

            # drawing first num_shown elements
            for index, arr_node in enumerate(self.array._array[:num_shown]):
                # draw rectangles
                x_0 = self.side_space + index * self.cell_w
                y_0 = (self.canvas.height - self.cell_h) / 2
                x_1 = x_0 + self.cell_w
                y_1 = y_0 + self.cell_h

                # tags used for animations
                element_tag = self.name + "_" + str(index)

                rect = self.canvas.create_rectangle(x_0,
                                                    y_0,
                                                    x_1,
                                                    y_1,
                                                    fill=arr_node.color,
                                                    tag=element_tag)

                # draw text for value
                if not self._hide_values:
                    val_text = self.canvas.create_text(x_0 + self.cell_w / 2,
                                                       y_0 + self.cell_h / 2,
                                                       text=arr_node.value,
                                                       font=default_font(),
                                                       tag=element_tag)

                # draw indices
                if not self._hide_indices:
                    ind = self.canvas.create_text(x_0 + self.cell_w / 2,
                                                  y_0 - self.cell_h / 2,
                                                  text=index,
                                                  font=default_font())

            # draw ...
            truncated_x_0 = self.side_space + num_shown * self.cell_w
            truncated_y_0 = (self.canvas.height - self.cell_h) / 2
            truncated_x_1 = truncated_x_0 + self.cell_w * 2
            truncated_y_1 = truncated_y_0 + self.cell_h
            tr = self.canvas.create_rectangle(truncated_x_0,
                                              truncated_y_0,
                                              truncated_x_1,
                                              truncated_y_1,
                                              fill=NODE_COLOR)
            # draw ... in array and for indices
            if not self._hide_values:
                tr1 = self.canvas.create_text(truncated_x_0 + self.cell_w,
                                              truncated_y_0 + self.cell_h / 2,
                                              text=" ... ",
                                              font=default_font())
            if not self._hide_indices:
                tr2 = self.canvas.create_text(truncated_x_0 + self.cell_w,
                                              truncated_y_0 - self.cell_h / 2,
                                              text=" ... ",
                                              font=default_font())

            # draw last num_shown elements
            for index, arr_node in enumerate(self.array._array[-num_shown:]):
                # draw rectangles
                x_0 = truncated_x_1 + index * self.cell_w
                y_0 = (self.canvas.height - self.cell_h) / 2
                x_1 = x_0 + self.cell_w
                y_1 = y_0 + self.cell_h

                true_index = index + len(self.array) - num_shown
                # tags used for animations
                element_tag = self.name + "_" + str(true_index)

                rect = self.canvas.create_rectangle(x_0,
                                                    y_0,
                                                    x_1,
                                                    y_1,
                                                    fill=arr_node.color,
                                                    tag=element_tag)

                # draw text for value
                if not self._hide_values:
                    val_text = self.canvas.create_text(x_0 + self.cell_w / 2,
                                                       y_0 + self.cell_h / 2,
                                                       text=arr_node.value,
                                                       font=default_font(),
                                                       tag=element_tag)

                # draw indices
                if not self._hide_indices:
                    ind = self.canvas.create_text(x_0 + self.cell_w / 2,
                                                  y_0 - self.cell_h / 2,
                                                  text=true_index,
                                                  font=default_font())