Actual link addition
This commit is contained in:
parent
c87c5b86c2
commit
a2677735f3
44
src/main.py
44
src/main.py
|
@ -10,20 +10,43 @@ RECT_START = None
|
||||||
|
|
||||||
|
|
||||||
class AddLinkDialog:
|
class AddLinkDialog:
|
||||||
def __init__(self, root, rect, toc) -> None:
|
def __init__(self, root, page, rect, toc) -> None:
|
||||||
self.top = tkinter.Toplevel(root)
|
self.top = tkinter.Toplevel(root)
|
||||||
frm = ttk.Frame(self.top, padding=10)
|
frm = ttk.Frame(self.top, padding=10)
|
||||||
frm.grid()
|
frm.grid()
|
||||||
|
|
||||||
|
self.rect = rect
|
||||||
|
self.page = page
|
||||||
|
self.toc = toc
|
||||||
toc_values = list(t for _, t, _ in toc)
|
toc_values = list(t for _, t, _ in toc)
|
||||||
|
|
||||||
ttk.Label(frm, text=f"Rectangle: {rect!r}").grid(column=0, row=0, columnspan=3)
|
ttk.Label(frm, text=f"Rectangle: {rect!r}").grid(column=0, row=0, columnspan=3)
|
||||||
ttk.Label(frm, text="Target:").grid(column=0, row=1)
|
ttk.Label(frm, text="Target:").grid(column=0, row=1)
|
||||||
entry = ttk.Entry(frm)
|
self.entry = ttk.Combobox(frm, values=toc_values)
|
||||||
entry.grid(column=1, row=1, columnspan=2, sticky="EW")
|
self.entry.grid(column=1, row=1, columnspan=2, sticky="EW")
|
||||||
ttk.Button(frm, text="Add", command=self.top.destroy).grid(column=2, row=2)
|
ttk.Button(frm, text="Add", command=self.do_add_link).grid(column=2, row=2)
|
||||||
ttk.Button(frm, text="Cancel", command=self.top.destroy).grid(column=3, row=2)
|
ttk.Button(frm, text="Cancel", command=self.top.destroy).grid(column=3, row=2)
|
||||||
|
|
||||||
|
def do_add_link(self):
|
||||||
|
toc_title = self.entry.get()
|
||||||
|
page = 0
|
||||||
|
for _, t, p in self.toc:
|
||||||
|
if toc_title == t:
|
||||||
|
page = p - 1
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise Exception("TOC page not found")
|
||||||
|
|
||||||
|
self.page.insert_link(
|
||||||
|
{
|
||||||
|
"kind": pymupdf.LINK_GOTO,
|
||||||
|
"from": pymupdf.Rect(self.rect),
|
||||||
|
"page": page,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.top.destroy()
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
@ -72,7 +95,7 @@ def main() -> None:
|
||||||
|
|
||||||
def load_page(page_num: int, rect_overlay=None):
|
def load_page(page_num: int, rect_overlay=None):
|
||||||
page = doc[page_num]
|
page = doc[page_num]
|
||||||
pix = page.get_pixmap(dpi=120)
|
pix = page.get_pixmap(dpi=72)
|
||||||
|
|
||||||
mode = "RGBA" if pix.alpha else "RGB"
|
mode = "RGBA" if pix.alpha else "RGB"
|
||||||
img = Image.frombytes(mode, [pix.width, pix.height], pix.samples)
|
img = Image.frombytes(mode, [pix.width, pix.height], pix.samples)
|
||||||
|
@ -101,13 +124,12 @@ def main() -> None:
|
||||||
if RECT_START is not None:
|
if RECT_START is not None:
|
||||||
print(f"UP @ {event.x}/{event.y}")
|
print(f"UP @ {event.x}/{event.y}")
|
||||||
load_page(CURRENT_PAGE, [*RECT_START, event.x, event.y])
|
load_page(CURRENT_PAGE, [*RECT_START, event.x, event.y])
|
||||||
AddLinkDialog(root, [*RECT_START, event.x, event.y], toc)
|
AddLinkDialog(root, doc[CURRENT_PAGE], [*RECT_START, event.x, event.y], toc)
|
||||||
RECT_START = None
|
RECT_START = None
|
||||||
|
|
||||||
page_view.bind("<ButtonPress>", start_rect)
|
page_view.bind("<ButtonPress>", start_rect)
|
||||||
page_view.bind("<ButtonRelease>", end_rect)
|
page_view.bind("<ButtonRelease>", end_rect)
|
||||||
|
|
||||||
|
|
||||||
def next_page():
|
def next_page():
|
||||||
global CURRENT_PAGE
|
global CURRENT_PAGE
|
||||||
CURRENT_PAGE += 1
|
CURRENT_PAGE += 1
|
||||||
|
@ -125,6 +147,14 @@ def main() -> None:
|
||||||
column=3, row=1, sticky="NS", padx=5, pady=5
|
column=3, row=1, sticky="NS", padx=5, pady=5
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def save_and_close():
|
||||||
|
doc.save("/tmp/outfile.pdf")
|
||||||
|
root.destroy()
|
||||||
|
|
||||||
|
ttk.Button(frm, text="Save & Close", command=save_and_close).grid(
|
||||||
|
column=1, row=2, columnspan=3, sticky="EW"
|
||||||
|
)
|
||||||
|
|
||||||
root.bind("<Up>", lambda _: previous_page())
|
root.bind("<Up>", lambda _: previous_page())
|
||||||
root.bind("<Left>", lambda _: previous_page())
|
root.bind("<Left>", lambda _: previous_page())
|
||||||
root.bind("<Prior>", lambda _: previous_page())
|
root.bind("<Prior>", lambda _: previous_page())
|
||||||
|
|
Loading…
Reference in a new issue