def testModelerParametersDialogAvailableValuesOfType(self): # test getAvailableValuesOfType from ModelerParametersDialog m = ModelerAlgorithm() string_param_1 = ModelerParameter(ParameterString('string', 'string desc')) m.addParameter(string_param_1) string_param_2 = ModelerParameter(ParameterString('string2', 'string desc')) m.addParameter(string_param_2) num_param = ModelerParameter(ParameterNumber('number', 'number desc')) m.addParameter(num_param) table_field_param = ModelerParameter(ParameterTableField('field', 'field desc')) m.addParameter(table_field_param) file_param = ModelerParameter(ParameterFile('file', 'file desc')) m.addParameter(file_param) dlg = ModelerParametersDialog(m, m) # test single types self.assertEqual(set(p.name for p in dlg.getAvailableValuesOfType(ParameterNumber)), set(['number'])) self.assertEqual(set(p.name for p in dlg.getAvailableValuesOfType(ParameterTableField)), set(['field'])) self.assertEqual(set(p.name for p in dlg.getAvailableValuesOfType(ParameterFile)), set(['file'])) # test multiple types self.assertEqual(set(p.name for p in dlg.getAvailableValuesOfType([ParameterString, ParameterNumber, ParameterFile])), set(['string', 'string2', 'number', 'file']))
def testModelerParametersDialogAvailableValuesOfType(self): # test getAvailableValuesOfType from ModelerParametersDialog m = ModelerAlgorithm() string_param_1 = ModelerParameter(ParameterString("string", "string desc")) m.addParameter(string_param_1) string_param_2 = ModelerParameter(ParameterString("string2", "string desc")) m.addParameter(string_param_2) num_param = ModelerParameter(ParameterNumber("number", "number desc")) m.addParameter(num_param) table_field_param = ModelerParameter(ParameterTableField("field", "field desc")) m.addParameter(table_field_param) file_param = ModelerParameter(ParameterFile("file", "file desc")) m.addParameter(file_param) dlg = ModelerParametersDialog(m, m) # test single types self.assertEqual(set(p.name for p in dlg.getAvailableValuesOfType(ParameterNumber)), set(["number"])) self.assertEqual(set(p.name for p in dlg.getAvailableValuesOfType(ParameterTableField)), set(["field"])) self.assertEqual(set(p.name for p in dlg.getAvailableValuesOfType(ParameterFile)), set(["file"])) # test multiple types self.assertEqual( set(p.name for p in dlg.getAvailableValuesOfType([ParameterString, ParameterNumber, ParameterFile])), set(["string", "string2", "number", "file"]), )
def testModelerParametersDialogAvailableValuesOfType(self): # test getAvailableValuesOfType from ModelerParametersDialog m = QgsProcessingModelAlgorithm() string_param_1 = QgsProcessingModelParameter('string') m.addModelParameter(QgsProcessingParameterString('string'), string_param_1) string_param_2 = QgsProcessingModelParameter('string2') m.addModelParameter(QgsProcessingParameterString('string2'), string_param_2) num_param = QgsProcessingModelParameter('number') m.addModelParameter(QgsProcessingParameterNumber('number'), num_param) table_field_param = QgsProcessingModelParameter('field') m.addModelParameter(QgsProcessingParameterField('field'), table_field_param) file_param = QgsProcessingModelParameter('file') m.addModelParameter(QgsProcessingParameterFile('file'), file_param) dlg = ModelerParametersDialog(m, m) # test single types self.assertEqual(set(p.parameterName() for p in dlg.getAvailableValuesOfType(QgsProcessingParameterNumber)), set(['number'])) self.assertEqual(set(p.parameterName() for p in dlg.getAvailableValuesOfType(QgsProcessingParameterField)), set(['field'])) self.assertEqual(set(p.parameterName() for p in dlg.getAvailableValuesOfType(QgsProcessingParameterFile)), set(['file'])) # test multiple types self.assertEqual(set(p.parameterName() for p in dlg.getAvailableValuesOfType([QgsProcessingParameterString, QgsProcessingParameterNumber, QgsProcessingParameterFile])), set(['string', 'string2', 'number', 'file']))
def __init__(self, alg, model, algName=None): ModelerParametersDialog.__init__(self, alg, model, algName) paramsLayout = self.paramPanel.layout() item = paramsLayout.itemAt(paramsLayout.count() - 1) if isinstance(item, QSpacerItem): paramsLayout.removeItem(item) item = None
def setPreviousValues(self): ModelerParametersDialog.setPreviousValues(self) if self._algName is not None: alg = self.model.algs[self._algName] for param in alg.algorithm.parameters: if isinstance(param, ParameterFieldsMapping): widget = self.valueItems[param.name] value = alg.params[param.name] if isinstance(value, unicode): # convert to list because of ModelerAlgorithme.resolveValue behavior with lists value = eval(value) widget.setValue(value)
def _addAlgorithm(self, alg, pos=None): dlg = ModelerParametersDialog(alg, self.model) if dlg.exec_(): alg = dlg.createAlgorithm() if pos is None: alg.setPosition(self.getPositionForAlgorithmItem()) else: alg.setPosition(pos) from processing.modeler.ModelerGraphicItem import ModelerGraphicItem for i, out in enumerate(alg.modelOutputs()): alg.modelOutput(out).setPosition(alg.position() + QPointF(ModelerGraphicItem.BOX_WIDTH, (i + 1.5) * ModelerGraphicItem.BOX_HEIGHT)) self.model.addChildAlgorithm(alg) self.repaintModel() self.hasChanged = True
def _addAlgorithm(self, alg, pos=None): dlg = alg.getCustomModelerParametersDialog(self.alg) if not dlg: dlg = ModelerParametersDialog(alg, self.alg) dlg.exec_() if dlg.alg is not None: if pos is None: dlg.alg.pos = self.getPositionForAlgorithmItem() else: dlg.alg.pos = pos if isinstance(dlg.alg.pos, QPoint): dlg.alg.pos = QPointF(pos) from processing.modeler.ModelerGraphicItem import ModelerGraphicItem for i, out in enumerate(dlg.alg.outputs): dlg.alg.outputs[out].pos = dlg.alg.pos + QPointF(ModelerGraphicItem.BOX_WIDTH, (i + 1.5) * ModelerGraphicItem.BOX_HEIGHT) self.alg.addAlgorithm(dlg.alg) self.repaintModel() self.hasChanged = True
def _addAlgorithm(self, alg, pos=None): dlg = alg.getCustomModelerParametersDialog(self.alg) if not dlg: dlg = ModelerParametersDialog(alg, self.alg) dlg.exec_() if dlg.alg is not None: if pos is None: dlg.alg.pos = self.getPositionForAlgorithmItem() else: dlg.alg.pos = pos if isinstance(dlg.alg.pos, QPoint): dlg.alg.pos = QPointF(pos) from processing.modeler.ModelerGraphicItem import ModelerGraphicItem for i, out in enumerate(dlg.alg.outputs): dlg.alg.outputs[out].pos = dlg.alg.pos + QPointF( ModelerGraphicItem.BOX_WIDTH, (i + 1.5) * ModelerGraphicItem.BOX_HEIGHT) self.alg.addAlgorithm(dlg.alg) self.repaintModel() self.hasChanged = True
def _addAlgorithm(self, alg, pos=None): dlg = None try: dlg = alg.getCustomModelerParametersDialog(self.model) except: pass if not dlg: dlg = ModelerParametersDialog(alg, self.model) dlg.exec_() if dlg.alg is not None: if pos is None: dlg.alg.setPosition(self.getPositionForAlgorithmItem()) else: dlg.alg.setPosition(pos) from processing.modeler.ModelerGraphicItem import ModelerGraphicItem for i, out in enumerate(dlg.alg.modelOutputs()): dlg.alg.modelOutput(out).setPosition(dlg.alg.position() + QPointF(ModelerGraphicItem.BOX_WIDTH, (i + 1.5) * ModelerGraphicItem.BOX_HEIGHT)) self.model.addChildAlgorithm(dlg.alg) self.repaintModel() self.hasChanged = True
def _addAlgorithm(self, alg, pos=None): dlg = None try: dlg = alg.getCustomModelerParametersDialog(self.model) except: pass if not dlg: dlg = ModelerParametersDialog(alg, self.model) if dlg.exec_(): alg = dlg.createAlgorithm() if pos is None: alg.setPosition(self.getPositionForAlgorithmItem()) else: alg.setPosition(pos) from processing.modeler.ModelerGraphicItem import ModelerGraphicItem for i, out in enumerate(alg.modelOutputs()): alg.modelOutput(out).setPosition(alg.position() + QPointF(ModelerGraphicItem.BOX_WIDTH, (i + 1.5) * ModelerGraphicItem.BOX_HEIGHT)) self.model.addChildAlgorithm(alg) self.repaintModel() self.hasChanged = True
def getWidgetFromParameter(self, param): if isinstance(param, ParameterBands): strings = self.getAvailableValuesOfType(ParameterBands) options = [(self.resolveValueDescription(s), s) for s in strings] item = QComboBox() item.setEditable(True) for desc, val in options: item.addItem(desc, val) item.setEditText(unicode(param.default or "")) else: item = ModelerParametersDialog.getWidgetFromParameter(self, param) return item
def edit(self, edit_comment=False): elemAlg = self.component().algorithm() dlg = ModelerParametersDialog(elemAlg, self.model(), self.component().childId(), self.component().configuration()) dlg.setComments(self.component().comment().description()) if edit_comment: dlg.switchToCommentTab() if dlg.exec_(): alg = dlg.createAlgorithm() alg.setChildId(self.component().childId()) alg.copyNonDefinitionPropertiesFromModel(self.model()) self.model().setChildAlgorithm(alg) self.requestModelRepaint.emit() self.changed.emit()
def addAlgorithm(self, alg_id, pos=None): alg = QgsApplication.processingRegistry().createAlgorithmById(alg_id) if not alg: return dlg = ModelerParametersDialog(alg, self.model()) if dlg.exec_(): alg = dlg.createAlgorithm() if pos is None or not pos: alg.setPosition(self.getPositionForAlgorithmItem()) else: alg.setPosition(pos) alg.comment().setPosition(alg.position() + QPointF( alg.size().width(), -1.5 * alg.size().height())) output_offset_x = alg.size().width() output_offset_y = 1.5 * alg.size().height() for out in alg.modelOutputs(): alg.modelOutput(out).setPosition(alg.position() + QPointF(output_offset_x, output_offset_y)) output_offset_y += 1.5 * alg.modelOutput(out).size().height() self.beginUndoCommand(self.tr('Add Algorithm')) id = self.model().addChildAlgorithm(alg) self.repaintModel() self.endUndoCommand() res, errors = self.model().validateChildAlgorithm(id) if not res: self.view().scene().showWarning( QCoreApplication.translate('ModelerDialog', 'Algorithm “{}” is invalid').format(alg.description()), self.tr('Algorithm is Invalid'), QCoreApplication.translate('ModelerDialog', "<p>The “{}” algorithm is invalid, because:</p><ul><li>{}</li></ul>").format(alg.description(), '</li><li>'.join(errors)), level=Qgis.Warning ) else: self.view().scene().messageBar().clearWidgets()
def edit(self, edit_comment=False): elemAlg = self.component().algorithm() dlg = ModelerParametersDialog(elemAlg, self.model(), self.component().childId(), self.component().configuration()) dlg.setComments(self.component().comment().description()) dlg.setCommentColor(self.component().comment().color()) if edit_comment: dlg.switchToCommentTab() if dlg.exec_(): alg = dlg.createAlgorithm() alg.setChildId(self.component().childId()) alg.copyNonDefinitionPropertiesFromModel(self.model()) self.aboutToChange.emit(self.tr('Edit {}').format(alg.description())) self.model().setChildAlgorithm(alg) self.requestModelRepaint.emit() self.changed.emit() res, errors = self.model().validateChildAlgorithm(alg.childId()) if not res: self.scene().showWarning(self.tr('Algorithm “{}” is invalid').format(alg.description()), self.tr('Algorithm is Invalid'), self.tr( "<p>The “{}” algorithm is invalid, because:</p><ul><li>{}</li></ul>").format(alg.description(), '</li><li>'.join(errors)), level=Qgis.Warning) else: self.scene().messageBar().clearWidgets()
def editElement(self): if isinstance(self.element, QgsProcessingModelParameter): dlg = ModelerParameterDefinitionDialog(self.model, param=self.model.parameterDefinition(self.element.parameterName())) if dlg.exec_() and dlg.param is not None: self.model.removeModelParameter(self.element.parameterName()) self.element.setParameterName(dlg.param.name()) self.element.setDescription(dlg.param.name()) self.model.addModelParameter(dlg.param, self.element) self.text = dlg.param.description() self.scene.dialog.repaintModel() elif isinstance(self.element, QgsProcessingModelChildAlgorithm): elemAlg = self.element.algorithm() dlg = ModelerParametersDialog(elemAlg, self.model, self.element.childId()) if dlg.exec_(): alg = dlg.createAlgorithm() alg.setChildId(self.element.childId()) self.updateAlgorithm(alg) self.scene.dialog.repaintModel()
def editElement(self): if isinstance(self.element, ModelerParameter): dlg = ModelerParameterDefinitionDialog(self.model, param=self.element.param) dlg.exec_() if dlg.param is not None: self.model.updateParameter(dlg.param) self.element.param = dlg.param self.text = dlg.param.description self.update() elif isinstance(self.element, Algorithm): dlg = self.element.algorithm.getCustomModelerParametersDialog(self.model, self.element.modeler_name) if not dlg: dlg = ModelerParametersDialog(self.element.algorithm, self.model, self.element.modeler_name) dlg.exec_() if dlg.alg is not None: dlg.alg.modeler_name = self.element.modeler_name self.model.updateAlgorithm(dlg.alg) self.model.updateModelerView()
def checkConstructWrapper(self, param, expected_wrapper_class): alg = QgsApplication.processingRegistry().createAlgorithmById( 'native:centroids') # algorithm dialog dlg = AlgorithmDialog(alg) wrapper = WidgetWrapperFactory.create_wrapper_from_class(param, dlg) self.assertIsNotNone(wrapper) self.assertIsInstance(wrapper, expected_wrapper_class) self.assertEqual(wrapper.dialog, dlg) self.assertIsNotNone(wrapper.widget) wrapper.widget.deleteLater() del wrapper.widget del wrapper alg = QgsApplication.processingRegistry().createAlgorithmById( 'native:centroids') # batch dialog dlg = BatchAlgorithmDialog(alg) wrapper = WidgetWrapperFactory.create_wrapper_from_class(param, dlg) self.assertIsNotNone(wrapper) self.assertIsInstance(wrapper, expected_wrapper_class) self.assertEqual(wrapper.dialog, dlg) self.assertIsNotNone(wrapper.widget) alg = QgsApplication.processingRegistry().createAlgorithmById( 'native:centroids') # modeler dialog model = QgsProcessingModelAlgorithm() dlg = ModelerParametersDialog(alg, model) wrapper = WidgetWrapperFactory.create_wrapper_from_class(param, dlg) self.assertIsNotNone(wrapper) self.assertIsInstance(wrapper, expected_wrapper_class) self.assertEqual(wrapper.dialog, dlg) self.assertIsNotNone(wrapper.widget) wrapper.widget.deleteLater() del wrapper.widget
def editElement(self): if isinstance(self.element, QgsProcessingModelParameter): dlg = ModelerParameterDefinitionDialog( self.model, param=self.model.parameterDefinition( self.element.parameterName())) if dlg.exec_() and dlg.param is not None: self.model.removeModelParameter(self.element.parameterName()) self.element.setParameterName(dlg.param.name()) self.element.setDescription(dlg.param.name()) self.model.addModelParameter(dlg.param, self.element) self.text = dlg.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 test_OTBParameterChoice_Gui(self): """ This test is similar to GuiTests in processing that is done on other parameter widget in processing Main difference is this test uses create_wrapper_from_metadata() rather than create_wrapper_from_class() like rest of processing widgets. """ param = OtbParameterChoice('test') alg = QgsApplication.processingRegistry().createAlgorithmById( 'otb:Smoothing') # algorithm dialog dlg = AlgorithmDialog(alg) wrapper = WidgetWrapperFactory.create_wrapper_from_metadata(param, dlg) self.assertIsNotNone(wrapper) self.assertIsInstance(wrapper, OtbChoiceWidgetWrapper) self.assertEqual(wrapper.dialog, dlg) self.assertIsNotNone(wrapper.widget) alg = QgsApplication.processingRegistry().createAlgorithmById( 'otb:Smoothing') # batch dialog dlg = BatchAlgorithmDialog(alg) wrapper = WidgetWrapperFactory.create_wrapper_from_metadata(param, dlg) self.assertIsNotNone(wrapper) self.assertIsInstance(wrapper, OtbChoiceWidgetWrapper) self.assertEqual(wrapper.dialog, dlg) self.assertIsNotNone(wrapper.widget) alg = QgsApplication.processingRegistry().createAlgorithmById( 'otb:Smoothing') # modeler dialog model = QgsProcessingModelAlgorithm() dlg = ModelerParametersDialog(alg, model) wrapper = WidgetWrapperFactory.create_wrapper_from_metadata(param, dlg) self.assertIsNotNone(wrapper) self.assertIsInstance(wrapper, OtbChoiceWidgetWrapper) self.assertEqual(wrapper.dialog, dlg) self.assertIsNotNone(wrapper.widget)
def editElement(self): self.model.setPositions(self.scene().getParameterPositions(), self.scene().getAlgorithmPositions(), self.scene().getOutputPositions()) if isinstance(self.element, Parameter): dlg = ModelerParameterDefinitionDialog(self.model, param=self.element) dlg.exec_() if dlg.param != None: self.model.updateParameter(self.elementIndex, dlg.param) self.element = dlg.param self.text = self.element.description self.update() elif isinstance(self.element, GeoAlgorithm): dlg = self.element.getCustomModelerParametersDialog( self.model, self.elementIndex) if not dlg: dlg = ModelerParametersDialog(self.element, self.model, self.elementIndex) dlg.exec_() if dlg.params != None: self.model.updateAlgorithm(self.elementIndex, dlg.params, dlg.values, dlg.outputs, dlg.dependencies)
def getWidgetFromParameter(self, param): if isinstance(param, ParameterFieldsMapping): return FieldsMappingPanel() return ModelerParametersDialog.getWidgetFromParameter(self, param)
def setParamValue(self, alg, param, widget): if isinstance(param, ParameterFieldsMapping): # convert to unicode because of ModelerAlgorithme.resolveValue behavior with lists alg.params[param.name] = unicode(widget.value()) return True return ModelerParametersDialog.setParamValue(self, alg, param, widget)