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

172 lines
6.6 KiB
Python
Raw Normal View History

2019-11-01 15:35:14 +00:00
"""
core node services
"""
import tkinter as tk
2019-11-12 20:13:53 +00:00
from tkinter import messagebox, ttk
2020-01-14 19:06:52 +00:00
from typing import TYPE_CHECKING, Any
2019-11-01 15:35:14 +00:00
2019-12-19 17:30:21 +00:00
from core.gui.dialogs.dialog import Dialog
from core.gui.dialogs.serviceconfig import ServiceConfigDialog
2019-12-19 17:30:21 +00:00
from core.gui.themes import FRAME_PAD, PADX, PADY
from core.gui.widgets import CheckboxList, ListboxScroll
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-01 15:35:14 +00:00
class NodeServiceDialog(Dialog):
2020-01-13 23:31:41 +00:00
def __init__(
2020-01-14 19:06:52 +00:00
self, master: Any, app: "Application", canvas_node: "CanvasNode", services=None
2020-01-13 23:31:41 +00:00
):
title = f"{canvas_node.core_node.name} Services"
super().__init__(master, app, title, modal=True)
2019-11-21 23:00:17 +00:00
self.app = app
self.canvas_node = canvas_node
2019-11-16 01:26:02 +00:00
self.node_id = canvas_node.core_node.id
2019-11-11 18:57:26 +00:00
self.groups = None
self.services = None
self.current = None
2019-11-22 00:59:55 +00:00
if services is None:
services = canvas_node.core_node.services
model = canvas_node.core_node.model
if len(services) == 0:
services = set(self.app.core.default_services[model])
else:
services = set(services)
self.current_services = services
self.draw()
def draw(self):
self.top.columnconfigure(0, weight=1)
self.top.rowconfigure(0, weight=1)
2019-11-01 15:35:14 +00:00
frame = ttk.Frame(self.top)
frame.grid(stick="nsew", pady=PADY)
2019-11-11 18:57:26 +00:00
frame.rowconfigure(0, weight=1)
for i in range(3):
frame.columnconfigure(i, weight=1)
label_frame = ttk.LabelFrame(frame, text="Groups", padding=FRAME_PAD)
label_frame.grid(row=0, column=0, sticky="nsew")
label_frame.rowconfigure(0, weight=1)
label_frame.columnconfigure(0, weight=1)
self.groups = ListboxScroll(label_frame)
self.groups.grid(sticky="nsew")
for group in sorted(self.app.core.services):
2019-11-11 18:57:26 +00:00
self.groups.listbox.insert(tk.END, group)
self.groups.listbox.bind("<<ListboxSelect>>", self.handle_group_change)
self.groups.listbox.selection_set(0)
2019-11-01 15:35:14 +00:00
label_frame = ttk.LabelFrame(frame, text="Services")
label_frame.grid(row=0, column=1, sticky="nsew")
label_frame.columnconfigure(0, weight=1)
label_frame.rowconfigure(0, weight=1)
2019-11-11 18:57:26 +00:00
self.services = CheckboxList(
label_frame, self.app, clicked=self.service_clicked, padding=FRAME_PAD
2019-11-01 15:35:14 +00:00
)
self.services.grid(sticky="nsew")
label_frame = ttk.LabelFrame(frame, text="Selected", padding=FRAME_PAD)
label_frame.grid(row=0, column=2, sticky="nsew")
label_frame.rowconfigure(0, weight=1)
label_frame.columnconfigure(0, weight=1)
self.current = ListboxScroll(label_frame)
self.current.grid(sticky="nsew")
2019-11-11 18:57:26 +00:00
for service in sorted(self.current_services):
self.current.listbox.insert(tk.END, service)
if self.is_custom_service(service):
self.current.listbox.itemconfig(tk.END, bg="green")
frame = ttk.Frame(self.top)
2019-11-11 18:57:26 +00:00
frame.grid(stick="ew")
for i in range(4):
2019-11-11 18:57:26 +00:00
frame.columnconfigure(i, weight=1)
2019-11-12 20:13:53 +00:00
button = ttk.Button(frame, text="Configure", command=self.click_configure)
button.grid(row=0, column=0, sticky="ew", padx=PADX)
button = ttk.Button(frame, text="Save", command=self.click_save)
button.grid(row=0, column=1, sticky="ew", padx=PADX)
button = ttk.Button(frame, text="Remove", command=self.click_remove)
button.grid(row=0, column=2, sticky="ew", padx=PADX)
button = ttk.Button(frame, text="Cancel", command=self.click_cancel)
button.grid(row=0, column=3, sticky="ew")
2019-11-11 18:57:26 +00:00
# trigger group change
self.groups.listbox.event_generate("<<ListboxSelect>>")
def handle_group_change(self, event: tk.Event = None):
2019-11-11 18:57:26 +00:00
selection = self.groups.listbox.curselection()
if selection:
index = selection[0]
group = self.groups.listbox.get(index)
self.services.clear()
2019-11-21 23:07:43 +00:00
for name in sorted(self.app.core.services[group]):
checked = name in self.current_services
self.services.add(name, checked)
2019-11-11 18:57:26 +00:00
def service_clicked(self, name: str, var: tk.IntVar):
2019-11-11 18:57:26 +00:00
if var.get() and name not in self.current_services:
self.current_services.add(name)
2019-11-11 18:57:26 +00:00
elif not var.get() and name in self.current_services:
self.current_services.remove(name)
self.current.listbox.delete(0, tk.END)
for name in sorted(self.current_services):
self.current.listbox.insert(tk.END, name)
if self.is_custom_service(name):
self.current.listbox.itemconfig(tk.END, bg="green")
2019-12-06 23:06:35 +00:00
self.canvas_node.core_node.services[:] = self.current_services
def click_configure(self):
2019-11-11 18:57:26 +00:00
current_selection = self.current.listbox.curselection()
if len(current_selection):
dialog = ServiceConfigDialog(
2019-11-11 18:57:26 +00:00
master=self,
app=self.app,
service_name=self.current.listbox.get(current_selection[0]),
node_id=self.node_id,
2019-11-11 18:57:26 +00:00
)
dialog.show()
2019-11-01 15:35:14 +00:00
else:
2019-11-11 18:57:26 +00:00
messagebox.showinfo(
"Node service configuration", "Select a service to configure"
)
def click_save(self):
2019-11-21 23:00:17 +00:00
if (
self.current_services
!= self.app.core.default_services[self.canvas_node.core_node.model]
):
self.canvas_node.core_node.services[:] = self.current_services
else:
if len(self.canvas_node.core_node.services) > 0:
self.canvas_node.core_node.services[:] = []
self.destroy()
2019-11-11 18:57:26 +00:00
def click_cancel(self):
self.current_services = None
self.destroy()
def click_remove(self):
cur = self.current.listbox.curselection()
if cur:
service = self.current.listbox.get(cur[0])
self.current.listbox.delete(cur[0])
self.current_services.remove(service)
for checkbutton in self.services.frame.winfo_children():
if checkbutton["text"] == service:
checkbutton.invoke()
return
def is_custom_service(self, service: str) -> bool:
service_configs = self.app.core.service_configs
file_configs = self.app.core.file_configs
if self.node_id in service_configs and service in service_configs[self.node_id]:
return True
if (
self.node_id in file_configs
and service in file_configs[self.node_id]
and file_configs[self.node_id][service]
):
return True
return False