2019-10-11 01:02:28 +01:00
|
|
|
from enum import Enum
|
2020-06-20 06:08:24 +01:00
|
|
|
from typing import Dict, Optional, Tuple
|
2019-09-18 19:26:26 +01:00
|
|
|
|
2020-06-20 06:08:24 +01:00
|
|
|
from PIL import Image
|
|
|
|
from PIL.ImageTk import PhotoImage
|
2019-09-16 07:45:13 +01:00
|
|
|
|
2020-08-27 19:02:02 +01:00
|
|
|
from core.api.grpc.wrappers import NodeType
|
2019-12-19 17:30:21 +00:00
|
|
|
from core.gui.appconfig import LOCAL_ICONS_PATH
|
2019-09-18 19:20:22 +01:00
|
|
|
|
2021-02-19 05:04:16 +00:00
|
|
|
NODE_SIZE: int = 48
|
|
|
|
ANTENNA_SIZE: int = 32
|
|
|
|
BUTTON_SIZE: int = 16
|
|
|
|
ERROR_SIZE: int = 24
|
|
|
|
DIALOG_SIZE: int = 16
|
|
|
|
IMAGES: Dict[str, str] = {}
|
2021-02-18 18:47:20 +00:00
|
|
|
|
2019-09-16 07:45:13 +01:00
|
|
|
|
2021-02-19 05:04:16 +00:00
|
|
|
def load_all() -> None:
|
|
|
|
for image in LOCAL_ICONS_PATH.glob("*"):
|
|
|
|
IMAGES[image.stem] = str(image)
|
2019-09-16 07:45:13 +01:00
|
|
|
|
2019-11-03 06:47:43 +00:00
|
|
|
|
2021-02-19 05:04:16 +00:00
|
|
|
def from_file(
|
|
|
|
file_path: str, *, width: int, height: int = None, scale: float = 1.0
|
|
|
|
) -> PhotoImage:
|
|
|
|
if height is None:
|
|
|
|
height = width
|
|
|
|
width = int(width * scale)
|
|
|
|
height = int(height * scale)
|
|
|
|
image = Image.open(file_path)
|
|
|
|
image = image.resize((width, height), Image.ANTIALIAS)
|
|
|
|
return PhotoImage(image)
|
2019-09-16 07:45:13 +01:00
|
|
|
|
2019-10-02 00:25:26 +01:00
|
|
|
|
2021-02-19 05:04:16 +00:00
|
|
|
def from_enum(
|
|
|
|
image_enum: "ImageEnum", *, width: int, height: int = None, scale: float = 1.0
|
|
|
|
) -> PhotoImage:
|
|
|
|
file_path = IMAGES[image_enum.value]
|
|
|
|
return from_file(file_path, width=width, height=height, scale=scale)
|
2019-11-07 19:33:40 +00:00
|
|
|
|
2021-02-19 05:04:16 +00:00
|
|
|
|
|
|
|
def from_name(
|
|
|
|
name: str, *, width: int, height: int = None, scale: float = 1.0
|
|
|
|
) -> PhotoImage:
|
|
|
|
file_path = IMAGES[name]
|
|
|
|
return from_file(file_path, width=width, height=height, scale=scale)
|
2020-01-28 00:27:21 +00:00
|
|
|
|
2019-10-02 00:25:26 +01:00
|
|
|
|
2019-10-11 01:02:28 +01:00
|
|
|
class ImageEnum(Enum):
|
|
|
|
SWITCH = "lanswitch"
|
|
|
|
CORE = "core-icon"
|
|
|
|
START = "start"
|
|
|
|
MARKER = "marker"
|
|
|
|
ROUTER = "router"
|
|
|
|
SELECT = "select"
|
|
|
|
LINK = "link"
|
|
|
|
HUB = "hub"
|
|
|
|
WLAN = "wlan"
|
2019-11-01 15:35:14 +00:00
|
|
|
EMANE = "emane"
|
2019-10-11 01:02:28 +01:00
|
|
|
RJ45 = "rj45"
|
|
|
|
TUNNEL = "tunnel"
|
|
|
|
OVAL = "oval"
|
|
|
|
RECTANGLE = "rectangle"
|
|
|
|
TEXT = "text"
|
|
|
|
HOST = "host"
|
|
|
|
PC = "pc"
|
|
|
|
MDR = "mdr"
|
2019-11-21 20:33:43 +00:00
|
|
|
PROUTER = "prouter"
|
2019-10-11 01:02:28 +01:00
|
|
|
OVS = "OVS"
|
2019-11-21 20:33:43 +00:00
|
|
|
EDITNODE = "edit-node"
|
2019-10-11 01:02:28 +01:00
|
|
|
PLOT = "plot"
|
|
|
|
TWONODE = "twonode"
|
2019-11-27 16:48:21 +00:00
|
|
|
PAUSE = "pause"
|
2019-10-11 01:02:28 +01:00
|
|
|
STOP = "stop"
|
|
|
|
OBSERVE = "observe"
|
|
|
|
RUN = "run"
|
2019-10-19 00:42:00 +01:00
|
|
|
DOCUMENTNEW = "document-new"
|
2019-11-12 00:34:41 +00:00
|
|
|
DOCUMENTSAVE = "document-save"
|
2019-10-19 00:42:00 +01:00
|
|
|
FILEOPEN = "fileopen"
|
|
|
|
EDITDELETE = "edit-delete"
|
|
|
|
ANTENNA = "antenna"
|
2019-11-15 21:09:53 +00:00
|
|
|
DOCKER = "docker"
|
|
|
|
LXC = "lxc"
|
2019-12-12 18:49:52 +00:00
|
|
|
ALERT = "alert"
|
2020-01-15 23:23:49 +00:00
|
|
|
DELETE = "delete"
|
|
|
|
SHUTDOWN = "shutdown"
|
|
|
|
CANCEL = "cancel"
|
2020-02-05 23:53:14 +00:00
|
|
|
ERROR = "error"
|
2021-01-06 17:57:19 +00:00
|
|
|
SHADOW = "shadow"
|
2020-02-13 20:15:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TypeToImage:
|
2020-06-20 06:08:24 +01:00
|
|
|
type_to_image: Dict[Tuple[NodeType, str], ImageEnum] = {
|
|
|
|
(NodeType.DEFAULT, "router"): ImageEnum.ROUTER,
|
|
|
|
(NodeType.DEFAULT, "PC"): ImageEnum.PC,
|
|
|
|
(NodeType.DEFAULT, "host"): ImageEnum.HOST,
|
|
|
|
(NodeType.DEFAULT, "mdr"): ImageEnum.MDR,
|
|
|
|
(NodeType.DEFAULT, "prouter"): ImageEnum.PROUTER,
|
|
|
|
(NodeType.HUB, ""): ImageEnum.HUB,
|
|
|
|
(NodeType.SWITCH, ""): ImageEnum.SWITCH,
|
|
|
|
(NodeType.WIRELESS_LAN, ""): ImageEnum.WLAN,
|
|
|
|
(NodeType.EMANE, ""): ImageEnum.EMANE,
|
|
|
|
(NodeType.RJ45, ""): ImageEnum.RJ45,
|
|
|
|
(NodeType.TUNNEL, ""): ImageEnum.TUNNEL,
|
|
|
|
(NodeType.DOCKER, ""): ImageEnum.DOCKER,
|
|
|
|
(NodeType.LXC, ""): ImageEnum.LXC,
|
2020-02-13 20:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
2020-06-20 06:08:24 +01:00
|
|
|
def get(cls, node_type, model) -> Optional[ImageEnum]:
|
|
|
|
return cls.type_to_image.get((node_type, model))
|