Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] show warning when file-based layer could not be loaded a…
…nd dependent params updated

For algorithms with multiple parameters depending on a vector layer parameter, the code that loads the layer in the background is called repeatedly, impacting performance. A small layer cache is implemented with these changes, so the dialog only tries to load the layer once.

(cherry picked from commit 6eaa511)
  • Loading branch information
volaya authored and nyalldawson committed Jan 29, 2019
1 parent 421fe45 commit 722e4fe
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions python/plugins/processing/gui/wrappers.py
Expand Up @@ -45,6 +45,7 @@
QgsSettings,
QgsProject,
QgsMapLayer,
QgsVectorLayer,
QgsProcessing,
QgsProcessingUtils,
QgsProcessingParameterDefinition,
Expand Down Expand Up @@ -78,7 +79,8 @@
QgsProcessingModelChildParameterSource,
QgsProcessingModelAlgorithm,
QgsRasterDataProvider,
NULL)
NULL,
Qgis)

from qgis.PyQt.QtWidgets import (
QCheckBox,
Expand Down Expand Up @@ -1049,6 +1051,7 @@ class FeatureSourceWidgetWrapper(WidgetWrapper):
NOT_SELECTED = '[Not selected]'

def createWidget(self):
self.fileBasedLayers = {}
if self.dialogType == DIALOG_STANDARD:
widget = QWidget()
layout = QHBoxLayout()
Expand Down Expand Up @@ -1391,6 +1394,7 @@ class VectorLayerWidgetWrapper(WidgetWrapper):
NOT_SELECTED = '[Not selected]'

def createWidget(self):
self.fileBasedLayers = {}
if self.dialogType == DIALOG_STANDARD:
widget = QWidget()
layout = QHBoxLayout()
Expand Down Expand Up @@ -1576,14 +1580,26 @@ def postInitialize(self, wrappers):
break

def parentValueChanged(self, wrapper):
self.setLayer(wrapper.parameterValue())
value = wrapper.parameterValue()
if value in wrapper.fileBasedLayers:
self.setLayer(wrapper.fileBasedLayers[value])
else:
self.setLayer(value)
wrapper.fileBasedLayers[value] = self._layer

def setLayer(self, layer):
if isinstance(layer, QgsProcessingFeatureSourceDefinition):
layer, ok = layer.source.valueAsString(self.context.expressionContext())
if isinstance(layer, str):
layer = QgsProcessingUtils.mapLayerFromString(layer, self.context)
if not isinstance(layer, QgsVectorLayer) or not layer.isValid():
self.dialog.messageBar().clearWidgets()
self.dialog.messageBar().pushMessage("", self.tr("Could not load selected layer/table. Dependent field could not be populated"),
level=Qgis.Warning, duration=5)
return

self._layer = layer

self.refreshItems()

def refreshItems(self):
Expand Down

0 comments on commit 722e4fe

Please sign in to comment.