Skip to content

Commit

Permalink
fix and re-enable tree visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
metasemi committed Mar 27, 2023
1 parent bac4e73 commit 18d7ce5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 40 deletions.
35 changes: 17 additions & 18 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def build_menus(self):
"View": [
('Toggle side pane', 'Alt-P', None, no_junk_args(self.toggle_side)),
('Toggle bottom pane', 'Alt-B', None, no_junk_args(self.toggle_bottom)),
# ('Toggle visualize mode', 'J', None, no_junk_args(self.toggle_visualization_mode)),
('Toggle visualize mode', 'J', None, no_junk_args(self.toggle_visualization_mode)),
('Toggle children', 'Alt-C', None, no_junk_args(self.toggle_show_children)),
"-",
('Reset zoom', 'Ctrl-0', None, no_junk_args(self.reset_zoom)),
Expand Down Expand Up @@ -1448,16 +1448,15 @@ def open_in_transformer(self, inputs, template=None):

@metadata(name="Visualize", keys=["<Key-j>", "<Control-j>"], display_key="j")
def toggle_visualization_mode(self):
pass
# if self.state.preferences['autosave']:
# self.save_edits()
# self.display.set_mode("Visualize" if self.display.mode != "Visualize" else "Read")
# self.refresh_display()
#
# self.refresh_visualization()
# self.refresh_textbox()
# self.display.textbox.update_idletasks()
# self.center_view()
if self.state.preferences['autosave']:
self.save_edits()
self.display.set_mode("Visualize" if self.display.mode != "Visualize" else "Read")
self.refresh_display()

self.refresh_visualization()
self.refresh_textbox()
self.display.textbox.update_idletasks()
self.center_view()


# @metadata(name="Wavefunction", keys=[])
Expand Down Expand Up @@ -2318,18 +2317,18 @@ def refresh_alt_textbox(self, **kwargs):
def refresh_visualization(self, center=False, **kwargs):
if self.display.mode != "Visualize":
return
self.display.vis.redraw(self.state.root(), self.state.selected_node)
#self.display.vis.draw(self.state.tree_raw_data["root"], self.state.selected_node, center_on_selection=False)
# if center:
# #self.display.vis.center_view_on_canvas_coords(*self.display.vis.node_coords[self.state.selected_node_id])
# self.display.vis.center_view_on_node(self.state.selected_node)
# self.display.vis.redraw(self.state.root(), self.state.selected_node)
self.display.vis.draw(self.state.tree_raw_data["root"], self.state.selected_node, center_on_selection=False)
if center:
# self.display.vis.center_view_on_canvas_coords(*self.display.vis.node_coords[self.state.selected_node_id])
self.display.vis.center_view_on_node(self.state.selected_node)


def refresh_vis_selection(self, **kwargs):
if self.display.mode != "Visualize":
return
self.display.vis.redraw(self.state.root(), self.state.selected_node)
#self.display.vis.refresh_selection(self.state.tree_raw_data["root"], self.state.selected_node)
# self.display.vis.redraw(self.state.root(), self.state.selected_node)
self.display.vis.refresh_selection(self.state.tree_raw_data["root"], self.state.selected_node)
# TODO Without redrawing, the new open state won't be reflected
# self.display.vis.draw(self.state.tree_raw_data["root"], self.state.selected_node)
# self.display.vis.center_view_on_canvas_coords(*self.display.vis.node_coords[self.state.selected_node_id])
Expand Down
11 changes: 8 additions & 3 deletions view/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ def __init__(self):
def init_icon(self, icon_name, filename, size=16):
self.icons[icon_name] = {}
self.icons[icon_name]["size"] = size
self.icons[icon_name]["zsize"] = size
self.icons[icon_name]["img"] = PIL.Image.open(f"./static/icons/{filename}")
with open(f"./static/icons/{filename}", "rb") as f:
img = PIL.Image.open(io.BytesIO(f.read()))
self.icons[icon_name]["img"] = img
# self.icons[icon_name]["img"] = PIL.Image.open(f"./static/icons/{filename}")

def get_icon(self, icon_name, size=16):
if 'icon' not in self.icons[icon_name]:
self.icons[icon_name]['icon'] = PIL.ImageTk.PhotoImage(self.icons[icon_name]['img'].resize((size, size)))
# zsize: zoomed size, physical size of image to be cached and returned
def get_icon(self, icon_name, zsize=None):
if 'icon' not in self.icons[icon_name] or (zsize is not None and zsize != self.icons[icon_name]["zsize"]):
if zsize is None:
zsize = self.icons[icon_name]["zsize"]
self.icons[icon_name]['icon'] = PIL.ImageTk.PhotoImage(self.icons[icon_name]['img'].resize((zsize, zsize)))
self.icons[icon_name]["zsize"] = zsize
return self.icons[icon_name]['icon']

def init_icons(self):
Expand Down
46 changes: 27 additions & 19 deletions view/tree_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,23 @@ def zoomer(event):
self.canvas.scale("all", event.x, event.y, 0.9, 0.9)
self.canvas.configure(scrollregion=self.canvas.bbox("all"))#self.canvas_bbox_padding(self.canvas.bbox("all")))
self.fix_text_zoom()
self.icon_visibility_due_to_zoom()
self.fix_image_zoom()

# # linux zoom
def zoom_in(event):
self.scroll_ratio *= 1.1
self.canvas.scale("all", event.x, event.y, 1.1, 1.1)
self.canvas.configure(scrollregion=self.canvas.bbox("all"))#self.canvas_bbox_padding(self.canvas.bbox("all")))
self.fix_text_zoom()
self.icon_visibility_due_to_zoom()
self.fix_image_zoom()

def zoom_out(event):
self.scroll_ratio *= 0.9
self.canvas.scale("all", event.x, event.y, 0.9, 0.9)
self.canvas.configure(scrollregion=self.canvas.bbox("all"))#self.canvas_bbox_padding(self.canvas.bbox("all")))
# self.showtext = event.text > 0.8
self.fix_text_zoom()
self.icon_visibility_due_to_zoom()
self.fix_image_zoom()

# Mac and then linux scrolls
self.canvas.bind("<MouseWheel>", zoomer)
Expand Down Expand Up @@ -202,7 +202,7 @@ def fix_text_zoom(self):
width=self.get_width(item))


