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

# Source Code for Module Camelot.camelot.view.controls.delegates.many2onedelegate

      1  from PyQt4 import QtGui, QtCore
      2  from PyQt4.QtCore import Qt
      3

      4  from customdelegate import CustomDelegate
      5  from camelot.view.controls import editors
      6  from camelot.core.utils import variant_to_pyobject, create_constant_function
      7  from camelot.view.proxy import ValueLoading
      8

      9  import logging
     10  logger = logging.getLogger('camelot.view.controls.delegates.many2onedelegate')
     11

     12 -class Many2OneDelegate(CustomDelegate):
     13      """Custom delegate for many 2 one relations

     14

     15    .. image:: ../_static/manytoone.png

     16    """
     17

     18      editor = editors.Many2OneEditor
     19

     20 -    def __init__(self,

     21                   parent=None,

     22                   admin=None,

     23                   embedded=False,

     24                   editable=True,

     25                   **kwargs):
     26          logger.debug('create many2onecolumn delegate')
     27          assert admin != None
     28          CustomDelegate.__init__(self, parent, editable, **kwargs)
     29          self.admin = admin
     30          self._embedded = embedded
     31          self._kwargs = kwargs
     32          self._width = self._width * 2
     33

     34 -    def paint(self, painter, option, index):
     35          painter.save()
     36          self.drawBackground(painter, option, index)
     37

     38

     39

     40          value = index.data(Qt.DisplayRole).toString()
     41

     42          background_color = QtGui.QColor(index.model().data(index, Qt.BackgroundRole))
     43

     44          if( option.state & QtGui.QStyle.State_Selected ):
     45              painter.fillRect(option.rect, option.palette.highlight())
     46              fontColor = QtGui.QColor()
     47              if self.editable:
     48                  Color = option.palette.highlightedText().color()
     49                  fontColor.setRgb(Color.red(), Color.green(), Color.blue())
     50              else:
     51                  fontColor.setRgb(130,130,130)
     52          else:
     53              if self.editable:
     54                  painter.fillRect(option.rect, background_color)
     55                  fontColor = QtGui.QColor()
     56                  fontColor.setRgb(0,0,0)
     57              else:
     58                  painter.fillRect(option.rect, option.palette.window())
     59                  fontColor = QtGui.QColor()
     60                  fontColor.setRgb(130,130,130)
     61

     62          painter.setPen(fontColor.toRgb())
     63

     64          painter.drawText(option.rect.x()+2,

     65                           option.rect.y(),

     66                           option.rect.width()-4,

     67                           option.rect.height(),

     68                           Qt.AlignVCenter | Qt.AlignLeft,

     69                           unicode(value))
     70

     71

     72          painter.restore()
     73

     74 -    def createEditor(self, parent, option, index):
     75          if self._embedded:
     76              editor = editors.EmbeddedMany2OneEditor(self.admin, parent, editable=self.editable)
     77          else:
     78              editor = editors.Many2OneEditor(self.admin, parent, editable=self.editable)
     79          if option.version != 5:
     80              editor.setAutoFillBackground(True)
     81          self.connect(editor,

     82                       QtCore.SIGNAL('editingFinished()'),

     83                       self.commitAndCloseEditor)
     84          return editor
     85

     86 -    def setEditorData(self, editor, index):
     87          value = variant_to_pyobject(index.data(Qt.EditRole))
     88          if value!=ValueLoading:
     89              editor.set_value(create_constant_function(value))
     90          else:
     91              editor.set_value(ValueLoading)
     92

     93 -    def setModelData(self, editor, model, index):
     94          if editor.entity_instance_getter:
     95              model.setData(index, editor.entity_instance_getter)
     96

     97  #  def sizeHint(self, option, index):

     98  #    return self._dummy_editor.sizeHint()
     99