Skip to content

Commit

Permalink
[processing] some fixes and changes in parameters and parameters panel
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Dec 1, 2016
1 parent a17facf commit fcc3437
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 103 deletions.
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/gdal/GdalAlgorithmDialog.py
Expand Up @@ -78,7 +78,8 @@ def __init__(self, parent, alg):
self.parametersHaveChanged()

def connectParameterSignals(self):
for w in list(self.widgets.values()):
for wrapper in list(self.wrappers.values()):
w = wrapper.widget
if isinstance(w, QLineEdit):
w.textChanged.connect(self.parametersHaveChanged)
elif isinstance(w, QComboBox):
Expand Down
18 changes: 13 additions & 5 deletions python/plugins/processing/core/GeoAlgorithm.py
Expand Up @@ -16,9 +16,6 @@
* *
***************************************************************************
"""
from builtins import str
from builtins import object


__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand All @@ -38,6 +35,9 @@

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.ProcessingConfig import ProcessingConfig
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
Expand All @@ -46,10 +46,8 @@
from processing.core.outputs import OutputVector, OutputRaster, OutputTable, OutputHTML, Output
from processing.algs.gdal.GdalUtils import GdalUtils
from processing.tools import dataobjects, vector
from processing.tools.system import setTempOutput
from processing.algs.help import shortHelp


class GeoAlgorithm(object):

def __init__(self):
Expand Down Expand Up @@ -134,6 +132,10 @@ def defineCharacteristics(self):
"""
pass


def getParametersPanel(self, parent):
return ParametersPanel(parent, self)

def getCustomParametersDialog(self):
"""If the algorithm has a custom parameters dialog, it should
be returned here, ready to be executed.
Expand Down Expand Up @@ -183,6 +185,12 @@ def checkParameterValuesBeforeExecuting(self):
calling from the console.
"""
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

# =========================================================

Expand Down
16 changes: 9 additions & 7 deletions python/plugins/processing/core/parameters.py
Expand Up @@ -123,7 +123,6 @@ def __init__(self, name='', description='', default=None, optional=False,

self.optional = parseBool(optional)

# TODO: make deep copy and deep update
self.metadata = deepcopy(self.default_metadata)
self.metadata.update(deepcopy(metadata))

Expand Down Expand Up @@ -179,7 +178,7 @@ def tr(self, string, context=''):
def wrapper(self, dialog, row=0, col=0):
wrapper = self.metadata.get('widget_wrapper', None)
# wrapper metadata should be a class path
if isinstance(wrapper, str):
if isinstance(wrapper, basestring):
tokens = wrapper.split('.')
mod = __import__('.'.join(tokens[:-1]), fromlist=[tokens[-1]])
wrapper = getattr(mod, tokens[-1])
Expand Down Expand Up @@ -362,7 +361,10 @@ def setValue(self, value):
return False

def getValueAsCommandLineParameter(self):
return '"' + str(self.value) + '"'
if self.value is not None:
return '"' + str(self.value) + '"'
else:
return str(None)

def getAsScriptCode(self):
param_type = ''
Expand Down Expand Up @@ -582,8 +584,8 @@ class ParameterMultipleInput(ParameterDataObject):

exported = None

def __init__(self, name='', description='', datatype=-1, optional=False):
ParameterDataObject.__init__(self, name, description, None, optional)
def __init__(self, name='', description='', datatype=-1, optional=False, metadata={}):
ParameterDataObject.__init__(self, name, description, None, optional, metadata=metadata)
self.datatype = int(float(datatype))
self.exported = None
self.minNumInputs = 0
Expand Down Expand Up @@ -1133,8 +1135,8 @@ class ParameterString(Parameter):
ESCAPED_NEWLINE = '\\n'

def __init__(self, name='', description='', default=None, multiline=False,
optional=False, evaluateExpressions=False):
Parameter.__init__(self, name, description, default, optional)
optional=False, evaluateExpressions=False, metadata={}):
Parameter.__init__(self, name, description, default, optional, metadata)
self.multiline = parseBool(multiline)
self.evaluateExpressions = parseBool(evaluateExpressions)