def icon_visibility_due_to_zoom(self):
def fix_image_zoom(self):
approx_size = math.floor(self.scroll_ratio * 18)
if approx_size < 12:
if not self.buttons_hidden:
Expand All @@ -215,6 +215,14 @@ def icon_visibility_due_to_zoom(self):
for item in self.canvas.find_withtag("image"):
self.canvas.itemconfigure(item, state='normal')

for icon in self.icons.icons:
new_size = math.floor(self.scroll_ratio * self.icons.icons[icon]["size"])
# self.old_icons.append(self.icons[icon]["icon"])
# self.icons.icons[icon]["icon"] = ImageTk.PhotoImage(self.icons.icons[icon]["img"].resize((new_size, new_size)))
_ = self.icons.get_icon(icon, new_size)
for resize_event in self.resize_icon_events:
resize_event()


# TODO save default widths (because some nodes have different widths)
def get_width(self, item):
Expand All @@ -224,7 +232,7 @@ def get_width(self, item):


def get_text_size(self):
return math.floor(self.state.visualization_settings['textsize'] * self.scroll_ratio)
return math.floor(self.state.visualization_settings['text_size'] * self.scroll_ratio)

#################################
# Drawing
Expand Down Expand Up @@ -372,21 +380,21 @@ def draw(self, root_node, selected_node, center_on_selection=False):

self.active = self.get_active()

self.compute_tree_coordinates(self.root, 100, 100, level=0)
self.center_about_ancestry(self.state.ancestry(self.selected_node))
self.draw_precomputed_tree(self.root)
# self.compute_tree_coordinates(self.root, 100, 100, level=0)
# self.center_about_ancestry(self.state.ancestry(self.selected_node))
# self.draw_precomputed_tree(self.root)

#self.draw_tree(self.root, 100, 100)
self.draw_tree(self.root, 100, 100)

# self.canvas.scale("all", 0, 0, self.scroll_ratio, self.scroll_ratio)
self.canvas.scale("all", 0, 0, self.scroll_ratio, self.scroll_ratio)

# region = self.canvas_bbox_padding(self.canvas.bbox("all"))
# self.canvas.configure(scrollregion=region)
# self.fix_text_zoom()
# self.icon_visibility_due_to_zoom()
region = self.canvas_bbox_padding(self.canvas.bbox("all"))
self.canvas.configure(scrollregion=region)
self.fix_text_zoom()
self.fix_image_zoom()

# if center_on_selection:
# self.center_view_on_node(self.selected_node)
if center_on_selection:
self.center_view_on_node(self.selected_node)


def refresh_selection(self, root_node, selected_node):
Expand Down Expand Up @@ -613,7 +621,7 @@ def draw_textbox(self, node, nodex, nodey):
if not self.state.visualization_settings["chapter_mode"]:
if node is not self.root:
self.draw_collapse_button(node, box)
if self.state.visualization_settings["showbuttons"]:
if self.state.visualization_settings["show_buttons"]:
self.draw_buttons(node, box)
self.draw_bookmark_star(node, box)
return box
Expand Down Expand Up @@ -697,7 +705,7 @@ def draw_icon(self, node, x_pos, y_pos, icon_name, name=None, method=None):
icon_id = self.canvas.create_image(x_pos, y_pos,
image=self.icons.get_icon(icon_name),
tags=[f'{name}-{node["id"]}', 'data', 'image'])
#self.resize_icon_events.append(lambda: self.canvas.itemconfig(icon_id, image=self.icons.get_icon(icon_name)))
self.resize_icon_events.append(lambda: self.canvas.itemconfig(icon_id, image=self.icons.get_icon(icon_name)))
self.canvas.tag_bind(
f'{name}-{node["id"]}', "<Button-1>", method)
return icon_id
Expand Down Expand Up @@ -1011,7 +1019,7 @@ def reset_zoom(self):
self.canvas.configure(scrollregion=self.canvas_bbox_padding(self.canvas.bbox("all")))
self.scroll_ratio = 1
self.fix_text_zoom()
self.icon_visibility_due_to_zoom()
self.fix_image_zoom()



Expand Down

0 comments on commit 18d7ce5

Please sign in to comment.