[processing] Always use the same method to launder names

Ultimately avoids forced removal of _ characters in model
parameter names
This commit is contained in:
Nyall Dawson 2022-03-30 11:13:31 +10:00
parent 4905a55748
commit dd89f3779a
5 changed files with 25 additions and 12 deletions

View File

@ -580,6 +580,15 @@ run through the designer.
%Docstring
Given a child algorithm ID and output name, attempts to match it to a parameter definition from the overall model.
.. versionadded:: 3.26
%End
static QString safeName( const QString &name, bool capitalize = false );
%Docstring
Makes a name "safe", by replacing any non-alphanumeric characters with underscores.
If ``capitalize`` is ``True`` then the string will be converted to a camel case string.
.. versionadded:: 3.26
%End

View File

@ -42,6 +42,7 @@ from qgis.core import (Qgis,
QgsProcessing,
QgsProject,
QgsProcessingModelParameter,
QgsProcessingModelAlgorithm,
QgsSettings,
QgsProcessingContext,
QgsFileUtils
@ -272,9 +273,7 @@ class ModelerDialog(QgsModelDesignerDialog):
Automatically generates and sets a new parameter's name, based on the parameter's
description and ensuring that it is unique for the model.
"""
validChars = \
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
safeName = ''.join(c for c in parameter.description() if c in validChars)
safeName = QgsProcessingModelAlgorithm.safeName(parameter.description())
name = safeName.lower()
i = 2
while self.model().parameterDefinition(name):

View File

@ -25,6 +25,7 @@ from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessingParameterDefinition,
QgsProcessingModelOutput,
QgsProcessingModelAlgorithm,
QgsProject,
Qgis)
from qgis.gui import (
@ -119,9 +120,7 @@ class ModelerInputGraphicItem(QgsModelParameterGraphicItem):
comment = dlg.comments()
comment_color = dlg.commentColor()
validChars = \
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
safeName = ''.join(c for c in new_param.description() if c in validChars)
safeName = QgsProcessingModelAlgorithm.safeName(new_param.description())
new_param.setName(safeName.lower())
if new_param is not None:

View File

@ -51,7 +51,8 @@ from qgis.core import (QgsApplication,
QgsProcessingParameterFileDestination,
QgsProcessingParameterFolderDestination,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterVectorDestination)
QgsProcessingParameterVectorDestination,
QgsProcessingModelAlgorithm)
from processing.core import parameters
from processing.modeler.exceptions import UndefinedParameterException
@ -198,9 +199,7 @@ class ModelerParameterDefinitionDialog(QDialog):
self.tr('Invalid parameter name'))
return
validChars = \
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
safeName = ''.join(c for c in description if c in validChars)
safeName = QgsProcessingModelAlgorithm.safeName(description)
name = safeName.lower()
# Destination parameter

View File

@ -531,6 +531,15 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
*/
const QgsProcessingParameterDefinition *modelParameterFromChildIdAndOutputName( const QString &childId, const QString &childOutputName ) const;
/**
* Makes a name "safe", by replacing any non-alphanumeric characters with underscores.
*
* If \a capitalize is TRUE then the string will be converted to a camel case string.
*
* \since QGIS 3.26
*/
static QString safeName( const QString &name, bool capitalize = false );
#ifndef SIP_RUN
//! Internal model versions
@ -577,8 +586,6 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
QStringList mParameterOrder;
static QString safeName( const QString &name, bool capitalize );
void dependsOnChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;
void dependentChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends, const QString &branch ) const;