Skip to content

Commit

Permalink
click text to edit history
Browse files Browse the repository at this point in the history
  • Loading branch information
socketteer committed Mar 4, 2021
1 parent 9cef963 commit bae2fc0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ooo what features! wow so cool

Open: `o`, `Control-o`

Import JSON as subtree: `Control-Shift-KeyPress-O`
Import JSON as subtree: `Control-Shift-O`

Save: `s`, `Control-s`

Expand Down
4 changes: 0 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ global edit mode, where all nodes turn into textboxes, but no zooming?
* toggle gray history
* highlight context window (toggle)
* gradient color for text box history
* rename node for nav tree
* sidebar adjustable
* display hotkeys window
* scroll at top of chapter by default and hotkey to go to top of chapter
* change darkmode in program

Expand Down Expand Up @@ -82,7 +79,6 @@ global edit mode, where all nodes turn into textboxes, but no zooming?
* global or associated with subtree
* bookmark-like tags define subsets of tree (and option to only display/navigate tag)
* save open status (not visible status) in tree dict
* click on textbox to edit history
* named bookmarks
* separate bookmarks (unique) and tags (category)
* open non-root node of json as root node
Expand Down
44 changes: 39 additions & 5 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

import PIL
import pyperclip
import bisect

from view.colors import history_color, not_visited_color, visited_color
from view.colors import history_color, not_visited_color, visited_color, darkmode
from view.display import Display
from view.dialogs import GenerationSettingsDialog, InfoDialog, VisualizationSettingsDialog, \
NodeChapterDialog, MultimediaDialog
from model import TreeModel
from util.util import clip_num, metadata
from util.util_tree import depth, height, flatten_tree, stochastic_transition
from util.util_tree import depth, height, flatten_tree, stochastic_transition, node_ancestry


def gated_call(f, condition):
def _gated_call(*args, _f=f, _cond=condition, **kwargs):
Expand All @@ -43,6 +45,8 @@ def __init__(self, root):
self.register_model_callbacks()
self.setup_key_bindings()
self.build_menus()
self.ancestor_end_indices = None
self.remember_node_id = None


#################################
Expand Down Expand Up @@ -222,7 +226,6 @@ def nav_select(self, *, node_id):
# node["open"] = self.display.nav_tree.item(node["id"], "open")
self.state.select_node(node_id)


@metadata(name="Bookmark", keys=["<Key-b>", "<Control-b>"], display_key="b")
def bookmark(self):
self.state.selected_node["bookmark"] = not self.state.selected_node.get("bookmark", False)
Expand Down Expand Up @@ -323,6 +326,15 @@ def escape_textbox(self):
self.display.set_mode("Read")
self.refresh_textbox()

@metadata(name="Edit history", keys=[], display_key="Escape")
def edit_history(self, index):
if self.display.mode == "Read":
ancestor_index = bisect.bisect_left(self.ancestor_end_indices, index)
selected_ancestor = node_ancestry(self.state.selected_node, self.state.tree_node_dict)[ancestor_index]
self.remember_node_id = self.state.selected_node_id
self.state.select_node(selected_ancestor["id"])
self.toggle_edit_mode()


@metadata(name="Reset zoom", keys=["<Control-0>"], display_key="Ctrl-0")
def reset_zoom(self):
Expand All @@ -339,7 +351,13 @@ def toggle_edit_mode(self, to_edit_mode=None):
if self.display.mode != "Visualize":
self.save_edits()
to_edit_mode = to_edit_mode if to_edit_mode is not None else not self.display.in_edit_mode
self.display.set_mode("Edit" if to_edit_mode else "Read")
if to_edit_mode:
self.display.set_mode("Edit")
else:
if self.remember_node_id is not None:
self.state.select_node(self.remember_node_id)
self.remember_node_id = None
self.display.set_mode("Read")
self.refresh_textbox()
else:
if self.display.vis.textbox is None:
Expand Down Expand Up @@ -459,6 +477,20 @@ def set_all_visited(self, status=True):
self.update_nav_tree_selected()


