Skip to content
Permalink
Browse files

[processing] Add mechanism for algorithms to alter their core behavior

(e.g. parameters) when they are run in the "edit in-place" mode

This allows algorithms to dynamically adapt their behavior to make them compatible
with in-place mode. Previously, some useful algorithms could not be
run in-place because they alter a layer's structure (e.g. adding new
fields).

Now, these algorithms have a means to detect that they are being
run in-place and change their input parameters accordingly. E.g.
an algorithm which usually adds new fields to store calculated
values (such as "add xy fields to layer") could instead expose
field parameter choices to ask the user to pick from existing
fields in which to store the calculated values, thereby avoiding
the need to change the table structure and making them eligable
for running in-place mode.

Note that this needs to be handled algorithm-by-algorithm, it's
not automatic! It's just the raw api to allow this...
  • Loading branch information
nyalldawson committed Jul 28, 2020
1 parent 8d7f090 commit 2104ae613af34c9fc730f98010520b4f39024e74
@@ -153,7 +153,7 @@ def execute_in_place_run(alg, parameters, context=None, feedback=None, raise_exc
if hasattr(alg, 'processFeature'): # in-place feature editing
# Make a clone or it will crash the second time the dialog
# is opened and run
alg = alg.create()
alg = alg.create({'IN_PLACE': True})
if not alg.prepare(parameters, context, feedback):
raise QgsProcessingException(tr("Could not prepare selected algorithm."))
# Check again for compatibility after prepare
@@ -218,7 +218,7 @@ def execute_in_place_run(alg, parameters, context=None, feedback=None, raise_exc
else:
selected_ids = []

results, ok = alg.run(parameters, context, feedback)
results, ok = alg.run(parameters, context, feedback, configuration={'IN_PLACE': True})

if ok:
result_layer = QgsProcessingUtils.mapLayerFromString(results['OUTPUT'], context)
@@ -233,7 +233,10 @@ def executeAlgorithmAsBatchProcess(self):
dlg.exec_()

def executeAlgorithm(self):
alg = self.algorithmTree.selectedAlgorithm().create() if self.algorithmTree.selectedAlgorithm() is not None else None
config = {}
if self.in_place_mode:
config['IN_PLACE'] = True
alg = self.algorithmTree.selectedAlgorithm().create(config) if self.algorithmTree.selectedAlgorithm() is not None else None
if alg is not None:
ok, message = alg.canExecute()
if not ok:

0 comments on commit 2104ae6

Please sign in to comment.
You can’t perform that action at this time.