Package Camelot :: Package camelot :: Package view :: Package controls :: Module filter_operator |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.controls.filter_operator

      1  #  ============================================================================
      2  #
      3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.
      4  #  www.conceptive.be / project-camelot@conceptive.be
      5  #
      6  #  This file is part of the Camelot Library.
      7  #
      8  #  This file may be used under the terms of the GNU General Public
      9  #  License version 2.0 as published by the Free Software Foundation
     10  #  and appearing in the file LICENSE.GPL included in the packaging of
     11  #  this file.  Please review the following information to ensure GNU
     12  #  General Public Licensing requirements will be met:
     13  #  http://www.trolltech.com/products/qt/opensource.html
     14  #
     15  #  If you are unsure which license is appropriate for your use, please
     16  #  review the following information:
     17  #  http://www.trolltech.com/products/qt/licensing.html or contact
     18  #  project-camelot@conceptive.be.
     19  #
     20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
     21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     22  #
     23  #  For use of this library in commercial applications, please contact
     24  #  project-camelot@conceptive.be
     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
     35 -class FilterOperator(QtGui.QGroupBox):
     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          # explicitely set a value, otherways the current value remains ValueLoading
     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
     70 -    def combobox_changed(self, index):
     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
     87 -    def editor_editing_finished(self):
     88          self._value = self._editor.get_value()
     89          self._value2 = self._editor2.get_value()
     90          self.emit(filter_changed_signal)
     91
     92 -    def decorate_query(self, query):
     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
    107 -    def get_operator_and_arity(self):
    108          op = self._operators[self._index-2]
    109          try:
    110              func_code = op.func_code
    111          except AttributeError:
    112              arity = 1 # probably a builtin function, assume arity == 1
    113          else:
    114              arity = func_code.co_argcount - 1
    115          return op, arity
    116