def test_init(self): """Test init.""" float_parameter = FloatParameter() float_parameter.name = 'Female Ratio.' float_parameter.is_required = True float_parameter.minimum_allowed_value = 0 float_parameter.maximum_allowed_value = 1 float_parameter.help_text = 'The percentage of female..' float_parameter.description = ( 'A <b>test _description</b> that is very long so that you need ' 'to read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you ' 'read this description loudly so that all of your friends will be ' 'able to hear you') float_parameter.value = 0.5 widget = PercentageParameterWidget(float_parameter) expected_value = float_parameter.name real_value = widget.label.text() message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) expected_value = float_parameter.value real_value = widget.get_parameter().value message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) widget._input.setValue(1.5) expected_value = 1 real_value = widget.get_parameter().value message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) widget._input.setValue(0.55555) expected_value = 0.556 real_value = widget.get_parameter().value message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) widget._input.setValue(-7) expected_value = 0 real_value = widget.get_parameter().value message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) expected_value = 'PercentageSpinBox' real_value = widget._input.__class__.__name__ message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message)
def default_field_to_parameter(self, default_field): """Obtain parameter from default field. :param default_field: A default field definition. :type default_field: dict :returns: A parameter object. :rtype: FloatParameter, IntegerParameter """ if default_field.get('type') == QVariant.Double: parameter = FloatParameter() elif default_field.get('type') in qvariant_whole_numbers: parameter = IntegerParameter() else: return default_value = default_field.get('default_value') if not default_value: message = ( 'InaSAFE default field %s does not have default value' % default_field.get('name')) LOGGER.exception(message) return parameter.guid = default_field.get('key') parameter.name = default_value.get('name') parameter.is_required = True parameter.precision = default_field.get('precision') parameter.minimum_allowed_value = default_value.get( 'min_value', 0) parameter.maximum_allowed_value = default_value.get( 'max_value', 100000000) parameter.help_text = default_value.get('help_text') parameter.description = default_value.get('description') # Check if user ask to restore to the most default value. if self.is_restore_default: parameter._value = default_value.get('default_value') else: # Current value qsetting_default_value = get_inasafe_default_value_qsetting( self.settings, GLOBAL, default_field['key']) # To avoid python error if qsetting_default_value > parameter.maximum_allowed_value: qsetting_default_value = parameter.maximum_allowed_value if qsetting_default_value < parameter.minimum_allowed_value: qsetting_default_value = parameter.minimum_allowed_value parameter.value = qsetting_default_value return parameter
def test_init(self): unit_feet = Unit('130790') unit_feet.load_dictionary(unit_feet_depth) unit_metres = Unit('900713') unit_metres.load_dictionary(unit_metres_depth) float_parameter = FloatParameter() float_parameter.name = 'Flood Depth' float_parameter.is_required = True float_parameter.precision = 3 float_parameter.minimum_allowed_value = 1.0 float_parameter.maximum_allowed_value = 2.0 float_parameter.help_text = 'The depth of flood.' float_parameter.description = ( 'A <b>test _description</b> that is very long so that you need ' 'to read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you ' 'read this description loudly so that all of your friends will be ' 'able to hear you') float_parameter.unit = unit_feet float_parameter.allowed_units = [unit_metres, unit_feet] float_parameter.value = 1.12 widget = FloatParameterWidget(float_parameter) expected_value = float_parameter.name real_value = widget.label.text() message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) expected_value = float_parameter.value real_value = widget.get_parameter().value message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) widget._input.setValue(1.5) expected_value = 1.5 real_value = widget.get_parameter().value message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) widget._input.setValue(1.55555) expected_value = 1.556 real_value = widget.get_parameter().value message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) widget._input.setValue(7) expected_value = 2 real_value = widget.get_parameter().value message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) expected_value = 'QComboBox' real_value = widget._unit_widget.__class__.__name__ message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) expected_value = 'feet' real_value = widget.get_parameter().unit.name message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message) expected_value = 'metres' widget._unit_widget.setCurrentIndex(0) real_value = widget.get_parameter().unit.name message = 'Expected %s get %s' % (expected_value, real_value) self.assertEqual(expected_value, real_value, message)
def main(): """Main function""" app = QApplication([]) def validate_min_max(parent_container): """ :param parent_container: The container that use this validator. :type parent_container: ParameterContainer :return: """ min_value_parameter = parent_container.get_parameter_by_guid( min_integer_parameter.guid) max_value_parameter = parent_container.get_parameter_by_guid( max_integer_parameter.guid) min_value = min_value_parameter.value max_value = max_value_parameter.value print('min', min_value) print('max', max_value) if min_value > max_value: print('Not valid') message = ('Your minimum value (%d) should be less than your ' 'maximum value (%d)' % (min_value, max_value)) return { 'valid': False, 'message': message } print('Valid') return {'valid': True, 'message': ''} unit_feet = Unit('130790') unit_feet.load_dictionary(unit_feet_depth) unit_metres = Unit('900713') unit_metres.load_dictionary(unit_metres_depth) string_parameter = StringParameter('28082014') string_parameter.name = 'Province Name' string_parameter.description = 'Name of province.' string_parameter.help_text = ( 'A <b>test help</b> that is very long so that you need to ' 'read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you read ' 'this description loudly so that all of your friends will be able ' 'to hear you') string_parameter.is_required = True string_parameter.value = 'Daerah Istimewa Yogyakarta' boolean_parameter = BooleanParameter('1231231') boolean_parameter.name = 'Post processor' boolean_parameter.description = 'This is post processor parameter.' boolean_parameter.help_text = ( 'A <b>test help text</b> that is very long so that you need to ' 'read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you read ' 'this description loudly so that all of your friends will be able ' 'to hear you') boolean_parameter.is_required = True boolean_parameter.value = True float_parameter = FloatParameter() float_parameter.name = 'Flood Depth' float_parameter.is_required = True float_parameter.precision = 3 float_parameter.minimum_allowed_value = 1.0 float_parameter.maximum_allowed_value = 2.0 float_parameter.description = 'The depth of flood.' float_parameter.help_text = ( 'A <b>test _description</b> that is very long so that you need to ' 'read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you read ' 'this description loudly so that all of your friends will be able ' 'to hear you') float_parameter.unit = unit_feet float_parameter.allowed_units = [unit_metres, unit_feet] float_parameter.value = 1.12 integer_parameter = IntegerParameter() integer_parameter.name = 'Paper' integer_parameter.is_required = True integer_parameter.minimum_allowed_value = 1 integer_parameter.maximum_allowed_value = 5 integer_parameter.description = 'Number of paper' integer_parameter.help_text = ( 'A <b>test _description</b> that is very long so that you need to ' 'read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you read ' 'this description loudly so that all of your friends will be able ' 'to hear you') integer_parameter.unit = unit_feet integer_parameter.allowed_units = [unit_feet] integer_parameter.value = 3 point_parameter = PointParameter() point_parameter.name = 'Point Parameter' point_parameter.is_required = True point_parameter.description = 'Short help.' point_parameter.help_text = 'Long description for parameter.' point_parameter.value = (0, 1) min_integer_parameter = IntegerParameter() min_integer_parameter.name = 'Minimal Stick Length' min_integer_parameter.is_required = True min_integer_parameter.minimum_allowed_value = 1 min_integer_parameter.maximum_allowed_value = 50 min_integer_parameter.description = 'Minimum length of a stick' min_integer_parameter.help_text = ( 'Minimum length of a stick that are allowed') min_integer_parameter.unit = unit_metres min_integer_parameter.allowed_units = [unit_metres] min_integer_parameter.value = 3 max_integer_parameter = IntegerParameter() max_integer_parameter.name = 'Maximum Stick Length' max_integer_parameter.is_required = True max_integer_parameter.minimum_allowed_value = 1 max_integer_parameter.maximum_allowed_value = 50 max_integer_parameter.description = 'Maximum length of a stick' max_integer_parameter.help_text = ( 'Maximum length of a stick that are allowed') max_integer_parameter.unit = unit_metres max_integer_parameter.allowed_units = [unit_metres] max_integer_parameter.value = 4 list_parameter = ListParameter() list_parameter.name = 'Affected Field' list_parameter.is_required = True list_parameter.maximum_item_count = 3 list_parameter.minimum_item_count = 1 list_parameter.description = 'Column used for affected field' list_parameter.help_text = 'Column used for affected field in the vector' list_parameter.element_type = str list_parameter.options_list = ['FLOODPRONE', 'affected', 'floodprone', 'yes/no', '\xddounicode test'] list_parameter.value = ['FLOODPRONE', 'affected', 'floodprone'] select_parameter = SelectParameter() select_parameter.name = 'Select Affected Field' select_parameter.is_required = True select_parameter.description = 'Column used for affected field' select_parameter.help_text = ( 'Column used for affected field in the vector') select_parameter.element_type = str select_parameter.options_list = [ 'FLOODPRONE', 'affected', 'floodprone', 'yes/no', '\xddounicode test'] select_parameter.value = 'affected' input_list_parameter = InputListParameter() input_list_parameter.name = 'Thresholds' input_list_parameter.is_required = True input_list_parameter.maximum_item_count = 3 input_list_parameter.minimum_item_count = 1 input_list_parameter.description = 'Specified List of thresholds' input_list_parameter.help_text = 'Some help text' input_list_parameter.element_type = int input_list_parameter.ordering = InputListParameter.DescendingOrder input_list_parameter.value = [1] dict_parameter = DictParameter() dict_parameter.name = 'Dict Parameter' dict_parameter.is_required = True dict_parameter.maximum_item_count = 5 dict_parameter.minimum_item_count = 1 dict_parameter.description = 'Dict Parameter example' dict_parameter.help_text = 'Dict Parameter help text.' dict_parameter.element_type = str dict_parameter.value = { 'foo': 'True', 'bar': '10', 'woo': 'False', 'sub_dict_sample': { 'key1': 'val1', 'key2': 'val2' } } group_parameter = GroupParameter() group_parameter.name = 'Age ratios' group_parameter.is_required = True group_parameter.value = [ string_parameter, integer_parameter, boolean_parameter ] def _custom_validator(value): valid = True if string_parameter.value == 'foo' and integer_parameter.value == \ 3 and boolean_parameter.value is True: valid = False if not valid: raise Exception('Parameter not valid') group_parameter.custom_validator = _custom_validator parameters = [ string_parameter, integer_parameter, boolean_parameter, float_parameter, float_parameter, boolean_parameter, integer_parameter, point_parameter, list_parameter, input_list_parameter, dict_parameter, group_parameter, select_parameter ] extra_parameters = [ (PointParameter, PointParameterWidget) ] min_max_parameters = [min_integer_parameter, max_integer_parameter] description_text = ( 'These parameters are merely created for showing example only') # description_text = '' parameter_container = ParameterContainer( parameters, extra_parameters=extra_parameters, description_text=description_text) parameter_container.setup_ui() # create error handler parameter_widget = parameter_container.get_parameter_widgets() try: input_list_widget = [ w.widget() for w in parameter_widget if isinstance(w.widget(), InputListParameterWidget)][0] def add_row_handler(exception): box = QMessageBox() box.critical(input_list_widget, 'Add Row Error', exception.message) input_list_widget.add_row_error_handler = add_row_handler except IndexError: pass parameter_container2 = ParameterContainer( extra_parameters=extra_parameters, description_text='Empty Parameter Container Description') parameter_container2.setup_ui() parameter_container3 = ParameterContainer( parameters=min_max_parameters, extra_parameters=extra_parameters, description_text='Minimum Maximum Parameter') parameter_container3.add_validator(validate_min_max) parameter_container3.setup_ui() def show_error_message(parent, exception): """Generate error message to handle parameter errors :param parent: The widget as a parent of message box :type parent: QWidget :param exception: python Exception or Error :type exception: Exception """ box = QMessageBox() box.critical(parent, 'Error occured', exception.message) def show_parameter(the_parameter_container): """Print the value of parameter. :param the_parameter_container: A parameter container :type the_parameter_container: ParameterContainer """ def show_parameter_value(a_parameter): if isinstance(a_parameter, GroupParameter): for param in a_parameter.value: show_parameter_value(param) else: print(a_parameter.guid, a_parameter.name, a_parameter.value) try: the_parameters = the_parameter_container.get_parameters() if the_parameters: for parameter in the_parameters: show_parameter_value(parameter) except Exception as inst: show_error_message(parameter_container, inst) button = QPushButton('Show parameters') # noinspection PyUnresolvedReferences button.clicked.connect( partial(show_parameter, the_parameter_container=parameter_container)) validate_button = QPushButton('Validate parameters') # noinspection PyUnresolvedReferences validate_button.clicked.connect( partial(show_parameter, the_parameter_container=parameter_container3)) widget = QWidget() layout = QGridLayout() layout.addWidget(parameter_container) layout.addWidget(button) layout.addWidget(parameter_container2) layout.addWidget(parameter_container3) layout.addWidget(validate_button) widget.setLayout(layout) widget.setGeometry(0, 0, 500, 500) widget.show() sys.exit(app.exec_())
def set_up_resource_parameters(self): """Set up the resource parameter for the add/edit view. """ name_parameter = StringParameter('UUID-1') name_parameter.name = self.resource_parameters['Resource name'] name_parameter.help_text = tr( 'Name of the resource that will be provided ' 'as part of minimum needs. ' 'e.g. Rice, Water etc.') name_parameter.description = tr( 'A <b>resource</b> is something that you provide to displaced ' 'persons in the event of a disaster. The resource will be made ' 'available at IDP camps and may need to be stockpiled by ' 'contingency planners in their preparations for a disaster.') name_parameter.is_required = True name_parameter.value = '' description_parameter = StringParameter('UUID-2') description_parameter.name = self.resource_parameters[ 'Resource description'] description_parameter.help_text = tr( 'Description of the resource that will be provided as part of ' 'minimum needs.') description_parameter.description = tr( 'This gives a detailed description of what the resource is and ') description_parameter.is_required = True description_parameter.value = '' unit_parameter = StringParameter('UUID-3') unit_parameter.name = self.resource_parameters['Unit'] unit_parameter.help_text = tr( 'Single unit for the resources spelled out. e.g. litre, ' 'kilogram etc.') unit_parameter.description = tr( 'A <b>unit</b> is the basic measurement unit used for computing ' 'the allowance per individual. For example when planning water ' 'rations the unit would be single litre.') unit_parameter.is_required = True unit_parameter.value = '' units_parameter = StringParameter('UUID-4') units_parameter.name = self.resource_parameters['Units'] units_parameter.help_text = tr( 'Multiple units for the resources spelled out. e.g. litres, ' 'kilogram etc.') units_parameter.description = tr( '<b>Units</b> are the basic measurement used for computing the ' 'allowance per individual. For example when planning water ' 'rations the units would be litres.') units_parameter.is_required = True units_parameter.value = '' unit_abbreviation_parameter = StringParameter('UUID-5') unit_abbreviation_parameter.name = \ self.resource_parameters['Unit abbreviation'] unit_abbreviation_parameter.help_text = tr( 'Abbreviations of unit for the resources. e.g. l, kg etc.') unit_abbreviation_parameter.description = tr( "A <b>unit abbreviation</b> is the basic measurement unit's " "shortened. For example when planning water rations " "the units would be l.") unit_abbreviation_parameter.is_required = True unit_abbreviation_parameter.value = '' minimum_parameter = FloatParameter('UUID-6') minimum_parameter.name = self.resource_parameters['Minimum allowed'] minimum_parameter.is_required = True minimum_parameter.precision = 6 minimum_parameter.minimum_allowed_value = 0.0 minimum_parameter.maximum_allowed_value = 99999.0 minimum_parameter.help_text = tr( 'The minimum allowable quantity per person. ') minimum_parameter.description = tr( 'The <b>minimum</b> is the minimum allowed quantity of the ' 'resource per person. For example you may dictate that the water ' 'ration per person per day should never be allowed to be less ' 'than 0.5l. This is enforced when tweaking a minimum needs set ' 'before an impact evaluation') minimum_parameter.value = 0.00 maximum_parameter = FloatParameter('UUID-7') maximum_parameter.name = self.resource_parameters['Maximum allowed'] maximum_parameter.is_required = True maximum_parameter.precision = 6 maximum_parameter.minimum_allowed_value = 0.0 maximum_parameter.maximum_allowed_value = 99999.0 maximum_parameter.help_text = tr( 'The maximum allowable quantity per person. ') maximum_parameter.description = tr( 'The <b>maximum</b> is the maximum allowed quantity of the ' 'resource per person. For example you may dictate that the water ' 'ration per person per day should never be allowed to be more ' 'than 67l. This is enforced when tweaking a maximum needs set ' 'before an impact evaluation.') maximum_parameter.value = 100.0 default_parameter = FloatParameter('UUID-8') default_parameter.name = self.resource_parameters['Default'] default_parameter.is_required = True default_parameter.precision = 6 default_parameter.minimum_allowed_value = 0.0 default_parameter.maximum_allowed_value = 99999.0 default_parameter.help_text = tr( 'The default allowable quantity per person. ') default_parameter.description = tr( "The <b>default</b> is the default allowed quantity of the " "resource per person. For example you may indicate that the water " "ration per person weekly should be 67l.") default_parameter.value = 10.0 frequency_parameter = StringParameter('UUID-9') frequency_parameter.name = self.resource_parameters['Frequency'] frequency_parameter.help_text = tr( "The frequency that this resource needs to be provided to a " "displaced person. e.g. weekly, daily, once etc.") frequency_parameter.description = tr( "The <b>frequency</b> informs the aid worker how regularly this " "resource needs to be provided to the displaced person.") frequency_parameter.is_required = True frequency_parameter.value = tr('weekly') sentence_parameter = TextParameter('UUID-10') sentence_parameter.name = self.resource_parameters['Readable sentence'] sentence_parameter.help_text = tr( 'A readable presentation of the resource.') sentence_parameter.description = tr( "A <b>readable sentence</b> is a presentation of the resource " "that displays all pertinent information. If you are unsure then " "use the default. Properties should be included using double " "curly brackets '{{' '}}'. Including the resource name would be " "achieved by including e.g. {{ Resource name }}") sentence_parameter.is_required = True sentence_parameter.value = tr( 'A displaced person should be provided with ' '%(default value)s %(unit)s/%(units)s/%(unit abbreviation)s of ' '%(resource name)s. Though no less than %(minimum allowed)s ' 'and no more than %(maximum allowed)s. This should be provided ' '%(frequency)s.' % { 'default value': '{{ Default }}', 'unit': '{{ Unit }}', 'units': '{{ Units }}', 'unit abbreviation': '{{ Unit abbreviation }}', 'resource name': '{{ Resource name }}', 'minimum allowed': '{{ Minimum allowed }}', 'maximum allowed': '{{ Maximum allowed }}', 'frequency': '{{ Frequency }}' }) parameters = [ name_parameter, description_parameter, unit_parameter, units_parameter, unit_abbreviation_parameter, default_parameter, minimum_parameter, maximum_parameter, frequency_parameter, sentence_parameter ] parameter_container = ParameterContainer(parameters) parameter_container.setup_ui() layout = QGridLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) layout.addWidget(parameter_container) self.parameters_scrollarea.setLayout(layout)
def main(): """Main function""" application = QApplication([]) name_parameter = StringParameter('UUID-1') name_parameter.name = 'Resource name' name_parameter.help_text = ( 'Name of the resource that will be provided as part of minimum needs.' 'e.g. Tea, Water etc.') name_parameter.description = ( 'A <b>resource</b> is something that you provide to displaced persons ' 'in the event of a disaster. The resource will be made available ' 'at IDP camps and may need to be stockpiled by contingency planners ' 'in their preparations for a disaster.') name_parameter.is_required = True name_parameter.value = '' description_parameter = StringParameter('UUID-1') description_parameter.name = 'Resource description' description_parameter.help_text = ( 'Description of the resource that will be provided as part of minimum ' 'needs. e.g. Tea, Water etc.') description_parameter.description = ( 'Description of the resource that will be provided as part of minimum ' 'needs. e.g. Tea, Water etc.') description_parameter.is_required = True description_parameter.value = '' unit_parameter = StringParameter('UUID-2') unit_parameter.name = 'Units' unit_parameter.help_text = ( 'Unit for the resources. e.g. litres, kg etc.') unit_parameter.description = ( 'A <b>unit</b> the basic measurement unit used for computing the ' 'allowance per individual. For example when planning water rations ' 'the units would be litres.') unit_parameter.is_required = True unit_parameter.value = '' minimum_parameter = FloatParameter('UUID-3') minimum_parameter.name = 'Minimum allowed' minimum_parameter.is_required = True minimum_parameter.precision = 3 minimum_parameter.minimum_allowed_value = -99999.0 minimum_parameter.maximum_allowed_value = 99999.0 minimum_parameter.help_text = 'The minimum allowable quantity per person. ' minimum_parameter.description = ( 'The <b>minimum</b> is the minimum allowed quantity of the resource ' 'per person. For example you may dictate that the water ration per ' 'person per day should never be allowed to be less than 0.5l.') minimum_parameter.value = 1.0 maximum_parameter = FloatParameter('UUID-3') maximum_parameter.name = 'Minimum allowed' maximum_parameter.is_required = True maximum_parameter.precision = 3 maximum_parameter.minimum_allowed_value = -99999.0 maximum_parameter.maximum_allowed_value = 99999.0 maximum_parameter.help_text = 'The maximum allowable quantity per person. ' maximum_parameter.description = ( 'The <b>maximum</b> is the maximum allowed quantity of the resource ' 'per person. For example you may dictate that the water ration per ' 'person per day should never be allowed to be more than 50l.') maximum_parameter.value = 1.0 maximum_parameter = FloatParameter('UUID-4') maximum_parameter.name = 'Minimum allowed' maximum_parameter.is_required = True maximum_parameter.precision = 3 maximum_parameter.minimum_allowed_value = -99999.0 maximum_parameter.maximum_allowed_value = 99999.0 maximum_parameter.help_text = 'The maximum allowable quantity per person. ' maximum_parameter.description = ( 'The <b>maximum</b> is the maximum allowed quantity of the resource ' 'per person. For example you may dictate that the water ration per ' 'person per day should never be allowed to be more than 50l.') maximum_parameter.value = 1.0 default_parameter = FloatParameter('UUID-5') default_parameter.name = 'Default' default_parameter.is_required = True default_parameter.precision = 3 default_parameter.minimum_allowed_value = -99999.0 default_parameter.default_allowed_value = 99999.0 default_parameter.help_text = 'The default allowable quantity per person. ' default_parameter.description = ( 'The <b>default</b> is the default allowed quantity of the resource ' 'per person. For example you may indicate that the water ration per ' 'person per day should be 25l.') default_parameter.value = 1.0 parameters = [ name_parameter, description_parameter, unit_parameter, minimum_parameter, maximum_parameter, default_parameter] parameter_container = ParameterContainer(parameters) widget = QWidget() layout = QGridLayout() layout.addWidget(parameter_container) widget.setLayout(layout) widget.setGeometry(0, 0, 500, 500) widget.show() new_parameters = parameter_container.get_parameters() for new_parameter in new_parameters: print(new_parameter.name, new_parameter.value) sys.exit(application.exec_())
def set_up_resource_parameters(self): """Set up the resource parameter for the add/edit view. """ name_parameter = StringParameter('UUID-1') name_parameter.name = self.resource_parameters['Resource name'] name_parameter.help_text = tr( 'Name of the resource that will be provided ' 'as part of minimum needs. ' 'e.g. Rice, Water etc.') name_parameter.description = tr( 'A <b>resource</b> is something that you provide to displaced ' 'persons in the event of a disaster. The resource will be made ' 'available at IDP camps and may need to be stockpiled by ' 'contingency planners in their preparations for a disaster.') name_parameter.is_required = True name_parameter.value = '' description_parameter = StringParameter('UUID-2') description_parameter.name = self.resource_parameters[ 'Resource description'] description_parameter.help_text = tr( 'Description of the resource that will be provided as part of ' 'minimum needs.') description_parameter.description = tr( 'This gives a detailed description of what the resource is and ') description_parameter.is_required = True description_parameter.value = '' unit_parameter = StringParameter('UUID-3') unit_parameter.name = self.resource_parameters['Unit'] unit_parameter.help_text = tr( 'Single unit for the resources spelled out. e.g. litre, ' 'kilogram etc.') unit_parameter.description = tr( 'A <b>unit</b> is the basic measurement unit used for computing ' 'the allowance per individual. For example when planning water ' 'rations the unit would be single litre.') unit_parameter.is_required = True unit_parameter.value = '' units_parameter = StringParameter('UUID-4') units_parameter.name = self.resource_parameters['Units'] units_parameter.help_text = tr( 'Multiple units for the resources spelled out. e.g. litres, ' 'kilogram etc.') units_parameter.description = tr( '<b>Units</b> are the basic measurement used for computing the ' 'allowance per individual. For example when planning water ' 'rations the units would be litres.') units_parameter.is_required = True units_parameter.value = '' unit_abbreviation_parameter = StringParameter('UUID-5') unit_abbreviation_parameter.name = \ self.resource_parameters['Unit abbreviation'] unit_abbreviation_parameter.help_text = tr( 'Abbreviations of unit for the resources. e.g. l, kg etc.') unit_abbreviation_parameter.description = tr( "A <b>unit abbreviation</b> is the basic measurement unit's " "shortened. For example when planning water rations " "the units would be l.") unit_abbreviation_parameter.is_required = True unit_abbreviation_parameter.value = '' minimum_parameter = FloatParameter('UUID-6') minimum_parameter.name = self.resource_parameters['Minimum allowed'] minimum_parameter.is_required = True minimum_parameter.precision = 2 minimum_parameter.minimum_allowed_value = -99999.0 minimum_parameter.maximum_allowed_value = 99999.0 minimum_parameter.help_text = tr( 'The minimum allowable quantity per person. ') minimum_parameter.description = tr( 'The <b>minimum</b> is the minimum allowed quantity of the ' 'resource per person. For example you may dictate that the water ' 'ration per person per day should never be allowed to be less ' 'than 0.5l. This is enforced when tweaking a minimum needs set ' 'before an impact evaluation') minimum_parameter.value = 0.00 maximum_parameter = FloatParameter('UUID-7') maximum_parameter.name = self.resource_parameters['Maximum allowed'] maximum_parameter.is_required = True maximum_parameter.precision = 2 maximum_parameter.minimum_allowed_value = -99999.0 maximum_parameter.maximum_allowed_value = 99999.0 maximum_parameter.help_text = tr( 'The maximum allowable quantity per person. ') maximum_parameter.description = tr( 'The <b>maximum</b> is the maximum allowed quantity of the ' 'resource per person. For example you may dictate that the water ' 'ration per person per day should never be allowed to be more ' 'than 67l. This is enforced when tweaking a maximum needs set ' 'before an impact evaluation.') maximum_parameter.value = 100.0 default_parameter = FloatParameter('UUID-8') default_parameter.name = self.resource_parameters['Default'] default_parameter.is_required = True default_parameter.precision = 2 default_parameter.minimum_allowed_value = -99999.0 default_parameter.maximum_allowed_value = 99999.0 default_parameter.help_text = tr( 'The default allowable quantity per person. ') default_parameter.description = tr( "The <b>default</b> is the default allowed quantity of the " "resource per person. For example you may indicate that the water " "ration per person weekly should be 67l.") default_parameter.value = 10.0 frequency_parameter = StringParameter('UUID-9') frequency_parameter.name = self.resource_parameters['Frequency'] frequency_parameter.help_text = tr( "The frequency that this resource needs to be provided to a " "displaced person. e.g. weekly, daily, once etc.") frequency_parameter.description = tr( "The <b>frequency</b> informs the aid worker how regularly this " "resource needs to be provided to the displaced person.") frequency_parameter.is_required = True frequency_parameter.value = tr('weekly') sentence_parameter = TextParameter('UUID-10') sentence_parameter.name = self.resource_parameters['Readable sentence'] sentence_parameter.help_text = tr( 'A readable presentation of the resource.') sentence_parameter.description = tr( "A <b>readable sentence</b> is a presentation of the resource " "that displays all pertinent information. If you are unsure then " "use the default. Properties should be included using double " "curly brackets '{{' '}}'. Including the resource name would be " "achieved by including e.g. {{ Resource name }}") sentence_parameter.is_required = True sentence_parameter.value = tr( 'A displaced person should be provided with ' '%(default value)s %(unit)s/%(units)s/%(unit abbreviation)s of ' '%(resource name)s. Though no less than %(minimum allowed)s ' 'and no more than %(maximum allowed)s. This should be provided ' '%(frequency)s.' % { 'default value': '{{ Default }}', 'unit': '{{ Unit }}', 'units': '{{ Units }}', 'unit abbreviation': '{{ Unit abbreviation }}', 'resource name': '{{ Resource name }}', 'minimum allowed': '{{ Minimum allowed }}', 'maximum allowed': '{{ Maximum allowed }}', 'frequency': '{{ Frequency }}' }) parameters = [ name_parameter, description_parameter, unit_parameter, units_parameter, unit_abbreviation_parameter, default_parameter, minimum_parameter, maximum_parameter, frequency_parameter, sentence_parameter ] parameter_container = ParameterContainer(parameters) parameter_container.setup_ui() layout = QGridLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) layout.addWidget(parameter_container) self.parameters_scrollarea.setLayout(layout)
def main(): """Main function""" application = QApplication([]) name_parameter = StringParameter('UUID-1') name_parameter.name = 'Resource name' name_parameter.help_text = ( 'Name of the resource that will be provided as part of minimum needs.' 'e.g. Tea, Water etc.') name_parameter.description = ( 'A <b>resource</b> is something that you provide to displaced persons ' 'in the event of a disaster. The resource will be made available ' 'at IDP camps and may need to be stockpiled by contingency planners ' 'in their preparations for a disaster.') name_parameter.is_required = True name_parameter.value = '' description_parameter = StringParameter('UUID-1') description_parameter.name = 'Resource description' description_parameter.help_text = ( 'Description of the resource that will be provided as part of minimum ' 'needs. e.g. Tea, Water etc.') description_parameter.description = ( 'Description of the resource that will be provided as part of minimum ' 'needs. e.g. Tea, Water etc.') description_parameter.is_required = True description_parameter.value = '' unit_parameter = StringParameter('UUID-2') unit_parameter.name = 'Units' unit_parameter.help_text = ('Unit for the resources. e.g. litres, kg etc.') unit_parameter.description = ( 'A <b>unit</b> the basic measurement unit used for computing the ' 'allowance per individual. For example when planning water rations ' 'the units would be litres.') unit_parameter.is_required = True unit_parameter.value = '' minimum_parameter = FloatParameter('UUID-3') minimum_parameter.name = 'Minimum allowed' minimum_parameter.is_required = True minimum_parameter.precision = 3 minimum_parameter.minimum_allowed_value = -99999.0 minimum_parameter.maximum_allowed_value = 99999.0 minimum_parameter.help_text = 'The minimum allowable quantity per person. ' minimum_parameter.description = ( 'The <b>minimum</b> is the minimum allowed quantity of the resource ' 'per person. For example you may dictate that the water ration per ' 'person per day should never be allowed to be less than 0.5l.') minimum_parameter.value = 1.0 maximum_parameter = FloatParameter('UUID-3') maximum_parameter.name = 'Minimum allowed' maximum_parameter.is_required = True maximum_parameter.precision = 3 maximum_parameter.minimum_allowed_value = -99999.0 maximum_parameter.maximum_allowed_value = 99999.0 maximum_parameter.help_text = 'The maximum allowable quantity per person. ' maximum_parameter.description = ( 'The <b>maximum</b> is the maximum allowed quantity of the resource ' 'per person. For example you may dictate that the water ration per ' 'person per day should never be allowed to be more than 50l.') maximum_parameter.value = 1.0 maximum_parameter = FloatParameter('UUID-4') maximum_parameter.name = 'Minimum allowed' maximum_parameter.is_required = True maximum_parameter.precision = 3 maximum_parameter.minimum_allowed_value = -99999.0 maximum_parameter.maximum_allowed_value = 99999.0 maximum_parameter.help_text = 'The maximum allowable quantity per person. ' maximum_parameter.description = ( 'The <b>maximum</b> is the maximum allowed quantity of the resource ' 'per person. For example you may dictate that the water ration per ' 'person per day should never be allowed to be more than 50l.') maximum_parameter.value = 1.0 default_parameter = FloatParameter('UUID-5') default_parameter.name = 'Default' default_parameter.is_required = True default_parameter.precision = 3 default_parameter.minimum_allowed_value = -99999.0 default_parameter.default_allowed_value = 99999.0 default_parameter.help_text = 'The default allowable quantity per person. ' default_parameter.description = ( 'The <b>default</b> is the default allowed quantity of the resource ' 'per person. For example you may indicate that the water ration per ' 'person per day should be 25l.') default_parameter.value = 1.0 parameters = [ name_parameter, description_parameter, unit_parameter, minimum_parameter, maximum_parameter, default_parameter ] parameter_container = ParameterContainer(parameters) widget = QWidget() layout = QGridLayout() layout.addWidget(parameter_container) widget.setLayout(layout) widget.setGeometry(0, 0, 500, 500) widget.show() new_parameters = parameter_container.get_parameters() for new_parameter in new_parameters: print new_parameter.name, new_parameter.value sys.exit(application.exec_())
def main(): """Main function""" app = QApplication([]) def validate_min_max(parent_container): """ :param parent_container: The container that use this validator. :type parent_container: ParameterContainer :return: """ min_value_parameter = parent_container.get_parameter_by_guid( min_integer_parameter.guid) max_value_parameter = parent_container.get_parameter_by_guid( max_integer_parameter.guid) min_value = min_value_parameter.value max_value = max_value_parameter.value print 'min', min_value print 'max', max_value if min_value > max_value: print 'Not valid' return { 'valid': False, 'message': ('Your minimum value (%d) should be less than your maximum ' 'value (%d)' % (min_value, max_value)) } print 'Valid' return {'valid': True, 'message': ''} unit_feet = Unit('130790') unit_feet.load_dictionary(unit_feet_depth) unit_metres = Unit('900713') unit_metres.load_dictionary(unit_metres_depth) string_parameter = StringParameter('28082014') string_parameter.name = 'Province Name' string_parameter.description = 'Name of province.' string_parameter.help_text = ( 'A <b>test help</b> that is very long so that you need to ' 'read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you read ' 'this description loudly so that all of your friends will be able ' 'to hear you') string_parameter.is_required = True string_parameter.value = 'Daerah Istimewa Yogyakarta' boolean_parameter = BooleanParameter('1231231') boolean_parameter.name = 'Post processor' boolean_parameter.description = 'This is post processor parameter.' boolean_parameter.help_text = ( 'A <b>test help text</b> that is very long so that you need to ' 'read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you read ' 'this description loudly so that all of your friends will be able ' 'to hear you') boolean_parameter.is_required = True boolean_parameter.value = True float_parameter = FloatParameter() float_parameter.name = 'Flood Depth' float_parameter.is_required = True float_parameter.precision = 3 float_parameter.minimum_allowed_value = 1.0 float_parameter.maximum_allowed_value = 2.0 float_parameter.description = 'The depth of flood.' float_parameter.help_text = ( 'A <b>test _description</b> that is very long so that you need to ' 'read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you read ' 'this description loudly so that all of your friends will be able ' 'to hear you') float_parameter.unit = unit_feet float_parameter.allowed_units = [unit_metres, unit_feet] float_parameter.value = 1.12 integer_parameter = IntegerParameter() integer_parameter.name = 'Paper' integer_parameter.is_required = True integer_parameter.minimum_allowed_value = 1 integer_parameter.maximum_allowed_value = 5 integer_parameter.description = 'Number of paper' integer_parameter.help_text = ( 'A <b>test _description</b> that is very long so that you need to ' 'read it for one minute and you will be tired after read this ' 'description. You are the best user so far. Even better if you read ' 'this description loudly so that all of your friends will be able ' 'to hear you') integer_parameter.unit = unit_feet integer_parameter.allowed_units = [unit_feet] integer_parameter.value = 3 point_parameter = PointParameter() point_parameter.name = 'Point Parameter' point_parameter.is_required = True point_parameter.description = 'Short help.' point_parameter.help_text = 'Long description for parameter.' point_parameter.value = (0, 1) min_integer_parameter = IntegerParameter() min_integer_parameter.name = 'Minimal Stick Length' min_integer_parameter.is_required = True min_integer_parameter.minimum_allowed_value = 1 min_integer_parameter.maximum_allowed_value = 50 min_integer_parameter.description = 'Minimum length of a stick' min_integer_parameter.help_text = ( 'Minimum length of a stick that are allowed') min_integer_parameter.unit = unit_metres min_integer_parameter.allowed_units = [unit_metres] min_integer_parameter.value = 3 max_integer_parameter = IntegerParameter() max_integer_parameter.name = 'Maximum Stick Length' max_integer_parameter.is_required = True max_integer_parameter.minimum_allowed_value = 1 max_integer_parameter.maximum_allowed_value = 50 max_integer_parameter.description = 'Maximum length of a stick' max_integer_parameter.help_text = ( 'Maximum length of a stick that are allowed') max_integer_parameter.unit = unit_metres max_integer_parameter.allowed_units = [unit_metres] max_integer_parameter.value = 4 list_parameter = ListParameter() list_parameter.name = 'Affected Field' list_parameter.is_required = True list_parameter.maximum_item_count = 3 list_parameter.minimum_item_count = 1 list_parameter.description = 'Column used for affected field' list_parameter.help_text = 'Column used for affected field in the vector' list_parameter.element_type = str list_parameter.options_list = [ 'FLOODPRONE', 'affected', 'floodprone', 'yes/no', '\xddounicode test' ] list_parameter.value = ['FLOODPRONE', 'affected', 'floodprone'] select_parameter = SelectParameter() select_parameter.name = 'Select Affected Field' select_parameter.is_required = True select_parameter.description = 'Column used for affected field' select_parameter.help_text = ( 'Column used for affected field in the vector') select_parameter.element_type = str select_parameter.options_list = [ 'FLOODPRONE', 'affected', 'floodprone', 'yes/no', '\xddounicode test' ] select_parameter.value = 'affected' input_list_parameter = InputListParameter() input_list_parameter.name = 'Thresholds' input_list_parameter.is_required = True input_list_parameter.maximum_item_count = 3 input_list_parameter.minimum_item_count = 1 input_list_parameter.description = 'Specified List of thresholds' input_list_parameter.help_text = 'Some help text' input_list_parameter.element_type = int input_list_parameter.ordering = InputListParameter.DescendingOrder input_list_parameter.value = [1] dict_parameter = DictParameter() dict_parameter.name = 'Dict Parameter' dict_parameter.is_required = True dict_parameter.maximum_item_count = 5 dict_parameter.minimum_item_count = 1 dict_parameter.description = 'Dict Parameter example' dict_parameter.help_text = 'Dict Parameter help text.' dict_parameter.element_type = str dict_parameter.value = { 'foo': 'True', 'bar': '10', 'woo': 'False', 'sub_dict_sample': { 'key1': 'val1', 'key2': 'val2' } } group_parameter = GroupParameter() group_parameter.name = 'Age ratios' group_parameter.is_required = True group_parameter.value = [ string_parameter, integer_parameter, boolean_parameter ] def _custom_validator(value): valid = True if string_parameter.value == 'foo' and integer_parameter.value == \ 3 and boolean_parameter.value is True: valid = False if not valid: raise Exception('Parameter not valid') group_parameter.custom_validator = _custom_validator parameters = [ string_parameter, integer_parameter, boolean_parameter, float_parameter, float_parameter, boolean_parameter, integer_parameter, point_parameter, list_parameter, input_list_parameter, dict_parameter, group_parameter, select_parameter ] extra_parameters = [(PointParameter, PointParameterWidget)] min_max_parameters = [min_integer_parameter, max_integer_parameter] description_text = ( 'These parameters are merely created for showing example only') # description_text = '' parameter_container = ParameterContainer(parameters, extra_parameters=extra_parameters, description_text=description_text) parameter_container.setup_ui() # create error handler parameter_widget = parameter_container.get_parameter_widgets() try: input_list_widget = [ w.widget() for w in parameter_widget if isinstance(w.widget(), InputListParameterWidget) ][0] def add_row_handler(exception): box = QMessageBox() box.critical(input_list_widget, 'Add Row Error', exception.message) input_list_widget.add_row_error_handler = add_row_handler except IndexError: pass parameter_container2 = ParameterContainer( extra_parameters=extra_parameters, description_text='Empty Parameter Container Description') parameter_container2.setup_ui() parameter_container3 = ParameterContainer( parameters=min_max_parameters, extra_parameters=extra_parameters, description_text='Minimum Maximum Parameter') parameter_container3.add_validator(validate_min_max) parameter_container3.setup_ui() def show_error_message(parent, exception): """Generate error message to handle parameter errors :param parent: The widget as a parent of message box :type parent: QWidget :param exception: python Exception or Error :type exception: Exception """ box = QMessageBox() box.critical(parent, 'Error occured', exception.message) def show_parameter(the_parameter_container): """Print the value of parameter. :param the_parameter_container: A parameter container :type the_parameter_container: ParameterContainer """ def show_parameter_value(a_parameter): if isinstance(a_parameter, GroupParameter): for param in a_parameter.value: show_parameter_value(param) else: print a_parameter.guid, a_parameter.name, a_parameter.value try: the_parameters = the_parameter_container.get_parameters() if the_parameters: for parameter in the_parameters: show_parameter_value(parameter) except Exception as inst: show_error_message(parameter_container, inst) button = QPushButton('Show parameters') # noinspection PyUnresolvedReferences button.clicked.connect( partial(show_parameter, the_parameter_container=parameter_container)) validate_button = QPushButton('Validate parameters') # noinspection PyUnresolvedReferences validate_button.clicked.connect( partial(show_parameter, the_parameter_container=parameter_container3)) widget = QWidget() layout = QGridLayout() layout.addWidget(parameter_container) layout.addWidget(button) layout.addWidget(parameter_container2) layout.addWidget(parameter_container3) layout.addWidget(validate_button) widget.setLayout(layout) widget.setGeometry(0, 0, 500, 500) widget.show() sys.exit(app.exec_())