[processing] some fixes and changes in parameters and parameters panel

This commit is contained in:
volaya 2016-11-17 12:06:24 +01:00
parent a17facf6cb
commit fcc343724e
8 changed files with 87 additions and 103 deletions

View File

@ -78,7 +78,8 @@ class GdalParametersPanel(ParametersPanel):
self.parametersHaveChanged() self.parametersHaveChanged()
def connectParameterSignals(self): def connectParameterSignals(self):
for w in list(self.widgets.values()): for wrapper in list(self.wrappers.values()):
w = wrapper.widget
if isinstance(w, QLineEdit): if isinstance(w, QLineEdit):
w.textChanged.connect(self.parametersHaveChanged) w.textChanged.connect(self.parametersHaveChanged)
elif isinstance(w, QComboBox): elif isinstance(w, QComboBox):

View File

@ -16,9 +16,6 @@
* * * *
*************************************************************************** ***************************************************************************
""" """
from builtins import str
from builtins import object
__author__ = 'Victor Olaya' __author__ = 'Victor Olaya'
__date__ = 'August 2012' __date__ = 'August 2012'
@ -38,6 +35,9 @@ from qgis.PyQt.QtCore import QCoreApplication, QSettings
from qgis.core import QgsVectorLayer, QgsRasterLayer from qgis.core import QgsVectorLayer, QgsRasterLayer
from builtins import str
from builtins import object
from processing.gui.ParametersPanel import ParametersPanel
from processing.core.ProcessingLog import ProcessingLog from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
@ -46,10 +46,8 @@ from processing.core.parameters import ParameterRaster, ParameterVector, Paramet
from processing.core.outputs import OutputVector, OutputRaster, OutputTable, OutputHTML, Output from processing.core.outputs import OutputVector, OutputRaster, OutputTable, OutputHTML, Output
from processing.algs.gdal.GdalUtils import GdalUtils from processing.algs.gdal.GdalUtils import GdalUtils
from processing.tools import dataobjects, vector from processing.tools import dataobjects, vector
from processing.tools.system import setTempOutput
from processing.algs.help import shortHelp from processing.algs.help import shortHelp
class GeoAlgorithm(object): class GeoAlgorithm(object):
def __init__(self): def __init__(self):
@ -134,6 +132,10 @@ class GeoAlgorithm(object):
""" """
pass pass
def getParametersPanel(self, parent):
return ParametersPanel(parent, self)
def getCustomParametersDialog(self): def getCustomParametersDialog(self):
"""If the algorithm has a custom parameters dialog, it should """If the algorithm has a custom parameters dialog, it should
be returned here, ready to be executed. be returned here, ready to be executed.
@ -184,6 +186,12 @@ class GeoAlgorithm(object):
""" """
return None return None
def processBeforeAddingToModeler(self, alg, model):
"""Add here any task that has to be performed before adding an algorithm
to a model, such as changing the value of a parameter depending on value
of another one"""
pass
# ========================================================= # =========================================================
def execute(self, progress=SilentProgress(), model=None): def execute(self, progress=SilentProgress(), model=None):

View File

@ -123,7 +123,6 @@ class Parameter(object):
self.optional = parseBool(optional) self.optional = parseBool(optional)
# TODO: make deep copy and deep update
self.metadata = deepcopy(self.default_metadata) self.metadata = deepcopy(self.default_metadata)
self.metadata.update(deepcopy(metadata)) self.metadata.update(deepcopy(metadata))
@ -179,7 +178,7 @@ class Parameter(object):
def wrapper(self, dialog, row=0, col=0): def wrapper(self, dialog, row=0, col=0):
wrapper = self.metadata.get('widget_wrapper', None) wrapper = self.metadata.get('widget_wrapper', None)
# wrapper metadata should be a class path # wrapper metadata should be a class path
if isinstance(wrapper, str): if isinstance(wrapper, basestring):
tokens = wrapper.split('.') tokens = wrapper.split('.')
mod = __import__('.'.join(tokens[:-1]), fromlist=[tokens[-1]]) mod = __import__('.'.join(tokens[:-1]), fromlist=[tokens[-1]])
wrapper = getattr(mod, tokens[-1]) wrapper = getattr(mod, tokens[-1])
@ -362,7 +361,10 @@ class ParameterExtent(Parameter):
return False return False
def getValueAsCommandLineParameter(self): def getValueAsCommandLineParameter(self):
if self.value is not None:
return '"' + str(self.value) + '"' return '"' + str(self.value) + '"'
else:
return str(None)
def getAsScriptCode(self): def getAsScriptCode(self):
param_type = '' param_type = ''
@ -582,8 +584,8 @@ class ParameterMultipleInput(ParameterDataObject):
exported = None exported = None
def __init__(self, name='', description='', datatype=-1, optional=False): def __init__(self, name='', description='', datatype=-1, optional=False, metadata={}):
ParameterDataObject.__init__(self, name, description, None, optional) ParameterDataObject.__init__(self, name, description, None, optional, metadata=metadata)
self.datatype = int(float(datatype)) self.datatype = int(float(datatype))
self.exported = None self.exported = None
self.minNumInputs = 0 self.minNumInputs = 0
@ -1133,8 +1135,8 @@ class ParameterString(Parameter):
ESCAPED_NEWLINE = '\\n' ESCAPED_NEWLINE = '\\n'
def __init__(self, name='', description='', default=None, multiline=False, def __init__(self, name='', description='', default=None, multiline=False,
optional=False, evaluateExpressions=False): optional=False, evaluateExpressions=False, metadata={}):
Parameter.__init__(self, name, description, default, optional) Parameter.__init__(self, name, description, default, optional, metadata)
self.multiline = parseBool(multiline) self.multiline = parseBool(multiline)
self.evaluateExpressions = parseBool(evaluateExpressions) self.evaluateExpressions = parseBool(evaluateExpressions)

