core-extra/daemon/core/gui/images.py

112 lines
2.9 KiB
Python
Raw Normal View History

from enum import Enum
from typing import Dict, Optional, Tuple
2019-09-18 19:26:26 +01:00
from PIL import Image
from PIL.ImageTk import PhotoImage
from core.api.grpc.wrappers import NodeType
2019-12-19 17:30:21 +00:00
from core.gui.appconfig import LOCAL_ICONS_PATH
NODE_SIZE: int = 48
ANTENNA_SIZE: int = 32
BUTTON_SIZE: int = 16
ERROR_SIZE: int = 24
DIALOG_SIZE: int = 16
IMAGES: Dict[str, str] = {}
def load_all() -> None:
for image in LOCAL_ICONS_PATH.glob("*"):
IMAGES[image.stem] = str(image)
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)
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)
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)
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"
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"
OVS = "OVS"
2019-11-21 20:33:43 +00:00
EDITNODE = "edit-node"
PLOT = "plot"
TWONODE = "twonode"
PAUSE = "pause"
STOP = "stop"
OBSERVE = "observe"
RUN = "run"
2019-10-19 00:42:00 +01:00
DOCUMENTNEW = "document-new"
DOCUMENTSAVE = "document-save"
2019-10-19 00:42:00 +01:00
FILEOPEN = "fileopen"
EDITDELETE = "edit-delete"
ANTENNA = "antenna"
DOCKER = "docker"
LXC = "lxc"
ALERT = "alert"
DELETE = "delete"
SHUTDOWN = "shutdown"
CANCEL = "cancel"
ERROR = "error"
SHADOW = "shadow"
class TypeToImage:
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,
}
@classmethod
def get(cls, node_type, model) -> Optional[ImageEnum]:
return cls.type_to_image.get((node_type, model))