pygui: cleanup for canvas metadata creation and parsing

This commit is contained in:
Blake Harnden 2020-12-17 13:10:28 -08:00
parent 367a2096fa
commit b937e316c4
3 changed files with 62 additions and 51 deletions

View file

@ -6,7 +6,6 @@ import json
import logging
import os
import tkinter as tk
from pathlib import Path
from tkinter import messagebox
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Set, Tuple
@ -40,7 +39,7 @@ from core.api.grpc.wrappers import (
SessionState,
ThroughputsEvent,
)
from core.gui.appconfig import BACKGROUNDS_PATH, XMLS_PATH, CoreServer, Observer
from core.gui.appconfig import XMLS_PATH, CoreServer, Observer
from core.gui.dialogs.emaneinstall import EmaneInstallDialog
from core.gui.dialogs.error import ErrorDialog
from core.gui.dialogs.mobilityplayer import MobilityPlayer
@ -330,31 +329,9 @@ class CoreClient:
logging.debug("canvas metadata: %s", canvas_config)
if canvas_config:
canvas_config = json.loads(canvas_config)
# get configured dimensions and gridlines option
dimensions = self.app.manager.default_dimensions
dimensions = canvas_config.get("dimensions", dimensions)
gridlines = canvas_config.get("gridlines", True)
self.app.manager.show_grid.set(gridlines)
self.app.manager.redraw_canvases(dimensions)
self.app.manager.parse_metadata(canvas_config)
# get background configurations
for background_config in canvas_config.get("canvases", []):
canvas_id = background_config.get("id")
if canvas_id is None:
logging.error("canvas config id not provided")
continue
canvas = self.app.manager.get(canvas_id)
fit_image = background_config.get("fit_image", False)
wallpaper_style = background_config.get("wallpaper-style", 1)
wallpaper = background_config.get("wallpaper")
canvas.adjust_to_dim.set(fit_image)
canvas.scale_option.set(wallpaper_style)
logging.info("canvas config: %s", background_config)
if wallpaper:
wallpaper = str(BACKGROUNDS_PATH.joinpath(wallpaper))
canvas.set_wallpaper(wallpaper)
# load saved shapes
# load saved shapes
shapes_config = config.get("shapes")
if shapes_config:
shapes_config = json.loads(shapes_config)
@ -362,7 +339,7 @@ class CoreClient:
logging.debug("loading shape: %s", shape_config)
Shape.from_metadata(self.app, shape_config)
# load edges config
# load edges config
edges_config = config.get("edges")
if edges_config:
edges_config = json.loads(edges_config)
@ -538,27 +515,7 @@ class CoreClient:
def set_metadata(self) -> None:
# create canvas data
canvases = []
for canvas in self.app.manager.all():
wallpaper_path = None
if canvas.wallpaper_file:
wallpaper = Path(canvas.wallpaper_file)
if BACKGROUNDS_PATH == wallpaper.parent:
wallpaper_path = wallpaper.name
else:
wallpaper_path = str(wallpaper)
config = {
"id": canvas.id,
"wallpaper": wallpaper_path,
"wallpaper-style": canvas.scale_option.get(),
"fit_image": canvas.adjust_to_dim.get(),
}
canvases.append(config)
canvas_config = dict(
gridlines=self.app.manager.show_grid.get(),
dimensions=self.app.manager.current_dimensions,
canvases=canvases,
)
canvas_config = self.app.manager.get_metadata()
canvas_config = json.dumps(canvas_config)
# create shapes data

View file

@ -1,7 +1,8 @@
import logging
import tkinter as tk
from copy import deepcopy
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple
from PIL import Image
from PIL.ImageTk import PhotoImage
@ -14,6 +15,7 @@ from core.api.grpc.wrappers import (
Session,
ThroughputsEvent,
)
from core.gui import appconfig
from core.gui.dialogs.shapemod import ShapeDialog
from core.gui.graph import tags
from core.gui.graph.edges import (
@ -925,7 +927,9 @@ class CanvasGraph(tk.Canvas):
)
if not copy:
continue
node = CanvasNode(self.app, scaled_x, scaled_y, copy, canvas_node.image)
node = CanvasNode(
self.app, self, scaled_x, scaled_y, copy, canvas_node.image
)
# copy configurations and services
node.core_node.services = core_node.services.copy()
node.core_node.config_services = core_node.config_services.copy()
@ -1058,3 +1062,28 @@ class CanvasGraph(tk.Canvas):
for edge_id in self.find_withtag(tags.EDGE):
self.itemconfig(edge_id, width=int(EDGE_WIDTH * self.app.app_scale))
def get_metadata(self) -> Dict[str, Any]:
wallpaper_path = None
if self.wallpaper_file:
wallpaper = Path(self.wallpaper_file)
if appconfig.BACKGROUNDS_PATH == wallpaper.parent:
wallpaper_path = wallpaper.name
else:
wallpaper_path = str(wallpaper)
return dict(
id=self.id,
wallpaper=wallpaper_path,
wallpaper_style=self.scale_option.get(),
fit_image=self.adjust_to_dim.get(),
)
def parse_metadata(self, config: Dict[str, Any]) -> None:
fit_image = config.get("fit_image", False)
wallpaper_style = config.get("wallpaper-style", 1)
wallpaper = config.get("wallpaper")
self.adjust_to_dim.set(fit_image)
self.scale_option.set(wallpaper_style)
if wallpaper:
wallpaper = str(appconfig.BACKGROUNDS_PATH.joinpath(wallpaper))
self.set_wallpaper(wallpaper)

View file

@ -1,7 +1,7 @@
import logging
import tkinter as tk
from tkinter import BooleanVar, messagebox, ttk
from typing import TYPE_CHECKING, Dict, Optional, Set, Tuple, ValuesView
from typing import TYPE_CHECKING, Any, Dict, Optional, Set, Tuple, ValuesView
from core.api.grpc.wrappers import LinkType, Session
from core.gui.graph import tags
@ -200,3 +200,28 @@ class CanvasManager:
canvas.redraw_canvas(dimensions)
if canvas.wallpaper:
canvas.redraw_wallpaper()
def get_metadata(self) -> Dict[str, Any]:
canvases = [x.get_metadata() for x in self.all()]
return dict(
gridlines=self.app.manager.show_grid.get(),
dimensions=self.app.manager.current_dimensions,
canvases=canvases,
)
def parse_metadata(self, config: Dict[str, Any]) -> None:
# get configured dimensions and gridlines option
dimensions = self.default_dimensions
dimensions = config.get("dimensions", dimensions)
gridlines = config.get("gridlines", True)
self.show_grid.set(gridlines)
self.redraw_canvases(dimensions)
# get background configurations
for canvas_config in config.get("canvases", []):
canvas_id = canvas_config.get("id")
if canvas_id is None:
logging.error("canvas config id not provided")
continue
canvas = self.get(canvas_id)
canvas.parse_metadata(canvas_config)