Skip to content

Commit 6efa765

Browse files
authored
Merge pull request #4770 from nyalldawson/proc
More processing model porting, better invalid geometry feedback
2 parents b7e66b4 + d667bf5 commit 6efa765

19 files changed

+262
-73
lines changed

python/core/processing/qgsprocessingcontext.sip

+17-5
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ Destination project
142142
.. seealso:: invalidGeometryCheck()
143143
%End
144144

145-
146145
void setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
147146
%Docstring
148147
Sets a callback function to use when encountering an invalid geometry and
@@ -198,16 +197,29 @@ Destination project
198197
%Docstring
199198
Sets the default ``encoding`` to use for newly created files.
200199
.. seealso:: defaultEncoding()
200+
%End
201+
202+
QgsProcessingFeedback *feedback();
203+
%Docstring
204+
Returns the associated feedback object.
205+
.. seealso:: setFeedback()
206+
:rtype: QgsProcessingFeedback
207+
%End
208+
209+
void setFeedback( QgsProcessingFeedback *feedback );
210+
%Docstring
211+
Sets an associated ``feedback`` object. This allows context related functions
212+
to report feedback and errors to users and processing logs. While ideally this feedback
213+
object should outlive the context, only a weak pointer to ``feedback`` is stored
214+
and no errors will occur if feedback is deleted before the context.
215+
Ownership of ``feedback`` is not transferred.
216+
.. seealso:: setFeedback()
201217
%End
202218

203219
private:
204220
QgsProcessingContext( const QgsProcessingContext &other );
205221
};
206222

207-
208-
209-
210-
211223
QFlags<QgsProcessingContext::Flag> operator|(QgsProcessingContext::Flag f1, QFlags<QgsProcessingContext::Flag> f2);
212224

213225

python/core/processing/qgsprocessingmodelalgorithm.sip

+35
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,15 @@ Copies are protected to avoid slicing
566566

567567
virtual QString svgIconPath() const;
568568

569+
virtual QString shortHelpString() const;
570+
571+
virtual QString helpUrl() const;
572+
569573

570574
virtual bool canExecute( QString *errorMessage /Out/ = 0 ) const;
571575

576+
virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;
577+
572578

573579
void setName( const QString &name );
574580
%Docstring
@@ -767,6 +773,35 @@ Copies are protected to avoid slicing
767773
:rtype: bool
768774
%End
769775

776+
QVariantMap &helpContent();
777+
%Docstring
778+
Returns the model's help contents (a free-form map of values describing the algorithm's
779+
use and metadata).
780+
.. seealso:: setHelpContent()
781+
:rtype: QVariantMap
782+
%End
783+
784+
785+
void setHelpContent( const QVariantMap &contents );
786+
%Docstring
787+
Sets the model's help ``contents`` (a free-form map of values describing the algorithm's
788+
use and metadata).
789+
.. seealso:: helpContent()
790+
%End
791+
792+
QString sourceFilePath() const;
793+
%Docstring
794+
Returns the source file path for the model, if available.
795+
.. seealso:: setSourceFilePath()
796+
:rtype: str
797+
%End
798+
799+
void setSourceFilePath( const QString &path );
800+
%Docstring
801+
Sets the source file ``path`` for the model, if available.
802+
.. seealso:: sourceFilePath()
803+
%End
804+
770805
protected:
771806

772807
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,

python/core/processing/qgsprocessingutils.sip

+7
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ class QgsProcessingUtils
151151
:rtype: str
152152
%End
153153

154+
static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
155+
%Docstring
156+
Returns a HTML formatted version of the help text encoded in a variant ``map`` for
157+
a specified ``algorithm``.
158+
:rtype: str
159+
%End
160+
154161
};
155162

156163

python/plugins/processing/algs/qgis/AutoincrementalField.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def processAlgorithm(self, parameters, context, feedback):
7575
fields, source.wkbType(), source.sourceCrs())
7676

7777
features = source.getFeatures()
78-
total = total = 100.0 / source.featureCount() if source.featureCount() else 0
78+
total = 100.0 / source.featureCount() if source.featureCount() else 0
7979
for current, input_feature in enumerate(features):
8080
if feedback.isCanceled():
8181
break

python/plugins/processing/core/GeoAlgorithm.py

-4
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,3 @@ def executeAlgorithm(alg, parameters, context=None, feedback=None, model=None):
341341
return result, ok
342342
#self.convertUnsupportedFormats(context, feedback)
343343
#self.runPostExecutionScript(feedback)
344-
345-
def helpUrl(self):
346-
return QgsHelp.helpUrl("processing_algs/{}/{}".format(
347-
self.provider().id(), self.id())).toString()

python/plugins/processing/gui/AlgorithmDialogBase.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from qgis.core import (QgsProject,
3939
QgsProcessingFeedback,
4040
QgsSettings)
41-
41+
from qgis.gui import QgsHelp
4242

