Skip to content

Commit

Permalink
Merge pull request #5544 from nyalldawson/proc_api
Browse files Browse the repository at this point in the history
[processing] Improve api handling for provider format support
  • Loading branch information
nyalldawson committed Nov 7, 2017
2 parents edea38f + bf75747 commit e9e7740
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 32 deletions.
18 changes: 18 additions & 0 deletions python/core/processing/qgsprocessingparameters.sip
Expand Up @@ -353,13 +353,31 @@ class QgsProcessingParameterDefinition
:rtype: list of str
%End

QgsProcessingAlgorithm *algorithm() const;
%Docstring
Returns a pointer to the algorithm which owns this parameter. May be None
for non-owned parameters.
.. seealso:: provider()
:rtype: QgsProcessingAlgorithm
%End

QgsProcessingProvider *provider() const;
%Docstring
Returns a pointer to the provider for the algorithm which owns this parameter. May be None
for non-owned parameters or algorithms.
.. seealso:: algorithm()
:rtype: QgsProcessingProvider
%End

protected:








};

QFlags<QgsProcessingParameterDefinition::Flag> operator|(QgsProcessingParameterDefinition::Flag f1, QFlags<QgsProcessingParameterDefinition::Flag> f2);
Expand Down
34 changes: 28 additions & 6 deletions python/core/processing/qgsprocessingprovider.sip
Expand Up @@ -94,25 +94,47 @@ class QgsProcessingProvider : QObject
%Docstring
Returns a list of the raster format file extensions supported by this provider.
.. seealso:: supportedOutputVectorLayerExtensions()
.. seealso:: supportedOutputTableExtensions()
:rtype: list of str
%End

virtual QStringList supportedOutputVectorLayerExtensions() const;
%Docstring
Returns a list of the vector format file extensions supported by this provider.
.. seealso:: defaultVectorFileExtension()
.. seealso:: supportedOutputRasterLayerExtensions()
.. seealso:: supportedOutputTableExtensions()
.. seealso:: supportsNonFileBasedOutput()
:rtype: list of str
%End

virtual QStringList supportedOutputTableExtensions() const;
virtual QString defaultVectorFileExtension( bool hasGeometry = true ) const;
%Docstring
Returns a list of the table format file extensions supported by this provider.
.. seealso:: supportedOutputRasterLayerExtensions()
Returns the default file extension to use for vector outputs created by the
provider.

If ``hasGeometry`` is true then the output file format must have support for
geometry. If ``hasGeometry`` is false then non-spatial formats can be used.

The default implementation returns the user's default Processing vector output format
setting, if it's supported by the provider (see supportedOutputVectorLayerExtensions()).
Otherwise the first reported supported vector format will be used.

.. seealso:: supportedOutputVectorLayerExtensions()
:rtype: list of str
.. seealso:: defaultRasterFileExtension()
:rtype: str
%End

virtual QString defaultRasterFileExtension() const;
%Docstring
Returns the default file extension to use for raster outputs created by the
provider.

The default implementation returns the user's default Processing raster output format
setting, if it's supported by the provider (see supportedOutputRasterLayerExtensions()).
Otherwise the first reported supported raster format will be used.

.. seealso:: supportedOutputRasterLayerExtensions()
.. seealso:: defaultVectorFileExtension()
:rtype: str
%End

virtual bool supportsNonFileBasedOutput() const;
Expand Down
12 changes: 5 additions & 7 deletions python/plugins/processing/gui/ParameterGuiUtils.py
Expand Up @@ -63,13 +63,11 @@ def getFileFilter(param):
for i in range(len(exts)):
exts[i] = tr('{0} files (*.{1})', 'QgsProcessingParameterRasterDestination').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts) + ';;' + tr('All files (*.*)')
elif param.type() == 'table':
exts = ['csv', 'dbf']
for i in range(len(exts)):
exts[i] = tr('{0} files (*.{1})', 'ParameterTable').format(exts[i].upper(), exts[i].lower())
return tr('All files (*.*)') + ';;' + ';;'.join(exts)
elif param.type() == 'sink':
exts = QgsVectorFileWriter.supportedFormatExtensions()
elif param.type() in ('sink', 'vectorDestination'):
if param.provider() is not None:
exts = param.provider().supportedOutputVectorLayerExtensions()
else:
exts = QgsVectorFileWriter.supportedFormatExtensions()
for i in range(len(exts)):
exts[i] = tr('{0} files (*.{1})', 'ParameterVector').format(exts[i].upper(), exts[i].lower())
return ';;'.join(exts) + ';;' + tr('All files (*.*)')
Expand Down
1 change: 1 addition & 0 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -242,6 +242,7 @@ bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *def
}

