Skip to content

Commit

Permalink
[processing] more robust layer sorting in multiple selection widget (f…
Browse files Browse the repository at this point in the history
…ixes #11429)
  • Loading branch information
alexbruy committed Oct 18, 2014
1 parent 99cec14 commit b1e7ede
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/AlgorithmExecutionDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ def setParamValue(self, param, widget):
return param.setValue(widget.selectedoptions)
else:
if param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
options = dataobjects.getVectorLayers()
options = dataobjects.getVectorLayers(sorting=False)
else:
options = dataobjects.getRasterLayers()
options = dataobjects.getRasterLayers(sorting=False)
return param.setValue([options[i] for i in widget.selectedoptions])
elif isinstance(param, (ParameterNumber, ParameterFile, ParameterCrs,
ParameterExtent)):
Expand Down
31 changes: 17 additions & 14 deletions python/plugins/processing/tools/dataobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,30 @@ def getSupportedOutputTableExtensions():


def getRasterLayers(sorting=True):
layers = QgsMapLayerRegistry.instance().mapLayers().values()
layers = QgsProject.instance().layerTreeRoot().findLayers()
raster = []

for layer in layers:
if layer.type() == layer.RasterLayer:
if layer.providerType() == 'gdal': # only gdal file-based layers
raster.append(layer)
mapLayer = layer.layer()
if mapLayer.type() == QgsMapLayer.RasterLayer:
if mapLayer.providerType() == 'gdal': # only gdal file-based layers
raster.append(mapLayer)
if sorting:
return sorted(raster, key=lambda layer: layer.name().lower())
else:
return raster


def getVectorLayers(shapetype=[-1], sorting=True):
layers = QgsMapLayerRegistry.instance().mapLayers().values()
layers = QgsProject.instance().layerTreeRoot().findLayers()
vector = []
for layer in layers:
if layer.type() == layer.VectorLayer:
if shapetype == ALL_TYPES or layer.geometryType() in shapetype:
uri = unicode(layer.source())
mapLayer = layer.layer()
if mapLayer.type() == QgsMapLayer.VectorLayer:
if shapetype == ALL_TYPES or mapLayer.geometryType() in shapetype:
uri = unicode(mapLayer.source())
if not uri.lower().endswith('csv') and not uri.lower().endswith('dbf'):
vector.append(layer)
vector.append(mapLayer)
if sorting:
return sorted(vector, key=lambda layer: layer.name().lower())
else:
Expand All @@ -105,11 +107,12 @@ def getAllLayers():


def getTables(sorting=True):
layers = QgsMapLayerRegistry.instance().mapLayers().values()
tables = list()
layers = QgsProject.instance().layerTreeRoot().findLayers()
tables = []
for layer in layers:
if layer.type() == layer.VectorLayer:
tables.append(layer)
mapLayer = layer.layer()
if mapLayer.type() == QgsMapLayer.VectorLayer:
tables.append(mapLayer)
if sorting:
return sorted(tables, key=lambda table: table.name().lower())
else:
Expand All @@ -119,7 +122,7 @@ def getTables(sorting=True):
def extent(layers):
first = True
for layer in layers:
if not isinstance(layer, (QgsRasterLayer, QgsVectorLayer)):
if not isinstance(layer, (QgsMapLayer.QgsRasterLayer, QgsMapLayer.QgsVectorLayer)):
layer = getObjectFromUri(layer)
if layer is None:
continue
Expand Down

0 comments on commit b1e7ede

Please sign in to comment.