adjust open xml

This commit is contained in:
Huy Pham 2019-10-29 09:04:16 -07:00
parent 5829e3ae42
commit 2a32a5b1a2
13 changed files with 277 additions and 88 deletions

View file

@ -1,6 +1,7 @@
import logging import logging
import tkinter as tk import tkinter as tk
import coretk.appcache as appcache
import coretk.images as images import coretk.images as images
from coretk.coregrpc import CoreGrpc from coretk.coregrpc import CoreGrpc
from coretk.coremenubar import CoreMenubar from coretk.coremenubar import CoreMenubar
@ -13,6 +14,8 @@ from coretk.menuaction import MenuAction
class Application(tk.Frame): class Application(tk.Frame):
def __init__(self, master=None): def __init__(self, master=None):
super().__init__(master) super().__init__(master)
appcache.cache_variable(self)
print(self.is_open_xml)
self.load_images() self.load_images()
self.setup_app() self.setup_app()
self.menubar = None self.menubar = None
@ -51,7 +54,7 @@ class Application(tk.Frame):
def create_widgets(self): def create_widgets(self):
edit_frame = tk.Frame(self) edit_frame = tk.Frame(self)
edit_frame.pack(side=tk.LEFT, fill=tk.Y, ipadx=2, ipady=2) edit_frame.pack(side=tk.LEFT, fill=tk.Y, ipadx=2, ipady=2)
self.core_editbar = CoreToolbar(self.master, edit_frame, self.menubar) self.core_editbar = CoreToolbar(self, edit_frame, self.menubar)
self.core_editbar.create_toolbar() self.core_editbar.create_toolbar()
def draw_canvas(self): def draw_canvas(self):
@ -63,7 +66,7 @@ class Application(tk.Frame):
) )
self.canvas.pack(fill=tk.BOTH, expand=True) self.canvas.pack(fill=tk.BOTH, expand=True)
self.core_editbar.update_canvas(self.canvas) self.core_editbar.canvas = self.canvas
scroll_x = tk.Scrollbar( scroll_x = tk.Scrollbar(
self.canvas, orient=tk.HORIZONTAL, command=self.canvas.xview self.canvas, orient=tk.HORIZONTAL, command=self.canvas.xview

33
coretk/coretk/appcache.py Normal file
View file

@ -0,0 +1,33 @@
"""
stores some information helpful for setting starting values for some tables
like size and scale, set wallpaper, etc
"""
import tkinter as tk
def cache_variable(application):
# for menubar
application.is_open_xml = False
application.size_and_scale = None
application.set_wallpaper = None
# set wallpaper variables
# canvas id of the wallpaper
application.wallpaper_id = None
# current image for wallpaper
application.current_wallpaper = None
# wallpaper option
application.radiovar = tk.IntVar()
application.radiovar.set(1)
# show grid option
application.show_grid_var = tk.IntVar()
application.show_grid_var.set(1)
# adjust canvas to image dimension variable
application.adjust_to_dim_var = tk.IntVar()
application.adjust_to_dim_var.set(0)

View file

@ -190,7 +190,7 @@ class CoreGrpc:
logging.info("delete nodes %s", response) logging.info("delete nodes %s", response)
def delete_links(self, delete_session=None): def delete_links(self, delete_session=None):
sid = None # sid = None
if delete_session is None: if delete_session is None:
sid = self.session_id sid = self.session_id
else: else:
@ -228,7 +228,6 @@ class CoreGrpc:
ip4=interface.ipv4, ip4=interface.ipv4,
ip4mask=interface.ip4prefix, ip4mask=interface.ip4prefix,
) )
# if1 = core_pb2.Interface(id=id1, name=edge.interface_1.name, ip4=edge.interface_1.ipv4, ip4mask=edge.interface_1.ip4prefix)
logging.debug("create interface 1 %s", if1) logging.debug("create interface 1 %s", if1)
# interface1 = self.interface_helper.create_interface(id1, 0) # interface1 = self.interface_helper.create_interface(id1, 0)
@ -241,15 +240,12 @@ class CoreGrpc:
ip4=interface.ipv4, ip4=interface.ipv4,
ip4mask=interface.ip4prefix, ip4mask=interface.ip4prefix,
) )
# if2 = core_pb2.Interface(id=id2, name=edge.interface_2.name, ip4=edge.interface_2.ipv4, ip4mask=edge.interface_2.ip4prefix)
logging.debug("create interface 2: %s", if2) logging.debug("create interface 2: %s", if2)
# interface2 = self.interface_helper.create_interface(id2, 0)
# response = self.core.add_link(self.session_id, id1, id2, interface1, interface2)
response = self.core.add_link(self.session_id, id1, id2, if1, if2) response = self.core.add_link(self.session_id, id1, id2, if1, if2)
logging.info("created link: %s", response) logging.info("created link: %s", response)
self.core.get_node_links(self.session_id, id1) # self.core.get_node_links(self.session_id, id1)
# def get_session(self): # def get_session(self):
# response = self.core.get_session(self.session_id) # response = self.core.get_session(self.session_id)
@ -283,10 +279,7 @@ class CoreGrpc:
""" """
response = self.core.open_xml(file_path) response = self.core.open_xml(file_path)
self.session_id = response.session_id self.session_id = response.session_id
# print("Sessionz") logging.debug("coreprgc.py open_xml(): %s", response.result)
# self.core.events(self.session_id, self.log_event)
# return response.session_id
# logging.info("coregrpc.py open_xml()", type(response))
def close(self): def close(self):
""" """

