added common tags file for canvas created items, to reduce duplicate strings

This commit is contained in:
Blake Harnden 2019-12-09 14:13:21 -08:00
parent afdacf0c94
commit b04e61ccee
9 changed files with 81 additions and 68 deletions

View file

@ -11,24 +11,12 @@ from core.api.grpc import client, core_pb2
from coretk import appconfig
from coretk.dialogs.mobilityplayer import MobilityPlayer
from coretk.dialogs.sessions import SessionsDialog
from coretk.graph import tags
from coretk.graph.shape import AnnotationData, Shape
from coretk.graph.shapeutils import ShapeType
from coretk.interface import InterfaceManager
from coretk.nodeutils import NodeDraw, NodeUtils
LIFT_ORDER = [
"wallpaper",
"shape",
"gridline",
"shapetext",
"text",
"edge",
"antenna",
"nodename",
"linkinfo",
"node",
]
OBSERVERS = {
"processes": "ps",
"ifconfig": "ifconfig",
@ -363,7 +351,7 @@ class CoreClient:
except ValueError:
logging.exception("unknown shape: %s", shape_type)
for tag in LIFT_ORDER:
for tag in tags.ABOVE_WALLPAPER_TAGS:
self.app.canvas.tag_raise(tag)
def create_new_session(self):

View file

@ -5,6 +5,7 @@ import tkinter as tk
from tkinter import colorchooser, font, ttk
from coretk.dialogs.dialog import Dialog
from coretk.graph import tags
from coretk.graph.shapeutils import is_draw_shape, is_shape_text
FONT_SIZES = [8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72]
@ -230,7 +231,7 @@ class ShapeDialog(Dialog):
text=shape_text,
fill=self.text_color,
font=text_font,
tags="shapetext",
tags=tags.SHAPE_TEXT,
)
self.shape.created = True
else:

View file

@ -1,5 +1,6 @@
import tkinter as tk
from coretk.graph import tags
from coretk.nodeutils import NodeUtils
@ -10,7 +11,7 @@ class CanvasWirelessEdge:
self.dst = dst
self.canvas = canvas
self.id = self.canvas.create_line(
*position, tags="wireless", width=1.5, fill="#009933"
*position, tags=tags.WIRELESS_EDGE, width=1.5, fill="#009933"
)
def delete(self):
@ -40,7 +41,7 @@ class CanvasEdge:
self.dst_interface = None
self.canvas = canvas
self.id = self.canvas.create_line(
x1, y1, x2, y2, tags="edge", width=self.width, fill="#ff0000"
x1, y1, x2, y2, tags=tags.EDGE, width=self.width, fill="#ff0000"
)
self.token = None
self.link_info = None

View file

