changeset e8411f308368 in tryton:default
details: https://hg.tryton.org/tryton?cmd=changeset&node=e8411f308368
description:
Add document widget
issue10173
review330471004
diffstat:
CHANGELOG | 1 +
bin/tryton | 1 +
setup-freeze.py | 4 +
tryton/__init__.py | 5 +
tryton/gui/window/view_form/view/form.py | 2 +
tryton/gui/window/view_form/view/form_gtk/document.py | 91 +++++++++++++++++++
6 files changed, 104 insertions(+), 0 deletions(-)
diffs (169 lines):
diff -r bedc052ab189 -r e8411f308368 CHANGELOG
--- a/CHANGELOG Sat Mar 27 11:41:13 2021 +0100
+++ b/CHANGELOG Sat Mar 27 17:17:00 2021 +0100
@@ -1,3 +1,4 @@
+* Support document widget
* Support icon of type URL
* Allow sending email with existing attachments
* Add paths and model context to action data
diff -r bedc052ab189 -r e8411f308368 bin/tryton
--- a/bin/tryton Sat Mar 27 11:41:13 2021 +0100
+++ b/bin/tryton Sat Mar 27 17:17:00 2021 +0100
@@ -17,6 +17,7 @@
prefix = os.path.dirname(sys.executable)
os.environ['GTK_EXE_PREFIX'] = prefix
os.environ['GTK_DATA_PREFIX'] = prefix
+ os.environ['EVINCE_BACKENDS_DIR'] = prefix
os.environ['XDG_DATA_DIRS'] = os.path.join(prefix, 'share')
etc = os.path.join(prefix, 'etc')
os.environ['GDK_PIXBUF_MODULE_FILE'] = os.path.join(
diff -r bedc052ab189 -r e8411f308368 setup-freeze.py
--- a/setup-freeze.py Sat Mar 27 11:41:13 2021 +0100
+++ b/setup-freeze.py Sat Mar 27 17:17:00 2021 +0100
@@ -29,6 +29,8 @@
os.path.join('lib', 'gtk-3.0')),
(os.path.join(sys.prefix, 'lib', 'gdk-pixbuf-2.0'),
os.path.join('lib', 'gdk-pixbuf-2.0')),
+ (os.path.join(sys.prefix, 'lib', 'evince'),
+ os.path.join('lib', 'evince')),
(os.path.join(sys.prefix, 'share', 'locale'),
os.path.join('share', 'locale')),
(os.path.join(sys.prefix, 'share', 'icons', 'Adwaita'),
@@ -41,6 +43,8 @@
required_gi_namespaces = [
'Atk-1.0',
+ 'EvinceDocument-3.0',
+ 'EvinceView-3.0',
'GLib-2.0',
'GModule-2.0',
'GObject-2.0',
diff -r bedc052ab189 -r e8411f308368 tryton/__init__.py
--- a/tryton/__init__.py Sat Mar 27 11:41:13 2021 +0100
+++ b/tryton/__init__.py Sat Mar 27 17:17:00 2021 +0100
@@ -11,6 +11,11 @@
gi.require_version('GtkSpell', '3.0')
except ValueError:
pass
+try:
+ gi.require_version('EvinceDocument', '3.0')
+ gi.require_version('EvinceView', '3.0')
+except ValueError:
+ pass
try:
# Import earlier otherwise there is a segmentation fault on MSYS2
diff -r bedc052ab189 -r e8411f308368 tryton/gui/window/view_form/view/form.py
--- a/tryton/gui/window/view_form/view/form.py Sat Mar 27 11:41:13 2021 +0100
+++ b/tryton/gui/window/view_form/view/form.py Sat Mar 27 17:17:00 2021 +0100
@@ -13,6 +13,7 @@
from tryton.common.button import Button
from tryton.config import CONFIG
from .form_gtk.calendar_ import Date, Time, DateTime
+from .form_gtk.document import Document
from .form_gtk.float import Float
from .form_gtk.integer import Integer
from .form_gtk.selection import Selection
@@ -167,6 +168,7 @@
'date': Date,
'datetime': DateTime,
'dict': DictWidget,
+ 'document': Document,
'email': Email,
'float': Float,
'html': HTML,
diff -r bedc052ab189 -r e8411f308368
tryton/gui/window/view_form/view/form_gtk/document.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tryton/gui/window/view_form/view/form_gtk/document.py Sat Mar 27
17:17:00 2021 +0100
@@ -0,0 +1,91 @@
+# This file is part of Tryton. The COPYRIGHT file at the top level of
+# this repository contains the full copyright notices and license terms.
+from pathlib import Path
+from tempfile import NamedTemporaryFile
+
+from gi.repository import Gtk, Gdk, GLib
+try:
+ from gi.repository import EvinceDocument, EvinceView
+ EvinceDocument.init()
+except ImportError:
+ EvinceDocument = EvinceView = None
+
+from tryton.common import data2pixbuf, resize_pixbuf
+
+from .widget import Widget
+from .binary import BinaryMixin
+
+
+class Document(BinaryMixin, Widget):
+ expand = True
+
+ def __init__(self, view, attrs):
+ super().__init__(view, attrs)
+
+ self.widget = Gtk.VBox(spacing=3)
+ self.image = Gtk.DrawingArea()
+ self.widget.pack_start(
+ self.image, expand=True, fill=True, padding=0)
+
+ if EvinceView:
+ self.evince_view = EvinceView.View()
+ self.evince_scroll = Gtk.ScrolledWindow()
+ self.evince_scroll.add(self.evince_view)
+ self.widget.pack_start(
+ self.evince_scroll, expand=True, fill=True, padding=0)
+ else:
+ self.evince_view = self.evince_scroll = None
+
+ def _draw(self, drawing_area, cr, pixbuf):
+ width = drawing_area.get_allocated_width()
+ height = drawing_area.get_allocated_height()
+ pixbuf = resize_pixbuf(pixbuf, width, height)
+ width = (width - pixbuf.get_width()) / 2
+ height = (height - pixbuf.get_height()) / 2
+ Gdk.cairo_set_source_pixbuf(cr, pixbuf, width, height)
+ cr.paint()
+
+ def display(self):
+ super().display()
+ data = self.field.get_data(self.record)
+
+ if not data:
+ self.image.hide()
+ if self.evince_view:
+ self.evince_view.set_model(EvinceView.DocumentModel())
+ self.evince_scroll.hide()
+ return
+
+ pixbuf = data2pixbuf(data)
+ if pixbuf:
+ try:
+ self.image.disconnect_by_func(self._draw)
+ except TypeError:
+ pass
+ self.image.connect('draw', self._draw, pixbuf)
+ self.image.queue_draw()
+ self.image.show()
+ if self.evince_view:
+ self.evince_view.set_model(EvinceView.DocumentModel())
+ self.evince_scroll.hide()
+ else:
+ self.image.hide()
+ if self.evince_view:
+ self.evince_scroll.show()
+ if self.filename_field:
+ suffix = self.filename_field.get(self.record)
+ else:
+ suffix = None
+ try:
+ with NamedTemporaryFile(suffix=suffix) as fp:
+ fp.write(data)
+ path = Path(fp.name)
+ document = (
+ EvinceDocument.Document.factory_get_document(
+ path.as_uri()))
+ model = EvinceView.DocumentModel()
+ model.set_document(document)
+ self.evince_view.set_model(model)
+ except GLib.GError:
+ self.evince_view.set_model(EvinceView.DocumentModel())
+ self.evince_scroll.hide()