View file

@ -66,13 +66,11 @@ class CoreMenubar(object):
underline=0, underline=0,
) )
file_menu.add_command(label="Reload", command=action.file_reload, underline=0) file_menu.add_command(label="Reload", command=action.file_reload, underline=0)
file_menu.add_command( # file_menu.add_command(
label="Save", command=action.file_save, accelerator="Ctrl+S", underline=0 # label="Save", command=action.file_save, accelerator="Ctrl+S", underline=0
) # )
# file_menu.add_command(label="Save As XML...", command=action.file_save_as_xml) # file_menu.add_command(label="Save As XML...", command=action.file_save_as_xml)
file_menu.add_command( file_menu.add_command(label="Save", command=self.menu_action.file_save_as_xml)
label="Save As XML...", command=self.menu_action.file_save_as_xml
)
file_menu.add_separator() file_menu.add_separator()

View file

@ -3,12 +3,11 @@ import tkinter as tk
from enum import Enum from enum import Enum
from core.api.grpc import core_pb2 from core.api.grpc import core_pb2
from coretk.coretoolbarhelp import CoreToolbarHelp
from coretk.graph import GraphMode from coretk.graph import GraphMode
from coretk.images import ImageEnum, Images from coretk.images import ImageEnum, Images
from coretk.tooltip import CreateToolTip from coretk.tooltip import CreateToolTip
# from coretk.graph_helper import WlanConnection
class SessionStateEnum(Enum): class SessionStateEnum(Enum):
NONE = "none" NONE = "none"
@ -25,13 +24,14 @@ class CoreToolbar(object):
Core toolbar class Core toolbar class
""" """
def __init__(self, master, edit_frame, menubar): def __init__(self, application, edit_frame, menubar):
""" """
Create a CoreToolbar instance Create a CoreToolbar instance
:param tkinter.Frame edit_frame: edit frame :param tkinter.Frame edit_frame: edit frame
""" """
self.master = master self.application = application
self.master = application.master
self.edit_frame = edit_frame self.edit_frame = edit_frame
self.menubar = menubar self.menubar = menubar
self.radio_value = tk.IntVar() self.radio_value = tk.IntVar()
@ -50,15 +50,6 @@ class CoreToolbar(object):
self.canvas = None self.canvas = None
def update_canvas(self, canvas):
"""
Update canvas variable in CoreToolbar class
:param tkinter.Canvas canvas: core canvas
:return: nothing
"""
self.canvas = canvas
def destroy_previous_frame(self): def destroy_previous_frame(self):
""" """
Destroy any extra frame from previous before drawing a new one Destroy any extra frame from previous before drawing a new one
@ -169,6 +160,7 @@ class CoreToolbar(object):
:return: nothing :return: nothing
""" """
logging.debug("Click START STOP SESSION button") logging.debug("Click START STOP SESSION button")
helper = CoreToolbarHelp(self.application)
# self.destroy_children_widgets(self.edit_frame) # self.destroy_children_widgets(self.edit_frame)
self.destroy_children_widgets() self.destroy_children_widgets()
self.canvas.mode = GraphMode.SELECT self.canvas.mode = GraphMode.SELECT
@ -176,24 +168,29 @@ class CoreToolbar(object):
# set configuration state # set configuration state
state = self.canvas.core_grpc.get_session_state() state = self.canvas.core_grpc.get_session_state()
if state == core_pb2.SessionState.SHUTDOWN: if state == core_pb2.SessionState.SHUTDOWN or self.application.is_open_xml:
self.canvas.core_grpc.set_session_state(SessionStateEnum.DEFINITION.value) self.canvas.core_grpc.set_session_state(SessionStateEnum.DEFINITION.value)
self.application.is_open_xml = False
self.canvas.core_grpc.set_session_state(SessionStateEnum.CONFIGURATION.value) self.canvas.core_grpc.set_session_state(SessionStateEnum.CONFIGURATION.value)
for node in self.canvas.grpc_manager.nodes.values(): helper.add_nodes()
self.canvas.core_grpc.add_node( helper.add_edges()
node.type, node.model, int(node.x), int(node.y), node.name, node.node_id # for node in self.canvas.grpc_manager.nodes.values():
) # print(node.type, node.model, int(node.x), int(node.y), node.name, node.node_id)
# self.canvas.core_grpc.add_node(
for edge in self.canvas.grpc_manager.edges.values(): # node.type, node.model, int(node.x), int(node.y), node.name, node.node_id
self.canvas.core_grpc.add_link( # )
edge.id1, edge.id2, edge.type1, edge.type2, edge
)
# print(len(self.canvas.grpc_manager.edges))
# for edge in self.canvas.grpc_manager.edges.values():
# print(edge.id1, edge.id2, edge.type1, edge.type2)
# self.canvas.core_grpc.add_link(
# edge.id1, edge.id2, edge.type1, edge.type2, edge
# )
self.canvas.core_grpc.set_session_state(SessionStateEnum.INSTANTIATION.value) self.canvas.core_grpc.set_session_state(SessionStateEnum.INSTANTIATION.value)
# self.canvas.core_grpc.get_session() # self.canvas.core_grpc.get_session()
# self.application.is_open_xml = False
self.create_runtime_toolbar() self.create_runtime_toolbar()
def click_link_tool(self): def click_link_tool(self):

