Skip to content

Commit f1c53c3

Browse files
committed
Refactor Algorithm setParamValues -> getParamValues
Now returns a dict of parameter inputs for the algorithm
1 parent 9997ab6 commit f1c53c3

File tree

6 files changed

+107
-56
lines changed

6 files changed

+107
-56
lines changed

python/plugins/processing/algs/gdal/GdalAlgorithmDialog.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ def connectParameterSignals(self):
107107

108108
def parametersHaveChanged(self):
109109
try:
110-
self.parent.setParamValues()
111-
for output in self.alg.outputs:
112-
if output.value is None:
113-
output.value = self.tr("[temporary file]")
110+
parameters = self.parent.getParamValues()
111+
for output in self.alg.destinationParameterDefinitions():
112+
if parameters[output.name()] is None:
113+
parameters[output.name()] = self.tr("[temporary file]")
114114
commands = self.alg.getConsoleCommands()
115115
commands = [c for c in commands if c not in ['cmd.exe', '/C ']]
116116
self.text.setPlainText(" ".join(commands))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
QgisAlgorithm.py
6+
----------------
7+
Date : May 2017
8+
Copyright : (C) 2017 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'May2017'
22+
__copyright__ = '(C) 2017, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from processing.core import GeoAlgorithm
29+
from processing.algs.help import shortHelp
30+
31+
32+
class QgisAlgorithm(QgisAlgorithm):
33+
34+
def __init__(self):
35+
super().__init__()
36+
37+
def shortHelpString(self):
38+
return shortHelp.get(self.id(), None)

python/plugins/processing/gui/AlgorithmDialog.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from qgis.PyQt.QtGui import QCursor, QColor, QPalette
3232

3333
from qgis.core import (QgsProject,
34-
QgsProcessingUtils)
34+
QgsProcessingUtils,
35+
QgsProcessingParameterDefinition)
3536
from qgis.gui import QgsMessageBar
3637
from qgis.utils import iface
3738

@@ -88,23 +89,26 @@ def runAsBatch(self):
8889
dlg.show()
8990
dlg.exec_()
9091

91-
def setParamValues(self):
92-
params = self.alg.parameters
93-
outputs = self.alg.outputs
92+
def getParamValues(self):
93+
parameters = {}
9494

95-
for param in params:
96-
if param.hidden:
95+
for param in self.alg.parameterDefinitions():
96+
if param.flags() & QgsProcessingParameterDefinition.FlagHidden:
9797
continue
98-
wrapper = self.mainWidget.wrappers[param.name]
99-
if not self.setParamValue(param, wrapper):
100-
raise AlgorithmDialogBase.InvalidParameterValue(param, wrapper.widget)
101-
102-
for output in outputs:
103-
if output.hidden:
104-
continue
105-
output.value = self.mainWidget.outputWidgets[output.name].getValue()
106-
if isinstance(output, (OutputRaster, OutputVector, OutputTable)):
107-
output.open = self.mainWidget.checkBoxes[output.name].isChecked()
98+
if not param.isDestination():
99+
wrapper = self.mainWidget.wrappers[param.name()]
100+
if wrapper.widget:
101+
value = wrapper.value()
102+
parameters[param.name()] = value
103+
104+
#TODO
105+
#if not self.setParamValue(param, wrapper):
106+
# raise AlgorithmDialogBase.InvalidParameterValue(param, wrapper.widget)
107+
else:
108+
parameters[param.name()] = self.mainWidget.outputWidgets[param.name()].getValue()
109+
# TODO
110+
#if isinstance(output, (OutputRaster, OutputVector, OutputTable)):
111+
# output.open = self.mainWidget.checkBoxes[param.name()].isChecked()
108112

109113
return True
110114

@@ -154,7 +158,7 @@ def accept(self):
154158

155159
checkCRS = ProcessingConfig.getSetting(ProcessingConfig.WARN_UNMATCHING_CRS)
156160
try:
157-
self.setParamValues()
161+
parameters = self.getParamValues()
158162
if checkCRS and not self.alg.checkInputCRS():
159163
reply = QMessageBox.question(self, self.tr("Unmatching CRS's"),
160164
self.tr('Layers do not all use the same CRS. This can '

python/plugins/processing/gui/AlgorithmDialogBase.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ def setText(self, text):
219219
self.setInfo(text, False)
220220
QCoreApplication.processEvents()
221221

222-
def setParamValues(self):
223-
pass
222+
def getParamValues(self):
223+
return {}
224224

225225
def setParamValue(self, param, widget, alg=None):
226226
pass

python/plugins/processing/gui/OutputSelectionPanel.py renamed to python/plugins/processing/gui/DestinationSelectionPanel.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
from qgis.gui import QgsEncodingFileDialog, QgsExpressionBuilderDialog
3737
from qgis.core import (QgsDataSourceUri,
3838
QgsCredentials,
39-
QgsSettings)
39+
QgsSettings,
40+
QgsProcessingOutputVectorLayer)
4041
from processing.core.ProcessingConfig import ProcessingConfig
4142
from processing.core.outputs import OutputVector
4243
from processing.core.outputs import OutputDirectory
@@ -47,22 +48,22 @@
4748
os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))
4849

