updated canvas wallpaper to scale with zoom level
This commit is contained in:
parent
0c61c6bffe
commit
737c14cc0f
1 changed files with 42 additions and 34 deletions
|
@ -12,7 +12,6 @@ from coretk.graph.linkinfo import LinkInfo, Throughput
|
||||||
from coretk.graph.node import CanvasNode
|
from coretk.graph.node import CanvasNode
|
||||||
from coretk.graph.shape import Shape
|
from coretk.graph.shape import Shape
|
||||||
from coretk.graph.shapeutils import is_draw_shape
|
from coretk.graph.shapeutils import is_draw_shape
|
||||||
from coretk.images import Images
|
|
||||||
from coretk.nodeutils import NodeUtils
|
from coretk.nodeutils import NodeUtils
|
||||||
|
|
||||||
SCROLL_BUFFER = 25
|
SCROLL_BUFFER = 25
|
||||||
|
@ -410,7 +409,8 @@ class CanvasGraph(tk.Canvas):
|
||||||
)
|
)
|
||||||
logging.info("ratio: %s", self.ratio)
|
logging.info("ratio: %s", self.ratio)
|
||||||
logging.info("offset: %s", self.offset)
|
logging.info("offset: %s", self.offset)
|
||||||
self.redraw_wallpaper()
|
if self.wallpaper:
|
||||||
|
self.redraw_wallpaper()
|
||||||
|
|
||||||
def click_press(self, event):
|
def click_press(self, event):
|
||||||
"""
|
"""
|
||||||
|
@ -422,7 +422,7 @@ class CanvasGraph(tk.Canvas):
|
||||||
x, y = self.canvas_xy(event)
|
x, y = self.canvas_xy(event)
|
||||||
self.cursor = x, y
|
self.cursor = x, y
|
||||||
selected = self.get_selected(event)
|
selected = self.get_selected(event)
|
||||||
logging.debug(f"click press: %s", selected)
|
logging.debug("click press: %s", selected)
|
||||||
is_node = selected in self.nodes
|
is_node = selected in self.nodes
|
||||||
if self.mode == GraphMode.EDGE and is_node:
|
if self.mode == GraphMode.EDGE and is_node:
|
||||||
x, y = self.coords(selected)
|
x, y = self.coords(selected)
|
||||||
|
@ -546,30 +546,41 @@ class CanvasGraph(tk.Canvas):
|
||||||
canvas_h = abs(y0 - y1)
|
canvas_h = abs(y0 - y1)
|
||||||
return canvas_w, canvas_h
|
return canvas_w, canvas_h
|
||||||
|
|
||||||
def draw_wallpaper(self, image):
|
def get_wallpaper_image(self):
|
||||||
x1, y1, x2, y2 = self.bbox(self.grid)
|
width = int(self.wallpaper.width * self.ratio)
|
||||||
x = (x1 + x2) / 2
|
height = int(self.wallpaper.height * self.ratio)
|
||||||
y = (y1 + y2) / 2
|
image = self.wallpaper.resize((width, height), Image.ANTIALIAS)
|
||||||
self.wallpaper_id = self.create_image(
|
return image
|
||||||
(x + 1, y + 1), image=image, tags=tags.WALLPAPER
|
|
||||||
)
|
def draw_wallpaper(self, image, x=None, y=None):
|
||||||
|
if x is None and y is None:
|
||||||
|
x1, y1, x2, y2 = self.bbox(self.grid)
|
||||||
|
x = (x1 + x2) / 2
|
||||||
|
y = (y1 + y2) / 2
|
||||||
|
|
||||||
|
self.wallpaper_id = self.create_image((x, y), image=image, tags=tags.WALLPAPER)
|
||||||
self.wallpaper_drawn = image
|
self.wallpaper_drawn = image
|
||||||
|
|
||||||
def wallpaper_upper_left(self):
|
def wallpaper_upper_left(self):
|
||||||
self.delete(self.wallpaper_id)
|
self.delete(self.wallpaper_id)
|
||||||
|
|
||||||
# place left corner of image to the left corner of the canvas
|
# create new scaled image, cropped if needed
|
||||||
tk_img = ImageTk.PhotoImage(self.wallpaper)
|
|
||||||
width, height = self.width_and_height()
|
width, height = self.width_and_height()
|
||||||
cropx = image_width = tk_img.width()
|
image = self.get_wallpaper_image()
|
||||||
cropy = image_height = tk_img.height()
|
cropx = image.width
|
||||||
if image_width > width:
|
cropy = image.height
|
||||||
cropx = width
|
if image.width > width:
|
||||||
if image_height > height:
|
cropx = image.width
|
||||||
cropy = height
|
if image.height > height:
|
||||||
cropped = self.wallpaper.crop((0, 0, cropx, cropy))
|
cropy = image.height
|
||||||
|
cropped = image.crop((0, 0, cropx, cropy))
|
||||||
image = ImageTk.PhotoImage(cropped)
|
image = ImageTk.PhotoImage(cropped)
|
||||||
self.draw_wallpaper(image)
|
|
||||||
|
# draw on canvas
|
||||||
|
x1, y1, _, _ = self.bbox(self.grid)
|
||||||
|
x = (cropx / 2) + x1
|
||||||
|
y = (cropy / 2) + y1
|
||||||
|
self.draw_wallpaper(image, x, y)
|
||||||
|
|
||||||
def wallpaper_center(self):
|
def wallpaper_center(self):
|
||||||
"""
|
"""
|
||||||
|
@ -580,21 +591,19 @@ class CanvasGraph(tk.Canvas):
|
||||||
self.delete(self.wallpaper_id)
|
self.delete(self.wallpaper_id)
|
||||||
|
|
||||||
# dimension of the cropped image
|
# dimension of the cropped image
|
||||||
tk_img = ImageTk.PhotoImage(self.wallpaper)
|
|
||||||
width, height = self.width_and_height()
|
width, height = self.width_and_height()
|
||||||
image_width = tk_img.width()
|
image = self.get_wallpaper_image()
|
||||||
image_height = tk_img.height()
|
|
||||||
cropx = 0
|
cropx = 0
|
||||||
if image_width > width:
|
if image.width > width:
|
||||||
cropx = (image_width - width) / 2
|
cropx = (image.width - width) / 2
|
||||||
cropy = 0
|
cropy = 0
|
||||||
if image_height > height:
|
if image.height > height:
|
||||||
cropy = (image_height - height) / 2
|
cropy = (image.height - height) / 2
|
||||||
x1 = 0 + cropx
|
x1 = 0 + cropx
|
||||||
y1 = 0 + cropy
|
y1 = 0 + cropy
|
||||||
x2 = image_width - cropx
|
x2 = image.width - cropx
|
||||||
y2 = image_height - cropy
|
y2 = image.height - cropy
|
||||||
cropped = self.wallpaper.crop((x1, y1, x2, y2))
|
cropped = image.crop((x1, y1, x2, y2))
|
||||||
image = ImageTk.PhotoImage(cropped)
|
image = ImageTk.PhotoImage(cropped)
|
||||||
self.draw_wallpaper(image)
|
self.draw_wallpaper(image)
|
||||||
|
|
||||||
|
@ -606,15 +615,14 @@ class CanvasGraph(tk.Canvas):
|
||||||
"""
|
"""
|
||||||
self.delete(self.wallpaper_id)
|
self.delete(self.wallpaper_id)
|
||||||
canvas_w, canvas_h = self.width_and_height()
|
canvas_w, canvas_h = self.width_and_height()
|
||||||
image = Images.create(self.wallpaper_file, int(canvas_w), int(canvas_h))
|
image = self.wallpaper.resize((int(canvas_w), int(canvas_h)), Image.ANTIALIAS)
|
||||||
|
image = ImageTk.PhotoImage(image)
|
||||||
self.draw_wallpaper(image)
|
self.draw_wallpaper(image)
|
||||||
|
|
||||||
def resize_to_wallpaper(self):
|
def resize_to_wallpaper(self):
|
||||||
self.delete(self.wallpaper_id)
|
self.delete(self.wallpaper_id)
|
||||||
image = ImageTk.PhotoImage(self.wallpaper)
|
image = ImageTk.PhotoImage(self.wallpaper)
|
||||||
image_width = image.width()
|
self.redraw_canvas(image.width(), image.height())
|
||||||
image_height = image.height()
|
|
||||||
self.redraw_canvas(image_width, image_height)
|
|
||||||
self.draw_wallpaper(image)
|
self.draw_wallpaper(image)
|
||||||
|
|
||||||
def redraw_canvas(self, width, height):
|
def redraw_canvas(self, width, height):
|
||||||
|
|
Loading…
Add table
Reference in a new issue