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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
# 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 math
import operator
from gi.repository import Gdk, GLib, GObject, Gtk
from tryton.common import RPCException, RPCExecute, eval_domain
class SelectionMixin(object):
def __init__(self, *args, **kwargs):
super(SelectionMixin, self).__init__(*args, **kwargs)
self.nullable_widget = True
self.selection = None
self.inactive_selection = []
self._last_domain = None
self._values2selection = {}
self._domain_cache = {}
def init_selection(self, value=None):
if value is None:
value = dict((k, None)
for k in self.attrs.get('selection_change_with') or [])
key = freeze_value(value)
selection = self.attrs.get('selection', [])[:]
help_ = self.attrs.get('help_selection', {})
if (not isinstance(selection, (list, tuple))
and key not in self._values2selection):
try:
if self.attrs.get('selection_change_with'):
selection = RPCExecute('model', self.model_name, selection,
value)
else:
selection = RPCExecute('model', self.model_name, selection)
except RPCException:
selection = []
self._values2selection[key] = selection
elif key in self._values2selection:
selection = self._values2selection[key]
if self.attrs.get('sort', True):
selection.sort(key=operator.itemgetter(1))
self.selection = selection[:]
self.help = help_
self.inactive_selection = []
def update_selection(self, record, field):
if not field:
return
if not self.selection:
self.init_selection()
domain = field.domain_get(record)
if 'relation' not in self.attrs:
change_with = self.attrs.get('selection_change_with') or []
value = record._get_on_change_args(change_with)
value.pop('id', None)
self.init_selection(value)
self.filter_selection(domain, record, field)
else:
context = field.get_context(record)
domain_cache_key = (freeze_value(domain), freeze_value(context))
if domain_cache_key in self._domain_cache:
self.selection = self._domain_cache[domain_cache_key]
self._last_domain = (domain, context)
if (domain, context) == self._last_domain:
return
fields = ['rec_name']
help_field = self.attrs.get('help_field')
if help_field:
fields.append(help_field)
try:
result = RPCExecute('model', self.attrs['relation'],
'search_read', domain, 0, None, None, fields,
context=context, process_exception=False)
except RPCException:
result = False
if isinstance(result, list):
selection = [(x['id'], x['rec_name']) for x in result]
if self.nullable_widget:
selection.append((None, ''))
if help_field:
help_ = {x['id']: x[help_field] for x in result}
else:
help_ = {}
self._last_domain = (domain, context)
self._domain_cache[domain_cache_key] = selection
else:
selection = []
if self.nullable_widget:
selection.append((None, ''))
help_ = {}
self._last_domain = None
self.selection = selection[:]
self.help = help_
self.inactive_selection = []
def filter_selection(self, domain, record, field):
if not domain:
return
def _value_evaluator(value):
return eval_domain(domain, {
self.field_name: value[0],
})
def _model_evaluator(allowed_models):
def test(value):
return value[0] in allowed_models or not allowed_models
return test
type_ = field.attrs['type']
if type_ == 'reference':
allowed_models = field.get_models(record)
evaluator = _model_evaluator(allowed_models)
elif type_ == 'multiselection':
return
else:
evaluator = _value_evaluator
self.selection = list(filter(evaluator, self.selection))
def get_inactive_selection(self, value):
if 'relation' not in self.attrs:
return ''
if value is None:
return ''
for val, text in self.inactive_selection:
if str(val) == str(value):
return text
else:
try:
result, = RPCExecute('model', self.attrs['relation'], 'read',
[value], ['rec_name'])
self.inactive_selection.append((result['id'],
result['rec_name']))
return result['rec_name']
except RPCException:
return ''
def selection_shortcuts(entry):
def key_press(widget, event):
if (event.type == Gdk.EventType.KEY_PRESS
and event.state & Gdk.ModifierType.CONTROL_MASK
and event.keyval == Gdk.KEY_space):
widget.popup()
entry.connect('key_press_event', key_press)
return entry
def freeze_value(value):
if isinstance(value, dict):
return tuple(sorted((k, freeze_value(v))
for k, v in value.items()))
elif isinstance(value, (list, set, tuple)):
return tuple(freeze_value(v) for v in value)
else:
return value
class PopdownMixin(object):
def set_popdown(self, selection, entry):
child = entry.get_child()
if not child: # entry is destroyed
return
model, lengths = self.get_popdown_model(selection)
entry.set_model(model)
entry.set_entry_text_column(0)
completion = Gtk.EntryCompletion()
completion.set_inline_selection(True)
completion.set_model(model)
child.set_completion(completion)
if lengths:
pop = sorted(lengths, 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 abs(x - average) < (deviation * 2)),
10),
10)
else:
width = 10
child.set_width_chars(width)
if lengths:
child.set_max_length(max(lengths))
completion.set_text_column(0)
completion.connect('match-selected', self.match_selected, entry)
def get_popdown_model(self, selection):
model = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_PYOBJECT)
lengths = []
for (value, name) in selection:
name = str(name)
model.append((name, value))
lengths.append(len(name))
return model, lengths
def match_selected(self, completion, model, iter_, entry):
value, = model.get(iter_, 1)
model = entry.get_model()
for i, values in enumerate(model):
if values[1] == value:
GLib.idle_add(entry.set_active, i)
break
def get_popdown_value(self, entry, index=1):
active = entry.get_active()
if active < 0:
return None
else:
model = entry.get_model()
return model[active][index]
def get_popdown_text(self, entry):
return self.get_popdown_value(entry, index=0)
def set_popdown_value(self, entry, value):
active = -1
model = entry.get_model()
for i, selection in enumerate(model):
if selection[1] == value:
active = i
break
else:
if value:
return False
entry.set_active(active)
if active == -1:
# When setting no item GTK doesn't clear the entry
entry.get_child().set_text('')
return True
|