scale edge text font (ipv4 and ipv6 address, scale edge, scale node when first drawn on canvas and when joining session

This commit is contained in:
Huy Pham 2020-02-14 13:34:00 -08:00
parent 55b6cbbd90
commit 0ea99ca809
5 changed files with 39 additions and 21 deletions

View file

@ -24,6 +24,7 @@ class Application(tk.Frame):
self.fonts_size = {name: font.nametofont(name)["size"] for name in font.names()}
self.icon_text_font = font.Font(family="TkIconFont", size=12)
self.edge_font = font.Font(family="TkDefaultFont", size=8)
# widgets
self.menubar = None

View file

@ -125,6 +125,7 @@ class PreferencesDialog(Dialog):
# scale fonts
scale_fonts(self.app.fonts_size, app_scale)
self.app.icon_text_font.config(size=int(12 * app_scale))
self.app.edge_font.config(size=int(8 * app_scale))
# scale application widow size
screen_width = self.app.master.winfo_screenwidth()

View file

@ -1,6 +1,5 @@
import logging
import tkinter as tk
from tkinter.font import Font
from typing import TYPE_CHECKING, Any, Tuple
from core.gui import themes
@ -31,7 +30,10 @@ class CanvasWirelessEdge:
self.dst = dst
self.canvas = canvas
self.id = self.canvas.create_line(
*position, tags=tags.WIRELESS_EDGE, width=1.5, fill="#009933"
*position,
tags=tags.WIRELESS_EDGE,
width=1.5 * self.canvas.app_scale,
fill="#009933",
)
def delete(self):
@ -61,13 +63,18 @@ class CanvasEdge:
self.dst_interface = None
self.canvas = canvas
self.id = self.canvas.create_line(
x1, y1, x2, y2, tags=tags.EDGE, width=EDGE_WIDTH, fill=EDGE_COLOR
x1,
y1,
x2,
y2,
tags=tags.EDGE,
width=EDGE_WIDTH * self.canvas.app_scale,
fill=EDGE_COLOR,
)
self.text_src = None
self.text_dst = None
self.text_middle = None
self.token = None
self.font = Font(size=8)
self.link = None
self.asymmetric_link = None
self.throughput = None
@ -117,7 +124,7 @@ class CanvasEdge:
y1,
text=label_one,
justify=tk.CENTER,
font=self.font,
font=self.canvas.app.edge_font,
tags=tags.LINK_INFO,
)
self.text_dst = self.canvas.create_text(
@ -125,7 +132,7 @@ class CanvasEdge:
y2,
text=label_two,
justify=tk.CENTER,
font=self.font,
font=self.canvas.app.edge_font,
tags=tags.LINK_INFO,
)
@ -146,7 +153,7 @@ class CanvasEdge:
if self.text_middle is None:
x, y = self.get_midpoint()
self.text_middle = self.canvas.create_text(
x, y, tags=tags.THROUGHPUT, font=self.font, text=value
x, y, tags=tags.THROUGHPUT, font=self.canvas.app.edge_font, text=value
)
else:
self.canvas.itemconfig(self.text_middle, text=value)

View file

@ -7,7 +7,7 @@ from PIL import Image, ImageTk
from core.api.grpc import core_pb2
from core.gui.dialogs.shapemod import ShapeDialog
from core.gui.graph import tags
from core.gui.graph.edges import CanvasEdge, CanvasWirelessEdge
from core.gui.graph.edges import EDGE_WIDTH, CanvasEdge, CanvasWirelessEdge
from core.gui.graph.enums import GraphMode, ScaleOption
from core.gui.graph.node import CanvasNode
from core.gui.graph.shape import Shape
@ -223,10 +223,10 @@ class CanvasGraph(tk.Canvas):
# peer to peer node is not drawn on the GUI
if NodeUtils.is_ignore_node(core_node.type):
continue
image = NodeUtils.node_image(core_node, self.app.guiconfig)
image = NodeUtils.node_image(core_node, self.app.guiconfig, self.app_scale)
# if the gui can't find node's image, default to the "edit-node" image
if not image:
image = Images.get(ImageEnum.EDITNODE, ICON_SIZE)
image = Images.get(ImageEnum.EDITNODE, int(ICON_SIZE * self.app_scale))
x = core_node.position.x
y = core_node.position.y
node = CanvasNode(self.master, x, y, core_node, image)
@ -666,6 +666,9 @@ class CanvasGraph(tk.Canvas):
core_node = self.core.create_node(
actual_x, actual_y, self.node_draw.node_type, self.node_draw.model
)
self.node_draw.image = Images.get(
self.node_draw.image_enum, int(ICON_SIZE * self.app_scale)
)
node = CanvasNode(self.master, x, y, core_node, self.node_draw.image)
self.core.canvas_nodes[core_node.id] = node
self.nodes[node.id] = node
@ -925,3 +928,6 @@ class CanvasGraph(tk.Canvas):
canvas_node.image = img
canvas_node.scale_text()
for edge_id in self.find_withtag(tags.EDGE):
self.itemconfig(edge_id, width=int(EDGE_WIDTH * self.app_scale))

View file

@ -2,7 +2,7 @@ import logging
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, Union
from core.api.grpc.core_pb2 import NodeType
from core.gui.images import ImageEnum, Images
from core.gui.images import ImageEnum, Images, TypeToImage
if TYPE_CHECKING:
from core.api.grpc import core_pb2
@ -96,25 +96,28 @@ class NodeUtils:
node_type: NodeType,
model: str,
gui_config: Dict[str, List[Dict[str, str]]],
scale=1.0,
) -> "ImageTk.PhotoImage":
if model == "":
model = None
try:
image = cls.NODE_ICONS[(node_type, model)]
return image
except KeyError:
image_enum = TypeToImage.get(node_type, model)
if image_enum:
return Images.get(image_enum, int(ICON_SIZE * scale))
else:
image_stem = cls.get_image_file(gui_config, model)
if image_stem:
return Images.get_with_image_file(image_stem, ICON_SIZE)
return Images.get_with_image_file(image_stem, int(ICON_SIZE * scale))
@classmethod
def node_image(
cls, core_node: "core_pb2.Node", gui_config: Dict[str, List[Dict[str, str]]]
cls,
core_node: "core_pb2.Node",
gui_config: Dict[str, List[Dict[str, str]]],
scale=1.0,
) -> "ImageTk.PhotoImage":
image = cls.node_icon(core_node.type, core_node.model, gui_config)
image = cls.node_icon(core_node.type, core_node.model, gui_config, scale)
if core_node.icon:
try:
image = Images.create(core_node.icon, ICON_SIZE)
image = Images.create(core_node.icon, int(ICON_SIZE * scale))
except OSError:
logging.error("invalid icon: %s", core_node.icon)
return image