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

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

      1  # !/usr/bin/python
      2  # -*- coding: utf-8 -*-
      3
      4  """"pyforms.gui.Controls.Control Tree"""
      5
      6  from pyforms.gui.Controls.ControlBase import ControlBase
      7  from PyQt4.QtGui import QTreeWidget, QTreeWidgetItem, QTreeView
      8  from PyQt4.QtGui import QAbstractItemView
      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 ControlTree(ControlBase, QTreeWidget):
     21      """This class represents a wrapper to the QTreeWidget"""
     22
     23 -    def __init__(self, label='', default=''):
     24          QTreeWidget.__init__(self)
     25          ControlBase.__init__(self, label, default)
     26
     27
     28 -    def initForm(self):
     29          self.setSelectionBehavior(QAbstractItemView.SelectRows)
     30          self.setUniformRowHeights(True)
     31          self.setDragDropMode(QAbstractItemView.InternalMove)
     32          self.setDragEnabled(True)
     33          self.setAcceptDrops(True)
     34
     35          self.model().dataChanged.connect(self.__itemChangedEvent)
     36
     37          self.selectionChanged = self.selectionChanged
     38
     39      @property
     40 -    def showHeader(self):
     41          return self.header().isVisible()
     42
     43      @showHeader.setter
     44 -    def showHeader(self, value):
     45          if value:
     46              self.header().show()
     47          else:
     48              self.header().hide()
     49
     50 -    def __itemChangedEvent(self, item): self.itemChangedEvent(item)
     51
     52 -    def itemChangedEvent(self, item): pass
     53
     54 -    def itemSelectionChanged(self): pass
     55
     56 -    def rowsInsertedEvent(self, parent, start, end):
     57          """ This event is called every time a new row is added to the tree"""
     58          pass
     59
     60 -    def rowsInserted(self, parent, start, end):
     61          super(ControlTree, self).rowsInserted(parent, start, end)
     62          self.rowsInsertedEvent(parent, start, end)
     63
     64 -    def selectionChanged(self, selected, deselected):
     65          super(QTreeView, self).selectionChanged(selected, deselected)
     66          self.itemSelectionChanged()
     67
     68      @property
     69 -    def mouseSelectedRowsIndexes(self):
     70          result = []
     71          for index in self.selectedIndexes():
     72              result.append(index.row())
     73          return list(set(result))
     74
     75      @property
     76 -    def mouseSelectedRowIndex(self):
     77          indexes = self.mouseSelectedRowsIndexes
     78          if len(indexes) > 0:
     79              return indexes[0]
     80          else:
     81              return None
     82
     83      @property
     84 -    def selectedItem(self):
     85          for index in self.selectedIndexes():
     86              item = index.model().itemFromIndex(index)
     87              return item
     88          else:
     89              return None
     90
     91      @property
     92 -    def cells(self):
     93          results = []
     94          for row in range(self.model().rowCount()):
     95              r = []
     96              for col in range(self.model().columnCount()):
     97                  r.append(self.model.item(row, col))
     98
     99              if len(r) > 0:
    100                  results.append(r)
    101
    102          return results
    103
    104 -    def __add__(self, other):
    105          if isinstance(other, QTreeWidgetItem):
    106              self.model().invisibleRootItem().appendRow(other)
    107
    108          elif isinstance(other, list):
    109              for x in other:
    110                  item = QTreeWidgetItem(x)
    111                  self.form.addTopLevelItem(item)
    112          else:
    113              item = QTreeWidgetItem(other)
    114              self.form.addTopLevelItem(item)
    115
    116          self.setFirstColumnSpanned(
    117              self.model().rowCount() - 1, self.rootIndex(), True)
    118          return self
    119
    120 -    def __sub__(self, other):
    121          if isinstance(other, int):
    122              if other < 0:
    123                  indexToRemove = self.mouseSelectedRowIndex
    124              else:
    125                  indexToRemove = other
    126              self.model().removeRow(indexToRemove)
    127          return self
    128
    129      @property
    130 -    def form(self): return self
    131
    132      @property
    133 -    def value(self): return None
    134
    135      @value.setter
    136 -    def value(self, value): self.addTopLevelItem(value)
    137
    138 -    def save(self, data): pass
    139
    140 -    def load(self, data): pass
    141