View File

@ -39,7 +39,6 @@ from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
from processing.gui.ParametersPanel import ParametersPanel
from processing.gui.AlgorithmDialogBase import AlgorithmDialogBase from processing.gui.AlgorithmDialogBase import AlgorithmDialogBase
from processing.gui.AlgorithmExecutor import runalg, runalgIterating from processing.gui.AlgorithmExecutor import runalg, runalgIterating
from processing.gui.Postprocessing import handleAlgorithmResults from processing.gui.Postprocessing import handleAlgorithmResults
@ -63,7 +62,7 @@ class AlgorithmDialog(AlgorithmDialogBase):
self.alg = alg self.alg = alg
self.setMainWidget(ParametersPanel(self, alg)) self.setMainWidget(alg.getParametersPanel(self))
self.bar = QgsMessageBar() self.bar = QgsMessageBar()
self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed) self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
@ -99,14 +98,17 @@ class AlgorithmDialog(AlgorithmDialogBase):
for output in outputs: for output in outputs:
if output.hidden: if output.hidden:
continue continue
output.value = self.mainWidget.valueItems[output.name].getValue() output.value = self.mainWidget.outputWidgets[output.name].getValue()
if isinstance(output, (OutputRaster, OutputVector, OutputTable)): if isinstance(output, (OutputRaster, OutputVector, OutputTable)):
output.open = self.mainWidget.checkBoxes[output.name].isChecked() output.open = self.mainWidget.checkBoxes[output.name].isChecked()
return True return True
def setParamValue(self, param, wrapper, alg=None): def setParamValue(self, param, wrapper):
if wrapper.widget:
return param.setValue(wrapper.value()) return param.setValue(wrapper.value())
else:
return True
def checkExtentCRS(self): def checkExtentCRS(self):
unmatchingCRS = False unmatchingCRS = False
@ -131,7 +133,7 @@ class AlgorithmDialog(AlgorithmDialogBase):
if p.crs() != projectCRS: if p.crs() != projectCRS:
unmatchingCRS = True unmatchingCRS = True
if isinstance(param, ParameterExtent): if isinstance(param, ParameterExtent):
value = self.mainWidget.valueItems[param.name].leText.text().strip() value = self.mainWidget.wrappers[param.name].widget.leText.text().strip()
if value: if value:
hasExtent = True hasExtent = True

View File

@ -59,7 +59,7 @@ class ExtentSelectionPanel(BASE, WIDGET):
if self.param.optional: if self.param.optional:
if hasattr(self.leText, 'setPlaceholderText'): if hasattr(self.leText, 'setPlaceholderText'):
self.leText.setPlaceholderText( self.leText.setPlaceholderText(
self.tr('[Use "auto" to use min covering extent]')) self.tr('[Leave blank to use min covering extent]'))
self.btnSelect.clicked.connect(self.selectExtent) self.btnSelect.clicked.connect(self.selectExtent)
@ -104,7 +104,7 @@ class ExtentSelectionPanel(BASE, WIDGET):
popupmenu.exec_(QCursor.pos()) popupmenu.exec_(QCursor.pos())
def useMinCoveringExtent(self): def useMinCoveringExtent(self):
self.leText.setText('auto') self.leText.setText('')
def useLayerExtent(self): def useLayerExtent(self):
CANVAS_KEY = 'Use canvas extent' CANVAS_KEY = 'Use canvas extent'

View File