@ -5,6 +5,7 @@ from PIL import Image, ImageTk
from core.api.grpc import core_pb2
from coretk.dialogs.shapemod import ShapeDialog
from coretk.graph import tags
from coretk.graph.edges import CanvasEdge, CanvasWirelessEdge
from coretk.graph.enums import GraphMode, ScaleOption
from coretk.graph.linkinfo import LinkInfo, Throughput
@ -14,30 +15,6 @@ from coretk.graph.shapeutils import is_draw_shape
from coretk.images import Images
from coretk.nodeutils import NodeUtils
ABOVE_WALLPAPER = [
"gridline",
"shape",
"shapetext",
"edge",
"linkinfo",
"wireless",
"antenna",
"nodename",
"node",
]
CANVAS_COMPONENT_TAGS = [
"edge",
"node",
"nodename",
"wallpaper",
"linkinfo",
"antenna",
"wireless",
"selectednodes",
"shape",
"shapetext",
]
class CanvasGraph(tk.Canvas):
def __init__(self, master, core, width, height, cnf=None, **kwargs):
@ -99,7 +76,7 @@ class CanvasGraph(tk.Canvas):
:return: nothing
"""
# delete any existing drawn items
for tag in CANVAS_COMPONENT_TAGS:
for tag in tags.COMPONENT_TAGS:
self.delete(tag)
# set the private variables to default value
@ -140,10 +117,10 @@ class CanvasGraph(tk.Canvas):
width = int(width)
height = int(height)
for i in range(0, width, 27):
self.create_line(i, 0, i, height, dash=(2, 4), tags="gridline")
self.create_line(i, 0, i, height, dash=(2, 4), tags=tags.GRIDLINE)
for i in range(0, height, 27):
self.create_line(0, i, width, i, dash=(2, 4), tags="gridline")
self.tag_lower("gridline")
self.create_line(0, i, width, i, dash=(2, 4), tags=tags.GRIDLINE)
self.tag_lower(tags.GRIDLINE)
self.tag_lower(self.grid)
def add_wireless_edge(self, src, dst):
@ -214,7 +191,7 @@ class CanvasGraph(tk.Canvas):
canvas_node_two.interfaces.append(link.interface_two)
# raise the nodes so they on top of the links
self.tag_raise("node")
self.tag_raise(tags.NODE)
def canvas_xy(self, event):
"""
@ -336,7 +313,7 @@ class CanvasGraph(tk.Canvas):
(x0 - 6, y0 - 6, x1 + 6, y1 + 6),
activedash=True,
dash="-",
tags="selectednodes",
tags=tags.SELECTION,
)
self.selection[object_id] = selection_id
else:
@ -553,7 +530,7 @@ class CanvasGraph(tk.Canvas):
self.delete(self.wallpaper_id)
# place left corner of image to the left corner of the canvas
self.wallpaper_id = self.create_image(
(cropx / 2, cropy / 2), image=cropped_tk, tags="wallpaper"
(cropx / 2, cropy / 2), image=cropped_tk, tags=tags.WALLPAPER
)
self.wallpaper_drawn = cropped_tk
@ -581,7 +558,7 @@ class CanvasGraph(tk.Canvas):
# place the center of the image at the center of the canvas
self.delete(self.wallpaper_id)
self.wallpaper_id = self.create_image(
(canvas_w / 2, canvas_h / 2), image=cropped_tk, tags="wallpaper"
(canvas_w / 2, canvas_h / 2), image=cropped_tk, tags=tags.WALLPAPER
)
self.wallpaper_drawn = cropped_tk
@ -595,7 +572,7 @@ class CanvasGraph(tk.Canvas):
image = Images.create(self.wallpaper_file, int(canvas_w), int(canvas_h))
self.delete(self.wallpaper_id)
self.wallpaper_id = self.create_image(
(canvas_w / 2, canvas_h / 2), image=image, tags="wallpaper"
(canvas_w / 2, canvas_h / 2), image=image, tags=tags.WALLPAPER
)
self.wallpaper_drawn = image
@ -619,7 +596,7 @@ class CanvasGraph(tk.Canvas):
self.coords(self.grid, 0, 0, width, height)
# redraw gridlines to new canvas size
self.delete("gridline")
self.delete(tags.GRIDLINE)
self.draw_grid()
self.update_grid()
@ -639,15 +616,15 @@ class CanvasGraph(tk.Canvas):
logging.warning("tiled background not implemented yet")
# raise items above wallpaper
for component in ABOVE_WALLPAPER:
for component in tags.ABOVE_WALLPAPER_TAGS:
self.tag_raise(component)
def update_grid(self):
logging.info("updating grid show: %s", self.show_grid.get())
if self.show_grid.get():
self.itemconfig("gridline", state=tk.NORMAL)
self.itemconfig(tags.GRIDLINE, state=tk.NORMAL)
else:
self.itemconfig("gridline", state=tk.HIDDEN)
self.itemconfig(tags.GRIDLINE, state=tk.HIDDEN)
def set_wallpaper(self, filename):
logging.info("setting wallpaper: %s", filename)

View file

@ -5,6 +5,7 @@ import tkinter as tk
from tkinter import font
from core.api.grpc import core_pb2
from coretk.graph import tags
TEXT_DISTANCE = 0.30
@ -52,10 +53,20 @@ class LinkInfo:
f"{self.link.interface_two.ip6}/{self.link.interface_two.ip6mask}\n"
)
self.id1 = self.canvas.create_text(
x1, y1, text=label_one, justify=tk.CENTER, font=self.font, tags="linkinfo"
x1,
y1,
text=label_one,
justify=tk.CENTER,
font=self.font,
tags=tags.LINK_INFO,
)
self.id2 = self.canvas.create_text(
x2, y2, text=label_two, justify=tk.CENTER, font=self.font, tags="linkinfo"
x2,
y2,
text=label_two,
justify=tk.CENTER,
font=self.font,
tags=tags.LINK_INFO,
)
def recalculate_info(self):

View file

