def _model_to_tree( item: QStandardItem, selection: List, with_selection: bool ) -> Union[Dict, OntoType]: tree = {} for i in range(item.rowCount()): tree[item.child(i).text()] = \ _model_to_tree(item.child(i), selection, with_selection) return (tree, item.index() in selection) if with_selection else tree
def __on_add(self): parent: QStandardItem = self.__root selection: List = self.__tree.selectionModel().selectedIndexes() if selection: sel_index: QModelIndex = selection[0] parent: QStandardItem = self.__model.itemFromIndex(sel_index) item = QStandardItem("") parent.appendRow(item) index: QModelIndex = item.index() with disconnected(self.__model.dataChanged, self.dataChanged): self.__model.setItemData(index, {Qt.EditRole: ""}) self.__tree.setCurrentIndex(index) self.__tree.edit(index)
def _tree_to_model( tree: Dict, root: QStandardItem, sel_model: QItemSelectionModel ) -> None: # tuple of subtree and selection flag if isinstance(tree, tuple): tree, _ = tree # read from .json if isinstance(tree, list): tree = {t: {} for t in tree} for word, words in tree.items(): item = QStandardItem(word) root.appendRow(item) if isinstance(words, tuple): _, selected = words if selected: sel_model.select(item.index(), QItemSelectionModel.Select) if len(words): _tree_to_model(words, item, sel_model)
def _onAddAction(self): item = QStandardItem("") item.setFlags(item.flags() ^ Qt.ItemIsDropEnabled) self._listView.model().appendRow(item) self._listView.setCurrentIndex(item.index()) self._listView.edit(item.index())