pygui cleanup of edge code to use position tuples instead of individual params

This commit is contained in:
Blake Harnden 2020-04-15 12:51:35 -07:00
parent 23562cd294
commit 6f87986364
3 changed files with 18 additions and 19 deletions

View file

@ -130,7 +130,8 @@ class Edge:
def redraw(self): def redraw(self):
self.canvas.itemconfig(self.id, width=self.scaled_width(), fill=self.color) self.canvas.itemconfig(self.id, width=self.scaled_width(), fill=self.color)
src_x, src_y, _, _, _, _ = self.canvas.coords(self.id) src_x, src_y, _, _, _, _ = self.canvas.coords(self.id)
self.move_src(src_x, src_y) src_pos = src_x, src_y
self.move_src(src_pos)
def middle_label_pos(self) -> Tuple[float, float]: def middle_label_pos(self) -> Tuple[float, float]:
_, _, x, y, _, _ = self.canvas.coords(self.id) _, _, x, y, _, _ = self.canvas.coords(self.id)
@ -145,22 +146,20 @@ class Edge:
else: else:
self.canvas.itemconfig(self.middle_label, text=text) self.canvas.itemconfig(self.middle_label, text=text)
def move_node(self, node_id: int, x: float, y: float) -> None: def move_node(self, node_id: int, pos: Tuple[float, float]) -> None:
if self.src == node_id: if self.src == node_id:
self.move_src(x, y) self.move_src(pos)
else: else:
self.move_dst(x, y) self.move_dst(pos)
def move_dst(self, x: float, y: float) -> None: def move_dst(self, dst_pos: Tuple[float, float]) -> None:
dst_pos = (x, y)
src_x, src_y, _, _, _, _ = self.canvas.coords(self.id) src_x, src_y, _, _, _, _ = self.canvas.coords(self.id)
src_pos = (src_x, src_y) src_pos = src_x, src_y
self.moved(src_pos, dst_pos) self.moved(src_pos, dst_pos)
def move_src(self, x: float, y: float) -> None: def move_src(self, src_pos: Tuple[float, float]) -> None:
src_pos = (x, y)
_, _, _, _, dst_x, dst_y = self.canvas.coords(self.id) _, _, _, _, dst_x, dst_y = self.canvas.coords(self.id)
dst_pos = (dst_x, dst_y) dst_pos = dst_x, dst_y
self.moved(src_pos, dst_pos) self.moved(src_pos, dst_pos)
def moved(self, src_pos: Tuple[float, float], dst_pos: Tuple[float, float]) -> None: def moved(self, src_pos: Tuple[float, float], dst_pos: Tuple[float, float]) -> None:
@ -220,8 +219,8 @@ class CanvasEdge(Edge):
self.draw(src_pos, dst_pos) self.draw(src_pos, dst_pos)
self.set_binding() self.set_binding()
def move_node(self, node_id: int, x: float, y: float) -> None: def move_node(self, node_id: int, pos: Tuple[float, float]) -> None:
super().move_node(node_id, x, y) super().move_node(node_id, pos)
self.update_labels() self.update_labels()
def set_binding(self) -> None: def set_binding(self) -> None:
@ -302,8 +301,8 @@ class CanvasEdge(Edge):
def complete(self, dst: int) -> None: def complete(self, dst: int) -> None:
self.dst = dst self.dst = dst
self.token = create_edge_token(self.src, self.dst) self.token = create_edge_token(self.src, self.dst)
x, y = self.canvas.coords(self.dst) dst_pos = self.canvas.coords(self.dst)
self.move_dst(x, y) self.move_dst(dst_pos)
self.check_wireless() self.check_wireless()
self.canvas.tag_raise(self.src) self.canvas.tag_raise(self.src)
self.canvas.tag_raise(self.dst) self.canvas.tag_raise(self.dst)

View file

@ -628,7 +628,7 @@ class CanvasGraph(tk.Canvas):
self.cursor = x, y self.cursor = x, y
if self.mode == GraphMode.EDGE and self.drawing_edge is not None: if self.mode == GraphMode.EDGE and self.drawing_edge is not None:
self.drawing_edge.move_dst(x, y) self.drawing_edge.move_dst(self.cursor)
if self.mode == GraphMode.ANNOTATION: if self.mode == GraphMode.ANNOTATION:
if is_draw_shape(self.annotation_type) and self.shape_drawing: if is_draw_shape(self.annotation_type) and self.shape_drawing:
shape = self.shapes[self.selected] shape = self.shapes[self.selected]

View file

@ -130,7 +130,7 @@ class CanvasNode:
def motion(self, x_offset: int, y_offset: int, update: bool = True): def motion(self, x_offset: int, y_offset: int, update: bool = True):
original_position = self.canvas.coords(self.id) original_position = self.canvas.coords(self.id)
self.canvas.move(self.id, x_offset, y_offset) self.canvas.move(self.id, x_offset, y_offset)
x, y = self.canvas.coords(self.id) pos = self.canvas.coords(self.id)
# check new position # check new position
bbox = self.canvas.bbox(self.id) bbox = self.canvas.bbox(self.id)
@ -148,12 +148,12 @@ class CanvasNode:
# move edges # move edges
for edge in self.edges: for edge in self.edges:
edge.move_node(self.id, x, y) edge.move_node(self.id, pos)
for edge in self.wireless_edges: for edge in self.wireless_edges:
edge.move_node(self.id, x, y) edge.move_node(self.id, pos)
# set actual coords for node and update core is running # set actual coords for node and update core is running
real_x, real_y = self.canvas.get_actual_coords(x, y) real_x, real_y = self.canvas.get_actual_coords(*pos)
self.core_node.position.x = real_x self.core_node.position.x = real_x
self.core_node.position.y = real_y self.core_node.position.y = real_y
if self.app.core.is_runtime() and update: if self.app.core.is_runtime() and update: