/usr/share/pyshared/tryton/gui/window/view_form/view/form_gtk/selection.py is in tryton-client 3.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | #This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import gtk
import gobject
import math
from interface import WidgetInterface
from tryton.common.selection import SelectionMixin, selection_shortcuts
class PopdownMixin(object):
def __init__(self, *args, **kwargs):
super(PopdownMixin, self).__init__()
self._selection = {}
def set_popdown(self, selection, entry):
if not entry.child: # entry is destroyed
return
# Don't update popdown if not changed
if (list(x[0] for x in entry.get_model() or [])
== [x[1] for x in selection]):
return
model = gtk.ListStore(gobject.TYPE_STRING)
self._selection.clear()
for (value, name) in selection:
name = str(name)
self._selection[name] = value
model.append((name,))
entry.set_model(model)
entry.set_text_column(0)
completion = gtk.EntryCompletion()
#Only available in PyGTK 2.6 and above.
if hasattr(completion, 'set_inline_selection'):
completion.set_inline_selection(True)
completion.set_model(model)
entry.child.set_completion(completion)
if self._selection:
pop = sorted((len(x) for x in self._selection), reverse=True)
average = sum(pop) / len(pop)
deviation = int(math.sqrt(sum((x - average) ** 2 for x in pop) /
len(pop)))
width = max(next((x for x in pop if x < (deviation * 4)), 10), 10)
else:
width = 10
entry.child.set_width_chars(width)
if self._selection:
entry.child.set_max_length(
max(len(x) for x in self._selection))
completion.set_text_column(0)
completion.connect('match-selected', lambda *a: self._focus_out())
class Selection(WidgetInterface, SelectionMixin, PopdownMixin):
def __init__(self, field_name, model_name, attrs=None):
super(Selection, self).__init__(field_name, model_name, attrs=attrs)
self.widget = gtk.HBox(spacing=3)
self.entry = gtk.ComboBoxEntry()
child = self.entry.child
child.set_property('activates_default', True)
child.set_max_length(int(attrs.get('size', 0)))
child.set_width_chars(10)
selection_shortcuts(self.entry)
child.connect('key_press_event', self.sig_key_press)
child.connect('activate', self.sig_activate)
child.connect_after('focus-out-event', self.sig_activate)
child.connect('changed', self.send_modified)
self.entry.connect('notify::active', lambda *a: self._focus_out())
self.widget.pack_start(self.entry)
self.widget.set_focus_chain([child])
self._selection = {}
self.selection = attrs.get('selection', [])[:]
self.attrs = attrs
self.init_selection()
self.set_popdown(self.selection, self.entry)
def grab_focus(self):
return self.entry.grab_focus()
def _readonly_set(self, value):
super(Selection, self)._readonly_set(value)
self.entry.set_sensitive(not value)
def _color_widget(self):
return self.entry.child
def get_value(self):
if not self.entry.child: # entry is destroyed
return
text = self.entry.child.get_text()
value = None
if text:
for txt, val in self._selection.items():
if not val:
continue
if txt[:len(text)].lower() == text.lower():
value = val
if len(txt) == len(text):
break
if not value:
for val, txt in self.inactive_selection:
if txt == text:
value = val
break
return value
def sig_key_press(self, widget, event):
self.send_modified()
def sig_activate(self, widget, event=None):
if not self.field:
return
self.field.set_client(self.record, self.get_value())
@property
def modified(self):
if self.record and self.field:
return self.field.get(self.record) != self.get_value()
return False
def set_value(self, record, field):
field.set_client(record, self.get_value())
def _menu_sig_default_set(self, reset=False):
self.set_value(self.record, self.field)
super(Selection, self)._menu_sig_default_set(reset=reset)
def display(self, record, field):
self.update_selection(record, field)
self.set_popdown(self.selection, self.entry)
child = self.entry.child
if not field:
child.set_text('')
return False
super(Selection, self).display(record, field)
value = field.get(record)
if isinstance(value, (list, tuple)):
# Compatibility with Many2One
value = value[0]
if not value:
child.set_text('')
else:
found = False
for long_text, sel_value in self._selection.items():
if str(sel_value) == str(value):
child.set_text(long_text)
found = True
break
if not found:
for sel_value, long_text in self.selection:
if str(sel_value) == str(value):
self._selection[long_text] = sel_value
model = self.entry.get_model()
i = model.append()
model.set(i, 0, long_text)
child.set_text(long_text)
found = True
break
if not found:
text = self.get_inactive_selection(value)
if text:
child.set_text(text)
found = True
if not found:
child.set_text('')
|