def parent(self, index): if not index.isValid(): return QModelIndex() child_item = index.internalPointer() parent_item = child_item.next if not parent_item: # hidden top level node doesn't have an index return QModelIndex() else: # provenance only has a single "previous", so row is always 0 return self.createIndex(0, 0, parent_item)
def test_rmf_hierarchy_model_none(self): """Test RMFHierarchyModel class with null hierarchy""" resolutions = set((1.0, 10.0)) m = src.tool._RMFHierarchyModel(None, resolutions) self.assertEqual(m.columnCount(None), 1) self.assertEqual(m.rowCount(QModelIndex()), 0) self.assertFalse(m.index(0, 0, QModelIndex()).isValid()) self.assertFalse(m.parent(QModelIndex()).isValid()) self.assertIsNone(m.data(QModelIndex(), Qt.DisplayRole))
def parent(self, index): if not index.isValid(): return QModelIndex() child_item = index.internalPointer() parent_item = child_item.parent if parent_item is None: return QModelIndex() parent_item = parent_item() if parent_item.parent is None: row = self.rmf_features.index(parent_item) else: row = parent_item.parent().children.index(parent_item) return self.createIndex(row, 0, parent_item)
def index_for_node(self, rmf_node): """Return the index for a given node in the hierarchy""" parent = rmf_node.parent if parent is None: return self.index(0, 0, QModelIndex()) else: parent = parent() try: row = parent._filtered_children.index(rmf_node) except ValueError: # The node is not in the (filtered) hierarchy return QModelIndex() return self.createIndex(row, 0, rmf_node)
def index(self, row, column, parent): if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): # top level only has one child (the top of the RMF hierarchy) # so return that if self.rmf_hierarchy is None: return QModelIndex() else: return self.createIndex(row, column, self.rmf_hierarchy) else: parent_item = parent.internalPointer() return self.createIndex(row, column, parent_item._filtered_children[row])
def _is_item_dropped(self, event, strict=False): """ Returns whether or not an item has been dropped in given event :param event: QDropEvent :param strict: bool, True to handle ordered alphabetically list; False otherwise. :return: bool """ is_dropped = False index = self.indexAt(event.pos()) if event.source == self and event.dropAction() == Qt.MoveAction or \ self.dragDropMode() == QAbstractItemView.InternalMove: top_index = QModelIndex() col = -1 row = -1 event_list = [event, row, col, top_index] if self._drop_on(event_list): event, row, col, top_index = event_list if row > -1: if row == index.row() - 1: is_dropped = False elif row == -1: is_dropped = True elif row == index.row() + 1: is_dropped = False if strict else True return is_dropped
def test_rmf_provenance_model(self): """Test RMFProvenanceModel class""" f1 = make_provenance("f1", 1) f2 = make_provenance("f2", 2) child = make_provenance("child", 3) f1.previous = child child.next = weakref.proxy(f1) provs = [f1, f2] m = src.tool._RMFProvenanceModel(provs) top = QModelIndex() self.assertEqual(m.columnCount(None), 1) self.assertEqual(m.rowCount(top), 2) f1_ind = m.createIndex(0, 0, f1) child_ind = m.createIndex(0, 0, child) self.assertEqual(m.columnCount(f1_ind), 1) # Test indices self.assertEqual(m.index(0, 0, top).internalPointer().name, 'f1') self.assertEqual(m.index(1, 0, top).internalPointer().name, 'f2') self.assertFalse(m.index(2, 0, top).isValid()) self.assertEqual(m.index(0, 0, f1_ind).internalPointer().name, 'child') # No parents for top level self.assertFalse(m.parent(top).isValid()) self.assertFalse(m.parent(f1_ind).isValid()) self.assertEqual(m.parent(child_ind).internalPointer().name, 'f1') self.assertEqual(m.data(f1_ind, Qt.DisplayRole), "f1") self.assertIsNone(m.data(f1_ind, Qt.SizeHintRole))
def parent(self, index): """ Overrides parent base function :param index: QModelIndex, QModelIndex to be used in lookup :return: QModelIndex, QModelIndex of the respective parent item """ if not index.isValid(): return QModelIndex() child_item = index.internalPointer() parent_item = child_item.parent() if parent_item is None or parent_item == self._root: return QModelIndex() return self.createIndex(parent_item.row(), parent_item.column(), parent_item)
def index(self, row, column, parent): if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): return self.createIndex(row, column, self.rmf_provenance[row]) else: parent_item = parent.internalPointer() return self.createIndex(row, column, parent_item.previous)
def index(self, row, column, parent): if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): parent_list = self.rmf_features else: parent_list = parent.internalPointer().children return self.createIndex(row, column, parent_list[row])
def insertColumns(self, position, columns, parent=QModelIndex()): self.beginInsertColumns(parent, position, position + columns - 1) row_count = len(self._items) for i in range(columns): for j in range(row_count): self._items[j].insert(position, '') self.endInsertColumns() return True
def remove_item(self, item, parent=QModelIndex()): """ Removes an existing AbstractDataTreeItem from the model :param item: AbstractDataTreeItem, item to remove :param parent: QModelIndex, index of the parent item in the model :return: bool, True if the removal was successful; False otherwise """ return self.removeRows(item.row(), 1, parent)
def removeColumns(self, position, columns, parent=QModelIndex()): self.beginRemoveColumns(parent, position, position + columns - 1) success = self.rootItem.removeColumns(position, columns) self.endRemoveColumns() if self.rootItem.columnCount() == 0: self.removeRows(0, self.rowCount()) return success
def columnCount(self, parent=QModelIndex()): """ Overrides columnCount base function Returns the number of columns in the model :param parent: QModelIndex :return: int """ parent_item = self.item(parent) return parent_item.column_count()
def rowCount(self, parent=QModelIndex()): """ Overrides rowCount base function Returns the number of rows in this model :param parent: QModelIndex :return: int """ parent_item = self.item(parent) return parent_item.row_count()
def index(self, row, column, parent=QModelIndex()): """ Returns the index of the internal data item in the model specified by the given row, column and parent index :param row: int, row index into the model :param column: int column index into the model :param parent: QModelIndex, option parent QModelIndex :return: QModelIndex, QModelIndex of the lookuo operation """ if not self.hasIndex(row, column, parent) or (parent.isValid() and parent.column() != 0): return QModelIndex() parent_item = self.item(parent) child_item = parent_item.child(row) if child_item: return self.createIndex(row, column, child_item) else: return QModelIndex()
def removeRows(self, position, rows, parent=QModelIndex()): """ Removes a row from the model """ self.beginRemoveRows(parent, position, position + rows - 1) for i in range(rows): value = self._items[position] self._items.remove(value) self.endRemoveRows() return True
def item_index(self, item): """ Returns the index for a given item :param item: BaseTreeItem, item already in the model :return: QModelIndex, lookup operation """ if not item or item == self._root: return QModelIndex() return self.createIndex(item.row(), item.column(), item)
def insertRows(self, position, rows, parent=QModelIndex()): """ Inserts a row with empty AbstractDataItems into the model """ self.beginInsertRows(parent, position, position + rows - 1) for i in range(rows): self._items.insert(position, 'test') self.endInsertRows() return True
def parent(self, index): """Get the parent of the given index (as another index)""" if not index.isValid(): return QModelIndex() child_item = index.internalPointer() parent_item = child_item.parent if parent_item is None: # hidden top level node doesn't have an index return QModelIndex() else: parent_item = parent_item() if parent_item.parent is None: # top of the RMF hierarchy is always the 0th row of the # hidden top level node row = 0 else: # otherwise, look up the parent in the grandparent's list of # children to determine its row row = parent_item.parent()._filtered_children.index( parent_item) return self.createIndex(row, 0, parent_item)
def insertRows(self, position, rows, parent=QModelIndex()): """ Inserts a new item into the table """ self.beginInsertRows(parent, position, position + rows - 1) for i in range(rows): default_values = ['' for i in range(self.columnCount())] self._items.insert(position, default_values) self.endInsertRows() return True
def expand_tree(self, root_node): """ Expands all the collapsed elements in a tree starting at the rootNode :param root_node: Start node from which we start expanding the tree """ parent = root_node.parent() parent_id = self.model().createIndex(parent.row(), 0, parent) if parent else QModelIndex() index = self.model().index(root_node.row(), 0, parent_id) self.setExpanded(index, True) for child in root_node.children: self.expand_tree(child)
def append_item(self, item): """ Appends an existing AbstractDataItem into the model :param item: AbstractDataItem :return: bool """ self.beginInsertRows(QModelIndex(), len(self._items), len(self._items)) self._item_insert(item) self.endInsertRows() return True
def test_rmf_hierarchy_model(self): """Test RMFHierarchyModel class""" root = make_node("root", 0) child1 = make_node("child1", 1) child2 = make_node("child2", 2) grandchild = make_node("grandchild", 3) child2.add_children([grandchild]) root.add_children((child1, child2)) resolutions = set((None, )) m = src.tool._RMFHierarchyModel(root, resolutions) self.assertEqual(m.columnCount(None), 1) # Top level has one child (RMF root) self.assertEqual(m.rowCount(QModelIndex()), 1) # RMF root has two children ind = m.createIndex(0, 0, root) self.assertEqual(m.rowCount(ind), 2) # Test indices under RMF root self.assertEqual(m.index(0, 0, ind).internalPointer().name, 'child1') self.assertEqual(m.index(1, 0, ind).internalPointer().name, 'child2') self.assertFalse(m.index(2, 0, ind).isValid()) # Test top level index self.assertEqual( m.index(0, 0, QModelIndex()).internalPointer().name, 'root') i = m.index_for_node(child2) self.assertEqual(i.row(), 1) self.assertEqual(i.column(), 0) self.assertEqual(i.internalPointer().name, 'child2') # Top level doesn't have a parent self.assertFalse(m.parent(ind).isValid()) self.assertFalse(m.parent(QModelIndex()).isValid()) childind = m.createIndex(0, 0, child1) self.assertEqual(m.parent(childind).internalPointer().name, 'root') grandchildind = m.createIndex(0, 0, grandchild) parentind = m.parent(grandchildind) self.assertEqual(parentind.internalPointer().name, 'child2') self.assertEqual(parentind.row(), 1) self.assertEqual(m.data(childind, Qt.DisplayRole), "child1") self.assertIsNone(m.data(childind, Qt.SizeHintRole))
def append_item(self, item): """ Appends an existing AbstractDataItem into the model :param item: AbstractDataItem :return: bool """ next_index = self.rowCount() last_index = next_index self.beginInsertRows(QModelIndex(), next_index, last_index) self._item_insert(item) self.endInsertRows() return True
def append_item(self, item, parent=QModelIndex()): """ Appends an existing AbstractDataTreeItem into the model :param item: AbstractDataTreeItem, item to insert :param parent: QModelIndex, index of the parent item in the model :return: bool, True if the insertion was successful; False otherwise """ parent_item = self.item(parent) next_index = parent_item.child_count() last_index = next_index self.beginInsertRows(parent, next_index, last_index) self._item_insert(parent_item, item, next_index) self.endInsertRows() return self.item_index(item)
def insertRows(self, position, rows, parent=QModelIndex()): """ Inserts a new BaseTreeItem into the model :param position: int, position to insert the row :param rows: int, number of rows to insert :param parent: QModelIndex, index of the parent item in the model :return: bool, True if the insertion was successful; False otherwise """ parent_item = self.item(parent) end_index = position + rows next_index = position last_index = end_index - 1 self.beginInsertRows(parent, next_index, last_index) for i in range(next_index, end_index): self._item_insert(parent_item, self.create_item(parent_item), i) self.endInsertRows() return True
def removeRows(self, position, rows, parent=QModelIndex()): """ Removes BaseTreeItem from the model :param position: int, position to remove the row :param rows: int, number of rows to remove :param parent: QModelIndex, index of the parent item in the model :return: bool, True if the removal was successful; False otherwise """ parent_item = self.item(parent) end_index = position + rows next_index = position last_index = end_index - 1 self.beginRemoveRows(parent, next_index, last_index) for i in sorted(range(next_index, end_index), reverse=True): self._item_remove_position(parent_item, i) self.endRemoveRows() return True
def test_rmf_features_model(self): """Test RMFFeaturesModel class""" f1 = make_feature("f1", 1) f2 = make_feature("f2", 2) child1 = make_feature("child1", 3) f2.add_child(child1) child2 = make_feature("child2", 4) f2.add_child(child2) grandchild = make_feature("child3", 5) child2.add_child(grandchild) features = [f1, f2] m = src.tool._RMFFeaturesModel(features) top = QModelIndex() f2_ind = m.createIndex(1, 0, features[1]) child1_ind = m.createIndex(0, 0, child1) self.assertEqual(m.columnCount(None), 1) self.assertEqual(m.rowCount(top), 2) self.assertEqual(m.rowCount(f2_ind), 2) self.assertEqual(m.rowCount(child1_ind), 0) # Test indices self.assertEqual(m.index(0, 0, top).internalPointer().name, 'f1') self.assertEqual(m.index(1, 0, top).internalPointer().name, 'f2') self.assertFalse(m.index(2, 0, top).isValid()) self.assertEqual( m.index(0, 0, f2_ind).internalPointer().name, 'child1') self.assertEqual( m.index(1, 0, f2_ind).internalPointer().name, 'child2') self.assertFalse(m.index(2, 0, f2_ind).isValid()) # No parents self.assertFalse(m.parent(top).isValid()) self.assertFalse(m.parent(f2_ind).isValid()) self.assertEqual(m.parent(child1_ind).internalPointer().name, 'f2') grandchild_ind = m.createIndex(0, 0, grandchild) self.assertEqual( m.parent(grandchild_ind).internalPointer().name, 'child2') self.assertEqual(m.data(f2_ind, Qt.DisplayRole), "f2") self.assertIsNone(m.data(f2_ind, Qt.SizeHintRole))
def insertColumns(self, position, columns, parent=QModelIndex()): self.beginInsertColumns(parent, position, position + columns - 1) success = self.rootItem.insertColumns(position, columns) self.endInsertColumns() return success