View file

@ -0,0 +1,31 @@
"""
CoreToolbar help to draw on canvas, and make grpc client call
"""
class CoreToolbarHelp:
def __init__(self, application):
self.application = application
self.core_grpc = application.core_grpc
def add_nodes(self):
"""
add the nodes stored in grpc manager
:return: nothing
"""
grpc_manager = self.application.canvas.grpc_manager
for node in grpc_manager.nodes.values():
self.application.core_grpc.add_node(
node.type, node.model, int(node.x), int(node.y), node.name, node.node_id
)
def add_edges(self):
"""
add the edges stored in grpc manager
:return:
"""
grpc_manager = self.application.canvas.grpc_manager
for edge in grpc_manager.edges.values():
self.application.core_grpc.add_link(
edge.id1, edge.id2, edge.type1, edge.type2, edge
)

View file

@ -43,7 +43,7 @@ class CanvasGraph(tk.Canvas):
self.core_grpc = grpc self.core_grpc = grpc
self.grpc_manager = GrpcManager(grpc) self.grpc_manager = GrpcManager(grpc)
self.helper = GraphHelper(self) self.helper = GraphHelper(self, grpc)
# self.core_id_to_canvas_id = {} # self.core_id_to_canvas_id = {}
# self.core_map = CoreToCanvasMapping() # self.core_map = CoreToCanvasMapping()
# self.draw_existing_component() # self.draw_existing_component()
@ -61,7 +61,8 @@ class CanvasGraph(tk.Canvas):
:return: :return:
""" """
# delete any existing drawn items # delete any existing drawn items
self.delete_components() # self.delete_components()
self.helper.delete_canvas_components()
# set the private variables to default value # set the private variables to default value
self.mode = GraphMode.SELECT self.mode = GraphMode.SELECT
@ -73,12 +74,14 @@ class CanvasGraph(tk.Canvas):
self.edges = {} self.edges = {}
self.drawing_edge = None self.drawing_edge = None
print("graph.py create a new grpc manager")
self.grpc_manager = GrpcManager(new_grpc) self.grpc_manager = GrpcManager(new_grpc)
# new grpc # new grpc
self.core_grpc = new_grpc self.core_grpc = new_grpc
print("grpah.py draw existing component")
self.draw_existing_component() self.draw_existing_component()
print(self.grpc_manager.edges)
def setup_bindings(self): def setup_bindings(self):
""" """
@ -130,6 +133,8 @@ class CanvasGraph(tk.Canvas):
for node in session.nodes: for node in session.nodes:
# peer to peer node is not drawn on the GUI # peer to peer node is not drawn on the GUI
if node.type != core_pb2.NodeType.PEER_TO_PEER: if node.type != core_pb2.NodeType.PEER_TO_PEER:
# draw nodes on the canvas
image, name = Images.convert_type_and_model_to_image( image, name = Images.convert_type_and_model_to_image(
node.type, node.model node.type, node.model
) )
@ -138,7 +143,10 @@ class CanvasGraph(tk.Canvas):
) )
self.nodes[n.id] = n self.nodes[n.id] = n
core_id_to_canvas_id[node.id] = n.id core_id_to_canvas_id[node.id] = n.id
# store the node in grpc manager
self.grpc_manager.add_preexisting_node(n, session_id, node, name) self.grpc_manager.add_preexisting_node(n, session_id, node, name)
self.grpc_manager.update_reusable_id() self.grpc_manager.update_reusable_id()
# draw existing links # draw existing links
@ -165,6 +173,9 @@ class CanvasGraph(tk.Canvas):
self, self,
is_wired=False, is_wired=False,
) )
edge_token = tuple(sorted((n1.id, n2.id)))
e.token = edge_token
e.dst = n2.id
n1.edges.add(e) n1.edges.add(e)
n2.edges.add(e) n2.edges.add(e)
self.edges[e.token] = e self.edges[e.token] = e
@ -195,8 +206,8 @@ class CanvasGraph(tk.Canvas):
) )
# TODO will include throughput and ipv6 in the future # TODO will include throughput and ipv6 in the future
if1 = Interface(grpc_if1.name, grpc_if1.ip4) if1 = Interface(grpc_if1.name, grpc_if1.ip4, ifid=grpc_if1.id)
if2 = Interface(grpc_if2.name, grpc_if2.ip4) if2 = Interface(grpc_if2.name, grpc_if2.ip4, ifid=grpc_if2.id)
self.grpc_manager.edges[e.token].interface_1 = if1 self.grpc_manager.edges[e.token].interface_1 = if1
self.grpc_manager.edges[e.token].interface_2 = if2 self.grpc_manager.edges[e.token].interface_2 = if2
self.grpc_manager.nodes[ self.grpc_manager.nodes[
@ -207,14 +218,16 @@ class CanvasGraph(tk.Canvas):
].interfaces.append(if2) ].interfaces.append(if2)
# lift the nodes so they on top of the links # lift the nodes so they on top of the links
for i in core_id_to_canvas_id.values(): # for i in core_id_to_canvas_id.values():
# self.lift(i)
for i in self.find_withtag("node"):
self.lift(i) self.lift(i)
def delete_components(self): # def delete_components(self):
tags = ["node", "edge", "linkinfo", "nodename"] # tags = ["node", "edge", "linkinfo", "nodename"]
for i in tags: # for i in tags:
for id in self.find_withtag(i): # for id in self.find_withtag(i):
self.delete(id) # self.delete(id)
def canvas_xy(self, event): def canvas_xy(self, event):
""" """
@ -237,7 +250,6 @@ class CanvasGraph(tk.Canvas):
:return: the item that the mouse point to :return: the item that the mouse point to
""" """
overlapping = self.find_overlapping(event.x, event.y, event.x, event.y) overlapping = self.find_overlapping(event.x, event.y, event.x, event.y)
print(overlapping)
nodes = set(self.find_withtag("node")) nodes = set(self.find_withtag("node"))
selected = None selected = None
for _id in overlapping: for _id in overlapping:
@ -263,7 +275,6 @@ class CanvasGraph(tk.Canvas):
self.focus_set() self.focus_set()
self.selected = self.get_selected(event) self.selected = self.get_selected(event)
logging.debug(f"click release selected: {self.selected}") logging.debug(f"click release selected: {self.selected}")
print(self.mode)
if self.mode == GraphMode.EDGE: if self.mode == GraphMode.EDGE:
self.handle_edge_release(event) self.handle_edge_release(event)
elif self.mode == GraphMode.NODE: elif self.mode == GraphMode.NODE:

