Files
YMS/yms/view/documents.py
T

235 lines
8.9 KiB
Python

# SPDX-License-Identifier: WTFPL
import os
from gi.repository import GLib, Gtk, Gdk, GdkPixbuf, Rsvg
import fitz
import sane
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.scandialog = self.builder.get_object("dlg_scanner")
print(self.scandialog)
# TODO self.uploaddialog = self.builder.get_object("dlg_upload")
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)
# Status
self.dd_type = self.context.lookup.enum('document.doctype')
combobox.load_from_dict(self.combo['doctype'], self.dd_type)
# Roleaccess
self.dd_access = self.context.lookup.enum('document.roleaccess')
combobox.load_from_dict(self.combo['accesslevel'], self.dd_access)
def clear_record(self):
self.doc.clear()
combobox.activate_row(self.combo['doctype'], 'generic')
self.entry['title'].set_text('')
self.entry['filename'].set_text('')
combobox.activate_row(self.combo['accesslevel'], 'captain')
self.text['remarks'].get_buffer().set_text('')
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):
combobox.activate_row(self.combo['doctype'], self.doc.doctype)
self.entry['title'].set_text(self.doc.title or '')
self.entry['filename'].set_text(self.doc.filename or '')
def save_record(self):
# fields must have been validated beforehand
self.doc.doctype = combobox.get_list_value(self.combo['type'])
self.doc.title = self.entry['title'].get_text()
self.doc.filename = self.entry['filename'].get_text()
self.doc.remarks = gtkutils.get_text_contents(self.text['remarks'])
# buttons on list page
def on_but_new_clicked(self, widget):
self.newrecord = True
self.label['title_edit'].set_text(_("Add new Document"))
self.clear_record()
self.topwidget.set_visible_child(self.box['doc_edit'])
def on_but_edit_clicked(self, widget):
self.newrecord = False
docid = treeview.get_value(self.tree['documents'], 0)
if docid is None:
return
self.doc.load(docid)
self.edit_record()
self.topwidget.set_visible_child(self.box['doc_edit'])
def on_but_detail_clicked(self, widget):
docid = treeview.get_value(self.tree['documents'], 0)
if docid is None:
return
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):
answer = gtkutils.questionbox(
_("Delete document!\n"
"Are you sure?"), title="YMS"
)
if answer == False:
return
self.topwidget.set_visible_child(self.box['documents'])
def scan_progress(self, current_line, total_lines):
print(f"Fortschritt: {current_line} / {total_lines}")
# buttons on edit page
def on_but_scan_clicked(self, widget):
"""
# python sane
tl-x: X-Koordinate der linken oberen Ecke
tl-y: Y-Koordinate der linken oberen Ecke
br-x: X-Koordinate der rechten unteren Ecke
br-y: Y-Koordinate der rechten unteren Ecke
verfügbarer bereich ist über constraints ermittelbar
verfügbare resolution auch über constraints
verfügbare modes auch über constraints
Einheit: sane.UNIT_MM
scanner.tl_x = 10.0
scanner.tl_y = 10.0
scanner.br_x = 100.0
scanner.br_y = 150.0
"""
response = self.scandialog.run()
self.scandialog.hide()
if response == Gtk.ResponseType.OK:
print("OK")
else:
print("Not OK")
# sane.init()
# devices = sane.get_devices()
# if more than one device: Show device selector
# device_name = devices[0][0]
# scanner = sane.open(device_name)
# scanner.get_options()
# Options: index, name, title, desc, opt_type, unit, size, cap, constraint
# option.is_active()
# option.is_settable()
# scanner.get_parameters()
# format, last_frame, (pixels_per_line, lines), depth = scanner.get_parameters()
# scanner.mode = "Color"
# scanner.resolution = 150
# image = scanner.scan(progress=self.scan_progress)
# image.save("scan.png")
# scanner.close()
# sane.exit()
pass
def on_but_upload_clicked(self, widget):
pass
def on_but_save_clicked(self, widget):
print(self.doc)
def on_but_cancel_clicked(self, widget):
self.topwidget.set_visible_child(self.box['documents'])