2020-02-03 17:10:46 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2019-09-25 16:29:34 +01:00
|
|
|
import tkinter as tk
|
2019-11-11 21:23:02 +00:00
|
|
|
from functools import partial
|
2020-01-14 19:06:52 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2019-12-19 17:30:21 +00:00
|
|
|
import core.gui.menuaction as action
|
|
|
|
from core.gui.coreclient import OBSERVERS
|
2020-02-27 23:24:36 +00:00
|
|
|
from core.gui.dialogs.executepython import ExecutePythonDialog
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2020-01-14 19:06:52 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from core.gui.app import Application
|
|
|
|
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2019-11-08 05:46:40 +00:00
|
|
|
class Menubar(tk.Menu):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Core menubar
|
|
|
|
"""
|
|
|
|
|
2020-01-14 19:06:52 +00:00
|
|
|
def __init__(self, master: tk.Tk, app: "Application", cnf={}, **kwargs):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create a CoreMenubar instance
|
|
|
|
"""
|
2019-11-08 05:46:40 +00:00
|
|
|
super().__init__(master, cnf, **kwargs)
|
|
|
|
self.master.config(menu=self)
|
2019-11-01 20:42:49 +00:00
|
|
|
self.app = app
|
|
|
|
self.menuaction = action.MenuAction(app, master)
|
2020-02-04 21:49:46 +00:00
|
|
|
self.recent_menu = None
|
2020-02-24 20:51:47 +00:00
|
|
|
self.edit_menu = None
|
2019-11-08 05:46:40 +00:00
|
|
|
self.draw()
|
|
|
|
|
|
|
|
def draw(self):
|
|
|
|
"""
|
|
|
|
Create core menubar and bind the hot keys to their matching command
|
|
|
|
"""
|
|
|
|
self.draw_file_menu()
|
|
|
|
self.draw_edit_menu()
|
|
|
|
self.draw_canvas_menu()
|
|
|
|
self.draw_view_menu()
|
|
|
|
self.draw_tools_menu()
|
|
|
|
self.draw_widgets_menu()
|
|
|
|
self.draw_session_menu()
|
|
|
|
self.draw_help_menu()
|
|
|
|
|
|
|
|
def draw_file_menu(self):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create file menu
|
|
|
|
"""
|
2019-11-08 18:07:23 +00:00
|
|
|
menu = tk.Menu(self)
|
2019-12-12 19:06:52 +00:00
|
|
|
menu.add_command(
|
|
|
|
label="New Session",
|
|
|
|
accelerator="Ctrl+N",
|
2020-03-04 21:25:22 +00:00
|
|
|
command=self.menuaction.new_session,
|
2019-12-12 19:06:52 +00:00
|
|
|
)
|
|
|
|
self.app.bind_all("<Control-n>", lambda e: self.app.core.create_new_session())
|
2020-04-18 07:18:11 +01:00
|
|
|
menu.add_command(label="Save", accelerator="Ctrl+S", command=self.save)
|
|
|
|
self.app.bind_all("<Control-s>", self.save)
|
|
|
|
menu.add_command(label="Save As...", command=self.menuaction.file_save_as_xml)
|
2019-11-08 18:07:23 +00:00
|
|
|
menu.add_command(
|
|
|
|
label="Open...", command=self.menuaction.file_open_xml, accelerator="Ctrl+O"
|
2019-09-25 16:29:34 +01:00
|
|
|
)
|
2019-11-08 05:46:40 +00:00
|
|
|
self.app.bind_all("<Control-o>", self.menuaction.file_open_xml)
|
2020-02-04 21:49:46 +00:00
|
|
|
self.recent_menu = tk.Menu(menu)
|
|
|
|
for i in self.app.guiconfig["recentfiles"]:
|
|
|
|
self.recent_menu.add_command(
|
|
|
|
label=i, command=partial(self.open_recent_files, i)
|
|
|
|
)
|
2020-04-18 07:18:11 +01:00
|
|
|
menu.add_cascade(label="Recent Files", menu=self.recent_menu)
|
2019-11-08 18:07:23 +00:00
|
|
|
menu.add_separator()
|
2020-04-18 07:18:11 +01:00
|
|
|
menu.add_command(label="Execute Python Script...", command=self.execute_python)
|
2019-11-08 18:07:23 +00:00
|
|
|
menu.add_separator()
|
|
|
|
menu.add_command(
|
|
|
|
label="Quit", accelerator="Ctrl+Q", command=self.menuaction.on_quit
|
|
|
|
)
|
|
|
|
self.app.bind_all("<Control-q>", self.menuaction.on_quit)
|
|
|
|
self.add_cascade(label="File", menu=menu)
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2019-11-08 05:46:40 +00:00
|
|
|
def draw_edit_menu(self):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create edit menu
|
|
|
|
"""
|
2019-11-08 18:07:23 +00:00
|
|
|
menu = tk.Menu(self)
|
2019-12-11 22:09:50 +00:00
|
|
|
menu.add_command(label="Preferences", command=self.menuaction.gui_preferences)
|
2019-11-08 18:07:23 +00:00
|
|
|
menu.add_command(label="Undo", accelerator="Ctrl+Z", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="Redo", accelerator="Ctrl+Y", state=tk.DISABLED)
|
|
|
|
menu.add_separator()
|
|
|
|
menu.add_command(label="Cut", accelerator="Ctrl+X", state=tk.DISABLED)
|
2019-12-20 00:15:29 +00:00
|
|
|
menu.add_command(
|
|
|
|
label="Copy", accelerator="Ctrl+C", command=self.menuaction.copy
|
|
|
|
)
|
|
|
|
menu.add_command(
|
|
|
|
label="Paste", accelerator="Ctrl+V", command=self.menuaction.paste
|
|
|
|
)
|
2020-03-27 05:24:23 +00:00
|
|
|
menu.add_command(
|
|
|
|
label="Delete", accelerator="Ctrl+D", command=self.menuaction.delete
|
|
|
|
)
|
2019-11-08 18:07:23 +00:00
|
|
|
self.add_cascade(label="Edit", menu=menu)
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2019-12-20 00:15:29 +00:00
|
|
|
self.app.master.bind_all("<Control-c>", self.menuaction.copy)
|
|
|
|
self.app.master.bind_all("<Control-v>", self.menuaction.paste)
|
2020-03-27 05:24:23 +00:00
|
|
|
self.app.master.bind_all("<Control-d>", self.menuaction.delete)
|
2020-02-24 20:51:47 +00:00
|
|
|
self.edit_menu = menu
|
2019-12-20 00:15:29 +00:00
|
|
|
|
2019-11-08 05:46:40 +00:00
|
|
|
def draw_canvas_menu(self):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create canvas menu
|
|
|
|
"""
|
2019-11-08 18:07:23 +00:00
|
|
|
menu = tk.Menu(self)
|
|
|
|
menu.add_command(
|
2020-04-18 07:18:11 +01:00
|
|
|
label="Size / Scale", command=self.menuaction.canvas_size_and_scale
|
2019-10-22 21:17:47 +01:00
|
|
|
)
|
2019-11-08 18:07:23 +00:00
|
|
|
menu.add_command(
|
2020-04-18 07:18:11 +01:00
|
|
|
label="Wallpaper", command=self.menuaction.canvas_set_wallpaper
|
2019-10-25 00:50:24 +01:00
|
|
|
)
|
2019-11-08 18:07:23 +00:00
|
|
|
self.add_cascade(label="Canvas", menu=menu)
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2019-11-08 05:46:40 +00:00
|
|
|
def draw_view_menu(self):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create view menu
|
|
|
|
"""
|
2020-04-18 07:18:11 +01:00
|
|
|
menu = tk.Menu(self)
|
2019-11-08 18:07:23 +00:00
|
|
|
menu.add_command(label="All", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="None", state=tk.DISABLED)
|
|
|
|
menu.add_separator()
|
|
|
|
menu.add_command(label="Interface Names", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="IPv4 Addresses", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="IPv6 Addresses", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="Node Labels", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="Annotations", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="Grid", state=tk.DISABLED)
|
2020-04-18 07:18:11 +01:00
|
|
|
self.add_cascade(label="View", menu=menu)
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2019-11-08 05:46:40 +00:00
|
|
|
def draw_tools_menu(self):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create tools menu
|
|
|
|
"""
|
2019-11-08 18:07:23 +00:00
|
|
|
menu = tk.Menu(self)
|
2020-04-18 07:18:11 +01:00
|
|
|
menu.add_command(label="Auto Grid", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="IP Addresses", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="MAC Addresses", state=tk.DISABLED)
|
2019-11-08 18:07:23 +00:00
|
|
|
self.add_cascade(label="Tools", menu=menu)
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2020-01-10 23:32:16 +00:00
|
|
|
def create_observer_widgets_menu(self, widget_menu: tk.Menu):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create observer widget menu item and create the sub menu items inside
|
|
|
|
"""
|
2019-11-11 21:23:02 +00:00
|
|
|
var = tk.StringVar(value="none")
|
2019-11-08 18:07:23 +00:00
|
|
|
menu = tk.Menu(widget_menu)
|
2019-11-11 21:23:02 +00:00
|
|
|
menu.var = var
|
2019-11-11 21:33:37 +00:00
|
|
|
menu.add_command(
|
|
|
|
label="Edit Observers", command=self.menuaction.edit_observer_widgets
|
|
|
|
)
|
|
|
|
menu.add_separator()
|
2019-11-11 21:23:02 +00:00
|
|
|
menu.add_radiobutton(
|
|
|
|
label="None",
|
|
|
|
variable=var,
|
|
|
|
value="none",
|
|
|
|
command=lambda: self.app.core.set_observer(None),
|
|
|
|
)
|
2019-11-11 23:35:48 +00:00
|
|
|
for name in sorted(OBSERVERS):
|
|
|
|
cmd = OBSERVERS[name]
|
2019-11-11 21:23:02 +00:00
|
|
|
menu.add_radiobutton(
|
|
|
|
label=name,
|
|
|
|
variable=var,
|
|
|
|
value=name,
|
|
|
|
command=partial(self.app.core.set_observer, cmd),
|
|
|
|
)
|
2019-11-11 21:33:37 +00:00
|
|
|
for name in sorted(self.app.core.custom_observers):
|
2019-11-11 22:04:50 +00:00
|
|
|
observer = self.app.core.custom_observers[name]
|
2019-11-11 21:33:37 +00:00
|
|
|
menu.add_radiobutton(
|
|
|
|
label=name,
|
|
|
|
variable=var,
|
|
|
|
value=name,
|
2019-11-11 22:04:50 +00:00
|
|
|
command=partial(self.app.core.set_observer, observer.cmd),
|
2019-11-11 21:33:37 +00:00
|
|
|
)
|
2019-11-08 18:07:23 +00:00
|
|
|
widget_menu.add_cascade(label="Observer Widgets", menu=menu)
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2020-01-10 23:32:16 +00:00
|
|
|
def create_adjacency_menu(self, widget_menu: tk.Menu):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create adjacency menu item and the sub menu items inside
|
|
|
|
"""
|
2019-11-08 18:07:23 +00:00
|
|
|
menu = tk.Menu(widget_menu)
|
2020-04-18 07:18:11 +01:00
|
|
|
menu.add_command(label="Configure Adjacency", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="Enable OSPFv2?", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="Enable OSPFv3?", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="Enable OSLR?", state=tk.DISABLED)
|
|
|
|
menu.add_command(label="Enable OSLRv2?", state=tk.DISABLED)
|
2019-11-08 18:07:23 +00:00
|
|
|
widget_menu.add_cascade(label="Adjacency", menu=menu)
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2020-04-18 07:18:11 +01:00
|
|
|
def create_throughput_menu(self, widget_menu: tk.Menu):
|
|
|
|
menu = tk.Menu(widget_menu)
|
|
|
|
menu.add_command(
|
|
|
|
label="Configure Throughput", command=self.menuaction.config_throughput
|
|
|
|
)
|
|
|
|
menu.add_checkbutton(
|
|
|
|
label="Enable Throughput?", command=self.menuaction.throughput
|
|
|
|
)
|
|
|
|
widget_menu.add_cascade(label="Throughput", menu=menu)
|
|
|
|
|
2019-11-08 05:46:40 +00:00
|
|
|
def draw_widgets_menu(self):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create widget menu
|
|
|
|
"""
|
2019-11-08 18:07:23 +00:00
|
|
|
menu = tk.Menu(self)
|
|
|
|
self.create_observer_widgets_menu(menu)
|
|
|
|
self.create_adjacency_menu(menu)
|
2020-04-18 07:18:11 +01:00
|
|
|
self.create_throughput_menu(menu)
|
2019-11-08 18:07:23 +00:00
|
|
|
self.add_cascade(label="Widgets", menu=menu)
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2019-11-08 05:46:40 +00:00
|
|
|
def draw_session_menu(self):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create session menu
|
|
|
|
"""
|
2019-11-08 18:07:23 +00:00
|
|
|
menu = tk.Menu(self)
|
|
|
|
menu.add_command(
|
2020-04-18 07:18:11 +01:00
|
|
|
label="Sessions", command=self.menuaction.session_change_sessions
|
2019-09-25 16:29:34 +01:00
|
|
|
)
|
2020-04-18 07:18:11 +01:00
|
|
|
menu.add_command(label="Servers", command=self.menuaction.session_servers)
|
|
|
|
menu.add_command(label="Options", command=self.menuaction.session_options)
|
|
|
|
menu.add_command(label="Hooks", command=self.menuaction.session_hooks)
|
2019-11-08 18:07:23 +00:00
|
|
|
self.add_cascade(label="Session", menu=menu)
|
2019-09-25 16:29:34 +01:00
|
|
|
|
2019-11-08 05:46:40 +00:00
|
|
|
def draw_help_menu(self):
|
2019-09-25 16:29:34 +01:00
|
|
|
"""
|
|
|
|
Create help menu
|
|
|
|
"""
|
2019-11-08 18:07:23 +00:00
|
|
|
menu = tk.Menu(self)
|
|
|
|
menu.add_command(
|
2019-11-22 06:39:39 +00:00
|
|
|
label="Core GitHub (www)", command=self.menuaction.help_core_github
|
2019-09-25 16:29:34 +01:00
|
|
|
)
|
2019-11-08 18:07:23 +00:00
|
|
|
menu.add_command(
|
2019-10-11 01:02:28 +01:00
|
|
|
label="Core Documentation (www)",
|
2019-11-08 05:46:40 +00:00
|
|
|
command=self.menuaction.help_core_documentation,
|
2019-09-25 16:29:34 +01:00
|
|
|
)
|
2019-12-06 19:13:43 +00:00
|
|
|
menu.add_command(label="About", command=self.menuaction.show_about)
|
2019-11-08 18:07:23 +00:00
|
|
|
self.add_cascade(label="Help", menu=menu)
|
2020-01-17 23:59:12 +00:00
|
|
|
|
2020-02-03 17:10:46 +00:00
|
|
|
def open_recent_files(self, filename: str):
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
logging.debug("Open recent file %s", filename)
|
|
|
|
self.menuaction.open_xml_task(filename)
|
|
|
|
else:
|
|
|
|
logging.warning("File does not exist %s", filename)
|
|
|
|
|
2020-02-04 21:49:46 +00:00
|
|
|
def update_recent_files(self):
|
|
|
|
self.recent_menu.delete(0, tk.END)
|
|
|
|
for i in self.app.guiconfig["recentfiles"]:
|
|
|
|
self.recent_menu.add_command(
|
|
|
|
label=i, command=partial(self.open_recent_files, i)
|
|
|
|
)
|
|
|
|
|
2020-02-04 21:00:00 +00:00
|
|
|
def save(self, event=None):
|
2020-01-17 23:59:12 +00:00
|
|
|
xml_file = self.app.core.xml_file
|
|
|
|
if xml_file:
|
|
|
|
self.app.core.save_xml(xml_file)
|
|
|
|
else:
|
|
|
|
self.menuaction.file_save_as_xml()
|
2020-02-24 20:51:47 +00:00
|
|
|
|
2020-02-27 23:24:36 +00:00
|
|
|
def execute_python(self):
|
|
|
|
dialog = ExecutePythonDialog(self.app, self.app)
|
|
|
|
dialog.show()
|
2020-03-04 19:14:20 +00:00
|
|
|
|
2020-02-24 20:51:47 +00:00
|
|
|
def change_menubar_item_state(self, is_runtime: bool):
|
|
|
|
for i in range(self.edit_menu.index("end")):
|
|
|
|
try:
|
|
|
|
label_name = self.edit_menu.entrycget(i, "label")
|
|
|
|
if label_name in ["Copy", "Paste"]:
|
|
|
|
if is_runtime:
|
|
|
|
self.edit_menu.entryconfig(i, state="disabled")
|
|
|
|
else:
|
|
|
|
self.edit_menu.entryconfig(i, state="normal")
|
|
|
|
except tk.TclError:
|
|
|
|
logging.debug("Ignore separators")
|