Package pyforms :: Package gui :: Package Controls :: Module ControlCombo |
[hide private]
[frames] | no frames]

# Source Code for Module pyforms.gui.Controls.ControlCombo

      1  #!/usr/bin/python
      2  # -*- coding: utf-8 -*-
      3
      4  """ pyforms.gui.Controls.ControlCombo """
      5
      6  import pyforms.Utils.tools as tools
      7  from PyQt4 import uic
      8  from pyforms.gui.Controls.ControlBase import ControlBase
      9
     10  __author__ = "Ricardo Ribeiro"
     11  __copyright__ = ""
     12  __credits__ = "Ricardo Ribeiro"
     13  __license__ = "MIT"
     14  __version__ = "0.0"
     15  __maintainer__ = ["Ricardo Ribeiro", "Carlos Mão de Ferro"]
     16  __email__ = ["ricardojvr at gmail.com", "cajomferro at gmail.com"]
     17  __status__ = "Development"
     18
     19
     20 -class ControlCombo(ControlBase):
     21      """This class represents a wrapper to the combo box"""
     22
     23      _items = None
     24
     25 -    def initForm(self):
     26          control_path = tools.getFileInSameDirectory(__file__, "comboInput.ui")
     27          self._form = uic.loadUi(control_path)
     28          self._form.label.setText(self._label)
     29          self._form.comboBox.currentIndexChanged.connect(
     30              self.currentIndexChanged)
     31          self._form.comboBox.activated.connect(
     32              self._activated)
     33          self._form.comboBox.highlighted.connect(
     34              self._highlighted)
     35
     36          self._items = {}
     37
     38          self._addingItem = False
     39
     40 -    def currentIndexChanged(self, index):
     41          """Called when the user chooses an item in the combobox and
     42          the selected choice is different from the last one selected.
     43          @index: item's index
     44          """
     45          if not self._addingItem:
     46              item = self._form.comboBox.currentText()
     47              if len(item) >= 1:
     48                  ControlBase.value.fset(self, self._items[str(item)])
     49
     50 -    def _activated(self, index):
     51          self.activated(index)
     52
     53 -    def activated(self, index):
     54          """Called when the user chooses an item in the combobox.
     55          Note that this signal happens even when the choice is not changed
     56          @index: item's index
     57          """
     58          pass
     59
     60 -    def _highlighted(self, index):
     61          """Called when an item in the combobox popup
     62           list is highlighted by the user.
     63           @index: item's index
     64          """
     65          self.highlighted(index)
     66
     67 -    def highlighted(self, index):
     68          pass
     69
     70 -    def addItem(self, label, value=None):
     71          self._addingItem = True
     72          if not (label in self._items.keys()):
     73              self._form.comboBox.addItem(label)
     74
     75          firstValue = False
     76          if self._items == {}:
     77              firstValue = True
     78
     79          if value is None:
     80              self._items[label] = label
     81          else:
     82              self._items[label] = value
     83          self._addingItem = False
     84
     85          if firstValue:
     86              self.value = self._items[label]
     87
     88 -    def clearItems(self):
     89          self._items = {}
     90          self._value = None
     91          self._form.comboBox.clear()
     92
     93      @property
     94 -    def items(self): return self._items.items()
     95
     96      @property
     97 -    def value(self): return self._value
     98
     99      @value.setter
    100 -    def value(self, value):
    101          for key, val in self.items:
    102              if value == val:
    103                  index = self._form.comboBox.findText(key)
    104                  self._form.comboBox.setCurrentIndex(index)
    105                  if self._value != value:
    106                      self.changed()
    107                  self._value = val
    108
    109      @property
    110 -    def text(self): return str(self._form.comboBox.currentText())
    111
    112      @text.setter
    113 -    def text(self, value):
    114          for key, val in self.items:
    115              if value == key:
    116                  self.value = val
    117                  break
    118