def setup_thresholds_panel(self, classification):
        """Setup threshold panel in the right panel.

        :param classification: Classification definition.
        :type classification: dict
        """
        # Set text in the label
        layer_purpose = self.parent.step_kw_purpose.selected_purpose()
        layer_subcategory = self.parent.step_kw_subcategory.\
            selected_subcategory()

        if is_raster_layer(self.parent.layer):
            active_band = self.parent.step_kw_band_selector.selected_band()
            layer_extent = self.parent.layer.extent()
            statistics = self.parent.layer.dataProvider().bandStatistics(
                active_band, QgsRasterBandStats.All, layer_extent, 0)
            description_text = continuous_raster_question % (
                layer_purpose['name'],
                layer_subcategory['name'],
                classification['name'],
                statistics.minimumValue,
                statistics.maximumValue)
        else:
            field_name = self.parent.step_kw_field.selected_fields()
            field_index = self.parent.layer.fields().lookupField(field_name)
            min_value_layer = self.parent.layer.minimumValue(field_index)
            max_value_layer = self.parent.layer.maximumValue(field_index)
            description_text = continuous_vector_question % (
                layer_purpose['name'],
                layer_subcategory['name'],
                field_name,
                classification['name'],
                min_value_layer,
                max_value_layer)

        # Set description
        description_label = QLabel(description_text)
        description_label.setWordWrap(True)
        self.right_layout.addWidget(description_label)

        if self.thresholds:
            thresholds = self.thresholds
        else:
            thresholds = self.parent.get_existing_keyword('thresholds')
        selected_unit = self.parent.step_kw_unit.selected_unit()['key']

        self.threshold_classes = OrderedDict()
        classes = classification.get('classes')
        # Sort by value, put the lowest first
        classes = sorted(classes, key=lambda the_key: the_key['value'])

        grid_layout_thresholds = QGridLayout()

        for i, the_class in enumerate(classes):
            class_layout = QHBoxLayout()

            # Class label
            class_label = QLabel(the_class['name'])

            # Min label
            min_label = QLabel(tr('Min >'))

            # Min value as double spin
            min_value_input = QDoubleSpinBox()
            # TODO(IS) We can set the min and max depends on the unit, later
            min_value_input.setMinimum(0)
            min_value_input.setMaximum(999999)

            if thresholds.get(self.active_exposure['key']):
                exposure_thresholds = thresholds.get(
                    self.active_exposure['key'])
                if exposure_thresholds.get(classification['key']):
                    exposure_thresholds_classifications = exposure_thresholds\
                        .get(classification['key'])
                    min_value_input.setValue(
                        exposure_thresholds_classifications['classes'][
                            the_class['key']][0])
                else:
                    default_min = the_class['numeric_default_min']
                    if isinstance(default_min, dict):
                        default_min = the_class[
                            'numeric_default_min'][selected_unit]
                    min_value_input.setValue(default_min)
            else:
                default_min = the_class['numeric_default_min']
                if isinstance(default_min, dict):
                    default_min = the_class[
                        'numeric_default_min'][selected_unit]
                min_value_input.setValue(default_min)
            min_value_input.setSingleStep(0.1)

            # Max label
            max_label = QLabel(tr('Max <='))

            # Max value as double spin
            max_value_input = QDoubleSpinBox()
            # TODO(IS) We can set the min and max depends on the unit, later
            max_value_input.setMinimum(0)
            max_value_input.setMaximum(999999)
            if thresholds.get(self.active_exposure['key']):
                exposure_thresholds = thresholds.get(
                    self.active_exposure['key'])
                if exposure_thresholds.get(classification['key']):
                    exposure_thresholds_classifications = exposure_thresholds \
                        .get(classification['key'])
                    max_value_input.setValue(
                        exposure_thresholds_classifications['classes'][
                            the_class['key']][1])
                else:
                    default_max = the_class['numeric_default_max']
                    if isinstance(default_max, dict):
                        default_max = the_class[
                            'numeric_default_max'][selected_unit]
                    max_value_input.setValue(default_max)
            else:
                default_max = the_class['numeric_default_max']
                if isinstance(default_max, dict):
                    default_max = the_class[
                        'numeric_default_max'][selected_unit]
                max_value_input.setValue(default_max)
            max_value_input.setSingleStep(0.1)

            # Add to class_layout
            class_layout.addWidget(min_label)
            class_layout.addWidget(min_value_input)
            class_layout.addWidget(max_label)
            class_layout.addWidget(max_value_input)

            class_layout.setStretch(0, 1)
            class_layout.setStretch(1, 2)
            class_layout.setStretch(2, 1)
            class_layout.setStretch(3, 2)

            # Add to grid_layout
            grid_layout_thresholds.addWidget(class_label, i, 0)
            grid_layout_thresholds.addLayout(class_layout, i, 1)

            self.threshold_classes[the_class['key']] = [
                min_value_input, max_value_input]

        grid_layout_thresholds.setColumnStretch(0, 1)
        grid_layout_thresholds.setColumnStretch(0, 2)

        def min_max_changed(double_spin_index, mode):
            """Slot when min or max value change.

            :param double_spin_index: The index of the double spin.
            :type double_spin_index: int

            :param mode: The flag to indicate the min or max value.
            :type mode: int
            """
            if mode == MAX_VALUE_MODE:
                current_max_value = list(self.threshold_classes.values())[
                    double_spin_index][1]
                target_min_value = list(self.threshold_classes.values())[
                    double_spin_index + 1][0]
                if current_max_value.value() != target_min_value.value():
                    target_min_value.setValue(current_max_value.value())
            elif mode == MIN_VALUE_MODE:
                current_min_value = list(self.threshold_classes.values())[
                    double_spin_index][0]
                target_max_value = list(self.threshold_classes.values())[
                    double_spin_index - 1][1]
                if current_min_value.value() != target_max_value.value():
                    target_max_value.setValue(current_min_value.value())

        # Set behaviour
        for k, v in list(self.threshold_classes.items()):
            index = list(self.threshold_classes.keys()).index(k)
            if index < len(self.threshold_classes) - 1:
                # Max value changed
                v[1].valueChanged.connect(partial(
                    min_max_changed,
                    double_spin_index=index,
                    mode=MAX_VALUE_MODE))
            if index > 0:
                # Min value
                v[0].valueChanged.connect(partial(
                    min_max_changed,
                    double_spin_index=index,
                    mode=MIN_VALUE_MODE))

        grid_layout_thresholds.setSpacing(0)

        self.right_layout.addLayout(grid_layout_thresholds)