View file

@ -7,13 +7,26 @@ import tkinter as tk
from core.api.grpc import core_pb2 from core.api.grpc import core_pb2
from coretk.images import ImageEnum, Images from coretk.images import ImageEnum, Images
CANVAS_COMPONENT_TAGS = ["edge", "node", "nodename", "wallpaper", "linkinfo"]
class GraphHelper: class GraphHelper:
def __init__(self, canvas): def __init__(self, canvas, grpc):
""" """
create an instance of GraphHelper object create an instance of GraphHelper object
""" """
self.canvas = canvas self.canvas = canvas
self.core_grpc = grpc
def delete_canvas_components(self):
"""
delete the components of the graph leaving only the blank canvas
:return: nothing
"""
for tag in CANVAS_COMPONENT_TAGS:
for i in self.canvas.find_withtag(tag):
self.canvas.delete(i)
def draw_wireless_case(self, src_id, dst_id, edge): def draw_wireless_case(self, src_id, dst_id, edge):
src_node_name = self.canvas.nodes[src_id].node_type src_node_name = self.canvas.nodes[src_id].node_type

View file

@ -160,13 +160,17 @@ class GrpcManager:
""" """
Add preexisting nodes to grpc manager Add preexisting nodes to grpc manager
:param str name: node_type
:param core_pb2.Node core_node: core node grpc message :param core_pb2.Node core_node: core node grpc message
:param coretk.graph.CanvasNode canvas_node: canvas node :param coretk.graph.CanvasNode canvas_node: canvas node
:param int session_id: session id :param int session_id: session id
:return: nothing :return: nothing
""" """
# update the next available id
core_id = core_node.id core_id = core_node.id
if core_id >= self.id: print(core_id)
if self.id is None or core_id >= self.id:
self.id = core_id + 1 self.id = core_id + 1
self.preexisting.append(core_id) self.preexisting.append(core_id)
n = Node( n = Node(
@ -198,12 +202,13 @@ class GrpcManager:
:return: nothing :return: nothing
""" """
for i in range(1, self.id): if len(self.preexisting) > 0:
if i not in self.preexisting: for i in range(1, self.id):
self.reusable.append(i) if i not in self.preexisting:
self.reusable.append(i)
self.preexisting.clear() self.preexisting.clear()
logging.debug("Next id: %s, Reusable: %s", self.id, self.reusable) logging.debug("Next id: %s, Reusable: %s", self.id, self.reusable)
def delete_node(self, canvas_id): def delete_node(self, canvas_id):
""" """
@ -263,8 +268,6 @@ class GrpcManager:
edge.interface_1 = src_interface edge.interface_1 = src_interface
edge.interface_2 = dst_interface edge.interface_2 = dst_interface
print(src_interface)
print(dst_interface)
return src_interface, dst_interface return src_interface, dst_interface
def add_edge(self, session_id, token, canvas_id_1, canvas_id_2): def add_edge(self, session_id, token, canvas_id_1, canvas_id_2):

View file

@ -37,8 +37,8 @@ def file_reload():
logging.debug("Click file Reload") logging.debug("Click file Reload")
def file_save(): # def file_save():
logging.debug("Click file save") # logging.debug("Click file save")
def file_save_shortcut(event): def file_save_shortcut(event):
@ -390,6 +390,7 @@ class MenuAction:
def file_open_xml(self): def file_open_xml(self):
logging.info("menuaction.py file_open_xml()") logging.info("menuaction.py file_open_xml()")
self.application.is_open_xml = True
file_path = filedialog.askopenfilename( file_path = filedialog.askopenfilename(
initialdir=SAVEDIR, initialdir=SAVEDIR,
title="Open", title="Open",
@ -413,16 +414,19 @@ class MenuAction:
self.application.core_grpc = core_grpc self.application.core_grpc = core_grpc
self.application.core_editbar.destroy_children_widgets() self.application.core_editbar.destroy_children_widgets()
self.application.core_editbar.create_runtime_toolbar() self.application.core_editbar.create_toolbar()
# self.application.is_open_xml = False
# self.application.core_editbar.create_runtime_toolbar()
# self.application.canvas.draw_existing_component() # self.application.canvas.draw_existing_component()
# t1 = time.clock() # t1 = time.clock()
# print(t1 - t0) # print(t1 - t0)
def canvas_size_and_scale(self): def canvas_size_and_scale(self):
SizeAndScale(self.application) self.application.size_and_scale = SizeAndScale(self.application)
def canvas_set_wallpaper(self): def canvas_set_wallpaper(self):
CanvasWallpaper(self.application) self.application.set_wallpaper = CanvasWallpaper(self.application)
def help_core_github(self): def help_core_github(self):
webbrowser.open_new("https://github.com/coreemu/core") webbrowser.open_new("https://github.com/coreemu/core")

View file

@ -1,2 +0,0 @@
/home/ncs/Desktop/runningtest.xml
/home/ncs/Desktop/notrunning.xml

View file

@ -14,6 +14,7 @@ WALLPAPER_DIR = os.path.join(PATH, "wallpaper")
class ScaleOption(enum.Enum): class ScaleOption(enum.Enum):
NONE = 0
UPPER_LEFT = 1 UPPER_LEFT = 1
CENTERED = 2 CENTERED = 2
SCALED = 3 SCALED = 3
@ -22,15 +23,23 @@ class ScaleOption(enum.Enum):
class CanvasWallpaper: class CanvasWallpaper:
def __init__(self, application): def __init__(self, application):
"""
create an instance of CanvasWallpaper object
:param coretk.app.Application application: root application
"""
self.application = application self.application = application
self.canvas = self.application.canvas self.canvas = self.application.canvas
self.top = tk.Toplevel() self.top = tk.Toplevel()
self.top.title("Set Canvas Wallpaper") self.top.title("Set Canvas Wallpaper")
self.radiovar = tk.IntVar() self.radiovar = tk.IntVar()
print(self.application.radiovar.get())
self.radiovar.set(self.application.radiovar.get())
self.show_grid_var = tk.IntVar() self.show_grid_var = tk.IntVar()
self.show_grid_var.set(self.application.show_grid_var.get())
self.adjust_to_dim_var = tk.IntVar() self.adjust_to_dim_var = tk.IntVar()
self.wallpaper = None self.adjust_to_dim_var.set(self.application.adjust_to_dim_var.get())
self.create_image_label() self.create_image_label()
self.create_text_label() self.create_text_label()
@ -75,6 +84,11 @@ class CanvasWallpaper:
img_label.image = tk_img img_label.image = tk_img
def clear_link(self): def clear_link(self):
"""
delete like shown in image link entry if there is any
:return: nothing
"""
# delete entry # delete entry
img_open_frame = self.top.grid_slaves(2, 0)[0] img_open_frame = self.top.grid_slaves(2, 0)[0]
filename_entry = img_open_frame.grid_slaves(0, 0)[0] filename_entry = img_open_frame.grid_slaves(0, 0)[0]
@ -115,10 +129,28 @@ class CanvasWallpaper:
b4 = tk.Radiobutton(f, text="titled", value=4, variable=self.radiovar) b4 = tk.Radiobutton(f, text="titled", value=4, variable=self.radiovar)
b4.grid(row=0, column=3) b4.grid(row=0, column=3)
self.radiovar.set(1) # self.radiovar.set(1)
f.grid() f.grid()
def adjust_canvas_size(self):
# deselect all radio buttons and grey them out
if self.adjust_to_dim_var.get() == 1:
self.radiovar.set(0)
option_frame = self.top.grid_slaves(3, 0)[0]
for i in option_frame.grid_slaves():
i.config(state=tk.DISABLED)
# turn back the radio button to active state so that user can choose again
elif self.adjust_to_dim_var.get() == 0:
option_frame = self.top.grid_slaves(3, 0)[0]
for i in option_frame.grid_slaves():
i.config(state=tk.NORMAL)
self.radiovar.set(1)
else:
logging.error("setwallpaper.py adjust_canvas_size invalid value")
def additional_options(self): def additional_options(self):
b = tk.Checkbutton(self.top, text="Show grid", variable=self.show_grid_var) b = tk.Checkbutton(self.top, text="Show grid", variable=self.show_grid_var)
b.grid(sticky=tk.W, padx=5) b.grid(sticky=tk.W, padx=5)
@ -126,15 +158,21 @@ class CanvasWallpaper:
self.top, self.top,
text="Adjust canvas size to image dimensions", text="Adjust canvas size to image dimensions",
variable=self.adjust_to_dim_var, variable=self.adjust_to_dim_var,
command=self.adjust_canvas_size,
) )
b.grid(sticky=tk.W, padx=5) b.grid(sticky=tk.W, padx=5)
self.show_grid_var.set(1) self.show_grid_var.set(1)
self.adjust_to_dim_var.set(0) self.adjust_to_dim_var.set(0)
def delete_previous_wallpaper(self): def delete_canvas_components(self, tag_list):
prev_wallpaper = self.canvas.find_withtag("wallpaper") """
if prev_wallpaper: delete canvas items whose tag is in the tag list
for i in prev_wallpaper:
:param list[string] tag_list: list of tags
:return: nothing
"""
for tag in tag_list:
for i in self.canvas.find_withtag(tag):
self.canvas.delete(i) self.canvas.delete(i)
def get_canvas_width_and_height(self): def get_canvas_width_and_height(self):
@ -159,6 +197,7 @@ class CanvasWallpaper:
return return
def upper_left(self, img): def upper_left(self, img):
print("upperleft")
tk_img = ImageTk.PhotoImage(img) tk_img = ImageTk.PhotoImage(img)
# crop image if it is bigger than canvas # crop image if it is bigger than canvas
@ -178,7 +217,8 @@ class CanvasWallpaper:
# place left corner of image to the left corner of the canvas # place left corner of image to the left corner of the canvas
self.application.croppedwallpaper = cropped_tk self.application.croppedwallpaper = cropped_tk
self.delete_previous_wallpaper() self.delete_canvas_components(["wallpaper"])
# self.delete_previous_wallpaper()
wid = self.canvas.create_image( wid = self.canvas.create_image(
(cropx / 2, cropy / 2), image=cropped_tk, tags="wallpaper" (cropx / 2, cropy / 2), image=cropped_tk, tags="wallpaper"
@ -213,7 +253,8 @@ class CanvasWallpaper:
# place the center of the image at the center of the canvas # place the center of the image at the center of the canvas
self.application.croppedwallpaper = cropped_tk self.application.croppedwallpaper = cropped_tk
self.delete_previous_wallpaper() self.delete_canvas_components(["wallpaper"])
# self.delete_previous_wallpaper()
wid = self.canvas.create_image( wid = self.canvas.create_image(
(canvas_w / 2, canvas_h / 2), image=cropped_tk, tags="wallpaper" (canvas_w / 2, canvas_h / 2), image=cropped_tk, tags="wallpaper"
) )
@ -231,7 +272,8 @@ class CanvasWallpaper:
image_tk = ImageTk.PhotoImage(resized_image) image_tk = ImageTk.PhotoImage(resized_image)
self.application.croppedwallpaper = image_tk self.application.croppedwallpaper = image_tk
self.delete_previous_wallpaper() self.delete_canvas_components(["wallpaper"])
# self.delete_previous_wallpaper()
wid = self.canvas.create_image( wid = self.canvas.create_image(
(canvas_w / 2, canvas_h / 2), image=image_tk, tags="wallpaper" (canvas_w / 2, canvas_h / 2), image=image_tk, tags="wallpaper"
@ -241,11 +283,34 @@ class CanvasWallpaper:
def tiled(self, img): def tiled(self, img):
return return
def draw_new_canvas(self, canvas_width, canvas_height):
"""
delete the old canvas and draw a new one
:param int canvas_width: canvas width in pixel
:param int canvas_height: canvas height in pixel
:return:
"""
self.delete_canvas_components(["rectangle", "gridline"])
self.canvas.draw_grid(canvas_width, canvas_height)
def canvas_to_image_dimension(self, img):
image_tk = ImageTk.PhotoImage(img)
img_w = image_tk.width()
img_h = image_tk.height()
self.delete_canvas_components(["wallpaper"])
self.draw_new_canvas(img_w, img_h)
wid = self.canvas.create_image((img_w / 2, img_h / 2), image=image_tk)
self.application.croppedwallpaper = image_tk
self.application.wallpaper_id = wid
def show_grid(self): def show_grid(self):
""" """
:return: nothing :return: nothing
""" """
self.application.adjust_to_dim_var.set(self.adjust_to_dim_var.get())
if self.show_grid_var.get() == 0: if self.show_grid_var.get() == 0:
for i in self.canvas.find_withtag("gridline"): for i in self.canvas.find_withtag("gridline"):
self.canvas.itemconfig(i, state=tk.HIDDEN) self.canvas.itemconfig(i, state=tk.HIDDEN)
@ -256,28 +321,46 @@ class CanvasWallpaper:
else: else:
logging.error("setwallpaper.py show_grid invalid value") logging.error("setwallpaper.py show_grid invalid value")
def save_wallpaper_options(self):
self.application.radiovar.set(self.radiovar.get())
self.application.show_grid_var.set(self.show_grid_var.get())
self.application.adjust_to_dim_var.set(self.adjust_to_dim_var.get())
def click_apply(self): def click_apply(self):
img_link_frame = self.top.grid_slaves(2, 0)[0] img_link_frame = self.top.grid_slaves(2, 0)[0]
filename = img_link_frame.grid_slaves(0, 0)[0].get() filename = img_link_frame.grid_slaves(0, 0)[0].get()
if not filename: if not filename:
self.delete_canvas_components(["wallpaper"])
self.top.destroy() self.top.destroy()
self.application.current_wallpaper = None
self.save_wallpaper_options()
return return
try: try:
img = Image.open(filename) img = Image.open(filename)
self.application.current_wallpaper = img
except FileNotFoundError: except FileNotFoundError:
print("invalid filename, draw original white plot") print("invalid filename, draw original white plot")
if self.application.wallpaper_id: if self.application.wallpaper_id:
self.canvas.delete(self.application.wallpaper_id) self.canvas.delete(self.application.wallpaper_id)
self.top.destroy() self.top.destroy()
return return
if self.radiovar.get() == ScaleOption.UPPER_LEFT.value:
self.upper_left(img) self.application.adjust_to_dim_var.set(self.adjust_to_dim_var.get())
elif self.radiovar.get() == ScaleOption.CENTERED.value: if self.adjust_to_dim_var.get() == 0:
self.center(img)
elif self.radiovar.get() == ScaleOption.SCALED.value: self.application.radiovar.set(self.radiovar.get())
self.scaled(img)
elif self.radiovar.get() == ScaleOption.TILED.value: if self.radiovar.get() == ScaleOption.UPPER_LEFT.value:
print("not implemented yet") self.upper_left(img)
elif self.radiovar.get() == ScaleOption.CENTERED.value:
self.center(img)
elif self.radiovar.get() == ScaleOption.SCALED.value:
self.scaled(img)
elif self.radiovar.get() == ScaleOption.TILED.value:
print("not implemented yet")
elif self.adjust_to_dim_var.get() == 1:
self.canvas_to_image_dimension(img)
self.show_grid() self.show_grid()
self.top.destroy() self.top.destroy()

View file

@ -4,6 +4,8 @@ size and scale
import tkinter as tk import tkinter as tk
from functools import partial from functools import partial
from coretk.setwallpaper import ScaleOption
DRAW_OBJECT_TAGS = ["edge", "node", "nodename", "linkinfo", "antenna"] DRAW_OBJECT_TAGS = ["edge", "node", "nodename", "linkinfo", "antenna"]
@ -246,6 +248,26 @@ class SizeAndScale:
meter_per_pixel = float(scale_frame.grid_slaves(0, 1)[0].get()) / 100 meter_per_pixel = float(scale_frame.grid_slaves(0, 1)[0].get()) / 100
self.application.canvas.meters_per_pixel = meter_per_pixel self.application.canvas.meters_per_pixel = meter_per_pixel
self.redraw_grid(pixel_width, pixel_height) self.redraw_grid(pixel_width, pixel_height)
print(self.application.current_wallpaper)
print(self.application.radiovar)
# if there is a current wallpaper showing, redraw it based on current wallpaper options
wallpaper_tool = self.application.set_wallpaper
current_wallpaper = self.application.current_wallpaper
if current_wallpaper:
if self.application.adjust_to_dim_var.get() == 0:
if self.application.radiovar.get() == ScaleOption.UPPER_LEFT.value:
wallpaper_tool.upper_left(current_wallpaper)
elif self.application.radiovar.get() == ScaleOption.CENTERED.value:
wallpaper_tool.center(current_wallpaper)
elif self.application.radiovar.get() == ScaleOption.SCALED.value:
wallpaper_tool.scaled(current_wallpaper)
elif self.application.radiovar.get() == ScaleOption.TILED.value:
print("not implemented")
elif self.application.adjust_to_dim_var.get() == 1:
wallpaper_tool.canvas_to_image_dimension(current_wallpaper)
wallpaper_tool.show_grid()
self.top.destroy() self.top.destroy()
def apply_cancel(self): def apply_cancel(self):