4343
from processing.core.ProcessingConfig import ProcessingConfig
4444

@@ -189,7 +189,7 @@ def resetGUI(self):
189189

190190
def setInfo(self, msg, error=False, escape_html=True):
191191
if error:
192-
self.txtLog.append('<span style="color:red"><br>{}<br></span>'.format(msg, quote=False))
192+
self.txtLog.append('<span style="color:red">{}</span><br />'.format(msg, quote=False))
193193
elif escape_html:
194194
self.txtLog.append(html.escape(msg))
195195
else:
@@ -255,6 +255,10 @@ def splitterChanged(self, pos, index):
255255

256256
def openHelp(self):
257257
algHelp = self.alg.helpUrl()
258+
if not algHelp:
259+
algHelp = QgsHelp.helpUrl("processing_algs/{}/{}".format(
260+
self.alg.provider().id(), self.alg.id())).toString()
261+
258262
if algHelp not in [None, ""]:
259263
webbrowser.open(algHelp)
260264

python/plugins/processing/gui/HelpEditionDialog.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
from qgis.PyQt.QtWidgets import QDialog, QTreeWidgetItem
3434

3535
from qgis.core import (QgsMessageLog,
36-
QgsProcessingUtils)
36+
QgsProcessingUtils,
37+
QgsProcessingParameterDefinition)
3738
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
3839

3940
pluginPath = os.path.split(os.path.dirname(__file__))[0]
@@ -55,7 +56,7 @@ def __init__(self, alg):
5556
self.alg = alg
5657
self.descriptions = {}
5758
if isinstance(self.alg, ModelerAlgorithm):
58-
self.descriptions = self.alg.helpContent
59+
self.descriptions = self.alg.helpContent()
5960
else:
6061
if self.alg.descriptionFile is not None:
6162
helpfile = alg.descriptionFile + '.help'
@@ -90,7 +91,7 @@ def getHtml(self):
9091
s += '<h3>' + param.description() + '</h3>\n'
9192
s += '<p>' + self.getDescription(param.name()) + '</p>\n'
9293
s += self.tr('<h2>Outputs</h2>\n')
93-
for out in self.alg.outputs:
94+
for out in self.alg.outputDefinitions():
9495
s += '<h3>' + out.description() + '</h3>\n'
9596
s += '<p>' + self.getDescription(out.name()) + '</p>\n'
9697
return s
@@ -101,11 +102,14 @@ def fillTree(self):
101102
parametersItem = TreeDescriptionItem(self.tr('Input parameters'), None)
102103
self.tree.addTopLevelItem(parametersItem)
103104
for param in self.alg.parameterDefinitions():
105+
if param.flags() & QgsProcessingParameterDefinition.FlagHidden or param.isDestination():
106+
continue
107+
104108
item = TreeDescriptionItem(param.description(), param.name())
105109
parametersItem.addChild(item)
106110
outputsItem = TreeDescriptionItem(self.tr('Outputs'), None)
107111
self.tree.addTopLevelItem(outputsItem)
108-
for out in self.alg.outputs:
112+
for out in self.alg.outputDefinitions():
109113
item = TreeDescriptionItem(out.description(), out.name())
110114
outputsItem.addChild(item)
111115
item = TreeDescriptionItem(self.tr('Algorithm created by'), self.ALG_CREATOR)

python/plugins/processing/modeler/ModelerAlgorithm.py

-22
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,9 @@ def asPythonParameter(self):
174174

175175
class ModelerAlgorithm(QgsProcessingModelAlgorithm):
176176

177-
CANVAS_SIZE = 4000
178-
179177
def __init__(self):
180178
super().__init__()
181179

182-
self.descriptionFile = None
183-
self.helpContent = {}
184-
185180
# Geoalgorithms in this model. A dict of Algorithm objects, with names as keys
186181
self.algs = {}
187182

@@ -229,23 +224,6 @@ def resolveValue(self, value, param):
229224
v = value
230225
return param.evaluateForModeler(v, self)
231226

232-
def asPythonCommand(self, parameters, context):
233-
if self.descriptionFile:
234-
return QgsProcessingAlgorithm.asPythonCommand(self, parameters, context)
235-
else:
236-
return None
237-
238-
def helpUrl(self):
239-
try:
240-
return getHtmlFromDescriptionsDict(self, self.helpContent)
241-
except:
242-
return None
243-
244-
def shortHelpString(self):
245-
if 'ALG_DESC' in self.helpContent:
246-
return str(self.helpContent['ALG_DESC'])
247-
return None
248-
249227
def toPython(self):
250228
s = ['##%s=name' % self.name()]
251229
for param in list(self.parameterComponents().values()):