Expand Down
14 changes: 8 additions & 6 deletions python/plugins/processing/gui/AlgorithmDialog.py
Expand Up @@ -39,7 +39,6 @@
from processing.core.ProcessingConfig import ProcessingConfig

from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
from processing.gui.ParametersPanel import ParametersPanel
from processing.gui.AlgorithmDialogBase import AlgorithmDialogBase
from processing.gui.AlgorithmExecutor import runalg, runalgIterating
from processing.gui.Postprocessing import handleAlgorithmResults
Expand All @@ -63,7 +62,7 @@ def __init__(self, alg):

self.alg = alg

self.setMainWidget(ParametersPanel(self, alg))
self.setMainWidget(alg.getParametersPanel(self))

self.bar = QgsMessageBar()
self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
Expand Down Expand Up @@ -99,14 +98,17 @@ def setParamValues(self):
for output in outputs:
if output.hidden:
continue
output.value = self.mainWidget.valueItems[output.name].getValue()
output.value = self.mainWidget.outputWidgets[output.name].getValue()
if isinstance(output, (OutputRaster, OutputVector, OutputTable)):
output.open = self.mainWidget.checkBoxes[output.name].isChecked()

return True

def setParamValue(self, param, wrapper, alg=None):
return param.setValue(wrapper.value())
def setParamValue(self, param, wrapper):
if wrapper.widget:
return param.setValue(wrapper.value())
else:
return True

def checkExtentCRS(self):
unmatchingCRS = False
Expand All @@ -131,7 +133,7 @@ def checkExtentCRS(self):
if p.crs() != projectCRS:
unmatchingCRS = True
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:
hasExtent = True

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/ExtentSelectionPanel.py
Expand Up @@ -59,7 +59,7 @@ def __init__(self, dialog, param):
if self.param.optional:
if hasattr(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)

Expand Down Expand Up @@ -104,7 +104,7 @@ def selectExtent(self):
popupmenu.exec_(QCursor.pos())

def useMinCoveringExtent(self):
self.leText.setText('auto')
self.leText.setText('')

def useLayerExtent(self):
CANVAS_KEY = 'Use canvas extent'
Expand Down
70 changes: 33 additions & 37 deletions python/plugins/processing/gui/ParametersPanel.py
Expand Up @@ -67,10 +67,9 @@ def __init__(self, parent, alg):

self.parent = parent
self.alg = alg
self.valueItems = {}
self.wrappers = {}
self.outputWidgets = {}
self.labels = {}
self.widgets = {}
self.checkBoxes = {}
self.dependentItems = {}
self.iterateButtons = {}
Expand Down Expand Up @@ -102,41 +101,39 @@ def initWidgets(self):

wrapper = self.getWidgetWrapperFromParameter(param)
self.wrappers[param.name] = wrapper
self.valueItems[param.name] = wrapper.widget
widget = wrapper.widget

if isinstance(param, ParameterVector):
layout = QHBoxLayout()
layout.setSpacing(2)
layout.setMargin(0)
layout.addWidget(widget)
button = QToolButton()
icon = QIcon(os.path.join(pluginPath, 'images', 'iterate.png'))
button.setIcon(icon)
button.setToolTip(self.tr('Iterate over this layer'))
button.setCheckable(True)
layout.addWidget(button)
self.iterateButtons[param.name] = button
button.toggled.connect(self.buttonToggled)
widget = QWidget()
widget.setLayout(layout)

tooltips = self.alg.getParameterDescriptions()
widget.setToolTip(tooltips.get(param.name, param.description))

