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 from PyQt4 import QtGui, QtCore
29
30 from camelot.core.utils import ugettext
31 from camelot.view.utils import operator_names
32 from camelot.view.controls import editors
33 from camelot.view.controls.filterlist import filter_changed_signal
34
36 """Widget that allows applying various filter operators on a field"""
37
38 - def __init__(self, entity, field_name, field_attributes, parent):
39 QtGui.QGroupBox.__init__(self, unicode(field_attributes['name']), parent)
40 self._entity, self._field_name, self._field_attributes = entity, field_name, field_attributes
41 self._field_attributes['editable'] = True
42 layout = QtGui.QVBoxLayout()
43 self._operators = field_attributes.get('operators', [])
44 self._choices = [(0, ugettext('All')), (1, ugettext('None')),] + [(i+2, ugettext(operator_names[operator])) for i,operator in enumerate(self._operators)]
45 combobox = QtGui.QComboBox(self)
46 layout.addWidget(combobox)
47 for i,name in self._choices:
48 combobox.insertItem(i, unicode(name))
49 self.connect(combobox, QtCore.SIGNAL('currentIndexChanged(int)'), self.combobox_changed)
50 delegate = self._field_attributes['delegate'](**self._field_attributes)
51 option = QtGui.QStyleOptionViewItem()
52 option.version = 5
53 self._editor = delegate.createEditor( self, option, None )
54 self._editor2 = delegate.createEditor( self, option, None )
55
56 self._editor.set_value(None)
57 self._editor2.set_value(None)
58 self.connect(self._editor, editors.editingFinished, self.editor_editing_finished)
59 self.connect(self._editor2, editors.editingFinished, self.editor_editing_finished)
60 layout.addWidget(self._editor)
61 layout.addWidget(self._editor2)
62 self.setLayout(layout)
63 self._editor.setEnabled(False)
64 self._editor2.setEnabled(False)
65 self._editor2.hide()
66 self._index = 0
67 self._value = None
68 self._value2 = None
69
71 self._index = index
72 if index>=2:
73 _, arity = self.get_operator_and_arity()
74 self._editor.setEnabled(True)
75 if arity > 1:
76 self._editor2.setEnabled(True)
77 self._editor2.show()
78 else:
79 self._editor2.setEnabled(False)
80 self._editor2.hide()
81 else:
82 self._editor.setEnabled(False)
83 self._editor2.setEnabled(False)
84 self._editor2.hide()
85 self.emit(filter_changed_signal)
86
91
93 if self._index==0:
94 return query
95 if self._index==1:
96 return query.filter(getattr(self._entity, self._field_name)==None)
97 field = getattr(self._entity, self._field_name)
98 op, arity = self.get_operator_and_arity()
99 if arity == 1:
100 args = field, self._value
101 elif arity == 2:
102 args = field, self._value, self._value2
103 else:
104 assert False, 'Unsupported operator arity: %d' % arity
105 return query.filter(op(*args))
106
108 op = self._operators[self._index-2]
109 try:
110 func_code = op.func_code
111 except AttributeError:
112 arity = 1
113 else:
114 arity = func_code.co_argcount - 1
115 return op, arity
116