python/plugins/processing/modeler/ModelerAlgorithmProvider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def loadFromFolder(self, folder):
108108
alg = ModelerAlgorithm()
109109
if alg.fromFile(fullpath):
110110
if alg.name():
111-
alg.descriptionFile = fullpath
111+
alg.setSourceFilePath(fullpath)
112112
self.algs.append(alg)
113113
else:
114114
QgsMessageLog.logMessage(self.tr('Could not load model {0}', 'ModelerAlgorithmProvider').format(descriptionFile),

python/plugins/processing/modeler/ModelerDialog.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def editHelp(self):
279279
dlg = HelpEditionDialog(alg)
280280
dlg.exec_()
281281
if dlg.descriptions:
282-
self.model.helpContent = dlg.descriptions
282+
self.model.setHelpContent(dlg.descriptions)
283283
self.hasChanged = True
284284

285285
def runModel(self):
@@ -439,8 +439,8 @@ def saveModel(self, saveAs):
439439
return
440440
self.model.setName(str(self.textName.text()))
441441
self.model.setGroup(str(self.textGroup.text()))
442-
if self.model.descriptionFile is not None and not saveAs:
443-
filename = self.model.descriptionFile
442+
if self.model.sourceFilePath() is not None and not saveAs:
443+
filename = self.model.sourceFilePath()
444444
else:
445445
filename, filter = QFileDialog.getSaveFileName(self,
446446
self.tr('Save Model'),
@@ -449,7 +449,7 @@ def saveModel(self, saveAs):
449449
if filename:
450450
if not filename.endswith('.model3'):
451451
filename += '.model3'
452-
self.model.descriptionFile = filename
452+
self.model.setSourceFilePath(filename)
453453
if filename:
454454
if not self.model.toFile(filename):
455455
if saveAs:
@@ -492,8 +492,8 @@ def openModel(self):
492492

493493
def repaintModel(self, controls=True):
494494
self.scene = ModelerScene(self, dialog=self)
495-
self.scene.setSceneRect(QRectF(0, 0, ModelerAlgorithm.CANVAS_SIZE,
496-
ModelerAlgorithm.CANVAS_SIZE))
495+
self.scene.setSceneRect(QRectF(0, 0, self.CANVAS_SIZE,
496+
self.CANVAS_SIZE))
497497
self.scene.paintModel(self.model, controls)
498498
self.view.setScene(self.scene)
499499

@@ -512,6 +512,7 @@ def addInputOfType(self, paramType, pos=None):
512512
if isinstance(pos, QPoint):
513513
pos = QPointF(pos)
514514
component = QgsProcessingModelAlgorithm.ModelParameter(dlg.param.name())
515+
component.setDescription(dlg.param.name())
515516
component.setPosition(pos)
516517
self.model.addModelParameter(dlg.param, component)
517518
self.repaintModel()

python/plugins/processing/modeler/ModelerGraphicItem.py

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def editElement(self):
196196
if dlg.param is not None:
197197
self.model.removeModelParameter(self.element.parameterName())
198198
self.element.setParameterName(dlg.param.name())
199+
self.element.setDescription(dlg.param.name())
199200
self.model.addModelParameter(dlg.param, self.element)
200201
self.text = dlg.param.description()
201202
self.update()

python/plugins/processing/tests/QgisAlgorithmsTest.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ def displayName(self):
4949
return 'testalg'
5050

5151
def processAlgorithm(self, parameters, context, feedback):
52-
raise GeoAlgorithmExecutionException(
53-
self.tr('Exception while processing'))
52+
raise GeoAlgorithmExecutionException('Exception while processing')
5453
return {}
5554

5655

python/plugins/processing/tools/dataobjects.py

+1-15
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,13 @@ def createContext(feedback=None):
7171
"""
7272
context = QgsProcessingContext()
7373
context.setProject(QgsProject.instance())
74+
context.setFeedback(feedback)
7475

7576
invalid_features_method = ProcessingConfig.getSetting(ProcessingConfig.FILTER_INVALID_GEOMETRIES)
7677
if invalid_features_method is None:
7778
invalid_features_method = QgsFeatureRequest.GeometryAbortOnInvalid
7879
context.setInvalidGeometryCheck(invalid_features_method)
7980

80-
def raise_invalid_geometry_error(f, feedback=feedback):
81-
if feedback:
82-
feedback.pushInfo(QCoreApplication.translate("FeatureIterator",
83-
'Feature with id {} has invalid geometry, skipping feature.'.format(f.id())))
84-
85-
if context.invalidGeometryCheck() == QgsFeatureRequest.GeometrySkipInvalid:
86-
context.setInvalidGeometryCallback(raise_invalid_geometry_error)
87-
88-
def raise_transform_error(f, feedback=feedback):
89-
if feedback:
90-
feedback.pushInfo(QCoreApplication.translate("FeatureIterator",
91-
'Encountered a transform error when reprojecting feature with id {}.'.format(f.id())))
92-
93-
context.setTransformErrorCallback(raise_transform_error)
94-
9581
settings = QgsSettings()
9682
context.setDefaultEncoding(settings.value("/Processing/encoding", "System"))
9783

0 commit comments

Comments
 (0)