label = QLabel(desc)
# label.setToolTip(tooltip)
self.labels[param.name] = label
if param.isAdvanced:
self.layoutAdvanced.addWidget(label)
self.layoutAdvanced.addWidget(widget)
else:
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, label)
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, widget)

self.widgets[param.name] = widget
if widget is not None:
if isinstance(param, ParameterVector):
layout = QHBoxLayout()
layout.setSpacing(2)
layout.setMargin(0)
layout.addWidget(widget)
button = QToolButton()
icon = QIcon(os.path.join(pluginPath, 'images', 'iterate.png'))
button.setIcon(icon)
button.setToolTip(self.tr('Iterate over this layer'))
button.setCheckable(True)
layout.addWidget(button)
self.iterateButtons[param.name] = button
button.toggled.connect(self.buttonToggled)
widget = QWidget()
widget.setLayout(layout)

tooltips = self.alg.getParameterDescriptions()
widget.setToolTip(tooltips.get(param.name, param.description))

label = QLabel(desc)
# label.setToolTip(tooltip)
self.labels[param.name] = label
if param.isAdvanced:
self.layoutAdvanced.addWidget(label)
self.layoutAdvanced.addWidget(widget)
else:
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, label)
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, widget)

for output in self.alg.outputs:
if output.hidden:
Expand All @@ -152,8 +149,7 @@ def initWidgets(self):
check.setChecked(True)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, check)
self.checkBoxes[output.name] = check
self.valueItems[output.name] = widget

self.outputWidgets[output.name] = widget
for wrapper in list(self.wrappers.values()):
wrapper.postInitialize(list(self.wrappers.values()))

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/wrappers.py
Expand Up @@ -831,7 +831,7 @@ def value(self):
else:
text = self.widget.getValue()
return text
if self.dialogType == DIALOG_BATCH:
elif self.dialogType == DIALOG_BATCH:
return self.widget.text()
else:
if self.param.multiline:
Expand Down
63 changes: 19 additions & 44 deletions python/plugins/processing/modeler/ModelerParametersDialog.py
Expand Up @@ -146,20 +146,21 @@ def setupUi(self):
self.wrappers[param.name] = wrapper

widget = wrapper.widget
self.valueItems[param.name] = widget
if param.name in list(tooltips.keys()):
tooltip = tooltips[param.name]
else:
tooltip = param.description
label.setToolTip(tooltip)
widget.setToolTip(tooltip)
if param.isAdvanced:
label.setVisible(self.showAdvanced)
widget.setVisible(self.showAdvanced)
self.widgets[param.name] = widget

self.verticalLayout.addWidget(label)
self.verticalLayout.addWidget(widget)
if widget:
self.valueItems[param.name] = widget
if param.name in list(tooltips.keys()):
tooltip = tooltips[param.name]
else:
tooltip = param.description
label.setToolTip(tooltip)
widget.setToolTip(tooltip)
if param.isAdvanced:
label.setVisible(self.showAdvanced)
widget.setVisible(self.showAdvanced)
self.widgets[param.name] = widget

self.verticalLayout.addWidget(label)
self.verticalLayout.addWidget(widget)

for output in self._alg.outputs:
if output.hidden:
Expand Down Expand Up @@ -301,34 +302,6 @@ def resolveValueDescription(self, value):
alg = self.model.algs[value.alg]
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):
if self._algName is not None:
alg = self.model.algs[self._algName]
Expand Down Expand Up @@ -376,12 +349,14 @@ def createAlgorithm(self):
for selected in selectedOptions:
alg.dependencies.append(availableDependencies[selected].name)

self._alg.processBeforeAddingToModeler(alg, self.model)
return alg

def setParamValue(self, alg, param, wrapper):
try:
value = wrapper.value()
alg.params[param.name] = value
if wrapper.widget:
value = wrapper.value()
alg.params[param.name] = value
return True
except InvalidParameterValue:
return False
Expand Down

0 comments on commit fcc3437

Please sign in to comment.