Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] Use outputi database layer name instead of file name when
'use filename as layer name' is checked

Avoids raw uri layer names for database type destinations
  • Loading branch information
nyalldawson committed Oct 23, 2018
1 parent a093dd7 commit e5f52f9
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions python/plugins/processing/gui/Postprocessing.py
Expand Up @@ -36,18 +36,41 @@
QgsProcessingUtils,
QgsMapLayer,
QgsWkbTypes,
QgsMessageLog)
QgsMessageLog,
QgsProviderRegistry)

from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.RenderingStyles import RenderingStyles


def set_layer_name(layer, context_layer_details):
"""
Sets the name for the given layer, either using the layer's file name
(or database layer name), or the name specified by the parameter definition.
"""
use_filename_as_layer_name = ProcessingConfig.getSetting(ProcessingConfig.USE_FILENAME_AS_LAYER_NAME)

if use_filename_as_layer_name or not context_layer_details.name:
source_parts = QgsProviderRegistry.instance().decodeUri(layer.dataProvider().name(), layer.source())
layer_name = source_parts.get('layerName', '')
# if source layer name exists, use that -- else use
if layer_name:
layer.setName(layer_name)
else:
path = source_parts.get('path', '')
if path:
layer.setName(os.path.splitext(os.path.basename(path))[0])
else:
layer.setName(context_layer_details.name)


def handleAlgorithmResults(alg, context, feedback=None, showResults=True):
wrongLayers = []
if feedback is None:
feedback = QgsProcessingFeedback()
feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers'))
i = 0

for l, details in context.layersToLoadOnCompletion().items():
if feedback.isCanceled():
return False
Expand All @@ -59,8 +82,7 @@ def handleAlgorithmResults(alg, context, feedback=None, showResults=True):
try:
layer = QgsProcessingUtils.mapLayerFromString(l, context, typeHint=details.layerTypeHint)
if layer is not None:
if not ProcessingConfig.getSetting(ProcessingConfig.USE_FILENAME_AS_LAYER_NAME):
layer.setName(details.name)
set_layer_name(layer, details)

style = None
if details.outputName:
Expand Down

0 comments on commit e5f52f9

Please sign in to comment.