class ObjectListModel (QtCore.QAbstractItemModel): """ TODO """ def __init__(self, header, *tree): QtCore.QAbstractItemModel.__init__(self) self._buffer = LockableRingBuffer() self._header = header l = len(tree[1]) def transform_tree(t): # Die Accessorenlisten sollten immmer die selbe Länge haben. a = to_length(t[1], length=l, fill=empty_accessor) if len(t) >= 3: return AccessorNode(t[0], a, transform_tree(t[2])) else: return AccessorNode(t[0], a) self.accessor_tree = transform_tree(list(tree)) def invalid_index(self, custom = True, index = QtCore.QModelIndex()): if custom: index = CustomModelIndex(index = index, obj = None, accessors = [], child = self.accessor_tree, parent = None) index.parent = index return index def valid_index(self, row, column, obj, accessors, child, parent): i = self._buffer.reserve() mindex = self.createIndex(row, column, i) index = CustomModelIndex(index = mindex, obj = obj, accessors = accessors, child = child, parent = parent) index.level = 1 + parent.level self._buffer[i] = index return index def _data2index(self, data): pass def _index2data(self, index): pass def ensure_nice_index(self, index): if index.isValid(): i = index.internalId() return self._buffer[i] else: return self.invalid_index(index=index) def rowCount(self, parent = QtCore.QModelIndex()): parent = self.ensure_nice_index(parent) if parent.child: return len(parent.child.list(parent.object)) else: return 0 def columnCount(self, parent = QtCore.QModelIndex()): parent = self.ensure_nice_index(parent) if parent.child: return len(parent.child.accessors) else: return 0 def parent(self, child): child = self.ensure_nice_index(child) if child.parent: return child.parent.index else: return self.invalid_index(False) def data(self, index, role = Qt.DisplayRole): index = self.ensure_nice_index(index) if index.isValid() and role == Qt.DisplayRole: d = index.accessors[index.column()](index.object) return d else: return None def data_raw(self, index): index = self.ensure_nice_index(index) if index.isValid(): return index.object else: return None def index(self, row, column, parent = QtCore.QModelIndex()): parent = self.ensure_nice_index(parent) if parent.child: lst = parent.child.list(parent.object) index = self.valid_index(row = row, column = column, obj = lst[row], accessors = parent.child.accessors, child = parent.child.child, parent = parent) return index.index else: raise InvalidTreeAdress def flags(self, index): # TODO invalid results ? if index.isValid(): return Qt.ItemIsEnabled | Qt.ItemIsSelectable else: return 0 def headerData(self, section, orientation, role = Qt.DisplayRole): if orientation == Qt.Horizontal and role == Qt.DisplayRole: return self._header[section] else: return None # TODO Benachrichtigung über Änderungen def attach_collection(self, collector): @pyqtSlot() def update(): self.reset() collector.add_changed_slot(update)