@ -67,10 +67,9 @@ class ParametersPanel(BASE, WIDGET):
self.parent = parent self.parent = parent
self.alg = alg self.alg = alg
self.valueItems = {}
self.wrappers = {} self.wrappers = {}
self.outputWidgets = {}
self.labels = {} self.labels = {}
self.widgets = {}
self.checkBoxes = {} self.checkBoxes = {}
self.dependentItems = {} self.dependentItems = {}
self.iterateButtons = {} self.iterateButtons = {}
@ -102,9 +101,9 @@ class ParametersPanel(BASE, WIDGET):
wrapper = self.getWidgetWrapperFromParameter(param) wrapper = self.getWidgetWrapperFromParameter(param)
self.wrappers[param.name] = wrapper self.wrappers[param.name] = wrapper
self.valueItems[param.name] = wrapper.widget
widget = wrapper.widget widget = wrapper.widget
if widget is not None:
if isinstance(param, ParameterVector): if isinstance(param, ParameterVector):
layout = QHBoxLayout() layout = QHBoxLayout()
layout.setSpacing(2) layout.setSpacing(2)
@ -136,8 +135,6 @@ class ParametersPanel(BASE, WIDGET):
self.layoutMain.insertWidget( self.layoutMain.insertWidget(
self.layoutMain.count() - 2, widget) self.layoutMain.count() - 2, widget)
self.widgets[param.name] = widget
for output in self.alg.outputs: for output in self.alg.outputs:
if output.hidden: if output.hidden:
continue continue
@ -152,8 +149,7 @@ class ParametersPanel(BASE, WIDGET):
check.setChecked(True) check.setChecked(True)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, check) self.layoutMain.insertWidget(self.layoutMain.count() - 1, check)
self.checkBoxes[output.name] = check self.checkBoxes[output.name] = check
self.valueItems[output.name] = widget self.outputWidgets[output.name] = widget
for wrapper in list(self.wrappers.values()): for wrapper in list(self.wrappers.values()):
wrapper.postInitialize(list(self.wrappers.values())) wrapper.postInitialize(list(self.wrappers.values()))

View File

@ -831,7 +831,7 @@ class StringWidgetWrapper(WidgetWrapper):
else: else:
text = self.widget.getValue() text = self.widget.getValue()
return text return text
if self.dialogType == DIALOG_BATCH: elif self.dialogType == DIALOG_BATCH:
return self.widget.text() return self.widget.text()
else: else:
if self.param.multiline: if self.param.multiline:

View File

@ -146,6 +146,7 @@ class ModelerParametersDialog(QDialog):
self.wrappers[param.name] = wrapper self.wrappers[param.name] = wrapper
widget = wrapper.widget widget = wrapper.widget
if widget:
self.valueItems[param.name] = widget self.valueItems[param.name] = widget
if param.name in list(tooltips.keys()): if param.name in list(tooltips.keys()):
tooltip = tooltips[param.name] tooltip = tooltips[param.name]
@ -301,34 +302,6 @@ class ModelerParametersDialog(QDialog):
alg = self.model.algs[value.alg] alg = self.model.algs[value.alg]
return self.tr("'%s' from algorithm '%s'") % (alg.algorithm.getOutputFromName(value.output).description, alg.description) return self.tr("'%s' from algorithm '%s'") % (alg.algorithm.getOutputFromName(value.output).description, alg.description)
def setTableContent(self):
params = self._alg.parameters
outputs = self._alg.outputs
visibleParams = [p for p in params if not p.hidden]
visibleOutputs = [p for o in outputs if not o.hidden]
self.tableWidget.setRowCount(len(visibleParams) + len(visibleOutputs))
for i, param in visibleParams:
item = QTableWidgetItem(param.description)
item.setFlags(Qt.ItemIsEnabled)
self.tableWidget.setItem(i, 0, item)
item = self.getWidgetFromParameter(param)
self.valueItems[param.name] = item
self.tableWidget.setCellWidget(i, 1, item)
self.tableWidget.setRowHeight(i, 22)
for i, output in visibleOutputs:
item = QTableWidgetItem(output.description + '<'
+ output.__module__.split('.')[-1] + '>')
item.setFlags(Qt.ItemIsEnabled)
self.tableWidget.setItem(i, 0, item)
item = QLineEdit()
if hasattr(item, 'setPlaceholderText'):
item.setPlaceholderText(ModelerParametersDialog.ENTER_NAME)
self.valueItems[output.name] = item
self.tableWidget.setCellWidget(i, 1, item)
self.tableWidget.setRowHeight(i, 22)
def setPreviousValues(self): def setPreviousValues(self):
if self._algName is not None: if self._algName is not None:
alg = self.model.algs[self._algName] alg = self.model.algs[self._algName]
@ -376,10 +349,12 @@ class ModelerParametersDialog(QDialog):
for selected in selectedOptions: for selected in selectedOptions:
alg.dependencies.append(availableDependencies[selected].name) alg.dependencies.append(availableDependencies[selected].name)
self._alg.processBeforeAddingToModeler(alg, self.model)
return alg return alg
def setParamValue(self, alg, param, wrapper): def setParamValue(self, alg, param, wrapper):
try: try:
if wrapper.widget:
value = wrapper.value() value = wrapper.value()
alg.params[param.name] = value alg.params[param.name] = value
return True return True