4950

50-
class OutputSelectionPanel(BASE, WIDGET):
51+
class DestinationSelectionPanel(BASE, WIDGET):
5152

5253
SAVE_TO_TEMP_FILE = QCoreApplication.translate(
53-
'OutputSelectionPanel', '[Save to temporary file]')
54+
'DestinationSelectionPanel', '[Save to temporary file]')
5455
SAVE_TO_TEMP_LAYER = QCoreApplication.translate(
55-
'OutputSelectionPanel', '[Create temporary layer]')
56+
'DestinationSelectionPanel', '[Create temporary layer]')
5657

57-
def __init__(self, output, alg):
58-
super(OutputSelectionPanel, self).__init__(None)
58+
def __init__(self, parameter, alg):
59+
super(DestinationSelectionPanel, self).__init__(None)
5960
self.setupUi(self)
6061

61-
self.output = output
62+
self.parameter = parameter
6263
self.alg = alg
6364

6465
if hasattr(self.leText, 'setPlaceholderText'):
65-
if isinstance(output, OutputVector) \
66+
if isinstance(self.parameter, QgsProcessingOutputVectorLayer) \
6667
and alg.provider().supportsNonFileBasedOutput():
6768
# use memory layers for temporary files if supported
6869
self.leText.setPlaceholderText(self.SAVE_TO_TEMP_LAYER)
@@ -72,12 +73,12 @@ def __init__(self, output, alg):
7273
self.btnSelect.clicked.connect(self.selectOutput)
7374

7475
def selectOutput(self):
75-
if isinstance(self.output, OutputDirectory):
76+
if isinstance(self.parameter, OutputDirectory):
7677
self.selectDirectory()
7778
else:
7879
popupMenu = QMenu()
7980

80-
if isinstance(self.output, OutputVector) \
81+
if isinstance(self.parameter, QgsProcessingOutputVectorLayer) \
8182
and self.alg.provider().supportsNonFileBasedOutput():
8283
# use memory layers for temporary layers if supported
8384
actionSaveToTemp = QAction(
@@ -98,7 +99,7 @@ def selectOutput(self):
9899
actionShowExpressionsBuilder.triggered.connect(self.showExpressionsBuilder)
99100
popupMenu.addAction(actionShowExpressionsBuilder)
100101

101-
if isinstance(self.output, OutputVector) \
102+
if isinstance(self.parameter, QgsProcessingOutputVectorLayer) \
102103
and self.alg.provider().supportsNonFileBasedOutput():
103104
actionSaveToSpatialite = QAction(
104105
self.tr('Save to Spatialite table...'), self.btnSelect)
@@ -118,7 +119,7 @@ def selectOutput(self):
118119

119120
def showExpressionsBuilder(self):
120121
dlg = QgsExpressionBuilderDialog(None, self.leText.text(), self, 'generic',
121-
self.output.expressionContext(self.alg))
122+
self.parameter.expressionContext(self.alg))
122123
dlg.setWindowTitle(self.tr('Expression based output'))
123124
if dlg.exec_() == QDialog.Accepted:
124125
self.leText.setText(dlg.expressionText())
@@ -127,7 +128,7 @@ def saveToTemporary(self):
127128
self.leText.setText('')
128129

129130
def saveToPostGIS(self):
130-
dlg = PostgisTableSelector(self, self.output.name.lower())
131+
dlg = PostgisTableSelector(self, self.parameter.name().lower())
131132
dlg.exec_()
132133
if dlg.connection:
133134
settings = QgsSettings()
@@ -140,7 +141,7 @@ def saveToPostGIS(self):
140141
uri = QgsDataSourceUri()
141142
uri.setConnection(host, str(port), dbname, user, password)
142143
uri.setDataSource(dlg.schema, dlg.table,
143-
"the_geom" if self.output.hasGeometry() else None)
144+
"the_geom" if self.parameter.hasGeometry() else None)
144145

145146
connInfo = uri.connectionInfo()
146147
(success, user, passwd) = QgsCredentials.instance().get(connInfo, None, None)
@@ -149,7 +150,7 @@ def saveToPostGIS(self):
149150
self.leText.setText("postgis:" + uri.uri())
150151

151152
def saveToSpatialite(self):
152-
fileFilter = self.output.tr('SpatiaLite files (*.sqlite)', 'OutputFile')
153+
fileFilter = self.tr('SpatiaLite files (*.sqlite)', 'OutputFile')
153154

