def addInputOfType(self, paramType, pos=None): new_param = None if ModelerParameterDefinitionDialog.use_legacy_dialog( paramType=paramType): dlg = ModelerParameterDefinitionDialog(self.model, paramType) if dlg.exec_(): new_param = dlg.param else: # yay, use new API! context = createContext() widget_context = self.create_widget_context() dlg = QgsProcessingParameterDefinitionDialog( type=paramType, context=context, widgetContext=widget_context, algorithm=self.model) if dlg.exec_(): new_param = dlg.createParameter() self.autogenerate_parameter_name(new_param) if new_param is not None: if pos is None: pos = self.getPositionForParameterItem() if isinstance(pos, QPoint): pos = QPointF(pos) component = QgsProcessingModelParameter(new_param.name()) component.setDescription(new_param.name()) component.setPosition(pos) self.model.addModelParameter(new_param, component) self.repaintModel() # self.view.ensureVisible(self.scene.getLastParameterItem()) self.hasChanged = True
def editElement(self): if isinstance(self.element, QgsProcessingModelParameter): existing_param = self.model.parameterDefinition( self.element.parameterName()) new_param = None if ModelerParameterDefinitionDialog.use_legacy_dialog( param=existing_param): # boo, old api dlg = ModelerParameterDefinitionDialog(self.model, param=existing_param) if dlg.exec_(): new_param = dlg.param 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) if dlg.exec_(): new_param = dlg.createParameter(existing_param.name()) if new_param is not None: self.model.removeModelParameter(self.element.parameterName()) self.element.setParameterName(new_param.name()) self.element.setDescription(new_param.name()) self.model.addModelParameter(new_param, self.element) self.text = new_param.description() self.scene.dialog.repaintModel() elif isinstance(self.element, QgsProcessingModelChildAlgorithm): elemAlg = self.element.algorithm() dlg = ModelerParametersDialog(elemAlg, self.model, self.element.childId(), self.element.configuration()) if dlg.exec_(): alg = dlg.createAlgorithm() alg.setChildId(self.element.childId()) self.updateAlgorithm(alg) self.scene.dialog.repaintModel() elif isinstance(self.element, QgsProcessingModelOutput): child_alg = self.model.childAlgorithm(self.element.childId()) param_name = '{}:{}'.format(self.element.childId(), self.element.name()) dlg = ModelerParameterDefinitionDialog( self.model, param=self.model.parameterDefinition(param_name)) if dlg.exec_() and dlg.param is not None: model_output = child_alg.modelOutput(self.element.name()) model_output.setDescription(dlg.param.description()) model_output.setDefaultValue(dlg.param.defaultValue()) model_output.setMandatory(not ( dlg.param.flags() & QgsProcessingParameterDefinition.FlagOptional)) self.model.updateDestinationParameters()
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 addInput(self, paramType, pos=None): if paramType not in [ param.id() for param in QgsApplication.instance(). processingRegistry().parameterTypes() ]: return new_param = None comment = None if ModelerParameterDefinitionDialog.use_legacy_dialog( paramType=paramType): dlg = ModelerParameterDefinitionDialog(self.model(), paramType) if dlg.exec_(): new_param = dlg.param comment = dlg.comments() else: # yay, use new API! context = createContext() widget_context = self.create_widget_context() dlg = QgsProcessingParameterDefinitionDialog( type=paramType, context=context, widgetContext=widget_context, algorithm=self.model()) dlg.registerProcessingContextGenerator(self.context_generator) if dlg.exec_(): new_param = dlg.createParameter() self.autogenerate_parameter_name(new_param) comment = dlg.comments() if new_param is not None: if pos is None or not pos: pos = self.getPositionForParameterItem() if isinstance(pos, QPoint): pos = QPointF(pos) component = QgsProcessingModelParameter(new_param.name()) component.setDescription(new_param.name()) component.setPosition(pos) component.comment().setDescription(comment) component.comment().setPosition(component.position() + QPointF( component.size().width(), -1.5 * component.size().height())) self.beginUndoCommand(self.tr('Add Model Input')) self.model().addModelParameter(new_param, component) self.repaintModel() # self.view().ensureVisible(self.scene.getLastParameterItem()) self.endUndoCommand()
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()