def test_get_name(self): """Test get_name method.""" flood_name = get_name(hazard_flood['key']) self.assertEqual(flood_name, hazard_flood['name']) not_exist_key = 'Mega flux capacitor' not_found_name = get_name(not_exist_key) self.assertEqual(not_exist_key, not_found_name)
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 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()