Reviewers: ,
Please review this at http://codereview.tryton.org/214002/
Affected files:
M tryton/common/cellrendererbinary.py
M tryton/common/common.py
M tryton/gui/window/view_form/view/form_gtk/binary.py
M tryton/gui/window/view_form/view/list_gtk/parser.py
Index: tryton/common/cellrendererbinary.py
===================================================================
--- 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 @@
gobject.PARAM_READWRITE),
'editable': (gobject.TYPE_INT, 'Editable', 'Editable', 0, 10, 0,
gobject.PARAM_READWRITE),
- 'content': (gobject.TYPE_INT, 'Content', 'Content', 0, 10, 0,
+ 'content': (gobject.TYPE_STRING, 'Content', 'Content', '',
gobject.PARAM_READWRITE),
}
__gsignals__ = {
@@ -55,9 +56,15 @@
def do_get_property(self, pspec):
return getattr(self, pspec.name)
- def button_width(self, cell_area):
- return ((cell_area.width - (len(self.buttons) + 1) * 2)
- / len(self.buttons))
+ def button_width(self, cell_area, text_width):
+ return ((cell_area.width - text_width / pango.SCALE
+ - (len(self.buttons) + 1) * 2) / len(self.buttons))
+
+ def text_width(self, widget, text):
+ layout = widget.create_pango_layout(self.content)
+ layout.set_font_description(widget.style.font_desc)
+ w, h = layout.get_size()
+ return max(73000, w) # Default minimal width of 73000
def on_get_size(self, widget, cell_area=None):
if cell_area is None:
@@ -67,10 +74,12 @@
def on_start_editing(self, event, widget, path, background_area,
cell_area, flags):
- button_width = self.button_width(cell_area)
+ w = self.text_width(widget, self.content)
+ button_width = self.button_width(cell_area, w)
+
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 + w / pango.SCALE + 5 + x_offset
if x_button < event.x < x_button + button_width:
break
else:
@@ -96,7 +105,21 @@
# Handle Pixmap window as pygtk failed
if type(window) == gtk.gdk.Pixmap:
return
- button_width = self.button_width(cell_area)
+
+ # display size
+ layout = widget.create_pango_layout(self.content)
+ 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)
+
+ w = max(73000, w)
+ # display buttons
+ button_width = self.button_width(cell_area, w)
for index, button_name in enumerate(self.buttons):
state = gtk.STATE_NORMAL
shadow = gtk.SHADOW_OUT
@@ -110,7 +133,8 @@
pixbuf = pxbf_insens
else:
pixbuf = pxbf_sens
- x_offset = button_width * index + 2 * (index + 1)
+ x_offset = (w / pango.SCALE + 5 + button_width * index
+ + 2 * (index + 1))
widget.style.paint_box(window, state, shadow,
None, widget, "button", cell_area.x + x_offset,
cell_area.y,
button_width, cell_area.height)
Index: tryton/common/common.py
===================================================================
--- a/tryton/common/common.py
+++ b/tryton/common/common.py
@@ -1455,3 +1455,9 @@
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
Index: tryton/gui/window/view_form/view/form_gtk/binary.py
===================================================================
--- 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 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 @@
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)))
Index: tryton/gui/window/view_form/view/list_gtk/parser.py
===================================================================
--- 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 @@
'one2many': 50,
'many2many': 50,
'boolean': 20,
+ 'binary': 200,
}
if 'width' in node_attrs:
width = int(node_attrs['width'])
@@ -532,7 +533,7 @@
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('content', common.humanize(size))
def _get_record_field(self, path):
store = self.treeview.get_model()
@@ -545,6 +546,9 @@
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:
--
[email protected] mailing list