This commit is contained in:
LunarEclipse 2024-04-27 23:29:33 +02:00
parent 00828d4c08
commit e526de786e
1 changed files with 41 additions and 33 deletions

View File

@ -3,8 +3,10 @@ import traceback
from typing import Dict, List, Optional, Sequence, Tuple, TypeAlias from typing import Dict, List, Optional, Sequence, Tuple, TypeAlias
import gi import gi
gi.require_version("Gtk", "3.0") gi.require_version("Gtk", "3.0")
from gi.repository import GLib, Gio, Gtk, GObject from gi.repository import GLib, Gio, Gtk, GObject
gi.require_foreign("cairo") gi.require_foreign("cairo")
import cairo import cairo
@ -14,41 +16,46 @@ TEST_FILENAME = "/home/luna/Documents/Resources/Praca Licencjacka/sources/2018_T
Coords: TypeAlias = Tuple[float, float] Coords: TypeAlias = Tuple[float, float]
class Selection: class Selection:
def __init__(self, bounds: Tuple[Coords, Coords], columns: Optional[Sequence[float]] = None): def __init__(self, bounds: Tuple[Coords, Coords], columns: Optional[Sequence[float]] = None):
self.bounds = bounds self.bounds = bounds
self.columns = columns self.columns = columns
class Document: class Document:
def __init__(self, filename: str): def __init__(self, filename: str):
self.filename = filename self.filename = filename
self.document = fitz.Document(filename) self.document = fitz.Document(filename)
self.selections: Dict[int, List[Selection]] = {} self.selections: Dict[int, List[Selection]] = {}
class State(): class State():
pass pass
class PdfPage(Gtk.DrawingArea): class PdfPage(Gtk.DrawingArea):
def __init__(self, page, *args, **kwargs): def __init__(self, page, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.page: fitz.Page = page self.page: fitz.Page = page
pix = self.page.get_pixmap(dpi=300) # type: ignore pix = self.page.get_pixmap(dpi=96) # type: ignore
self.set_size_request(pix.width, pix.height) self.set_size_request(pix.width, pix.height)
self.set_app_paintable(True) # type: ignore print(f"w: {pix.width}, h: {pix.height}")
self.set_app_paintable(True) # type: ignore
self.connect("draw", self.on_draw, {}) self.connect("draw", self.on_draw, {})
def on_draw(self, widget, cr: cairo.Context, data: GObject.GPointer): def on_draw(self, widget: Gtk.DrawingArea, cr: cairo.Context, data: GObject.GPointer):
#app: Application = widget.get_window().get_application() # type: ignore # app: Application = widget.get_window().get_application() # type: ignore
width = self.get_allocated_width() width = widget.get_allocated_width()
height = self.get_allocated_height() height = widget.get_allocated_height()
print(f"alloc w: {width}, h: {height}")
sctx = self.get_style_context() sctx = widget.get_style_context()
Gtk.render_background(sctx, cr, 0, 0, width, height) Gtk.render_background(sctx, cr, 0, 0, width, height)
pix = self.page.get_pixmap(dpi=96, alpha=True) # type: ignore
pix = self.page.get_pixmap(dpi=300) # type: ignore mv: memoryview = pix.samples_mv
png = pix.tobytes("png") ims = cairo.ImageSurface.create_for_data(mv, cairo.Format.RGB24, pix.width, pix.height)
ims = cairo.ImageSurface.create_from_png(png)
cr.set_source_surface(ims, 0, 0) cr.set_source_surface(ims, 0, 0)
cr.paint() cr.paint()
@ -63,27 +70,26 @@ class PdfPage(Gtk.DrawingArea):
class MainWindow(Gtk.ApplicationWindow): class MainWindow(Gtk.ApplicationWindow):
__gtype_name__ = "main_window" __gtype_name__ = "main_window"
open_button: Gtk.Button = Gtk.Template.Child() # type: ignore open_button: Gtk.Button = Gtk.Template.Child() # type: ignore
header_bar: Gtk.HeaderBar = Gtk.Template.Child() # type: ignore header_bar: Gtk.HeaderBar = Gtk.Template.Child() # type: ignore
main_paned: Gtk.Paned = Gtk.Template.Child() # type: ignore main_paned: Gtk.Paned = Gtk.Template.Child() # type: ignore
pdf_list_box: Gtk.ListBox = Gtk.Template.Child() # type: ignore pdf_list_box: Gtk.ListBox = Gtk.Template.Child() # type: ignore
pdfFileFilter: Gtk.FileFilter = Gtk.Template.Child() # type: ignore pdfFileFilter: Gtk.FileFilter = Gtk.Template.Child() # type: ignore
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.app: Application = self.get_application() # type: ignore self.app: Application = self.get_application() # type: ignore
assert self.app is not None assert self.app is not None
self.app.connect("notify::document", self.on_document_updated) self.app.connect("notify::document", self.on_document_updated)
#self.pdf_list_box.add(PdfPage()) # self.pdf_list_box.add(PdfPage())
# @Gtk.Template.Callback()
#@Gtk.Template.Callback() # def example_button_released_cb(self, widget: Gtk.Button, **kwargs):
#def example_button_released_cb(self, widget: Gtk.Button, **kwargs):
# assert self.example_button == widget # assert self.example_button == widget
# print(widget.get_label()) # print(widget.get_label())
# widget.set_label("woah") # widget.set_label("woah")
@ -92,7 +98,7 @@ class MainWindow(Gtk.ApplicationWindow):
def on_open_button_clicked(self, widget, *args, **kwargs): def on_open_button_clicked(self, widget, *args, **kwargs):
dialog = Gtk.FileChooserDialog( dialog = Gtk.FileChooserDialog(
title="Choose PDF File to open", title="Choose PDF File to open",
transient_for=self, # equivalent to parent= transient_for=self, # equivalent to parent=
action=Gtk.FileChooserAction.OPEN, action=Gtk.FileChooserAction.OPEN,
filter=self.pdfFileFilter, filter=self.pdfFileFilter,
modal=True, modal=True,
@ -100,9 +106,9 @@ class MainWindow(Gtk.ApplicationWindow):
dialog.add_button("Cancel", Gtk.ResponseType.CANCEL) dialog.add_button("Cancel", Gtk.ResponseType.CANCEL)
dialog.add_button("Open", Gtk.ResponseType.ACCEPT) dialog.add_button("Open", Gtk.ResponseType.ACCEPT)
response = dialog.run() # type: ignore response = dialog.run() # type: ignore
if response == Gtk.ResponseType.ACCEPT: if response == Gtk.ResponseType.ACCEPT:
filename: str = dialog.get_filename() # type: ignore filename: str = dialog.get_filename() # type: ignore
try: try:
self.app.set_property("document", Document(filename)) self.app.set_property("document", Document(filename))
except Exception as e: except Exception as e:
@ -115,7 +121,7 @@ class MainWindow(Gtk.ApplicationWindow):
secondary_text=traceback.format_exc(), secondary_text=traceback.format_exc(),
buttons=Gtk.ButtonsType.OK, buttons=Gtk.ButtonsType.OK,
) )
message_dialog.run() # type: ignore message_dialog.run() # type: ignore
message_dialog.destroy() message_dialog.destroy()
dialog.destroy() dialog.destroy()
@ -123,14 +129,16 @@ class MainWindow(Gtk.ApplicationWindow):
# editing an existing one won't trigger it # editing an existing one won't trigger it
def on_document_updated(self, recvobj, gparamstring): def on_document_updated(self, recvobj, gparamstring):
document: Document = self.app.get_property("document") document: Document = self.app.get_property("document")
self.header_bar.set_title(document.filename.split("/")[-1]) # type: ignore self.header_bar.set_title(document.filename.split("/")[-1]) # type: ignore
self.header_bar.set_subtitle(document.filename) # type: ignore self.header_bar.set_subtitle(document.filename) # type: ignore
for i in document.document.pages(): # type: ignore for i in document.document.pages(): # type: ignore
row = Gtk.ListBoxRow()
page = PdfPage(i) page = PdfPage(i)
box = Gtk.Box() row.add(page)
box.pack_start(page, True, True, 20) self.pdf_list_box.add(row) # type: ignore
self.pdf_list_box.add(box) # type: ignore
self.pdf_list_box.show_all()
@Gtk.Template.Callback() @Gtk.Template.Callback()
def on_open_button_small_clicked(self, widget, **kwargs): def on_open_button_small_clicked(self, widget, **kwargs):
@ -145,14 +153,14 @@ class Application(Gtk.Application):
*args, *args,
application_id="zone.lunareclipse.pdf_table_extractor", application_id="zone.lunareclipse.pdf_table_extractor",
flags=Gio.ApplicationFlags.FLAGS_NONE, flags=Gio.ApplicationFlags.FLAGS_NONE,
#flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, # TODO # flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, # TODO
**kwargs **kwargs
) )
self.window = None self.window = None
def do_activate(self): def do_activate(self):
self.window = self.window or MainWindow(application=self) self.window = self.window or MainWindow(application=self)
self.window.show_all() # type: ignore self.window.show_all() # type: ignore
if __name__ == "__main__": if __name__ == "__main__":