def _add_path(self, path: List[str], depth: int, parent_item: QStandardItem, path_str: str, path_len: int): is_file = False if depth == path_len: return if depth == path_len - 1: # 为文件 is_file = True node_name_list = path[depth] for node_name in node_name_list: # 检查节点名在是否重复 index = 0 child_item = parent_item.child(index) while child_item: child_name = child_item.index().data() if child_name == node_name: node_item = child_item break index += 1 child_item = parent_item.child(index) else: note_str = "文件" if is_file else "文件夹" icon = QIcon(self._root_abs_path + "\\resources\\folder.png") if not is_file else IconExtractor( self._file_mgr.get_abs_path(node_name)).get_icon() node_item = QStandardItem(icon, node_name) path_str += "\\" + node_name path_item = QStandardItem(path_str) note_item = QStandardItem(note_str) parent_item.appendRow([node_item, path_item, note_item]) self._add_path(path, depth + 1, node_item, path_str, path_len)
def _get_all_children(root: QStandardItem): result = [] row_count = root.rowCount() if not row_count == 0: for i in range(row_count): result.append(root.child(i)) return result
def _listSelectedModules(self, parentItem: QStandardItem, selectedModuleNames: list) -> None: '''Recursively gathers a list of selected module names.''' for i in range(parentItem.rowCount()): item = parentItem.child(i) if item.checkState() == Qt.Checked: selectedModuleNames.append(item.text()) self._listSelectedModules(item, selectedModuleNames)
def sortByComponents(self): ''' Sorts the treeview by components- Dataset then component then subsets ''' # Save the selected rows from the subset view if applicable try: selected = dict() for i in range(0, len(self.selected_indices)): item = self.model_subsets.itemFromIndex( self.selected_indices[i]) if item.parent().parent().text() == "Data": key = "All data (" + item.parent().text( ) + ")" + item.text() selected[key] = item.index() else: key = item.parent().text() + item.text() selected[key] = item.index() except: pass # Set Expand/collapse button to "expand all" self.expand_data.setText("Expand all data and subsets") self.selection_model = QAbstractItemView.MultiSelection self.treeview.setSelectionMode(self.selection_model) # See if the model already exists try: self.treeview.setModel(self.model_components) except: self.model_components = QStandardItemModel() self.model_components.setHorizontalHeaderLabels(['']) self.treeview.setModel(self.model_components) self.treeview.setUniformRowHeights(True) # populate the tree # Make all the datasets be parents, and make it so they are not selectable for i in range(0, len(dc)): grandparent = QStandardItem('{}'.format(self.dc.labels[i])) grandparent.setIcon(helpers.layer_icon(self.dc[i])) grandparent.setEditable(False) grandparent.setSelectable(False) # Make all the data components be children, nested under their parent for k in range(0, len(self.dc[i].components)): parent = QStandardItem('{}'.format( str(self.dc[i].components[k]))) parent.setEditable(False) parent.setSelectable(False) child = QStandardItem('{}'.format('All data (' + self.dc.labels[i] + ')')) child.setIcon(helpers.layer_icon(self.dc[i])) child.setEditable(False) parent.appendRow(child) for j in range(0, len(self.dc.subset_groups)): child = QStandardItem('{}'.format( self.dc.subset_groups[j].label)) child.setEditable(False) child.setIcon( helpers.layer_icon(self.dc.subset_groups[j])) try: self.dc[i].compute_statistic( 'mean', self.dc[i].subsets[j].components[k], subset_state=self.dc[i].subsets[j].subset_state ) except: # print("Glue has raised an Incompatible Attribute error on this component. Let's do this instead.") child.setEditable(False) child.setSelectable(False) child.setForeground(QtGui.QBrush(Qt.gray)) parent.appendRow(child) grandparent.appendRow(parent) self.model_components.appendRow(grandparent) # Fill out the dict now that the indices are connected to the QStandardItemModel for i in range(0, grandparent.rowCount()): for j in range(0, grandparent.child(i).rowCount()): if grandparent.child(i).child(j).row() == 0: key = grandparent.child(i).child( j).text() + grandparent.child(i).text() self.component_dict[key] = grandparent.child( i).child(j).index() else: key = grandparent.child(i).child( j).text() + " (" + grandparent.text( ) + ")" + grandparent.child(i).text() self.component_dict[key] = grandparent.child( i).child(j).index() self.treeview.setUniformRowHeights(True) selection_model = QItemSelectionModel(self.model_components) self.treeview.setSelectionModel(selection_model) selection_model.selectionChanged.connect(self.myPressedEvent) # Select the rows that should be selected sel_mod = self.treeview.selectionModel() for i in range(0, len(selected)): key = list(selected.keys())[i] index = self.component_dict[key] # self.treeview.selectionModel().select(index, QItemSelectionModel.Select) print(index.parent().row(), index.row()) # This causes an error when it runs # sel_mod.select(index, QItemSelectionModel.Select|QItemSelectionModel.Rows) self.treeview.setCurrentIndex(index) self.treeview.setSelectionModel(sel_mod)
def sortBySubsets(self): ''' Sorts the treeview by subsets- Dataset then subset then component. What we originally had as the default ''' # Save the selected rows from the component view try: selected = dict() for i in range(0, len(self.selected_indices)): item = self.model_components.itemFromIndex( self.selected_indices[i]) if item.row() != 0: key = item.text() + " (" + item.parent().parent().text( ) + ")" + item.parent().text() selected[key] = item.index() else: key = item.text() + item.parent().text() selected[key] = item.index() except: pass # Set Expand/collapse button to "expand all" self.expand_data.setText("Expand all data and subsets") #Allow the user to select multiple rows at a time self.selection_model = QAbstractItemView.MultiSelection self.treeview.setSelectionMode(self.selection_model) # See if the model already exists instead of regenerating try: self.treeview.setModel(self.model_subsets) except: self.model_subsets = QStandardItemModel() self.model_subsets.setHorizontalHeaderLabels(['']) self.treeview.setModel(self.model_subsets) self.treeview.setUniformRowHeights(True) # populate the tree # Make all the datasets be parents, and make it so they are not selectable parent_data = QStandardItem('{}'.format('Data')) parent_data.setEditable(False) parent_data.setSelectable(False) for i in range(0, len(self.dc)): parent = QStandardItem('{}'.format(self.dc.labels[i])) parent.setIcon(helpers.layer_icon(self.dc[i])) parent.setEditable(False) parent.setSelectable(False) # Make all the data components be children, nested under their parent for j in range(0, len(self.dc[i].components)): child = QStandardItem('{}'.format( str(self.dc[i].components[j]))) child.setEditable(False) # Add to the subset_dict key = self.dc[i].label + self.dc[i].components[ j].label + "All data-" + self.dc[i].label self.subset_dict[key] = child.index() parent.appendRow(child) parent_data.appendRow(parent) #Add the parents with their children to the QStandardItemModel self.model_subsets.appendRow(parent_data) parent_subset = QStandardItem('{}'.format('Subsets')) parent_subset.setEditable(False) parent_subset.setSelectable(False) # Set up the subsets as Subsets > choose subset > choose data set > choose component for j in range(0, len(self.dc.subset_groups)): grandparent = QStandardItem('{}'.format( self.dc.subset_groups[j].label)) grandparent.setIcon( helpers.layer_icon(self.dc.subset_groups[j])) grandparent.setEditable(False) grandparent.setSelectable(False) for i in range(0, len(self.dc)): parent = QStandardItem( '{}'.format(self.dc.subset_groups[j].label) + ' (' + '{}'.format(self.dc[i].label) + ')') # Set up the circles parent.setIcon(helpers.layer_icon( self.dc.subset_groups[j])) parent.setEditable(False) parent.setSelectable(False) try: self.dc[i].compute_statistic( 'mean', self.dc[i].subsets[j].components[0], subset_state=self.dc[i].subsets[j].subset_state) except: parent.setForeground(QtGui.QBrush(Qt.gray)) for k in range(0, len(self.dc[i].components)): child = QStandardItem('{}'.format( str(self.dc[i].components[k]))) child.setEditable(False) # Update the dict to keep track of row indices key = self.dc[i].label + self.dc[i].components[ k].label + self.dc[i].subsets[j].label self.subset_dict[key] = child.index() parent.appendRow(child) # Make gray and unselectable components that aren't defined for a subset try: self.dc[i].compute_statistic( 'mean', self.dc[i].subsets[j].components[k], subset_state=self.dc[i].subsets[j].subset_state ) except: # print("Glue has raised an Incompatible Attribute error on this component. Let's do this instead.") child.setEditable(False) child.setSelectable(False) child.setForeground(QtGui.QBrush(Qt.gray)) grandparent.appendRow(parent) parent_subset.appendRow(grandparent) self.model_subsets.appendRow(parent_subset) # Fill out the dict now that the indices are connected to the QStandardItemModel # Full datasets for i in range(0, parent_data.rowCount()): for j in range(0, parent_data.child(i).rowCount()): key = "All data (" + parent_data.child( i).text() + ")" + parent_data.child(i).child(j).text() self.subset_dict[key] = parent_data.child(i).child( j).index() # Subsets for i in range(0, parent_subset.rowCount()): for j in range(0, parent_subset.child(i).rowCount()): for k in range(0, parent_subset.child(i).child(j).rowCount()): key = parent_subset.child(i).child(j).text( ) + parent_subset.child(i).child(j).child(k).text() self.subset_dict[key] = parent_subset.child(i).child( j).child(k).index() self.treeview.setUniformRowHeights(True) selection_model = QItemSelectionModel(self.model_subsets) self.treeview.setSelectionModel(selection_model) selection_model.selectionChanged.connect(self.myPressedEvent) # Select rows that should be selected sel_mod = self.treeview.selectionModel() for i in range(0, len(selected)): # key = list(self.selected_dict.keys())[list(self.selected_dict.values()).index(self.selected_dict[i])] key = list(selected.keys())[i] index = self.subset_dict[key] print(index.parent().row(), index.row()) # print(index, type(index)) # print(type(self.treeview.selectionModel().select(index, QItemSelectionModel.Select))) # sel_mod.select(index, QItemSelectionModel.Select|QItemSelectionModel.Rows) self.treeview.setCurrentIndex(index) self.treeview.setSelectionModel(sel_mod)
def get_item_by_name(name: str, item: QStandardItem): cnt = item.rowCount() for i in range(cnt): if item.child(i).text() == name: return item.child(i) return None
def project_checked(self, item: QStandardItem): if item.hasChildren(): for index in range(item.rowCount()): item.child(index, 0).setCheckState(item.checkState())
def _parentContainsItem(self, parentItem: QStandardItem, id: str) -> bool: for row in range(parentItem.rowCount()): childId = parentItem.child(row).data()['id'] if childId == id: return True return False