Skip to content
Permalink
Browse files

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

…mode

on a file parameter

Fixes #40705
  • Loading branch information
nyalldawson committed Jan 4, 2021
1 parent 6fed3b3 commit b740bf201a6cbff60186da109bfcb4ef667ccc1f
@@ -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
@@ -81,7 +81,8 @@
QgsProcessing,
QgsExpression,
QgsRasterLayer,
QgsProcessingUtils
QgsProcessingUtils,
QgsFileFilterGenerator
)
from qgis.gui import (
QgsProcessingParameterWidgetContext,
@@ -152,13 +153,7 @@ def createMenu(self):
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)
@@ -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;
@@ -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).
@@ -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()

0 comments on commit b740bf2

Please sign in to comment.
You can’t perform that action at this time.