pygui: added multi canvas support to shapes, updated how shape metadata is saved and loaded to align

This commit is contained in:
Blake Harnden 2020-12-17 12:42:49 -08:00
parent 4a8f8557a6
commit 367a2096fa
4 changed files with 41 additions and 35 deletions

View file

@ -47,6 +47,7 @@ from core.gui.dialogs.mobilityplayer import MobilityPlayer
from core.gui.dialogs.sessions import SessionsDialog
from core.gui.graph.edges import CanvasEdge
from core.gui.graph.node import CanvasNode
from core.gui.graph.shape import Shape
from core.gui.interface import InterfaceManager
from core.gui.nodeutils import NodeDraw, NodeUtils
@ -354,35 +355,14 @@ class CoreClient:
canvas.set_wallpaper(wallpaper)
# load saved shapes
# shapes_config = config.get("shapes")
# if shapes_config:
# shapes_config = json.loads(shapes_config)
# for shape_config in shapes_config:
# logging.debug("loading shape: %s", shape_config)
# shape_type = shape_config["type"]
# try:
# shape_type = ShapeType(shape_type)
# coords = shape_config["iconcoords"]
# data = AnnotationData(
# shape_config["label"],
# shape_config["fontfamily"],
# shape_config["fontsize"],
# shape_config["labelcolor"],
# shape_config["color"],
# shape_config["border"],
# shape_config["width"],
# shape_config["bold"],
# shape_config["italic"],
# shape_config["underline"],
# )
# shape = Shape(
# self.app, self.app.canvas, shape_type, *coords, data=data
# )
# canvas.shapes[shape.id] = shape
# except ValueError:
# logging.exception("unknown shape: %s", shape_type)
shapes_config = config.get("shapes")
if shapes_config:
shapes_config = json.loads(shapes_config)
for shape_config in shapes_config:
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)
@ -559,7 +539,7 @@ class CoreClient:
def set_metadata(self) -> None:
# create canvas data
canvases = []
for canvas in self.app.manager.canvases.values():
for canvas in self.app.manager.all():
wallpaper_path = None
if canvas.wallpaper_file:
wallpaper = Path(canvas.wallpaper_file)
@ -583,9 +563,9 @@ class CoreClient:
# create shapes data
shapes = []
# TODO: handle shapes being on multiple canvases
# for shape in self.app.canvas.shapes.values():
# shapes.append(shape.metadata())
for canvas in self.app.manager.all():
for shape in canvas.shapes.values():
shapes.append(shape.metadata())
shapes = json.dumps(shapes)
# create edges config

View file

@ -27,7 +27,7 @@ class ShapeDialog(Dialog):
else:
title = "Add Text"
super().__init__(app, title)
self.canvas: "CanvasGraph" = app.canvas
self.canvas: "CanvasGraph" = app.manager.current()
self.fill: Optional[ttk.Label] = None
self.border: Optional[ttk.Label] = None
self.shape: "Shape" = shape

View file

@ -25,7 +25,7 @@ class ShowVar(BooleanVar):
return tk.NORMAL if self.get() else tk.HIDDEN
def click_handler(self) -> None:
for canvas in self.manager.canvases.values():
for canvas in self.manager.all():
canvas.itemconfigure(self.tag, state=self.state())

View file

@ -1,5 +1,5 @@
import logging
from typing import TYPE_CHECKING, Dict, List, Optional, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from core.gui.dialogs.shapemod import ShapeDialog
from core.gui.graph import tags
@ -69,6 +69,31 @@ class Shape:
self.shape_data = data
self.draw()
@classmethod
def from_metadata(cls, app: "Application", config: Dict[str, Any]) -> None:
shape_type = config["type"]
try:
shape_type = ShapeType(shape_type)
coords = config["iconcoords"]
data = AnnotationData(
config["label"],
config["fontfamily"],
config["fontsize"],
config["labelcolor"],
config["color"],
config["border"],
config["width"],
config["bold"],
config["italic"],
config["underline"],
)
canvas_id = config.get("canvas", 1)
canvas = app.manager.get(canvas_id)
shape = Shape(app, canvas, shape_type, *coords, data=data)
canvas.shapes[shape.id] = shape
except ValueError:
logging.exception("unknown shape: %s", shape_type)
def draw(self) -> None:
if self.created:
dash = None
@ -184,6 +209,7 @@ class Shape:
x1, y1 = self.canvas.get_actual_coords(x1, y1)
coords = (x1, y1)
return {
"canvas": self.canvas.id,
"type": self.shape_type.value,
"iconcoords": coords,
"label": self.shape_data.text,