Files
YMS/yms/view/documents.py
T

139 lines
5.4 KiB
Python

# SPDX-License-Identifier: WTFPL
import os
from gi.repository import GLib, Gtk, Gdk, GdkPixbuf, Rsvg
import fitz
from .view import View
from yms import gtkutils, combobox, treeview
from yms.model import document
class DocumentsView(View):
def __init__(self, context):
super().__init__(context, _("Documents"), "documents.ui")
self.context = context
self.doc = document.Document(context.db) # current record
self.newrecord = False
self.lst_documents = Gtk.ListStore(int, str, str, str, int)
sql = (
"SELECT d.docid, d.doctype, d.filename, d.title, COUNT(r.drid) AS refcount "
"FROM document AS d LEFT OUTER JOIN docref AS r USING (docid) "
"GROUP BY d.docid, d.doctype, d.filename, d.title "
"ORDER BY d.docid"
)
self.db.cur.execute(sql)
for row in self.db.cur.fetchall():
self.lst_documents.append(row)
treeview.add_column(self.tree['documents'], _('ID'), 0)
treeview.add_column(self.tree['documents'], _('Type'), 1)
treeview.add_column(self.tree['documents'], _('Filename'), 2)
treeview.add_column(self.tree['documents'], _('Title'), 3)
treeview.add_column(self.tree['documents'], _('References'), 3)
self.tree['documents'].set_model(self.lst_documents)
# buttons on list page
def on_but_new_clicked(self, widget):
self.topwidget.set_visible_child(self.box['doc_edit'])
def on_but_edit_clicked(self, widget):
self.topwidget.set_visible_child(self.box['doc_edit'])
def on_but_detail_clicked(self, widget):
docid = treeview.get_value(self.tree['documents'], 0)
self.doc.load(docid)
self.display_record()
self.topwidget.set_visible_child(self.box['doc_detail'])
self.button['close'].grab_focus()
def on_tvw_documents_row_activated(self, widget, path, column):
docid = treeview.get_value(widget, 0)
self.doc.load(docid)
self.display_record()
self.topwidget.set_visible_child(self.box['doc_detail'])
self.button['close'].grab_focus()
# buttons on detail page
# edit2 uses edit callback
def on_but_close_clicked(self, widget):
self.topwidget.set_visible_child(self.box['documents'])
def on_but_delete_clicked(self, widget):
self.topwidget.set_visible_child(self.box['documents'])
# buttons on edit page
def on_but_save_clicked(self, widget):
pass
def on_but_cancel_clicked(self, widget):
self.topwidget.set_visible_child(self.box['documents'])
def pdf_to_pixbuf(self, filename, max_width=400, max_height=400):
doc = fitz.open(filename)
page = doc[0]
rect = page.rect
scale = min(max_width / rect.width, max_height / rect.height)
matrix = fitz.Matrix(scale, scale)
pix = page.get_pixmap(matrix=matrix, alpha=False)
pixbuf = GdkPixbuf.Pixbuf.new_from_data(
pix.samples,
GdkPixbuf.Colorspace.RGB,
pix.alpha,
8,
pix.width,
pix.height,
pix.stride,
None,
None
)
return pixbuf
def display_record(self):
print(self.doc)
self.label['title'].set_text(self.doc.title or '')
self.label['filename'].set_text(self.doc.filename)
# self.label['extents'] -> see below
self.label['mimetype'].set_text(self.doc.mimetype)
self.label['filesize'].set_text(self.format_int(self.doc.docsize, suffix="Byte"))
self.label['date'].set_text(self.format_date(self.doc.docdate))
self.label['doctype'].set_text(self.doc.doctype)
self.label['roleaccess'].set_text(self.doc.roleaccess)
self.label['doctype'].set_text(self.doc.remarks or '')
# image preview
if self.doc.doctype == 'picture':
filename = f"pic-{self.doc.pk_value:06d}.{self.doc.extension}"
imgpath = os.path.join(self.context.cfg['docpath'], filename)
info = GdkPixbuf.Pixbuf.get_file_info(imgpath)
if info:
imgformat, imgwidth, imgheight = info
self.label['extents'].set_text(f"{imgwidth}x{imgheight} Pixel")
if imgwidth > 400 or imgheight > 400:
print("loading scaled")
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(imgpath, 400, 400, True)
else:
print("loading")
pixbuf = GdkPixbuf.Pixbuf.new_from_file(imgpath)
else:
print("cannot get image info")
self.image['preview'].set_from_pixbuf(pixbuf)
elif self.doc.doctype == 'drawing':
filename = f"drw-{self.doc.pk_value:06d}.{self.doc.extension}"
imgpath = os.path.join(self.context.cfg['docpath'], filename)
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(imgpath, 400, 400, True)
self.image['preview'].set_from_pixbuf(pixbuf)
elif self.doc.mimetype == "application/pdf":
filename = f"doc-{self.doc.pk_value:06d}.{self.doc.extension}"
imgpath = os.path.join(self.context.cfg['docpath'], filename)
print(imgpath)
pixbuf = self.pdf_to_pixbuf(imgpath)
self.image['preview'].set_from_pixbuf(pixbuf)
else:
self.image['preview'].clear()
def edit_record(self):
pass