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

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

      1  #!/usr/bin/python
      2  # -*- coding: utf-8 -*-
      3
      4  """ pyforms.gui.Controls.ControlList
      5
      6  """
      7
      8  from pyforms.gui.Controls.ControlBase import ControlBase
      9  from PyQt4 import uic, QtCore
     10  from PyQt4.QtGui import QWidget, QIcon, QTableWidgetItem, QAbstractItemView
     11  import os
     12
     13  __author__ = "Ricardo Ribeiro"
     14  __copyright__ = ""
     15  __credits__ = "Ricardo Ribeiro"
     16  __license__ = "MIT"
     17  __version__ = "0.0"
     18  __maintainer__ = ["Ricardo Ribeiro", "Carlos Mão de Ferro"]
     19  __email__ = ["ricardojvr at gmail.com", "cajomferro at gmail.com"]
     20  __status__ = "Development"
     21
     22
     23 -class ControlList(ControlBase, QWidget):
     24      """ This class represents a wrapper to the table widget
     25          It allows to implement a list view
     26      """
     27
     28 -    def __init__(self, label="", defaultValue="", plusFunction=None,
     29                   minusFunction=None):
     30          QWidget.__init__(self)
     31
     32          self._plusFunction = plusFunction
     33          self._minusFunction = minusFunction
     34          ControlBase.__init__(self, label, defaultValue)
     35
     36 -    def __repr__(self): return "ControlList "+str(self._value)
     37
     38
     39
     40 -    def initForm(self):
     41          plusFunction = self._plusFunction
     42          minusFunction = self._minusFunction
     43
     44          # Get the current path of the file
     45          rootPath = os.path.dirname(__file__)
     46          # Load the UI for the self instance
     47          uic.loadUi(os.path.join(rootPath, "list.ui"), self)
     48
     49          self.label = self._label
     50
     51          self.tableWidget.currentCellChanged.connect(
     52              self.tableWidgetCellChanged)
     53          self.tableWidget.currentItemChanged.connect(
     54              self.tableWidgetItemChanged)
     55          self.tableWidget.itemSelectionChanged.connect(
     56              self.tableWidgetItemSelectionChanged)
     57          self.tableWidget.model().dataChanged.connect(self._dataChangedEvent)
     58
     59          if plusFunction is None and minusFunction is None:
     60              self.bottomBar.hide()
     61          elif plusFunction is None:
     62              self.plusButton.hide()
     63              self.minusButton.pressed.connect(minusFunction)
     64          elif minusFunction is None:
     65              self.minusButton.hide()
     66              self.plusButton.pressed.connect(plusFunction)
     67          else:
     68              self.plusButton.pressed.connect(plusFunction)
     69              self.minusButton.pressed.connect(minusFunction)
     70
     71 -    def _dataChangedEvent(self, item):
     72          self.dataChangedEvent(
     73              item.row(), item.column(), self.tableWidget.model().data(item))
     74          self.changed()
     75
     76 -    def dataChangedEvent(self, row, col, item): pass
     77
     78 -    def tableWidgetCellChanged(self, nextRow, nextCol, previousRow,
     79                                 previousCol):
     80          self.currentCellChanged(nextRow, nextCol, previousRow, previousCol)
     81          self.changed()
     82
     83 -    def tableWidgetItemChanged(self, current, previous):
     84          self.currentItemChanged(current, previous)
     85          self.changed()
     86
     87 -    def tableWidgetItemSelectionChanged(self): self.itemSelectionChanged()
     88
     89 -    def itemSelectionChanged(self): pass
     90
     91 -    def currentCellChanged(self, nextRow, nextCol, previousRow, previousCol): pass
     92
     93 -    def currentItemChanged(self, current, previous): pass
     94
     95 -    def clear(self):
     96          self.tableWidget.clear()
     97          self.tableWidget.setColumnCount(3)
     98          self.tableWidget.setRowCount(0)
     99
    100 -    def __add__(self, other):
    101
    102          index = self.tableWidget.rowCount()
    103
    104          self.tableWidget.insertRow( index )
    105          if self.tableWidget.currentColumn()<len(other):
    106              self.tableWidget.setColumnCount(len(other))
    107
    108          for i in range(0, len(other)):
    109              v = other[i]
    110              args = [str(v)] if not hasattr(
    111                  v, 'icon') else [QIcon(v.icon), str(v)]
    112              self.tableWidget.setItem(index, i, QTableWidgetItem(*args))
    113
    114          self.tableWidget.resizeColumnsToContents()
    115          return self
    116
    117 -    def __sub__(self, other):
    118
    119          if isinstance(other, int):
    120              if other < 0:
    121                  indexToRemove = self.tableWidget.currentRow()
    122              else:
    123                  indexToRemove = other
    124              self.tableWidget.removeRow(indexToRemove)
    125          return self
    126
    127      @property
    128 -    def horizontalHeaders(self):
    129          return self._horizontalHeaders
    130
    131      @horizontalHeaders.setter
    132 -    def horizontalHeaders(self, horizontalHeaders):
    133          """Set horizontal headers in the table list."""
    134
    135          self._horizontalHeaders = horizontalHeaders
    136
    137          self.tableWidget.setColumnCount(len(horizontalHeaders))
    138          self.tableWidget.horizontalHeader().setVisible(True)
    139
    140          for idx, header in enumerate(horizontalHeaders):
    141              item = QTableWidgetItem()
    142              item.setText(header)
    143              self.tableWidget.setHorizontalHeaderItem(idx, item)
    144
    145 -    def setValue(self, column, row, value):
    146          self.tableWidget.item(row, column).setText(str(value))
    147
    148 -    def getValue(self, column, row):
    149          return str(self.tableWidget.item(row, column).text())
    150
    151      @property
    152 -    def selectEntireRow(self):
    153          return self.tableWidget.selectionBehavior()
    154
    155      @selectEntireRow.setter
    156 -    def selectEntireRow(self, value):
    157          if value:
    158              self.tableWidget.setSelectionBehavior(
    159                  QAbstractItemView.SelectRows)
    160          else:
    161              self.tableWidget.setSelectionBehavior(
    162                  QAbstractItemView.SelectItems)
    163
    164      @property
    165 -    def count(self): return self.tableWidget.rowCount()
    166
    167
    168
    169      @property
    170 -    def value(self):
    171          if hasattr(self, 'tableWidget'):
    172              results = []
    173              for row in range(self.tableWidget.rowCount()):
    174                  r = []
    175                  for col in range(self.tableWidget.columnCount()):
    176                      try:
    177                          r.append(self.getValue(col, row))
    178                      except:
    179                          r.append("")
    180                  results.append(r)
    181              return results
    182          return self._value
    183
    184      @value.setter
    185 -    def value(self, value):
    186          self.clear()
    187          for row in value:
    188              self += row
    189      # TODO: implement += on self.value? I want to add a list of tuples to
    190      # self.value
    191
    192      @property
    193 -    def mouseSelectedRowsIndexes(self):
    194          result = []
    195          for index in self.tableWidget.selectedIndexes():
    196              result.append(index.row())
    197          return list(set(result))
    198
    199      @property
    200 -    def mouseSelectedRowIndex(self):
    201          indexes = self.mouseSelectedRowsIndexes
    202          if len(indexes) > 0:
    203              return indexes[0]
    204          else:
    205              return None
    206
    207      @property
    208 -    def label(self): return self.labelWidget.getText()
    209
    210      @label.setter
    211 -    def label(self, value):
    212          if value != '':
    213              self.labelWidget.setText(value)
    214          else:
    215              self.labelWidget.hide()
    216
    217      @property
    218 -    def form(self): return self
    219
    220      @property
    221 -    def iconSize(self): return self.tableWidget.iconSize()
    222
    223      @iconSize.setter
    224 -    def iconSize(self, value):
    225          if isinstance(value, (tuple, list)):
    226              self.tableWidget.setIconSize(QtCore.QSize(*value))
    227          else:
    228              self.tableWidget.setIconSize(QtCore.QSize(value, value))
    229