pygui improved node context to properly use tk_popup, avoiding bandage code to compensate for other issues

This commit is contained in:
Blake Harnden 2020-05-02 09:20:36 -07:00
parent 65466909d3
commit ac2d60dad6
3 changed files with 62 additions and 94 deletions

View file

@ -68,11 +68,14 @@ class CanvasNode:
self.service_file_configs = {}
self.config_service_configs = {}
self.setup_bindings()
self.context = tk.Menu(self.canvas)
themes.style_menu(self.context)
def setup_bindings(self):
self.canvas.tag_bind(self.id, "<Double-Button-1>", self.double_click)
self.canvas.tag_bind(self.id, "<Enter>", self.on_enter)
self.canvas.tag_bind(self.id, "<Leave>", self.on_leave)
self.canvas.tag_bind(self.id, "<ButtonRelease-3>", self.show_context)
def delete(self):
logging.debug("Delete canvas node for %s", self.core_node)
@ -188,51 +191,55 @@ class CanvasNode:
else:
self.show_config()
def create_context(self) -> tk.Menu:
def show_context(self, event: tk.Event) -> None:
# clear existing menu
self.context.delete(0, tk.END)
is_wlan = self.core_node.type == NodeType.WIRELESS_LAN
is_emane = self.core_node.type == NodeType.EMANE
context = tk.Menu(self.canvas)
themes.style_menu(context)
if self.app.core.is_runtime():
context.add_command(label="Configure", command=self.show_config)
self.context.add_command(label="Configure", command=self.show_config)
if NodeUtils.is_container_node(self.core_node.type):
context.add_command(label="Services", state=tk.DISABLED)
context.add_command(label="Config Services", state=tk.DISABLED)
self.context.add_command(label="Services", state=tk.DISABLED)
self.context.add_command(label="Config Services", state=tk.DISABLED)
if is_wlan:
context.add_command(label="WLAN Config", command=self.show_wlan_config)
self.context.add_command(
label="WLAN Config", command=self.show_wlan_config
)
if is_wlan and self.core_node.id in self.app.core.mobility_players:
context.add_command(
self.context.add_command(
label="Mobility Player", command=self.show_mobility_player
)
context.add_command(label="Select Adjacent", state=tk.DISABLED)
self.context.add_command(label="Select Adjacent", state=tk.DISABLED)
if NodeUtils.is_container_node(self.core_node.type):
context.add_command(label="Shell Window", state=tk.DISABLED)
context.add_command(label="Tcpdump", state=tk.DISABLED)
context.add_command(label="Tshark", state=tk.DISABLED)
context.add_command(label="Wireshark", state=tk.DISABLED)
context.add_command(label="View Log", state=tk.DISABLED)
self.context.add_command(label="Shell Window", state=tk.DISABLED)
self.context.add_command(label="Tcpdump", state=tk.DISABLED)
self.context.add_command(label="Tshark", state=tk.DISABLED)
self.context.add_command(label="Wireshark", state=tk.DISABLED)
self.context.add_command(label="View Log", state=tk.DISABLED)
else:
context.add_command(label="Configure", command=self.show_config)
self.context.add_command(label="Configure", command=self.show_config)
if NodeUtils.is_container_node(self.core_node.type):
context.add_command(label="Services", command=self.show_services)
context.add_command(
self.context.add_command(label="Services", command=self.show_services)
self.context.add_command(
label="Config Services", command=self.show_config_services
)
if is_emane:
context.add_command(
self.context.add_command(
label="EMANE Config", command=self.show_emane_config
)
if is_wlan:
context.add_command(label="WLAN Config", command=self.show_wlan_config)
context.add_command(
self.context.add_command(
label="WLAN Config", command=self.show_wlan_config
)
self.context.add_command(
label="Mobility Config", command=self.show_mobility_config
)
if NodeUtils.is_wireless_node(self.core_node.type):
context.add_command(
self.context.add_command(
label="Link To Selected", command=self.wireless_link_selected
)
context.add_command(label="Select Members", state=tk.DISABLED)
unlink_menu = tk.Menu(context)
self.context.add_command(label="Select Members", state=tk.DISABLED)
unlink_menu = tk.Menu(self.context)
for edge in self.edges:
other_id = edge.src
if self.id == other_id:
@ -243,14 +250,14 @@ class CanvasNode:
label=other_node.core_node.name, command=func_unlink
)
themes.style_menu(unlink_menu)
context.add_cascade(label="Unlink", menu=unlink_menu)
edit_menu = tk.Menu(context)
self.context.add_cascade(label="Unlink", menu=unlink_menu)
edit_menu = tk.Menu(self.context)
themes.style_menu(edit_menu)
edit_menu.add_command(label="Cut", command=self.click_cut)
edit_menu.add_command(label="Copy", command=self.canvas_copy)
edit_menu.add_command(label="Delete", command=self.canvas_delete)
context.add_cascade(label="Edit", menu=edit_menu)
return context
self.context.add_cascade(label="Edit", menu=edit_menu)
self.context.tk_popup(event.x_root, event.y_root)
def click_cut(self) -> None:
self.canvas_copy()
@ -270,39 +277,32 @@ class CanvasNode:
self.canvas.copy()
def show_config(self):
self.canvas.context = None
dialog = NodeConfigDialog(self.app, self.app, self)
dialog.show()
def show_wlan_config(self):
self.canvas.context = None
dialog = WlanConfigDialog(self.app, self.app, self)
if not dialog.has_error:
dialog.show()
def show_mobility_config(self):
self.canvas.context = None
dialog = MobilityConfigDialog(self.app, self.app, self)
if not dialog.has_error:
dialog.show()
def show_mobility_player(self):
self.canvas.context = None
mobility_player = self.app.core.mobility_players[self.core_node.id]
mobility_player.show()
def show_emane_config(self):
self.canvas.context = None
dialog = EmaneConfigDialog(self.app, self.app, self)
dialog.show()
def show_services(self):
self.canvas.context = None
dialog = NodeServiceDialog(self.app.master, self.app, self)
dialog.show()
def show_config_services(self):
self.canvas.context = None
dialog = NodeConfigServiceDialog(self.app.master, self.app, self)
dialog.show()
@ -324,7 +324,6 @@ class CanvasNode:
return result
def wireless_link_selected(self):
self.canvas.context = None
for canvas_nid in [
x for x in self.canvas.selection if "node" in self.canvas.gettags(x)
]: