core-extra/daemon/core/gui/dialogs/emaneconfig.py

243 lines
7.7 KiB
Python
Raw Normal View History

2019-11-04 06:58:45 +00:00
"""
emane configuration
"""
import tkinter as tk
2019-11-04 18:48:22 +00:00
import webbrowser
from tkinter import ttk
2020-01-15 20:59:54 +00:00
from typing import TYPE_CHECKING, Any
2019-11-04 06:58:45 +00:00
import grpc
2020-01-13 23:31:41 +00:00
from core.api.grpc import core_pb2
2019-12-19 17:30:21 +00:00
from core.gui.dialogs.dialog import Dialog
from core.gui.errors import show_grpc_error
from core.gui.images import ImageEnum, Images
from core.gui.themes import PADX, PADY
from core.gui.widgets import ConfigFrame
2019-11-04 06:58:45 +00:00
2020-01-13 23:31:41 +00:00
if TYPE_CHECKING:
from core.gui.app import Application
from core.gui.graph.node import CanvasNode
2019-11-04 06:58:45 +00:00
class GlobalEmaneDialog(Dialog):
2020-01-14 19:06:52 +00:00
def __init__(self, master: Any, app: "Application"):
super().__init__(master, app, "EMANE Configuration", modal=True)
self.config_frame = None
self.draw()
2019-11-04 18:48:22 +00:00
def draw(self):
self.top.columnconfigure(0, weight=1)
self.top.rowconfigure(0, weight=1)
self.config_frame = ConfigFrame(self.top, self.app, self.app.core.emane_config)
self.config_frame.draw_config()
self.config_frame.grid(sticky="nsew", pady=PADY)
self.draw_spacer()
self.draw_buttons()
def draw_buttons(self):
frame = ttk.Frame(self.top)
frame.grid(sticky="ew")
for i in range(2):
frame.columnconfigure(i, weight=1)
button = ttk.Button(frame, text="Apply", command=self.click_apply)
button.grid(row=0, column=0, sticky="ew", padx=PADX)
button = ttk.Button(frame, text="Cancel", command=self.destroy)
button.grid(row=0, column=1, sticky="ew")
2019-11-04 18:48:22 +00:00
def click_apply(self):
self.config_frame.parse_config()
self.destroy()
2019-11-04 06:58:45 +00:00
class EmaneModelDialog(Dialog):
2020-01-13 23:31:41 +00:00
def __init__(
self,
2020-01-14 19:06:52 +00:00
master: Any,
2020-01-13 23:31:41 +00:00
app: "Application",
node: core_pb2.Node,
model: str,
2020-01-15 20:59:54 +00:00
interface: int = None,
2020-01-13 23:31:41 +00:00
):
super().__init__(master, app, f"{node.name} {model} Configuration", modal=True)
self.node = node
self.model = f"emane_{model}"
self.interface = interface
self.config_frame = None
self.has_error = False
try:
self.config = self.app.core.get_emane_model_config(
self.node.id, self.model, self.interface
)
self.draw()
except grpc.RpcError as e:
show_grpc_error(e, self.app, self.app)
self.has_error = True
self.destroy()
def draw(self):
self.top.columnconfigure(0, weight=1)
self.top.rowconfigure(0, weight=1)
self.config_frame = ConfigFrame(self.top, self.app, self.config)
self.config_frame.draw_config()
self.config_frame.grid(sticky="nsew", pady=PADY)
self.draw_spacer()
self.draw_buttons()
def draw_buttons(self):
frame = ttk.Frame(self.top)
frame.grid(sticky="ew")
for i in range(2):
frame.columnconfigure(i, weight=1)
button = ttk.Button(frame, text="Apply", command=self.click_apply)
button.grid(row=0, column=0, sticky="ew", padx=PADX)
button = ttk.Button(frame, text="Cancel", command=self.destroy)
button.grid(row=0, column=1, sticky="ew")
def click_apply(self):
self.config_frame.parse_config()
self.app.core.set_emane_model_config(
self.node.id, self.model, self.config, self.interface
)
self.destroy()
class EmaneConfigDialog(Dialog):
2020-01-14 19:06:52 +00:00
def __init__(
self, master: "Application", app: "Application", canvas_node: "CanvasNode"
):
super().__init__(
master, app, f"{canvas_node.core_node.name} EMANE Configuration", modal=True
)
self.app = app
self.canvas_node = canvas_node
self.node = canvas_node.core_node
self.radiovar = tk.IntVar()
self.radiovar.set(1)
self.emane_models = [x.split("_")[1] for x in self.app.core.emane_models]
self.emane_model = tk.StringVar(value=self.node.emane.split("_")[1])
self.emane_model_button = None
self.draw()
def draw(self):
self.top.columnconfigure(0, weight=1)
self.draw_emane_configuration()
self.draw_emane_models()
self.draw_emane_buttons()
self.draw_spacer()
self.draw_apply_and_cancel()
def draw_emane_configuration(self):
"""
draw the main frame for emane configuration
"""
label = ttk.Label(
self.top,
text="The EMANE emulation system provides more complex wireless radio emulation "
"\nusing pluggable MAC and PHY modules. Refer to the wiki for configuration option details",
justify=tk.CENTER,
)
label.grid(pady=PADY)
image = Images.get(ImageEnum.EDITNODE, 16)
button = ttk.Button(
self.top,
image=image,
text="EMANE Wiki",
compound=tk.RIGHT,
command=lambda: webbrowser.open_new(
"https://github.com/adjacentlink/emane/wiki"
),
)
button.image = image
button.grid(sticky="ew", pady=PADY)
def draw_emane_models(self):
"""
create a combobox that has all the known emane models
"""
frame = ttk.Frame(self.top)
frame.grid(sticky="ew", pady=PADY)
frame.columnconfigure(1, weight=1)
label = ttk.Label(frame, text="Model")
label.grid(row=0, column=0, sticky="w")
# create combo box and its binding
combobox = ttk.Combobox(
frame,
textvariable=self.emane_model,
values=self.emane_models,
state="readonly",
)
combobox.grid(row=0, column=1, sticky="ew")
combobox.bind("<<ComboboxSelected>>", self.emane_model_change)
def draw_emane_buttons(self):
frame = ttk.Frame(self.top)
frame.grid(sticky="ew", pady=PADY)
for i in range(2):
frame.columnconfigure(i, weight=1)
image = Images.get(ImageEnum.EDITNODE, 16)
self.emane_model_button = ttk.Button(
frame,
text=f"{self.emane_model.get()} options",
image=image,
2019-11-04 18:48:22 +00:00
compound=tk.RIGHT,
command=self.click_model_config,
2019-11-04 18:48:22 +00:00
)
self.emane_model_button.image = image
self.emane_model_button.grid(row=0, column=0, padx=PADX, sticky="ew")
image = Images.get(ImageEnum.EDITNODE, 16)
button = ttk.Button(
frame,
2019-11-04 18:48:22 +00:00
text="EMANE options",
image=image,
2019-11-04 18:48:22 +00:00
compound=tk.RIGHT,
command=self.click_emane_config,
2019-11-04 18:48:22 +00:00
)
button.image = image
button.grid(row=0, column=1, sticky="ew")
def draw_apply_and_cancel(self):
frame = ttk.Frame(self.top)
frame.grid(sticky="ew")
for i in range(2):
frame.columnconfigure(i, weight=1)
button = ttk.Button(frame, text="Apply", command=self.click_apply)
button.grid(row=0, column=0, padx=PADX, sticky="ew")
button = ttk.Button(frame, text="Cancel", command=self.destroy)
button.grid(row=0, column=1, sticky="ew")
2019-11-04 18:48:22 +00:00
def click_emane_config(self):
dialog = GlobalEmaneDialog(self, self.app)
dialog.show()
def click_model_config(self):
"""
draw emane model configuration
"""
model_name = self.emane_model.get()
dialog = EmaneModelDialog(
self, self.app, self.canvas_node.core_node, model_name
)
if not dialog.has_error:
dialog.show()
def emane_model_change(self, event: tk.Event):
"""
update emane model options button
"""
model_name = self.emane_model.get()
self.emane_model_button.config(text=f"{model_name} options")
def click_apply(self):
self.node.emane = f"emane_{self.emane_model.get()}"
self.destroy()