mParameters << definition;
definition->mAlgorithm = this;

if ( createOutput )
return createAutoOutputForParameter( definition );
Expand Down
53 changes: 43 additions & 10 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -16,8 +16,10 @@
***************************************************************************/

#include "qgsprocessingparameters.h"
#include "qgsprocessingprovider.h"
#include "qgsprocessingcontext.h"
#include "qgsprocessingutils.h"
#include "qgsprocessingalgorithm.h"
#include "qgsvectorlayerfeatureiterator.h"
#include "qgsprocessingoutputs.h"
#include "qgssettings.h"
Expand Down Expand Up @@ -1237,6 +1239,16 @@ bool QgsProcessingParameterDefinition::fromVariantMap( const QVariantMap &map )
return true;
}

QgsProcessingAlgorithm *QgsProcessingParameterDefinition::algorithm() const
{
return mAlgorithm;
}

QgsProcessingProvider *QgsProcessingParameterDefinition::provider() const
{
return mAlgorithm ? mAlgorithm->provider() : nullptr;
}

QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
{}
Expand Down Expand Up @@ -3087,14 +3099,21 @@ QgsProcessingOutputDefinition *QgsProcessingParameterFeatureSink::toOutputDefini

QString QgsProcessingParameterFeatureSink::defaultFileExtension() const
{
QgsSettings settings;
if ( hasGeometry() )
if ( QgsProcessingProvider *p = provider() )
{
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
return p->defaultVectorFileExtension( hasGeometry() );
}
else
{
return QStringLiteral( "dbf" );
QgsSettings settings;
if ( hasGeometry() )
{
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
}
else
{
return QStringLiteral( "dbf" );
}
}
}

Expand Down Expand Up @@ -3240,8 +3259,15 @@ QgsProcessingOutputDefinition *QgsProcessingParameterRasterDestination::toOutput

QString QgsProcessingParameterRasterDestination::defaultFileExtension() const
{
QgsSettings settings;
return settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "tif" ), QgsSettings::Core ).toString();
if ( QgsProcessingProvider *p = provider() )
{
return p->defaultRasterFileExtension();
}
else
{
QgsSettings settings;
return settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "tif" ), QgsSettings::Core ).toString();
}
}

QgsProcessingParameterRasterDestination *QgsProcessingParameterRasterDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
Expand Down Expand Up @@ -3536,14 +3562,21 @@ QgsProcessingOutputDefinition *QgsProcessingParameterVectorDestination::toOutput

QString QgsProcessingParameterVectorDestination::defaultFileExtension() const
{
QgsSettings settings;
if ( hasGeometry() )
if ( QgsProcessingProvider *p = provider() )
{
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
return p->defaultVectorFileExtension( hasGeometry() );
}
else
{
return QStringLiteral( "dbf" );
QgsSettings settings;
if ( hasGeometry() )
{
return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
}
else
{
return QStringLiteral( "dbf" );
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -34,6 +34,7 @@ class QgsFeatureSink;
class QgsProcessingFeatureSource;
class QgsProcessingOutputDefinition;
class QgsProcessingFeedback;
class QgsProcessingProvider;

/**
* \class QgsProcessingFeatureSourceDefinition
Expand Down Expand Up @@ -391,6 +392,20 @@ class CORE_EXPORT QgsProcessingParameterDefinition
*/
virtual QStringList dependsOnOtherParameters() const { return QStringList(); }

/**
* Returns a pointer to the algorithm which owns this parameter. May be nullptr
* for non-owned parameters.
* \see provider()
*/
QgsProcessingAlgorithm *algorithm() const;

/**
* Returns a pointer to the provider for the algorithm which owns this parameter. May be nullptr
* for non-owned parameters or algorithms.
* \see algorithm()
*/
QgsProcessingProvider *provider() const;

protected:

//! Parameter name
Expand All @@ -408,6 +423,12 @@ class CORE_EXPORT QgsProcessingParameterDefinition
//! Freeform metadata for parameter. Mostly used by widget wrappers to customise their appearance and behavior.
QVariantMap mMetadata;

//! Pointer to algorithm which owns this parameter
QgsProcessingAlgorithm *mAlgorithm = nullptr;

// To allow access to mAlgorithm. We don't want a public setter for this!
friend class QgsProcessingAlgorithm;

};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingParameterDefinition::Flags )
Expand Down
53 changes: 53 additions & 0 deletions src/core/processing/qgsprocessingprovider.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsprocessingprovider.h"
#include "qgsapplication.h"
#include "qgsvectorfilewriter.h"
#include "qgssettings.h"

QgsProcessingProvider::QgsProcessingProvider( QObject *parent SIP_TRANSFERTHIS )
: QObject( parent )
Expand All @@ -44,6 +45,11 @@ QString QgsProcessingProvider::longName() const
return name();
}

