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

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

      1  from pyforms.gui.Controls.ControlBase import ControlBase
      2  from PyQt4 import uic
      3  from PyQt4 import QtGui, QtCore
      4
      5
      6 -class ControlTreeView(ControlBase, QtGui.QTreeView):
      7
      8 -        def __init__(self, title='untitled'):
      9                  QtGui.QTreeView.__init__(self)
     10                  ControlBase.__init__(self, title)
     11
     12
     13 -        def initForm(self):
     14                  self.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
     15                  self.header().hide()
     16                  self.setUniformRowHeights(True)
     17                  self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
     18                  self.setDragEnabled(True)
     19                  self.setAcceptDrops(True)
     20
     21                  self.setModel(QtGui.QStandardItemModel())
     22                  self.model().itemChanged.connect(self.__itemChangedEvent)
     23
     24                  self.selectionChanged = self.selectionChanged
     25
     26                  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     27                  # populate data
     28                  """
     29                  for i in range(3):
     30                          parent1 = QtGui.QStandardItem('Family {}. Some long status text for sp'.format(i))
     31                          for j in range(3):
     32                                  child1 = QtGui.QStandardItem('Child {}'.format(i*3+j))
     33                                  parent1.appendRow(child1)
     34                          model.appendRow(parent1)
     35                          # span container columns
     36                          view.setFirstColumnSpanned(i, view.rootIndex(), True)
     37                  """
     38
     39
     40
     41
     42 -        def __itemChangedEvent(self, item): self.itemChangedEvent(item)
     43
     44 -        def itemChangedEvent(self, item): pass
     45
     46
     47 -        def itemSelectionChanged(self):pass
     48
     49 -        def selectionChanged(self, selected, deselected ):
     50                  super(QtGui.QTreeView, self.form).selectionChanged(selected, deselected)
     51                  self.itemSelectionChanged()
     52
     53          @property
     54 -        def mouseSelectedRowsIndexes(self):
     55                  result = []
     56                  for index in self.form.selectedIndexes():
     57                          result.append( index.row() )
     58                  return list( set(result) )
     59
     60
     61          @property
     62 -        def mouseSelectedRowIndex(self):
     63                  indexes = self.mouseSelectedRowsIndexes
     64                  if len(indexes)>0: return indexes[0]
     65                  else: return None
     66
     67          @property
     68 -        def selectedItem(self):
     69                  for index in self.form.selectedIndexes():
     70                          item = index.model().itemFromIndex(index)
     71                          return item
     72                  else:
     73                          return None
     74
     75          @property
     76 -        def cells(self):
     77                  results = []
     78                  for row in range(self._model.rowCount()):
     79                          r = []
     80                          for col in range(self._model.columnCount()):
     81                                  r.append( self._model.item(row, col) )
     82                          if len(r)>0: results.append(r)
     83                          # print r, '---'
     84
     85                  return results
     86
     87
     88 -        def __add__(self, other):
     89                  if isinstance(other, TreeItem):
     90                          self._model.invisibleRootItem().appendRow( other )
     91
     92                  elif isinstance(other, list):
     93                          for x in other:
     94                                  item = QtGui.QStandardItem( x )
     95                                  self._model.appendRow( item )
     96                  else:
     97                          item = QtGui.QStandardItem( other )
     98                          self._model.appendRow( item )
     99
    100                  self.form.setFirstColumnSpanned(self._model.rowCount()-1, self.form.rootIndex(), True)
    101                  return self
    102
    103 -        def __sub__(self, other):
    104                  if isinstance(other, int):
    105                          if other < 0:
    106                                  indexToRemove = self.mouseSelectedRowIndex
    107                          else:
    108                                  indexToRemove = other
    109                          self.model.removeRow(indexToRemove)
    110                  return self
    111
    112
    113
    114          @property
    115 -        def value(self):
    116                  return self.form.model().invisibleRootItem()
    117                  return self.recursivelyReadRoot(root)
    118
    119          @value.setter
    120 -        def value(self, value):
    121                  for row in value: self += row
    122
    123
    124 -        def getAllSceneObjects(self): return self._model.getChildrens()
    125
    126
    127          @property
    128 -        def form(self): return self
    129