@ -7,6 +7,7 @@ from coretk.dialogs.emaneconfig import EmaneConfigDialog
from coretk.dialogs.mobilityconfig import MobilityConfigDialog
from coretk.dialogs.nodeconfig import NodeConfigDialog
from coretk.dialogs.wlanconfig import WlanConfigDialog
from coretk.graph import tags
from coretk.graph.enums import GraphMode
from coretk.graph.tooltip import CanvasTooltip
from coretk.nodeutils import NodeUtils
@ -23,7 +24,7 @@ class CanvasNode:
x = self.core_node.position.x
y = self.core_node.position.y
self.id = self.canvas.create_image(
x, y, anchor=tk.CENTER, image=self.image, tags="node"
x, y, anchor=tk.CENTER, image=self.image, tags=tags.NODE
)
image_box = self.canvas.bbox(self.id)
y = image_box[3] + NODE_TEXT_OFFSET
@ -32,7 +33,7 @@ class CanvasNode:
x,
y,
text=self.core_node.name,
tags="nodename",
tags=tags.NODE_NAME,
font=text_font,
fill="#0000CD",
)
@ -62,13 +63,12 @@ class CanvasNode:
def add_antenna(self):
x, y = self.canvas.coords(self.id)
offset = len(self.antennae) * 8
antenna_id = self.canvas.create_image(
x - 16 + offset,
y - 23,
anchor=tk.CENTER,
image=NodeUtils.ANTENNA_ICON,
tags="antenna",
tags=tags.ANTENNA,
)
self.antennae.append(antenna_id)

View file

@ -1,10 +1,9 @@
import logging
from coretk.dialogs.shapemod import ShapeDialog
from coretk.graph import tags
from coretk.graph.shapeutils import ShapeType
ABOVE_COMPONENT = ["gridline", "edge", "linkinfo", "antenna", "node", "nodename"]
class AnnotationData:
def __init__(
@ -70,7 +69,7 @@ class Shape:
self.y1,
self.x2,
self.y2,
tags="shape",
tags=tags.SHAPE,
dash=dash,
fill=self.shape_data.fill_color,
outline=self.shape_data.border_color,
@ -83,7 +82,7 @@ class Shape:
self.y1,
self.x2,
self.y2,
tags="shape",
tags=tags.SHAPE,
dash=dash,
fill=self.shape_data.fill_color,
outline=self.shape_data.border_color,
@ -95,7 +94,7 @@ class Shape:
self.id = self.canvas.create_text(
self.x1,
self.y1,
tags="shapetext",
tags=tags.SHAPE_TEXT,
text=self.shape_data.text,
fill=self.shape_data.text_color,
font=font,
@ -122,7 +121,7 @@ class Shape:
self.text_id = self.canvas.create_text(
x,
y,
tags="shapetext",
tags=tags.SHAPE_TEXT,
text=self.shape_data.text,
fill=self.shape_data.text_color,
font=font,
@ -132,7 +131,7 @@ class Shape:
self.canvas.coords(self.id, self.x1, self.y1, x1, y1)
def shape_complete(self, x, y):
for component in ABOVE_COMPONENT:
for component in tags.ABOVE_SHAPE:
self.canvas.tag_raise(component)
s = ShapeDialog(self.app, self.app, self)
s.show()

View file

@ -0,0 +1,35 @@
GRIDLINE = "gridline"
SHAPE = "shape"
SHAPE_TEXT = "shapetext"
EDGE = "edge"
LINK_INFO = "linkinfo"
WIRELESS_EDGE = "wireless"
ANTENNA = "antenna"
NODE_NAME = "nodename"
NODE = "node"
WALLPAPER = "wallpaper"
SELECTION = "selectednodes"
ABOVE_WALLPAPER_TAGS = [
GRIDLINE,
SHAPE,
SHAPE_TEXT,
EDGE,
LINK_INFO,
WIRELESS_EDGE,
ANTENNA,
NODE,
NODE_NAME,
]
ABOVE_SHAPE = [GRIDLINE, EDGE, LINK_INFO, WIRELESS_EDGE, ANTENNA, NODE, NODE_NAME]
COMPONENT_TAGS = [
EDGE,
NODE,
NODE_NAME,
WALLPAPER,
LINK_INFO,
ANTENNA,
WIRELESS_EDGE,
SELECTION,
SHAPE,
SHAPE_TEXT,
]

View file

@ -5,6 +5,7 @@ from functools import partial
from tkinter import ttk
from coretk.dialogs.customnodes import CustomNodesDialog
from coretk.graph import tags
from coretk.graph.enums import GraphMode
from coretk.graph.shapeutils import ShapeType
from coretk.images import ImageEnum, Images
@ -369,7 +370,7 @@ class Toolbar(ttk.Frame):
self.app.statusbar.progress_bar.start(5)
thread = threading.Thread(target=self.app.core.stop_session)
thread.start()
self.app.canvas.delete("wireless")
self.app.canvas.delete(tags.WIRELESS_EDGE)
self.design_frame.tkraise()
self.click_selection()