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

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

      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  import logging
     29  logger = logging.getLogger('camelot.view.controls.delegates.comboboxdelegate')
     30
     31  from customdelegate import CustomDelegate, DocumentationMetaclass
     32  from PyQt4 import QtGui, QtCore
     33  from PyQt4.QtGui import QComboBox, QItemDelegate
     34  from PyQt4.QtCore import QVariant, QString, Qt
     35
     36  from camelot.view.model_thread import post
     37  from camelot.view.controls import editors
     38  from camelot.core.utils import variant_to_pyobject
     39  from camelot.view.proxy import ValueLoading
     40
     41 -class ComboBoxDelegate(CustomDelegate):
     42
     43      __metaclass__ = DocumentationMetaclass
     44      editor = editors.ChoicesEditor
     45
     46 -    def __init__(self, parent=None, choices=[], editable=True, **kwargs):
     47          CustomDelegate.__init__(self, parent, editable=editable, **kwargs)
     48          self.choices = choices
     49
     50 -    def setEditorData(self, editor, index):
     51          value = variant_to_pyobject(index.data(Qt.EditRole))
     52          editor.set_value(value)
     53
     54          if callable(self.choices):
     55              def create_choices_getter(model, row):
     56                  def choices_getter():
     57                      try:
     58                          return list(self.choices(model._get_object(row)))
     59                      except Exception, e:
     60                          logger.error('Programming error in choices getter for combo box', exc_info=e)
     61                      return []
     62                  return choices_getter
     63              post(create_choices_getter(index.model(), index.row()),
     64                   editor.set_choices)
     65          else:
     66              editor.set_choices(self.choices)
     67
     68 -    def paint(self, painter, option, index):
     69          painter.save()
     70          self.drawBackground(painter, option, index)
     71          value = variant_to_pyobject(index.data(Qt.DisplayRole))
     72          if value in (None, ValueLoading):
     73              value = ''
     74          c = index.model().data(index, Qt.BackgroundRole)
     75
     76          # let us be safe Qt.BackgroundRole valid only if set
     77          if c.type() == QVariant.Invalid:
     78              background_color = QtGui.QBrush()
     79          else:
     80              background_color = QtGui.QColor(c)
     81
     82          rect = option.rect
     83          rect = QtCore.QRect(rect.left() + 3,
     84                              rect.top() + 6,
     85                              rect.width() - 5,
     86                              rect.height())
     87
     88          if (option.state & QtGui.QStyle.State_Selected):
     89              painter.fillRect(option.rect, option.palette.highlight())
     90              fontColor = QtGui.QColor()
     91              if self.editable:
     92                  Color = option.palette.highlightedText().color()
     93                  fontColor.setRgb(Color.red(), Color.green(), Color.blue())
     94              else:
     95                  fontColor.setRgb(130, 130, 130)
     96          else:
     97              if self.editable:
     98                  painter.fillRect(option.rect, background_color)
     99                  fontColor = QtGui.QColor()
    100                  fontColor.setRgb(0, 0, 0)
    101              else:
    102                  painter.fillRect(option.rect, option.palette.window())
    103                  fontColor = QtGui.QColor()
    104                  fontColor.setRgb(130, 130, 130)
    105
    106          painter.setPen(fontColor.toRgb())
    107          rect = QtCore.QRect(option.rect.left()+2,
    108                              option.rect.top(),
    109                              option.rect.width()-4,
    110                              option.rect.height())
    111          painter.drawText(rect.x(),
    112                           rect.y(),
    113                           rect.width(),
    114                           rect.height(),
    115                           Qt.AlignVCenter | Qt.AlignLeft,
    116                           unicode(value))
    117          painter.restore()
    118