From 55aaeb90c84501fad5dbadfcc95eb771c58ac780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20=C3=89vrard?= Date: Fri, 6 Jan 2012 13:46:40 +0100 Subject: [PATCH] Display binary field size along the buttons in treeview review214002 issue2369 --- CHANGELOG | 1 + tryton/common/cellrendererbinary.py | 28 +++++++++++++++---- tryton/common/common.py | 6 ++++ .../window/view_form/view/form_gtk/binary.py | 9 ++---- .../window/view_form/view/list_gtk/parser.py | 6 +++- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f977acb719..1326819d10 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Display binary size alongside the buttons in treeview * Set correctly the focus on tab switching * Add shortcut to focus search entry * Add binary field to tree views diff --git a/tryton/common/cellrendererbinary.py b/tryton/common/cellrendererbinary.py index d9211555d4..0c7cc4b152 100644 --- a/tryton/common/cellrendererbinary.py +++ b/tryton/common/cellrendererbinary.py @@ -2,6 +2,7 @@ #this repository contains the full copyright notices and license terms. import gtk import gobject +import pango class CellRendererBinary(gtk.GenericCellRenderer): @@ -10,7 +11,7 @@ class CellRendererBinary(gtk.GenericCellRenderer): gobject.PARAM_READWRITE), 'editable': (gobject.TYPE_INT, 'Editable', 'Editable', 0, 10, 0, gobject.PARAM_READWRITE), - 'content': (gobject.TYPE_INT, 'Content', 'Content', 0, 10, 0, + 'size': (gobject.TYPE_STRING, 'Size', 'Size', '', gobject.PARAM_READWRITE), } __gsignals__ = { @@ -56,7 +57,7 @@ class CellRendererBinary(gtk.GenericCellRenderer): return getattr(self, pspec.name) def button_width(self, cell_area): - return ((cell_area.width - (len(self.buttons) + 1) * 2) + return ((cell_area.width / 2 - (len(self.buttons) + 1) * 2) / len(self.buttons)) def on_get_size(self, widget, cell_area=None): @@ -68,9 +69,10 @@ class CellRendererBinary(gtk.GenericCellRenderer): def on_start_editing(self, event, widget, path, background_area, cell_area, flags): button_width = self.button_width(cell_area) + for index, button_name in enumerate(self.buttons): x_offset = button_width * index + 2 * (index + 1) - x_button = cell_area.x + x_offset + x_button = cell_area.x + cell_area.width / 2 + 5 + x_offset if x_button < event.x < x_button + button_width: break else: @@ -79,7 +81,7 @@ class CellRendererBinary(gtk.GenericCellRenderer): return if not self.editable and button_name in ('new', 'clear'): return - if not self.content and button_name == 'save': + if not self.size and button_name == 'save': return if event is None or event.type == gtk.gdk.BUTTON_PRESS: self.clicking = button_name @@ -96,6 +98,19 @@ class CellRendererBinary(gtk.GenericCellRenderer): # Handle Pixmap window as pygtk failed if type(window) == gtk.gdk.Pixmap: return + + # display size + layout = widget.create_pango_layout(self.size) + layout.set_font_description(widget.style.font_desc) + w, h = layout.get_size() + x = int(cell_area.x + (cell_area.width - w / pango.SCALE) * 0) + y = int(cell_area.y + (cell_area.height - h / pango.SCALE) / 2) + state = gtk.STATE_NORMAL + if flags & gtk.CELL_RENDERER_SELECTED: + state = gtk.STATE_ACTIVE + window.draw_layout(widget.style.text_gc[state], x, y, layout) + + # display buttons button_width = self.button_width(cell_area) for index, button_name in enumerate(self.buttons): state = gtk.STATE_NORMAL @@ -105,12 +120,13 @@ class CellRendererBinary(gtk.GenericCellRenderer): state = gtk.STATE_ACTIVE shadow = gtk.SHADOW_IN if (not self.editable and button_name in ('new', 'clear') - or not self.content and button_name in ('open', 'save')): + or not self.size and button_name in ('open', 'save')): state = gtk.STATE_INSENSITIVE pixbuf = pxbf_insens else: pixbuf = pxbf_sens - x_offset = button_width * index + 2 * (index + 1) + x_offset = (cell_area.width / 2 + 5 + + button_width * index + 2 * (index + 1)) - 1 widget.style.paint_box(window, state, shadow, None, widget, "button", cell_area.x + x_offset, cell_area.y, button_width, cell_area.height) diff --git a/tryton/common/common.py b/tryton/common/common.py index b6618280a4..f38ce58ae4 100644 --- a/tryton/common/common.py +++ b/tryton/common/common.py @@ -1455,3 +1455,9 @@ def timezoned_date(date): ldt = sdt.astimezone(lzone) date = ldt return date + +def humanize(size): + for x in ('bytes', 'KB', 'MB', 'GB', 'TB', 'PB'): + if size < 1000: + return '%3.1f%s' % (size, x) + size /= 1000.0 diff --git a/tryton/gui/window/view_form/view/form_gtk/binary.py b/tryton/gui/window/view_form/view/form_gtk/binary.py index 21f8ccad98..982ab1d572 100644 --- a/tryton/gui/window/view_form/view/form_gtk/binary.py +++ b/tryton/gui/window/view_form/view/form_gtk/binary.py @@ -4,17 +4,12 @@ import gtk import gettext import os import tempfile +from tryton.common import common from tryton.common import file_selection, message, warning, Tooltips, file_open from interface import WidgetInterface _ = gettext.gettext -def humanize(size): - for x in ('bytes', 'KB', 'MB', 'GB', 'TB', 'PB'): - if size < 1000: - return '%3.1f%s' % (size, x) - size /= 1000.0 - class Binary(WidgetInterface): "Binary" @@ -173,7 +168,7 @@ class Binary(WidgetInterface): return False if self.wid_text: self.wid_text.set_text(self.filename_field.get(record) or '') - self.wid_size.set_text(humanize(field.get_size(record) or 0)) + self.wid_size.set_text(common.humanize(field.get_size(record) or 0)) if self.but_open: self.but_open.set_sensitive(bool(field.get_size(record))) self.but_save_as.set_sensitive(bool(field.get_size(record))) diff --git a/tryton/gui/window/view_form/view/list_gtk/parser.py b/tryton/gui/window/view_form/view/list_gtk/parser.py index cc0d922c18..05fad68be0 100644 --- a/tryton/gui/window/view_form/view/list_gtk/parser.py +++ b/tryton/gui/window/view_form/view/list_gtk/parser.py @@ -185,6 +185,7 @@ class ParserTree(ParserInterface): 'one2many': 50, 'many2many': 50, 'boolean': 20, + 'binary': 200, } if 'width' in node_attrs: width = int(node_attrs['width']) @@ -532,7 +533,7 @@ class Binary(Char): def setter(self, column, cell, store, iter): record = store.get_value(iter, 0) size = record[self.field_name].get_size(record) - cell.set_property('content', bool(size)) + cell.set_property('size', common.humanize(size) if size else '') def _get_record_field(self, path): store = self.treeview.get_model() @@ -545,6 +546,9 @@ class Binary(Char): record, field = self._get_record_field(path) if filename: field.set_client(record, open(filename, 'rb').read()) + if self.filename: + filename_field = record.group.fields[self.filename] + filename_field.set_client(record, os.path.basename(filename)) def open_binary(self, renderer, path): if not self.filename: