Skip to content

Commit

Permalink
[processing] Add mechanism for algorithms to alter their core behavior
Browse files Browse the repository at this point in the history
(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 2104ae6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/AlgorithmExecutor.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion python/plugins/processing/gui/ProcessingToolbox.py
Expand Up @@ -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:
Expand Down

0 comments on commit 2104ae6

Please sign in to comment.