[processing] Fix exception when calling "Select from Files" in batch mode

on a file parameter

Fixes #40705
This commit is contained in:
Nyall Dawson 2021-01-04 16:59:49 +10:00
parent 6fed3b3783
commit b740bf201a
5 changed files with 41 additions and 10 deletions

View File

@ -1743,7 +1743,7 @@ Creates a new parameter using the definition from a script code.
};
class QgsProcessingParameterFile : QgsProcessingParameterDefinition
class QgsProcessingParameterFile : QgsProcessingParameterDefinition, QgsFileFilterGenerator
{
%Docstring
An input file or folder parameter for processing algorithms.
@ -1785,6 +1785,8 @@ Returns the type name for the parameter class.
virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;
virtual QString createFileFilter() const;
Behavior behavior() const;
%Docstring

View File

@ -81,7 +81,8 @@ from qgis.core import (
QgsProcessing,
QgsExpression,
QgsRasterLayer,
QgsProcessingUtils
QgsProcessingUtils,
QgsFileFilterGenerator
)
from qgis.gui import (
QgsProcessingParameterWidgetContext,
@ -152,13 +153,7 @@ class BatchPanelFillWidget(QToolButton):
add_by_expression.setToolTip(self.tr('Adds new parameter values by evaluating an expression'))
self.menu.addAction(add_by_expression)
if isinstance(self.parameterDefinition, (QgsProcessingParameterFile,
QgsProcessingParameterMapLayer,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterMeshLayer,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterMultipleLayers)):
if not self.parameterDefinition.isDestination() and isinstance(self.parameterDefinition, QgsFileFilterGenerator):
self.menu.addSeparator()
find_by_pattern_action = QAction(QCoreApplication.translate('BatchPanel', 'Add Files by Pattern…'),
self.menu)

View File

@ -3385,6 +3385,26 @@ QString QgsProcessingParameterFile::asPythonString( const QgsProcessing::PythonO
return QString();
}
QString QgsProcessingParameterFile::createFileFilter() const
{
switch ( mBehavior )
{
case File:
{
if ( !mFileFilter.isEmpty() )
return mFileFilter != QObject::tr( "All files (*.*)" ) ? mFileFilter + QStringLiteral( ";;" ) + QObject::tr( "All files (*.*)" ) : mFileFilter;
else if ( !mExtension.isEmpty() )
return QObject::tr( "%1 files" ).arg( mExtension.toUpper() ) + QStringLiteral( " (*." ) + mExtension.toLower() + QStringLiteral( ");;" ) + QObject::tr( "All files (*.*)" );
else
return QObject::tr( "All files (*.*)" );
}
case Folder:
return QString();
}
return QString();
}
void QgsProcessingParameterFile::setExtension( const QString &extension )
{
mExtension = extension;

View File

@ -1772,7 +1772,7 @@ class CORE_EXPORT QgsProcessingParameterGeometry : public QgsProcessingParameter
* An input file or folder parameter for processing algorithms.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterFile : public QgsProcessingParameterDefinition
class CORE_EXPORT QgsProcessingParameterFile : public QgsProcessingParameterDefinition, public QgsFileFilterGenerator
{
public:
@ -1802,6 +1802,7 @@ class CORE_EXPORT QgsProcessingParameterFile : public QgsProcessingParameterDefi
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString asScriptCode() const override;
QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;
QString createFileFilter() const override;
/**
* Returns the parameter behavior (e.g. File or Folder).

View File

@ -3736,6 +3736,19 @@ void TestQgsProcessing::parameterFile()
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
QCOMPARE( fromCode->behavior(), def->behavior() );
// create file filter
// folder type
QCOMPARE( def->createFileFilter(), QString() );
def.reset( new QgsProcessingParameterFile( "optional", QString(), QgsProcessingParameterFile::File, QString(), QString( "/home/me" ), true ) );
// no filter/extension
QCOMPARE( def->createFileFilter(), QStringLiteral( "All files (*.*)" ) );
def->setExtension( QStringLiteral( "png" ) );
QCOMPARE( def->createFileFilter(), QStringLiteral( "PNG files (*.png);;All files (*.*)" ) );
def->setFileFilter( QStringLiteral( "PNG Files (*.png);;BMP Files (*.bmp)" ) );
QCOMPARE( def->createFileFilter(), QStringLiteral( "PNG Files (*.png);;BMP Files (*.bmp);;All files (*.*)" ) );
def->setFileFilter( QStringLiteral( "All files (*.*)" ) );
QCOMPARE( def->createFileFilter(), QStringLiteral( "All files (*.*)" ) );
}
void TestQgsProcessing::parameterMatrix()