Ejemplo n.º 1
0
    def edit(self, edit_comment=False):
        existing_param = self.model().parameterDefinition(
            self.component().parameterName())
        comment = self.component().comment().description()
        comment_color = self.component().comment().color()
        new_param = None
        if ModelerParameterDefinitionDialog.use_legacy_dialog(
                param=existing_param):
            # boo, old api
            dlg = ModelerParameterDefinitionDialog(self.model(),
                                                   param=existing_param)
            dlg.setComments(comment)
            dlg.setCommentColor(comment_color)
            if edit_comment:
                dlg.switchToCommentTab()
            if dlg.exec_():
                new_param = dlg.param
                comment = dlg.comments()
                comment_color = dlg.commentColor()
        else:
            # yay, use new API!
            context = createContext()
            widget_context = self.create_widget_context()
            dlg = QgsProcessingParameterDefinitionDialog(
                type=existing_param.type(),
                context=context,
                widgetContext=widget_context,
                definition=existing_param,
                algorithm=self.model())
            dlg.setComments(comment)
            dlg.setCommentColor(comment_color)
            dlg.registerProcessingContextGenerator(self.context_generator)

            if edit_comment:
                dlg.switchToCommentTab()

            if dlg.exec_():
                new_param = dlg.createParameter(existing_param.name())
                comment = dlg.comments()
                comment_color = dlg.commentColor()

        if new_param is not None:
            self.aboutToChange.emit(
                self.tr('Edit {}').format(new_param.description()))
            self.model().removeModelParameter(self.component().parameterName())
            self.component().setParameterName(new_param.name())
            self.component().setDescription(new_param.name())
            self.component().comment().setDescription(comment)
            self.component().comment().setColor(comment_color)
            self.model().addModelParameter(new_param, self.component())
            self.setLabel(new_param.description())
            self.requestModelRepaint.emit()
            self.changed.emit()
    def edit(self, edit_comment=False):
        child_alg = self.model().childAlgorithm(self.component().childId())
        param_name = '{}:{}'.format(self.component().childId(),
                                    self.component().name())
        dlg = ModelerParameterDefinitionDialog(
            self.model(), param=self.model().parameterDefinition(param_name))
        dlg.setComments(self.component().comment().description())
        dlg.setCommentColor(self.component().comment().color())
        if edit_comment:
            dlg.switchToCommentTab()

        if dlg.exec_():
            model_output = child_alg.modelOutput(self.component().name())
            model_output.setDescription(dlg.param.description())
            model_output.setDefaultValue(dlg.param.defaultValue())
            model_output.setMandatory(not (
                dlg.param.flags()
                & QgsProcessingParameterDefinition.FlagOptional))
            model_output.comment().setDescription(dlg.comments())
            model_output.comment().setColor(dlg.commentColor())
            self.aboutToChange.emit(
                self.tr('Edit {}').format(model_output.description()))
            self.model().updateDestinationParameters()
            self.requestModelRepaint.emit()
            self.changed.emit()
Ejemplo n.º 3
0
    def edit(self, edit_comment=False):
        existing_param = self.model().parameterDefinition(
            self.component().parameterName())
        old_name = existing_param.name()
        old_description = existing_param.description()

        comment = self.component().comment().description()
        comment_color = self.component().comment().color()
        new_param = None
        if ModelerParameterDefinitionDialog.use_legacy_dialog(
                param=existing_param):
            # boo, old api
            dlg = ModelerParameterDefinitionDialog(self.model(),
                                                   param=existing_param)
            dlg.setComments(comment)
            dlg.setCommentColor(comment_color)
            if edit_comment:
                dlg.switchToCommentTab()
            if dlg.exec_():
                new_param = dlg.param
                comment = dlg.comments()
                comment_color = dlg.commentColor()
        else:
            # yay, use new API!
            context = createContext()
            widget_context = self.create_widget_context()
            dlg = QgsProcessingParameterDefinitionDialog(
                type=existing_param.type(),
                context=context,
                widgetContext=widget_context,
                definition=existing_param,
                algorithm=self.model())
            dlg.setComments(comment)
            dlg.setCommentColor(comment_color)
            dlg.registerProcessingContextGenerator(self.context_generator)

            if edit_comment:
                dlg.switchToCommentTab()

            if dlg.exec_():
                new_param = dlg.createParameter(existing_param.name())
                comment = dlg.comments()
                comment_color = dlg.commentColor()

                safeName = QgsProcessingModelAlgorithm.safeName(
                    new_param.description())
                new_param.setName(safeName.lower())

        if new_param is not None:
            self.aboutToChange.emit(
                self.tr('Edit {}').format(new_param.description()))
            self.model().removeModelParameter(self.component().parameterName())

            if new_param.description() != old_description:
                # only update name if user has changed the description -- we don't force this, as it may cause
                # unwanted name updates which could potentially break the model's API
                name = new_param.name()

                base_name = name
                i = 2
                while self.model().parameterDefinition(name):
                    name = base_name + str(i)
                    i += 1

                new_param.setName(name)

                self.model().changeParameterName(old_name, new_param.name())

            self.component().setParameterName(new_param.name())
            self.component().setDescription(new_param.name())
            self.component().comment().setDescription(comment)
            self.component().comment().setColor(comment_color)
            self.model().addModelParameter(new_param, self.component())
            self.setLabel(new_param.description())
            self.requestModelRepaint.emit()
            self.changed.emit()