QStringList QgsProcessingProvider::supportedOutputRasterLayerExtensions() const
{
return QStringList() << QStringLiteral( "tif" );
}

void QgsProcessingProvider::refreshAlgorithms()
{
qDeleteAll( mAlgorithms );
Expand Down Expand Up @@ -84,3 +90,50 @@ QStringList QgsProcessingProvider::supportedOutputVectorLayerExtensions() const
return QgsVectorFileWriter::supportedFormatExtensions();
}

QString QgsProcessingProvider::defaultVectorFileExtension( bool hasGeometry ) const
{
QgsSettings settings;
const QString defaultExtension = hasGeometry ? QStringLiteral( "shp" ) : QStringLiteral( "dbf" );
const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), defaultExtension, QgsSettings::Core ).toString();

const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
{
// user set default is supported by provider, use that
return userDefault;
}
else if ( !supportedExtensions.empty() )
{
return supportedExtensions.at( 0 );
}
else
{
// who knows? provider says it has no file support at all...
// let's say shp. even MapInfo supports shapefiles.
return defaultExtension;
}
}

QString QgsProcessingProvider::defaultRasterFileExtension() const
{
QgsSettings settings;
const QString defaultExtension = QStringLiteral( "tif" );
const QString userDefault = settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), defaultExtension, QgsSettings::Core ).toString();

const QStringList supportedExtensions = supportedOutputRasterLayerExtensions();
if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
{
// user set default is supported by provider, use that
return userDefault;
}
else if ( !supportedExtensions.empty() )
{
return supportedExtensions.at( 0 );
}
else
{
// who knows? provider says it has no file support at all...
return defaultExtension;
}
}

33 changes: 27 additions & 6 deletions src/core/processing/qgsprocessingprovider.h
Expand Up @@ -104,24 +104,45 @@ class CORE_EXPORT QgsProcessingProvider : public QObject
/**
* Returns a list of the raster format file extensions supported by this provider.
* \see supportedOutputVectorLayerExtensions()
* \see supportedOutputTableExtensions()
*/
virtual QStringList supportedOutputRasterLayerExtensions() const { return QStringList() << QStringLiteral( "tif" ); }
virtual QStringList supportedOutputRasterLayerExtensions() const;

/**
* Returns a list of the vector format file extensions supported by this provider.
* \see defaultVectorFileExtension()
* \see supportedOutputRasterLayerExtensions()
* \see supportedOutputTableExtensions()
* \see supportsNonFileBasedOutput()
*/
virtual QStringList supportedOutputVectorLayerExtensions() const;

/**
* Returns a list of the table format file extensions supported by this provider.
* \see supportedOutputRasterLayerExtensions()
* Returns the default file extension to use for vector outputs created by the
* provider.
*
* If \a hasGeometry is true then the output file format must have support for
* geometry. If \a hasGeometry is false then non-spatial formats can be used.
*
* The default implementation returns the user's default Processing vector output format
* setting, if it's supported by the provider (see supportedOutputVectorLayerExtensions()).
* Otherwise the first reported supported vector format will be used.
*
* \see supportedOutputVectorLayerExtensions()
* \see defaultRasterFileExtension()
*/
virtual QString defaultVectorFileExtension( bool hasGeometry = true ) const;

/**
* Returns the default file extension to use for raster outputs created by the
* provider.
*
* The default implementation returns the user's default Processing raster output format
* setting, if it's supported by the provider (see supportedOutputRasterLayerExtensions()).
* Otherwise the first reported supported raster format will be used.
*
* \see supportedOutputRasterLayerExtensions()
* \see defaultVectorFileExtension()
*/
virtual QStringList supportedOutputTableExtensions() const { return QStringList() << QStringLiteral( "csv" ); }
virtual QString defaultRasterFileExtension() const;

/**
* Returns true if the provider supports non-file based outputs (such as memory layers
Expand Down

0 comments on commit e9e7740

Please sign in to comment.