Skip to content

Commit

Permalink
[processing] Fix exception when calling "Select from Files" in batch …
Browse files Browse the repository at this point in the history
…mode

on a file parameter

Fixes #40705

(cherry picked from commit b740bf2)
  • Loading branch information
nyalldawson committed Jan 15, 2021
1 parent 24cca6d commit 75672c7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
Expand Up @@ -1699,7 +1699,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.
Expand Down Expand Up @@ -1741,6 +1741,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
Expand Down
11 changes: 3 additions & 8 deletions python/plugins/processing/gui/BatchPanel.py
Expand Up @@ -81,7 +81,8 @@
QgsProcessing,
QgsExpression,
QgsRasterLayer,
QgsProcessingUtils
QgsProcessingUtils,
QgsFileFilterGenerator
)
from qgis.gui import (
QgsProcessingParameterWidgetContext,
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -3299,6 +3299,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;
Expand Down
3 changes: 2 additions & 1 deletion src/core/processing/qgsprocessingparameters.h
Expand Up @@ -1732,7 +1732,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:

Expand Down Expand Up @@ -1762,6 +1762,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).
Expand Down
13 changes: 13 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -3730,6 +3730,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()
Expand Down

0 comments on commit 75672c7

Please sign in to comment.