@metadata(name="Darkmode", keys=["<Control-Shift-KeyPress-D>"], display_key="Ctrl-Shift-D")
def toggle_darkmode(self):
global darkmode
darkmode = not darkmode
print("c",darkmode)
self.display.frame.pack_forget()
self.display.frame.destroy()
self.display = Display(self.root, self.callbacks, self.state)
self.refresh_textbox()
self.update_nav_tree()




#################################
# Chapters
#################################
Expand Down Expand Up @@ -620,7 +652,9 @@ def refresh_textbox(self):
self.display.textbox.delete("1.0", "end")

self.display.textbox.tag_config('history', foreground=self.HISTORY_COLOR)
for node_text in self.state.node_ancestry_text()[:-1]:
ancestry, indices = self.state.node_ancestry_text()
self.ancestor_end_indices = indices
for node_text in ancestry[:-1]:
# "end" includes the automatically inserted new line
self.display.textbox.insert("end-1c", node_text, "history")
self.display.textbox.insert("end-1c", self.state.selected_node["text"])
Expand Down
3 changes: 1 addition & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import PIL.Image
import PIL.ImageTk

from view.colors import darkmode

darkmode = True

class Application:

Expand Down
22 changes: 20 additions & 2 deletions model.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ def chapter(self, node):

def node_ancestry_text(self, node=None):
node = node if node else self.selected_node
return [node["text"] for node in node_ancestry(node, self.tree_node_dict)]
text = []
end_indices = []
index = 0
for node in node_ancestry(node, self.tree_node_dict):
text.append(node["text"])
index += len(node["text"])
end_indices.append(index)
return text, end_indices
#return [node["text"] for node in node_ancestry(node, self.tree_node_dict)]

@property
def selected_node(self):
Expand Down Expand Up @@ -323,7 +331,6 @@ def merge_with_children(self, node=None):
child["text"] = node["text"] + child["text"]
self.delete_node(node, reassign_children=True)

# TODO check if ancestor
# TODO indicate that change parent has been toggled
def change_parent(self, node=None, new_parent_id=None):
node = node if node else self.selected_node
Expand All @@ -348,12 +355,23 @@ def change_parent(self, node=None, new_parent_id=None):
new_parent["children"].append(node)
self.tree_updated()

# adds node to ghostchildren of new ghostparent
def add_parent(self, node=None, new_ghostparent=None):
pass

# changes parent id to new main parent, adds node to new main parent's children list, removes node from old parent's
# children list and adds to ghostchildren list
def change_main_parent(self, node=None, new_main_parent=None):
pass

# moves selected node up in sibling list, or if first node, moves to back
def move_up(self, node):
pass

# moves selected node down in sibling list, or if last node, moves to front
def move_down(self, node):
pass

# TODO Doesn't support deleting root
def delete_node(self, node=None, reassign_children=False):
node = node if node else self.selected_node
Expand Down
4 changes: 4 additions & 0 deletions view/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def build_textboxes(self, frame):
self._build_textbox(frame, "textbox_frame", "textbox", height=1)
self._build_textbox(frame, "secondary_textbox_frame", "secondary_textbox", height=3)

def click(self, txt, event=None):
char_index = txt.count("1.0", txt.index(tk.CURRENT), "chars")[0]
self.callbacks["Edit history"]["callback"](index=char_index)

# Text area and scroll bar TODO Make a scrollable textbox tkutil
def _build_textbox(self, frame, frame_attr, textbox_attr, height=1):
Expand All @@ -153,6 +156,7 @@ def _build_textbox(self, frame, frame_attr, textbox_attr, height=1):
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
textbox = TextAware(textbox_frame, bd=3, height=height, yscrollcommand=scrollbar.set, undo=True)
self.__setattr__(textbox_attr, textbox)
textbox.bind("<Button-1>", lambda event: self.click(txt=textbox))
textbox.pack(expand=True, fill='both')

readable_font = Font(family="Georgia", size=12) # Other nice options: Helvetica, Arial, Georgia
Expand Down

0 comments on commit bae2fc0

Please sign in to comment.