consolidated logic for drawing edge labels based on link to be contained within the edge class itself

This commit is contained in:
Blake Harnden 2019-12-18 16:51:05 -08:00
parent ccb433a32d
commit 1884cda271
6 changed files with 80 additions and 94 deletions

View file

@ -445,7 +445,7 @@ class CoreClient:
def start_session(self):
nodes = [x.core_node for x in self.canvas_nodes.values()]
links = list(self.links.values())
links = [x.link for x in self.links.values()]
wlan_configs = self.get_wlan_configs_proto()
mobility_configs = self.get_mobility_configs_proto()
emane_model_configs = self.get_emane_model_configs_proto()
@ -602,7 +602,7 @@ class CoreClient:
:return: nothing
"""
node_protos = [x.core_node for x in self.canvas_nodes.values()]
link_protos = list(self.links.values())
link_protos = [x.link for x in self.links.values()]
if self.state != core_pb2.SessionState.DEFINITION:
self.client.set_session_state(
self.session_id, core_pb2.SessionState.DEFINITION
@ -813,8 +813,8 @@ class CoreClient:
interface_one=src_interface,
interface_two=dst_interface,
)
self.links[edge.token] = link
return link
edge.set_link(link)
self.links[edge.token] = edge
def get_wlan_configs_proto(self):
configs = []

View file

@ -228,7 +228,7 @@ class LinkConfiguration(Dialog):
logging.debug("click apply")
self.app.canvas.itemconfigure(self.edge.id, width=self.width.get())
self.app.canvas.itemconfigure(self.edge.id, fill=self.color.get())
link = self.edge.link_info.link
link = self.edge.link
bandwidth = self.bandwidth.get()
if bandwidth != "":
link.options.bandwidth = int(bandwidth)
@ -292,7 +292,7 @@ class LinkConfiguration(Dialog):
self.width.set(width)
color = self.app.canvas.itemcget(self.edge.id, "fill")
self.color.set(color)
link = self.edge.link_info.link
link = self.edge.link
if link.HasField("options"):
self.bandwidth.set(str(link.options.bandwidth))
self.jitter.set(str(link.options.jitter))

View file

@ -1,11 +1,14 @@
import logging
import tkinter as tk
from tkinter.font import Font
from coretk import themes
from coretk.dialogs.linkconfig import LinkConfiguration
from coretk.graph import tags
from coretk.nodeutils import NodeUtils
TEXT_DISTANCE = 0.30
class CanvasWirelessEdge:
def __init__(self, token, position, src, dst, canvas):
@ -46,14 +49,74 @@ class CanvasEdge:
self.id = self.canvas.create_line(
x1, y1, x2, y2, tags=tags.EDGE, width=self.width, fill="#ff0000"
)
self.text_src = None
self.text_dst = None
self.token = None
self.link_info = None
self.font = Font(size=8)
self.link = None
self.throughput = None
self.set_binding()
def set_binding(self):
self.canvas.tag_bind(self.id, "<ButtonRelease-3>", self.create_context)
def set_link(self, link):
self.link = link
self.draw_labels()
def get_coordinates(self):
x1, y1, x2, y2 = self.canvas.coords(self.id)
v1 = x2 - x1
v2 = y2 - y1
ux = TEXT_DISTANCE * v1
uy = TEXT_DISTANCE * v2
x1 = x1 + ux
y1 = y1 + uy
x2 = x2 - ux
y2 = y2 - uy
return x1, y1, x2, y2
def draw_labels(self):
x1, y1, x2, y2 = self.get_coordinates()
label_one = None
if self.link.HasField("interface_one"):
label_one = (
f"{self.link.interface_one.ip4}/{self.link.interface_one.ip4mask}\n"
f"{self.link.interface_one.ip6}/{self.link.interface_one.ip6mask}\n"
)
label_two = None
if self.link.HasField("interface_two"):
label_two = (
f"{self.link.interface_two.ip4}/{self.link.interface_two.ip4mask}\n"
f"{self.link.interface_two.ip6}/{self.link.interface_two.ip6mask}\n"
)
self.text_src = self.canvas.create_text(
x1,
y1,
text=label_one,
justify=tk.CENTER,
font=self.font,
tags=tags.LINK_INFO,
)
self.text_dst = self.canvas.create_text(
x2,
y2,
text=label_two,
justify=tk.CENTER,
font=self.font,
tags=tags.LINK_INFO,
)
def update_labels(self):
"""
Move edge labels based on current position.
:return: nothing
"""
x1, y1, x2, y2 = self.get_coordinates()
self.canvas.coords(self.text_src, x1, y1)
self.canvas.coords(self.text_dst, x2, y2)
def complete(self, dst):
self.dst = dst
self.token = tuple(sorted((self.src, self.dst)))
@ -93,9 +156,9 @@ class CanvasEdge:
def delete(self):
self.canvas.delete(self.id)
if self.link_info:
self.canvas.delete(self.link_info.id1)
self.canvas.delete(self.link_info.id2)
if self.link:
self.canvas.delete(self.text_src)
self.canvas.delete(self.text_dst)
def create_context(self, event):
logging.debug("create link context")

View file

@ -9,7 +9,7 @@ from coretk.dialogs.shapemod import ShapeDialog
from coretk.graph import tags
from coretk.graph.edges import CanvasEdge, CanvasWirelessEdge
from coretk.graph.enums import GraphMode, ScaleOption
from coretk.graph.linkinfo import LinkInfo, Throughput
from coretk.graph.linkinfo import Throughput
from coretk.graph.node import CanvasNode
from coretk.graph.shape import Shape
from coretk.graph.shapeutils import ShapeType, is_draw_shape
@ -237,14 +237,14 @@ class CanvasGraph(tk.Canvas):
canvas_node_one.id,
self,
)
edge.set_link(link)
edge.token = tuple(sorted((canvas_node_one.id, canvas_node_two.id)))
edge.dst = canvas_node_two.id
edge.check_wireless()
canvas_node_one.edges.add(edge)
canvas_node_two.edges.add(edge)
self.edges[edge.token] = edge
self.core.links[edge.token] = link
edge.link_info = LinkInfo(self, edge, link)
self.core.links[edge.token] = edge
if link.HasField("interface_one"):
canvas_node_one.interfaces.append(link.interface_one)
if link.HasField("interface_two"):
@ -372,8 +372,7 @@ class CanvasGraph(tk.Canvas):
node_src.edges.add(edge)
node_dst = self.nodes[edge.dst]
node_dst.edges.add(edge)
link = self.core.create_link(edge, node_src, node_dst)
edge.link_info = LinkInfo(self, edge, link)
self.core.create_link(edge, node_src, node_dst)
def select_object(self, object_id, choose_multiple=False):
"""
@ -793,7 +792,7 @@ class CanvasGraph(tk.Canvas):
:param CanvasNode dest: destination node
:return: nothing
"""
if tuple([source.id, dest.id]) not in self.edges:
if (source.id, dest.id) not in self.edges:
pos0 = source.core_node.position
x0 = pos0.x
y0 = pos0.y
@ -802,5 +801,4 @@ class CanvasGraph(tk.Canvas):
self.edges[edge.token] = edge
self.nodes[source.id].edges.add(edge)
self.nodes[dest.id].edges.add(edge)
link = self.core.create_link(edge, source, dest)
edge.link_info = LinkInfo(self, edge, link)
self.core.create_link(edge, source, dest)

View file

@ -1,83 +1,8 @@
"""
Link information, such as IPv4, IPv6 and throughput drawn in the canvas
"""
import tkinter as tk
from tkinter import font
from core.api.grpc import core_pb2
from coretk.graph import tags
TEXT_DISTANCE = 0.30
class LinkInfo:
def __init__(self, canvas, edge, link):
"""
create an instance of LinkInfo object
:param coretk.graph.Graph canvas: canvas object
:param coretk.graph.CanvasEdge edge: canvas edge onject
:param link: core link to draw info for
"""
self.canvas = canvas
self.edge = edge
self.link = link
self.id1 = None
self.id2 = None
self.font = font.Font(size=8)
self.draw_labels()
def get_coordinates(self):
x1, y1, x2, y2 = self.canvas.coords(self.edge.id)
v1 = x2 - x1
v2 = y2 - y1
ux = TEXT_DISTANCE * v1
uy = TEXT_DISTANCE * v2
x1 = x1 + ux
y1 = y1 + uy
x2 = x2 - ux
y2 = y2 - uy
return x1, y1, x2, y2
def draw_labels(self):
x1, y1, x2, y2 = self.get_coordinates()
label_one = None
if self.link.HasField("interface_one"):
label_one = (
f"{self.link.interface_one.ip4}/{self.link.interface_one.ip4mask}\n"
f"{self.link.interface_one.ip6}/{self.link.interface_one.ip6mask}\n"
)
label_two = None
if self.link.HasField("interface_two"):
label_two = (
f"{self.link.interface_two.ip4}/{self.link.interface_two.ip4mask}\n"
f"{self.link.interface_two.ip6}/{self.link.interface_two.ip6mask}\n"
)
self.id1 = self.canvas.create_text(
x1,
y1,
text=label_one,
justify=tk.CENTER,
font=self.font,
tags=tags.LINK_INFO,
)
self.id2 = self.canvas.create_text(
x2,
y2,
text=label_two,
justify=tk.CENTER,
font=self.font,
tags=tags.LINK_INFO,
)
def recalculate_info(self):
"""
move the node info when the canvas node move
:return: nothing
"""
x1, y1, x2, y2 = self.get_coordinates()
self.canvas.coords(self.id1, x1, y1)
self.canvas.coords(self.id2, x2, y2)
class Throughput:

View file

@ -129,7 +129,7 @@ class CanvasNode:
else:
self.canvas.coords(edge.id, x1, y1, x, y)
self.canvas.throughput_draw.move(edge)
edge.link_info.recalculate_info()
edge.update_labels()
for edge in self.wireless_edges:
x1, y1, x2, y2 = self.canvas.coords(edge.id)