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

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

     1  import logging
     2  logger = logging.getLogger('camelot.view.controls.delegates.delegatemanager')
     3

     4  from PyQt4 import QtGui, QtCore
     5  from PyQt4.QtCore import Qt
     6

     7 -class DelegateManager(QtGui.QItemDelegate):
     8      """Manages custom delegates, should not be used by the application

     9    developer

    10    """
    11

    12 -    def __init__(self, parent=None, **kwargs):
    13          QtGui.QItemDelegate.__init__(self, parent)
    14          self.delegates = {}
    15

    16 -    def set_columns_desc(self, columnsdesc):
    17          self.columnsdesc = columnsdesc
    18

    19 -    def get_column_delegate(self, column):
    20          try:
    21              return self.delegates[column]
    22          except KeyError:
    23              logger.error('Programming Error, no delegate available for column %s'%column)
    24              logger.error('Available columns : %s'%unicode(self.delegates.keys()))
    25              raise KeyError
    26

    27 -    def insertColumnDelegate(self, column, delegate):
    28          """Inserts a custom column delegate"""
    29          logger.debug('inserting delegate for column %s' % column)
    30          assert delegate
    31          delegate.setParent(self)
    32          self.delegates[column] = delegate
    33          self.connect(delegate, QtCore.SIGNAL('commitData(QWidget*)'), self.commitData)
    34          self.connect(delegate, QtCore.SIGNAL('closeEditor(QWidget*)'), self.closeEditor)
    35

    36 -    def commitData(self, editor):
    37          self.emit(QtCore.SIGNAL('commitData(QWidget*)'), editor)
    38

    39 -    def closeEditor(self, editor):
    40          self.emit(QtCore.SIGNAL('closeEditor(QWidget*)'), editor)
    41

    42 -    def removeColumnDelegate(self, column):
    43          """Removes custom column delegate"""
    44          logger.debug('removing a custom column delegate')
    45          if column in self.delegates:
    46              del self.delegates[column]
    47

    48 -    def paint(self, painter, option, index):
    49          """Use a custom delegate paint method if it exists"""
    50          delegate = self.get_column_delegate(index.column())
    51          delegate.paint(painter, option, index)
    52

    53 -    def createEditor(self, parent, option, index):
    54          """Use a custom delegate createEditor method if it exists"""
    55          delegate = self.get_column_delegate(index.column())
    56          editor = delegate.createEditor(parent, option, index)
    57          assert editor
    58          return editor
    59

    60 -    def setEditorData(self, editor, index):
    61          """Use a custom delegate setEditorData method if it exists"""
    62          logger.debug('setting editor data for column %s' % index.column())
    63          try:
    64              delegate = self.get_column_delegate(index.column())
    65              delegate.setEditorData(editor, index)
    66          except Exception, e:
    67              logger.error('Programming Error : could not set editor data for editor at column %s'%(index.column()), exc_info=e)
    68              logger.error('value that could not be set : %s'%unicode(index.model().data(index, Qt.EditRole)))
    69

    70 -    def setModelData(self, editor, model, index):
    71          """Use a custom delegate setModelData method if it exists"""
    72          logger.debug('setting model data for column %s' % index.column())
    73          delegate = self.get_column_delegate(index.column())
    74          delegate.setModelData(editor, model, index)
    75

    76 -    def sizeHint(self, option, index):
    77          option = QtGui.QStyleOptionViewItem()
    78          delegate = self.get_column_delegate(index.column())
    79          return delegate.sizeHint(option, index)
    80