updates to draw custom nodes on the node picker frame

This commit is contained in:
Blake Harnden 2019-11-08 11:00:22 -08:00
parent 22177def1c
commit b0fe5660bd
4 changed files with 37 additions and 30 deletions

View file

@ -72,7 +72,8 @@ class Application(tk.Frame):
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) log_format = "%(asctime)s - %(levelname)s - %(module)s:%(funcName)s - %(message)s"
logging.basicConfig(level=logging.DEBUG, format=log_format)
appdirs.check_directory() appdirs.check_directory()
app = Application() app = Application()
app.mainloop() app.mainloop()

View file

@ -13,8 +13,8 @@ from coretk.interface import Interface, InterfaceManager
from coretk.mobilitynodeconfig import MobilityNodeConfig from coretk.mobilitynodeconfig import MobilityNodeConfig
from coretk.wlannodeconfig import WlanNodeConfig from coretk.wlannodeconfig import WlanNodeConfig
link_layer_nodes = ["switch", "hub", "wlan", "rj45", "tunnel", "emane"] NETWORK_NODES = ["switch", "hub", "wlan", "rj45", "tunnel", "emane"]
network_layer_nodes = ["router", "host", "PC", "mdr", "prouter"] DEFAULT_NODES = ["router", "host", "PC", "mdr", "prouter"]
class Node: class Node:
@ -422,7 +422,7 @@ class CoreClient:
""" """
node_type = None node_type = None
node_model = None node_model = None
if name in link_layer_nodes: if name in NETWORK_NODES:
if name == "switch": if name == "switch":
node_type = core_pb2.NodeType.SWITCH node_type = core_pb2.NodeType.SWITCH
elif name == "hub": elif name == "hub":
@ -437,7 +437,7 @@ class CoreClient:
node_type = core_pb2.NodeType.TUNNEL node_type = core_pb2.NodeType.TUNNEL
elif name == "emane": elif name == "emane":
node_type = core_pb2.NodeType.EMANE node_type = core_pb2.NodeType.EMANE
elif name in network_layer_nodes: elif name in DEFAULT_NODES or name in self.custom_nodes:
node_type = core_pb2.NodeType.DEFAULT node_type = core_pb2.NodeType.DEFAULT
node_model = name node_model = name
else: else:
@ -606,7 +606,7 @@ class CoreClient:
self.interfaces_manager.new_subnet() self.interfaces_manager.new_subnet()
src_node = self.nodes[src_canvas_id] src_node = self.nodes[src_canvas_id]
if src_node.model in network_layer_nodes: if src_node.model in DEFAULT_NODES:
ifid = len(src_node.interfaces) ifid = len(src_node.interfaces)
name = "eth" + str(ifid) name = "eth" + str(ifid)
src_interface = Interface( src_interface = Interface(
@ -620,7 +620,7 @@ class CoreClient:
) )
dst_node = self.nodes[dst_canvas_id] dst_node = self.nodes[dst_canvas_id]
if dst_node.model in network_layer_nodes: if dst_node.model in DEFAULT_NODES:
ifid = len(dst_node.interfaces) ifid = len(dst_node.interfaces)
name = "eth" + str(ifid) name = "eth" + str(ifid)
dst_interface = Interface( dst_interface = Interface(

View file

@ -208,7 +208,7 @@ class CustomNodesDialog(Dialog):
custom_node = self.app.core.custom_nodes.pop(previous_name) custom_node = self.app.core.custom_nodes.pop(previous_name)
custom_node.name = name custom_node.name = name
custom_node.image = self.image custom_node.image = self.image
custom_node.image_file = Path(self.image_file).name custom_node.image_file = Path(self.image_file).stem
custom_node.services = self.services custom_node.services = self.services
self.app.core.custom_nodes[name] = custom_node self.app.core.custom_nodes[name] = custom_node
self.nodes_list.listbox.delete(self.selected_index) self.nodes_list.listbox.delete(self.selected_index)

View file

@ -145,14 +145,22 @@ class Toolbar(tk.Frame):
(ImageEnum.PC, "PC"), (ImageEnum.PC, "PC"),
(ImageEnum.MDR, "mdr"), (ImageEnum.MDR, "mdr"),
(ImageEnum.PROUTER, "prouter"), (ImageEnum.PROUTER, "prouter"),
(ImageEnum.EDITNODE, "custom node types"),
] ]
# draw default nodes
for image_enum, tooltip in nodes: for image_enum, tooltip in nodes:
image = Images.get(image_enum)
func = partial(self.update_button, self.node_button, image, tooltip)
self.create_button(image, func, self.node_picker, tooltip)
# draw custom nodes
for name in sorted(self.app.core.custom_nodes):
custom_node = self.app.core.custom_nodes[name]
image = custom_node.image
func = partial(self.update_button, self.node_button, image, name)
self.create_button(image, func, self.node_picker, name)
# draw edit node
image = Images.get(ImageEnum.EDITNODE)
self.create_button( self.create_button(
Images.get(image_enum), image, self.click_edit_node, self.node_picker, "custom nodes"
partial(self.update_button, self.node_button, image_enum, tooltip),
self.node_picker,
tooltip,
) )
self.show_picker(self.node_button, self.node_picker) self.show_picker(self.node_button, self.node_picker)
@ -165,17 +173,17 @@ class Toolbar(tk.Frame):
self.wait_window(picker) self.wait_window(picker)
self.app.unbind_all("<Button-1>") self.app.unbind_all("<Button-1>")
def create_button(self, img, func, frame, tooltip): def create_button(self, image, func, frame, tooltip):
""" """
Create button and put it on the frame Create button and put it on the frame
:param PIL.Image img: button image :param PIL.Image image: button image
:param func: the command that is executed when button is clicked :param func: the command that is executed when button is clicked
:param tkinter.Frame frame: frame that contains the button :param tkinter.Frame frame: frame that contains the button
:param str tooltip: tooltip text :param str tooltip: tooltip text
:return: nothing :return: nothing
""" """
button = tk.Button(frame, width=self.width, height=self.height, image=img) button = tk.Button(frame, width=self.width, height=self.height, image=image)
button.bind("<Button-1>", lambda e: func()) button.bind("<Button-1>", lambda e: func())
button.grid(pady=1) button.grid(pady=1)
CreateToolTip(button, tooltip) CreateToolTip(button, tooltip)
@ -221,15 +229,13 @@ class Toolbar(tk.Frame):
logging.debug("Click LINK button") logging.debug("Click LINK button")
self.app.canvas.mode = GraphMode.EDGE self.app.canvas.mode = GraphMode.EDGE
def update_button(self, button, image_enum, name): def click_edit_node(self):
logging.info("update button(%s): %s, %s", button, image_enum, name)
self.hide_pickers()
if image_enum == ImageEnum.EDITNODE:
dialog = CustomNodesDialog(self.app, self.app) dialog = CustomNodesDialog(self.app, self.app)
dialog.show() dialog.show()
else:
image = Images.get(image_enum) def update_button(self, button, image, name):
logging.info("updating button(%s): %s", button, name) logging.info("update button(%s): %s", button, name)
self.hide_pickers()
button.configure(image=image) button.configure(image=image)
self.app.canvas.mode = GraphMode.NODE self.app.canvas.mode = GraphMode.NODE
self.app.canvas.draw_node_image = image self.app.canvas.draw_node_image = image