154155
settings = QgsSettings()
155156
if settings.contains('/Processing/LastOutputPath'):
@@ -167,7 +168,7 @@ def saveToSpatialite(self):
167168
if fileDialog.exec_() == QDialog.Accepted:
168169
files = fileDialog.selectedFiles()
169170
encoding = str(fileDialog.encoding())
170-
self.output.encoding = encoding
171+
self.parameter.encoding = encoding
171172
fileName = str(files[0])
172173
selectedFileFilter = str(fileDialog.selectedNameFilter())
173174
if not fileName.lower().endswith(
@@ -181,12 +182,12 @@ def saveToSpatialite(self):
181182

182183
uri = QgsDataSourceUri()
183184
uri.setDatabase(fileName)
184-
uri.setDataSource('', self.output.name.lower(),
185-
'the_geom' if self.output.hasGeometry() else None)
185+
uri.setDataSource('', self.parameter.name().lower(),
186+
'the_geom' if self.parameter.hasGeometry() else None)
186187
self.leText.setText("spatialite:" + uri.uri())
187188

188189
def selectFile(self):
189-
fileFilter = self.output.getFileFilter(self.alg)
190+
fileFilter = self.parameter.getFileFilter(self.alg)
190191

191192
settings = QgsSettings()
192193
if settings.contains('/Processing/LastOutputPath'):
@@ -204,7 +205,7 @@ def selectFile(self):
204205
if fileDialog.exec_() == QDialog.Accepted:
205206
files = fileDialog.selectedFiles()
206207
encoding = str(fileDialog.encoding())
207-
self.output.encoding = encoding
208+
self.parameter.encoding = encoding
208209
fileName = str(files[0])
209210
selectedFileFilter = str(fileDialog.selectedNameFilter())
210211
if not fileName.lower().endswith(

python/plugins/processing/gui/ParametersPanel.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@
3434
from qgis.core import (QgsProcessingParameterDefinition,
3535
QgsProcessingParameterExtent,
3636
QgsProcessingParameterPoint,
37-
QgsProcessingParameterVectorLayer)
37+
QgsProcessingParameterVectorLayer,
38+
QgsProcessingOutputVectorLayer,
39+
QgsProcessingParameterOutputVectorLayer)
3840
from qgis.PyQt import uic
3941
from qgis.PyQt.QtCore import QCoreApplication
4042
from qgis.PyQt.QtWidgets import (QWidget, QHBoxLayout, QToolButton,
4143
QLabel, QCheckBox)
4244
from qgis.PyQt.QtGui import QIcon
4345

44-
from processing.gui.OutputSelectionPanel import OutputSelectionPanel
46+
from processing.gui.DestinationSelectionPanel import DestinationSelectionPanel
4547
from processing.gui.wrappers import WidgetWrapperFactory
4648
from processing.core.parameters import ParameterVector, ParameterExtent, ParameterPoint
4749
from processing.core.outputs import OutputRaster
@@ -93,17 +95,7 @@ def initWidgets(self):
9395
continue
9496

9597
if param.isDestination():
96-
label = QLabel(param.description())
97-
widget = OutputSelectionPanel(param, self.alg)
98-
self.layoutMain.insertWidget(self.layoutMain.count() - 1, label)
99-
self.layoutMain.insertWidget(self.layoutMain.count() - 1, widget)
100-
if isinstance(param, (OutputRaster, QgsProcessingParameterOutputVectorLayer, OutputTable)):
101-
check = QCheckBox()
102-
check.setText(self.tr('Open output file after running algorithm'))
103-
check.setChecked(True)
104-
self.layoutMain.insertWidget(self.layoutMain.count() - 1, check)
105-
self.checkBoxes[param.name()] = check
106-
self.outputWidgets[param.name()] = widget
98+
continue
10799
else:
108100
desc = param.description()
109101
if isinstance(param, QgsProcessingParameterExtent):
@@ -157,6 +149,22 @@ def initWidgets(self):
157149
self.layoutMain.insertWidget(
158150
self.layoutMain.count() - 2, widget)
159151

152+
for output in self.alg.destinationParameterDefinitions():
153+
if output.flags() & QgsProcessingParameterDefinition.FlagHidden:
154+
continue
155+
156+
label = QLabel(output.description())
157+
widget = DestinationSelectionPanel(output, self.alg)
158+
self.layoutMain.insertWidget(self.layoutMain.count() - 1, label)
159+
self.layoutMain.insertWidget(self.layoutMain.count() - 1, widget)
160+
if isinstance(output, (OutputRaster, QgsProcessingParameterOutputVectorLayer, OutputTable)):
161+
check = QCheckBox()
162+
check.setText(self.tr('Open output file after running algorithm'))
163+
check.setChecked(True)
164+
self.layoutMain.insertWidget(self.layoutMain.count() - 1, check)
165+
self.checkBoxes[output.name()] = check
166+
self.outputWidgets[output.name()] = widget
167+
160168
for wrapper in list(self.wrappers.values()):
161169
wrapper.postInitialize(list(self.wrappers.values()))
162170

0 commit comments

Comments
 (0)