def populate_classified_values( self, unassigned_values, assigned_values, default_classes): """Populate lstUniqueValues and treeClasses.from the parameters. :param unassigned_values: List of values that haven't been assigned to a class. It will be put in self.lstUniqueValues. :type unassigned_values: list :param assigned_values: Dictionary with class as the key and list of value as the value of the dictionary. It will be put in self.treeClasses. :type assigned_values: dict :param default_classes: Default classes from unit. :type default_classes: list """ # Populate the unique values list self.lstUniqueValues.clear() self.lstUniqueValues.setSelectionMode( QAbstractItemView.ExtendedSelection) for value in unassigned_values: value_as_string = value is not None and str(value) or 'NULL' list_item = QListWidgetItem(self.lstUniqueValues) list_item.setFlags( Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled) list_item.setData(Qt.UserRole, value) list_item.setText(value_as_string) self.lstUniqueValues.addItem(list_item) # Populate assigned values tree self.treeClasses.clear() self.treeClasses.invisibleRootItem().setFlags(Qt.ItemIsEnabled) for default_class in default_classes: # Create branch for class tree_branch = QTreeWidgetItem(self.treeClasses) tree_branch.setFlags(Qt.ItemIsDropEnabled | Qt.ItemIsEnabled) tree_branch.setExpanded(True) tree_branch.setFont(0, bold_font) if 'name' in default_class: default_class_name = default_class['name'] else: default_class_name = default_class['key'] tree_branch.setText(0, default_class_name) tree_branch.setData(0, Qt.UserRole, default_class['key']) if 'description' in default_class: tree_branch.setToolTip(0, default_class['description']) # Assign known values for value in assigned_values[default_class['key']]: string_value = value is not None and str(value) or 'NULL' tree_leaf = QTreeWidgetItem(tree_branch) tree_leaf.setFlags( Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled) tree_leaf.setData(0, Qt.UserRole, value) tree_leaf.setText(0, string_value)
def create_views_node(self, root_node, views): children = [] title_node = QTreeWidgetItem(root_node, 0) title_node.setText(0, QApplication.translate("EntityDependencies", "Views")) title_node.setFont(0, self.title_font()) for view in views: node = QTreeWidgetItem(title_node, 0) node.setText(0, view) children.append(node) return children
def create_entities_node(self, root_node, entities): children = [] title_node = QTreeWidgetItem(root_node, 0) title_node.setText(0, QApplication.translate("EntityDependencies", "Entities")) title_node.setFont(0, self.title_font()) for entity in entities: node = QTreeWidgetItem(title_node, 0) node.setText(0, entity) children.append(node) return children
def populate_repositories_widget(self): """Populate the current dictionary repositories to the tree widget.""" # Clear the current tree widget self.tree_repositories.clear() installed_collections = self.collection_manager.get_installed_collections( ) # Export the updated ones from the repository manager repo_Font = QFont() repo_with_installed_Font = QFont() repo_with_installed_Font.setWeight(60) collection_brush = QBrush(Qt.darkGray) installed_collection_brush = QBrush(QColor(60, 25, 10)) for repo_name in self.repository_manager.directories: url = self.repository_manager.directories[repo_name]["url"] item = QTreeWidgetItem(self.tree_repositories, REPOSITORY_ITEM) # Is the repository in the QGIS resource directory? if url in self.repository_manager._online_directories.values(): repo_with_installed_Font.setUnderline(True) repo_Font.setUnderline(True) else: repo_with_installed_Font.setUnderline(False) repo_Font.setUnderline(False) item.setText(0, repo_name) item.setText(1, url) item.setFont(0, repo_Font) for coll_id in config.COLLECTIONS: if ("repository_name" in config.COLLECTIONS[coll_id].keys() and config.COLLECTIONS[coll_id]["repository_name"] == repo_name): coll_name = config.COLLECTIONS[coll_id]["name"] coll_tags = config.COLLECTIONS[coll_id]["tags"] collectionItem = QTreeWidgetItem(item, COLLECTION_ITEM) brush = collection_brush collectionFont = QFont() collectionFont.setStyle(QFont.StyleItalic) collitemtext = coll_name if (installed_collections and coll_id in installed_collections.keys()): collitemtext = coll_name + " (installed)" brush = installed_collection_brush item.setFont(0, repo_with_installed_Font) item.setForeground(0, brush) item.setForeground(1, brush) collectionItem.setFont(0, collectionFont) collectionItem.setForeground(0, brush) collectionItem.setText(0, collitemtext) collectionItem.setFont(1, collectionFont) collectionItem.setForeground(1, brush) collectionItem.setText(1, coll_tags) self.tree_repositories.resizeColumnToContents(0) self.tree_repositories.resizeColumnToContents(1) self.tree_repositories.sortItems(1, Qt.AscendingOrder)
def _populate_reporting_tab(self): """Populate trees about layers.""" self.tree.clear() self.add_layer.setEnabled(False) self.remove_layer.setEnabled(False) self.move_up.setEnabled(False) self.move_down.setEnabled(False) self.tree.setColumnCount(1) self.tree.setRootIsDecorated(False) self.tree.setHeaderHidden(True) analysis_branch = QTreeWidgetItem(self.tree.invisibleRootItem(), [FROM_ANALYSIS['name']]) analysis_branch.setFont(0, bold_font) analysis_branch.setExpanded(True) analysis_branch.setFlags(Qt.ItemIsEnabled) if self._multi_exposure_if: expected = self._multi_exposure_if.output_layers_expected() for group, layers in list(expected.items()): group_branch = QTreeWidgetItem(analysis_branch, [group]) group_branch.setFont(0, bold_font) group_branch.setExpanded(True) group_branch.setFlags(Qt.ItemIsEnabled) for layer in layers: layer = definition(layer) if layer.get('allowed_geometries', None): item = QTreeWidgetItem(group_branch, [layer.get('name')]) item.setData(0, LAYER_ORIGIN_ROLE, FROM_ANALYSIS['key']) item.setData(0, LAYER_PARENT_ANALYSIS_ROLE, group) item.setData(0, LAYER_PURPOSE_KEY_OR_ID_ROLE, layer['key']) item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable) canvas_branch = QTreeWidgetItem(self.tree.invisibleRootItem(), [FROM_CANVAS['name']]) canvas_branch.setFont(0, bold_font) canvas_branch.setExpanded(True) canvas_branch.setFlags(Qt.ItemIsEnabled) # List layers from the canvas loaded_layers = list(QgsProject.instance().mapLayers().values()) canvas_layers = self.iface.mapCanvas().layers() flag = setting('visibleLayersOnlyFlag', expected_type=bool) for loaded_layer in loaded_layers: if flag and loaded_layer not in canvas_layers: continue title = loaded_layer.name() item = QTreeWidgetItem(canvas_branch, [title]) item.setData(0, LAYER_ORIGIN_ROLE, FROM_CANVAS['key']) item.setData(0, LAYER_PURPOSE_KEY_OR_ID_ROLE, loaded_layer.id()) item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable) self.tree.resizeColumnToContents(0)
def display(self): ''' Initialize top-level items ''' if len(self.items) == 0: return self.tree.clear() # If there is only one item then set it as the root item if len(self.items) == 1: rootItem = self.items[0] # Set root font rtFont = rootItem.font(0) rtFont.setBold(True) rootItem.setFont(0, rtFont) # Add the tree item to the tree widget self.tree.addTopLevelItem(rootItem) rootItem.setExpanded(True) else: rootItem = QTreeWidgetItem(self.tree) rootItem.setText(0, self.title) rootItem.setIcon(0, QIcon(self.rootResource)) # Set root font rtFont = rootItem.font(0) rtFont.setBold(True) rootItem.setFont(0, rtFont) rootItem.addChildren(self.items) rootItem.setExpanded(True) # Force the horizontal scrollbar to show self.tree.header().setSectionResizeMode(QHeaderView.ResizeToContents)
class ProfileWidget(QTreeWidget, object): """Profile Widget.""" def __init__(self, data=None): """Constructor.""" super(ProfileWidget, self).__init__() # Attributes self.widget_items = [] # Set data if data: self.data = data # Set header self.header_tree_widget = QTreeWidgetItem( [tr('Classification'), tr('Affected'), tr('Displacement Rate')]) header_font = QFont() header_font.setBold(True) header_font.setPointSize(14) self.header_tree_widget.setFont(0, header_font) self.header_tree_widget.setFont(1, header_font) self.header_tree_widget.setFont(2, header_font) self.setHeaderItem(self.header_tree_widget) self.header().setSectionResizeMode(0, QHeaderView.Stretch) self.header().setSectionResizeMode(1, QHeaderView.Fixed) self.header().setSectionResizeMode(2, QHeaderView.ResizeToContents) self.header().setSectionsMovable(False) @property def data(self): """Get the data from the current state of widgets. :returns: Profile data in dictionary. :rtype: dict """ if len(self.widget_items) == 0: return data = {} for hazard_item in self.widget_items: hazard = hazard_item.data(0, Qt.UserRole) data[hazard] = {} classification_items = [ hazard_item.child(i) for i in range(hazard_item.childCount()) ] for classification_item in classification_items: classification = classification_item.data(0, Qt.UserRole) data[hazard][classification] = OrderedDict() class_items = [ classification_item.child(i) for i in range(classification_item.childCount()) ] for the_class_item in class_items: the_class = the_class_item.data(0, Qt.UserRole) affected_check_box = self.itemWidget(the_class_item, 1) displacement_rate_spin_box = self.itemWidget( the_class_item, 2) data[hazard][classification][the_class] = { 'affected': affected_check_box.isChecked(), 'displacement_rate': displacement_rate_spin_box.value() } return data @data.setter def data(self, profile_data): """Set data for the widget. :param profile_data: profile data. :type profile_data: dict It will replace the previous data. """ default_profile = generate_default_profile() self.clear() for hazard in sorted(default_profile.keys()): classifications = default_profile[hazard] hazard_widget_item = QTreeWidgetItem() hazard_widget_item.setData(0, Qt.UserRole, hazard) hazard_widget_item.setText(0, get_name(hazard)) for classification in sorted(classifications.keys()): # Filter out classification that doesn't support population. # TODO(IS): This is not the best place to put the filtering. # It's more suitable in the generate_default_profile method # in safe/definitions/utilities. classification_definition = definition(classification) supported_exposures = classification_definition.get( 'exposures', []) # Empty list means support all exposure if supported_exposures != []: if exposure_population not in supported_exposures: continue classes = classifications[classification] classification_widget_item = QTreeWidgetItem() classification_widget_item.setData(0, Qt.UserRole, classification) classification_widget_item.setText(0, get_name(classification)) hazard_widget_item.addChild(classification_widget_item) for the_class, the_value in list(classes.items()): the_class_widget_item = QTreeWidgetItem() the_class_widget_item.setData(0, Qt.UserRole, the_class) the_class_widget_item.setText( 0, get_class_name(the_class, classification)) classification_widget_item.addChild(the_class_widget_item) # Adding widget must be happened after addChild affected_check_box = QCheckBox(self) # Set from profile_data if exist, else get default profile_value = profile_data.get(hazard, {}).get( classification, {}).get(the_class, the_value) affected_check_box.setChecked(profile_value['affected']) self.setItemWidget(the_class_widget_item, 1, affected_check_box) displacement_rate_spinbox = PercentageSpinBox(self) displacement_rate_spinbox.setValue( profile_value['displacement_rate']) displacement_rate_spinbox.setEnabled( profile_value['affected']) self.setItemWidget(the_class_widget_item, 2, displacement_rate_spinbox) # Behaviour when the check box is checked # noinspection PyUnresolvedReferences affected_check_box.stateChanged.connect( displacement_rate_spinbox.setEnabled) if hazard_widget_item.childCount() > 0: self.widget_items.append(hazard_widget_item) self.addTopLevelItems(self.widget_items) self.expandAll() def clear(self): """Clear method to clear the widget items and the tree widget.""" super(ProfileWidget, self).clear() self.widget_items = []
class ProfileWidget(QTreeWidget, object): """Profile Widget.""" def __init__(self, data=None): """Constructor.""" super(ProfileWidget, self).__init__() # Attributes self.widget_items = [] # Set data if data: self.data = data # Set header self.header_tree_widget = QTreeWidgetItem( [tr('Classification'), tr('Affected'), tr('Displacement Rate')]) header_font = QFont() header_font.setBold(True) header_font.setPointSize(14) self.header_tree_widget.setFont(0, header_font) self.header_tree_widget.setFont(1, header_font) self.header_tree_widget.setFont(2, header_font) self.setHeaderItem(self.header_tree_widget) self.header().setSectionResizeMode(0, QHeaderView.Stretch) self.header().setSectionResizeMode(1, QHeaderView.Fixed) self.header().setSectionResizeMode(2, QHeaderView.ResizeToContents) self.header().setSectionsMovable(False) @property def data(self): """Get the data from the current state of widgets. :returns: Profile data in dictionary. :rtype: dict """ if len(self.widget_items) == 0: return data = {} for hazard_item in self.widget_items: hazard = hazard_item.data(0, Qt.UserRole) data[hazard] = {} classification_items = [ hazard_item.child(i) for i in range(hazard_item.childCount()) ] for classification_item in classification_items: classification = classification_item.data(0, Qt.UserRole) data[hazard][classification] = OrderedDict() class_items = [ classification_item.child(i) for i in range( classification_item.childCount() ) ] for the_class_item in class_items: the_class = the_class_item.data(0, Qt.UserRole) affected_check_box = self.itemWidget(the_class_item, 1) displacement_rate_spin_box = self.itemWidget( the_class_item, 2) data[hazard][classification][the_class] = { 'affected': affected_check_box.isChecked(), 'displacement_rate': displacement_rate_spin_box.value() } return data @data.setter def data(self, profile_data): """Set data for the widget. :param profile_data: profile data. :type profile_data: dict It will replace the previous data. """ default_profile = generate_default_profile() self.clear() for hazard in sorted(default_profile.keys()): classifications = default_profile[hazard] hazard_widget_item = QTreeWidgetItem() hazard_widget_item.setData(0, Qt.UserRole, hazard) hazard_widget_item.setText(0, get_name(hazard)) for classification in sorted(classifications.keys()): # Filter out classification that doesn't support population. # TODO(IS): This is not the best place to put the filtering. # It's more suitable in the generate_default_profile method # in safe/definitions/utilities. classification_definition = definition(classification) supported_exposures = classification_definition.get( 'exposures', []) # Empty list means support all exposure if supported_exposures != []: if exposure_population not in supported_exposures: continue classes = classifications[classification] classification_widget_item = QTreeWidgetItem() classification_widget_item.setData( 0, Qt.UserRole, classification) classification_widget_item.setText(0, get_name(classification)) hazard_widget_item.addChild(classification_widget_item) for the_class, the_value in list(classes.items()): the_class_widget_item = QTreeWidgetItem() the_class_widget_item.setData(0, Qt.UserRole, the_class) the_class_widget_item.setText( 0, get_class_name(the_class, classification)) classification_widget_item.addChild(the_class_widget_item) # Adding widget must be happened after addChild affected_check_box = QCheckBox(self) # Set from profile_data if exist, else get default profile_value = profile_data.get( hazard, {}).get(classification, {}).get( the_class, the_value) affected_check_box.setChecked(profile_value['affected']) self.setItemWidget( the_class_widget_item, 1, affected_check_box) displacement_rate_spinbox = PercentageSpinBox(self) displacement_rate_spinbox.setValue( profile_value['displacement_rate']) displacement_rate_spinbox.setEnabled( profile_value['affected']) self.setItemWidget( the_class_widget_item, 2, displacement_rate_spinbox) # Behaviour when the check box is checked # noinspection PyUnresolvedReferences affected_check_box.stateChanged.connect( displacement_rate_spinbox.setEnabled) if hazard_widget_item.childCount() > 0: self.widget_items.append(hazard_widget_item) self.addTopLevelItems(self.widget_items) self.expandAll() def clear(self): """Clear method to clear the widget items and the tree widget.""" super(ProfileWidget, self).clear() self.widget_items = []
def populate_classified_values( unassigned_values, assigned_values, default_classes, list_unique_values, tree_mapping_widget): """Populate lstUniqueValues and treeClasses.from the parameters. :param unassigned_values: List of values that haven't been assigned to a class. It will be put in list_unique_values. :type unassigned_values: list :param assigned_values: Dictionary with class as the key and list of value as the value of the dictionary. It will be put in tree_mapping_widget. :type assigned_values: dict :param default_classes: Default classes from unit. :type default_classes: list :param list_unique_values: List Widget for unique values :type list_unique_values: QListWidget :param tree_mapping_widget: Tree Widget for classifying. :type tree_mapping_widget: QTreeWidget """ # Populate the unique values list list_unique_values.clear() list_unique_values.setSelectionMode( QAbstractItemView.ExtendedSelection) for value in unassigned_values: value_as_string = value is not None and str(value) or 'NULL' list_item = QListWidgetItem(list_unique_values) list_item.setFlags( Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled) list_item.setData(Qt.UserRole, value) list_item.setText(value_as_string) list_unique_values.addItem(list_item) # Populate assigned values tree tree_mapping_widget.clear() bold_font = QFont() bold_font.setItalic(True) bold_font.setBold(True) bold_font.setWeight(75) tree_mapping_widget.invisibleRootItem().setFlags( Qt.ItemIsEnabled) for default_class in default_classes: # Create branch for class tree_branch = QTreeWidgetItem(tree_mapping_widget) tree_branch.setFlags( Qt.ItemIsDropEnabled | Qt.ItemIsEnabled) tree_branch.setExpanded(True) tree_branch.setFont(0, bold_font) if 'name' in default_class: default_class_name = default_class['name'] else: default_class_name = default_class['key'] tree_branch.setText(0, default_class_name) tree_branch.setData(0, Qt.UserRole, default_class['key']) if 'description' in default_class: tree_branch.setToolTip(0, default_class['description']) # Assign known values for value in assigned_values[default_class['key']]: string_value = value is not None and str(value) or 'NULL' tree_leaf = QTreeWidgetItem(tree_branch) tree_leaf.setFlags( Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled) tree_leaf.setData(0, Qt.UserRole, value) tree_leaf.setText(0, string_value)