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

# Source Code for Module Camelot.camelot.view.controls.editors.choiceseditor

     1  from PyQt4 import QtGui, QtCore
     2

     3  from customeditor import AbstractCustomEditor
     4  import sip
     5

     6 -class ChoicesEditor(QtGui.QComboBox, AbstractCustomEditor):
     7

     8 -    def __init__(self, parent=None, editable=True, **kwargs):
     9          QtGui.QComboBox.__init__(self, parent)
    10          AbstractCustomEditor.__init__(self)
    11          self.setEnabled(editable)
    12

    13 -    def set_choices(self, choices):
    14          """

    15      :param choices: a list of (value,name) tuples.  name will be displayed in the combobox,

    16      while value will be used within get_value and set_value.  This method changes the items

    17      in the combo box while preserving the current value, even if this value is not in the

    18      new list of choices.

    19          """
    20          if not sip.isdeleted(self):
    21              current_value = self.get_value()
    22              for i in range(self.count(), 0, -1):
    23                  self.removeItem(i-1)
    24              for i, (value, name) in enumerate(choices):
    25                  self.insertItem(i, unicode(name), QtCore.QVariant(value))
    26              self.set_value(current_value)
    27

    28 -    def set_enabled(self, editable=True):
    29          self.setEnabled(editable)
    30

    31 -    def get_choices(self):
    32          """

    33      :rtype: a list of (value,name) tuples

    34      """
    35          from camelot.core.utils import variant_to_pyobject
    36          return [(variant_to_pyobject(self.itemData(i)),unicode(self.itemText(i))) for i in range(self.count())]
    37

    38 -    def set_value(self, value):
    39          if not sip.isdeleted(self):
    40              from camelot.core.utils import variant_to_pyobject
    41              value = AbstractCustomEditor.set_value(self, value)
    42              if value not in (None, NotImplemented):
    43                  for i in range(self.count()):
    44                      if value == variant_to_pyobject(self.itemData(i)):
    45                          self.setCurrentIndex(i)
    46                          return
    47                  # it might happen, that when we set the editor data, the set_choices

    48                  # method has not happened yet, therefore, we temporary set ... in the

    49                  # text while setting the correct data to the editor

    50                  self.insertItem(self.count(), '...', QtCore.QVariant(value))
    51                  self.setCurrentIndex(self.count()-1)
    52

    53 -    def get_value(self):
    54          if not sip.isdeleted(self):
    55              from camelot.core.utils import variant_to_pyobject
    56              current_index = self.currentIndex()
    57              if current_index>=0:
    58                  value = variant_to_pyobject(self.itemData(self.currentIndex()))
    59              else:
    60                  value = None
    61              return AbstractCustomEditor.get_value(self) or value
    62