def setup_thresholds_panel ( self , classification ) : layer_purpose = self . parent . step_kw_purpose . selected_purpose ( ) layer_subcategory = self . parent . step_kw_subcategory . selected_subcategory ( ) if is_raster_layer ( self . parent . layer ) : active_band = self . parent . step_kw_band_selector . selected_band ( ) layer_extent = self . parent . layer . extent ( ) statistics = self . parent . layer . dataProvider ( ) . bandStatistics ( active_band , QgsRasterBandStats . All , layer_extent , 0 ) description_text = continuous_raster_question % ( layer_purpose [ 'name' ] , layer_subcategory [ 'name' ] , classification [ 'name' ] , statistics . minimumValue , statistics . maximumValue ) else : field_name = self . parent . step_kw_field . selected_fields ( ) field_index = self . parent . layer . fields ( ) . lookupField ( field_name ) min_value_layer = self . parent . layer . minimumValue ( field_index ) max_value_layer = self . parent . layer . maximumValue ( field_index ) description_text = continuous_vector_question % ( layer_purpose [ 'name' ] , layer_subcategory [ 'name' ] , field_name , classification [ 'name' ] , min_value_layer , max_value_layer ) description_label = QLabel ( description_text ) description_label . setWordWrap ( True ) self . right_layout . addWidget ( description_label ) if self . thresholds : thresholds = self . thresholds else : thresholds = self . parent . get_existing_keyword ( 'thresholds' ) selected_unit = self . parent . step_kw_unit . selected_unit ( ) [ 'key' ] self . threshold_classes = OrderedDict ( ) classes = classification . get ( 'classes' ) classes = sorted ( classes , key = lambda the_key : the_key [ 'value' ] ) grid_layout_thresholds = QGridLayout ( ) for i , the_class in enumerate ( classes ) : class_layout = QHBoxLayout ( ) class_label = QLabel ( the_class [ 'name' ] ) min_label = QLabel ( tr ( 'Min >' ) ) min_value_input = QDoubleSpinBox ( ) min_value_input . setMinimum ( 0 ) min_value_input . setMaximum ( 999999 ) if thresholds . get ( self . active_exposure [ 'key' ] ) : exposure_thresholds = thresholds . get ( self . active_exposure [ 'key' ] ) if exposure_thresholds . get ( classification [ 'key' ] ) : exposure_thresholds_classifications = exposure_thresholds . get ( classification [ 'key' ] ) min_value_input . setValue ( exposure_thresholds_classifications [ 'classes' ] [ the_class [ 'key' ] ] [ 0 ] ) else : default_min = the_class [ 'numeric_default_min' ] if isinstance ( default_min , dict ) : default_min = the_class [ 'numeric_default_min' ] [ selected_unit ] min_value_input . setValue ( default_min ) else : default_min = the_class [ 'numeric_default_min' ] if isinstance ( default_min , dict ) : default_min = the_class [ 'numeric_default_min' ] [ selected_unit ] min_value_input . setValue ( default_min ) min_value_input . setSingleStep ( 0.1 ) max_label = QLabel ( tr ( 'Max <=' ) ) max_value_input = QDoubleSpinBox ( ) max_value_input . setMinimum ( 0 ) max_value_input . setMaximum ( 999999 ) if thresholds . get ( self . active_exposure [ 'key' ] ) : exposure_thresholds = thresholds . get ( self . active_exposure [ 'key' ] ) if exposure_thresholds . get ( classification [ 'key' ] ) : exposure_thresholds_classifications = exposure_thresholds . get ( classification [ 'key' ] ) max_value_input . setValue ( exposure_thresholds_classifications [ 'classes' ] [ the_class [ 'key' ] ] [ 1 ] ) else : default_max = the_class [ 'numeric_default_max' ] if isinstance ( default_max , dict ) : default_max = the_class [ 'numeric_default_max' ] [ selected_unit ] max_value_input . setValue ( default_max ) else : default_max = the_class [ 'numeric_default_max' ] if isinstance ( default_max , dict ) : default_max = the_class [ 'numeric_default_max' ] [ selected_unit ] max_value_input . setValue ( default_max ) max_value_input . setSingleStep ( 0.1 ) class_layout . addWidget ( min_label ) class_layout . addWidget ( min_value_input ) class_layout . addWidget ( max_label ) class_layout . addWidget ( max_value_input ) class_layout . setStretch ( 0 , 1 ) class_layout . setStretch ( 1 , 2 ) class_layout . setStretch ( 2 , 1 ) class_layout . setStretch ( 3 , 2 ) grid_layout_thresholds . addWidget ( class_label , i , 0 ) grid_layout_thresholds . addLayout ( class_layout , i , 1 ) self . threshold_classes [ the_class [ 'key' ] ] = [ min_value_input , max_value_input ] grid_layout_thresholds . setColumnStretch ( 0 , 1 ) grid_layout_thresholds . setColumnStretch ( 0 , 2 ) def min_max_changed ( double_spin_index , mode ) : if mode == MAX_VALUE_MODE : current_max_value = list ( self . threshold_classes . values ( ) ) [ double_spin_index ] [ 1 ] target_min_value = list ( self . threshold_classes . values ( ) ) [ double_spin_index + 1 ] [ 0 ] if current_max_value . value ( ) != target_min_value . value ( ) : target_min_value . setValue ( current_max_value . value ( ) ) elif mode == MIN_VALUE_MODE : current_min_value = list ( self . threshold_classes . values ( ) ) [ double_spin_index ] [ 0 ] target_max_value = list ( self . threshold_classes . values ( ) ) [ double_spin_index - 1 ] [ 1 ] if current_min_value . value ( ) != target_max_value . value ( ) : target_max_value . setValue ( current_min_value . value ( ) ) for k , v in list ( self . threshold_classes . items ( ) ) : index = list ( self . threshold_classes . keys ( ) ) . index ( k ) if index < len ( self . threshold_classes ) - 1 : v [ 1 ] . valueChanged . connect ( partial ( min_max_changed , double_spin_index = index , mode = MAX_VALUE_MODE ) ) if index > 0 : v [ 0 ] . valueChanged . connect ( partial ( min_max_changed , double_spin_index = index , mode = MIN_VALUE_MODE ) ) grid_layout_thresholds . setSpacing ( 0 ) self . right_layout . addLayout ( grid_layout_thresholds )
Setup threshold panel in the right panel.

        :param classification: Classification definition.
        :type classification: dict