Skip to content
Permalink
Browse files

minor fixes and add tests

  • Loading branch information
vcloarec committed Sep 10, 2020
1 parent 5ffc4f2 commit 2446e7f0cd52ec721cf814f310987cc5d8280113
@@ -79,7 +79,7 @@ bool QgsTinMeshCreationAlgorithm::prepareAlgorithm( const QVariantMap &parameter
const QVariantList layersList = layersVariant.toList();

QgsCoordinateReferenceSystem destinationCrs = parameterAsCrs( parameters, QStringLiteral( "CRS_OUTPUT" ), context );
if ( !destinationCrs.isValid() )
if ( !destinationCrs.isValid() && context.project() )
destinationCrs = context.project()->crs();

for ( const QVariant &layer : layersList )
@@ -95,7 +95,7 @@ bool QgsTinMeshCreationAlgorithm::prepareAlgorithm( const QVariantMap &parameter
static_cast<QgsProcessingParameterTinInputLayers::Type>( layerMap.value( QStringLiteral( "type" ) ).toInt() );
int attributeIndex = layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt();

QgsProcessingFeatureSource *featureSource = QgsProcessingUtils::variantToSource( layerSource, context );
std::unique_ptr<QgsProcessingFeatureSource> featureSource( QgsProcessingUtils::variantToSource( layerSource, context ) );

if ( !featureSource )
continue;
@@ -37,6 +37,9 @@ bool QgsProcessingParameterTinInputLayers::checkValueIsAcceptable( const QVarian

const QVariantList variantLayers = input.toList();

if ( variantLayers.isEmpty() )
return false;

for ( const QVariant &variantLayer : variantLayers )
{
if ( variantLayer.type() != QVariant::Map )
@@ -76,7 +79,7 @@ QString QgsProcessingParameterTinInputLayers::valueAsPythonString( const QVarian
layerDefParts << QStringLiteral( "'source': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "source" ) ) );
layerDefParts << QStringLiteral( "'type': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "type" ) ) );
layerDefParts << QStringLiteral( "'attributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "attributeIndex" ) ) );
QString layerDef = QStringLiteral( "{ %1 }" ).arg( layerDefParts.join( ',' ) );
QString layerDef = QStringLiteral( "{%1}" ).arg( layerDefParts.join( ',' ) );
parts.append( layerDef );
}
return parts.join( ',' ).prepend( '[' ).append( ']' );
@@ -70,6 +70,8 @@ void QgsProcessingTinInputLayersWidget::setValue( const QVariant &value )
layer.attributeIndex = layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt();
mInputLayersModel.addLayer( layer );
}

emit changed();
}

void QgsProcessingTinInputLayersWidget::setProject( QgsProject *project )
@@ -336,6 +338,10 @@ void QgsProcessingTinInputLayersWidget::Delegate::setModelData( QWidget *editor,
model->setData( index, comboType->currentData(), Qt::EditRole );
}

QgsProcessingTinInputLayersWidgetWrapper::QgsProcessingTinInputLayersWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent ):
QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
{}

QString QgsProcessingTinInputLayersWidgetWrapper::parameterType() const
{
return QStringLiteral( "tininputlayers" );
@@ -346,6 +352,10 @@ QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingTinInputLayersWidgetWr
return new QgsProcessingTinInputLayersWidgetWrapper( parameter, type );
}

QStringList QgsProcessingTinInputLayersWidgetWrapper::compatibleParameterTypes() const {return QStringList();}

QStringList QgsProcessingTinInputLayersWidgetWrapper::compatibleOutputTypes() const {return QStringList();}

QWidget *QgsProcessingTinInputLayersWidgetWrapper::createWidget()
{
mWidget = new QgsProcessingTinInputLayersWidget( widgetContext().project() );
@@ -25,9 +25,9 @@
#include "qgsprocessingparametertininputlayers.h"
#include "ui_qgsprocessingtinmeshdatawidgetbase.h"

/// @cond private
/// @cond PRIVATE

class QgsProcessingTinInputLayersWidget: public QWidget, private Ui::QgsProcessingTinInputLayersWidgetBase
class GUI_EXPORT QgsProcessingTinInputLayersWidget: public QWidget, private Ui::QgsProcessingTinInputLayersWidgetBase
{
Q_OBJECT
public:
@@ -90,30 +90,30 @@ class QgsProcessingTinInputLayersWidget: public QWidget, private Ui::QgsProcessi
};


class QgsProcessingTinInputLayersWidgetWrapper : public QgsAbstractProcessingParameterWidgetWrapper, public QgsProcessingParameterWidgetFactoryInterface
class GUI_EXPORT QgsProcessingTinInputLayersWidgetWrapper : public QgsAbstractProcessingParameterWidgetWrapper, public QgsProcessingParameterWidgetFactoryInterface
{
Q_OBJECT

public:
QgsProcessingTinInputLayersWidgetWrapper( const QgsProcessingParameterDefinition *parameter = nullptr,
QgsProcessingGui::WidgetType type = QgsProcessingGui::Standard, QWidget *parent = nullptr ):
QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
{}
QgsProcessingGui::WidgetType type = QgsProcessingGui::Standard, QWidget *parent = nullptr );

QString parameterType() const override;
QgsAbstractProcessingParameterWidgetWrapper *createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type ) override;

protected:
QStringList compatibleParameterTypes() const override {return QStringList();}
QStringList compatibleOutputTypes() const override {return QStringList();}
QStringList compatibleParameterTypes() const override;
QStringList compatibleOutputTypes() const override;
QWidget *createWidget() override SIP_FACTORY;
void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
QVariant widgetValue() const override;

private:
QgsProcessingTinInputLayersWidget *mWidget = nullptr;

friend class TestProcessingGui;
};

/// @endcond
///@endcond

#endif // QGSPROCESSINGTININPUTLAYERSWIDGET_H
@@ -50,6 +50,7 @@
#include "qgsrasterfilewriter.h"
#include "qgsprocessingparameterfieldmap.h"
#include "qgsprocessingparameteraggregate.h"
#include "qgsprocessingparametertininputlayers.h"

class DummyAlgorithm : public QgsProcessingAlgorithm
{
@@ -600,6 +601,7 @@ class TestQgsProcessing: public QObject
void parameterDatabaseTable();
void parameterFieldMapping();
void parameterAggregate();
void parameterTinInputLayers();
void checkParamValues();
void combineLayerExtent();
void processingFeatureSource();
@@ -7761,6 +7763,47 @@ void TestQgsProcessing::parameterAggregate()
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterAggregate('non_optional', '', parentLayerParameterName='parent', optional=True)" ) );
}

void TestQgsProcessing::parameterTinInputLayers()
{
QgsProcessingContext context;
QgsProject project;
context.setProject( &project );
QgsVectorLayer *vectorLayer = new QgsVectorLayer( QStringLiteral( "Point" ),
QStringLiteral( "PointLayerForTin" ),
QStringLiteral( "memory" ) );
project.addMapLayer( vectorLayer );

std::unique_ptr< QgsProcessingParameterTinInputLayers > def( new QgsProcessingParameterTinInputLayers( "tin input layer" ) );
QVERIFY( !def->checkValueIsAcceptable( 1 ) );
QVERIFY( !def->checkValueIsAcceptable( "test" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVariantList layerList;
QVERIFY( !def->checkValueIsAcceptable( layerList ) );
QVariantMap layerMap;
layerList.append( layerMap );
QVERIFY( !def->checkValueIsAcceptable( layerList ) );
layerMap["source"] = "layerName";
layerMap["type"] = 0;
layerMap["attributeIndex"] = -1;
layerList[0] = layerMap;
QVERIFY( def->checkValueIsAcceptable( layerList ) );
QVERIFY( !def->checkValueIsAcceptable( layerList, &context ) ); //no corresponding layer in the context's project
layerMap["source"] = "PointLayerForTin";
layerMap["attributeIndex"] = 1; //change for invalid attribute index
layerList[0] = layerMap;
QVERIFY( !def->checkValueIsAcceptable( layerList, &context ) );

layerMap["attributeIndex"] = -1; //valid attribute index (-1 is for Z coordinate of features)
layerList[0] = layerMap;
QVERIFY( def->checkValueIsAcceptable( layerList, &context ) );

QString valueAsPythonString = def->valueAsPythonString( layerList, context );
QCOMPARE( valueAsPythonString, QStringLiteral( "[{'source': 'PointLayerForTin','type': 0,'attributeIndex': -1}]" ) );

QString pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterTinInputLayers('tin input layer', '')" ) );
}

void TestQgsProcessing::parameterDateTime()
{
QgsProcessingContext context;
@@ -87,6 +87,8 @@
#include "qgsprocessingparameterfieldmap.h"
#include "qgsprocessingaggregatewidgetwrapper.h"
#include "qgsprocessingparameteraggregate.h"
#include "qgsprocessingparametertininputlayers.h"
#include "qgsprocessingtininputlayerswidget.h"


class TestParamType : public QgsProcessingParameterDefinition
@@ -247,6 +249,7 @@ class TestProcessingGui : public QObject
void testRasterOutWrapper();
void testFileOutWrapper();
void testFolderOutWrapper();
void testTinInputLayerWrapper();
void testModelGraphicsView();

private:
@@ -8709,6 +8712,42 @@ void TestProcessingGui::testFolderOutWrapper()
testWrapper( QgsProcessingGui::Modeler );
}

void TestProcessingGui::testTinInputLayerWrapper()
{
QgsProcessingParameterTinInputLayers definition( QStringLiteral( "TIN input layers" ) ) ;
QgsProcessingTinInputLayersWidgetWrapper wrapper;

std::unique_ptr<QWidget> w( wrapper.createWidget() );
QVERIFY( w );

QSignalSpy spy( &wrapper, &QgsProcessingTinInputLayersWidgetWrapper::widgetValueHasChanged );

QgsProcessingContext context;
QgsProject project;
context.setProject( &project );
QgsVectorLayer *vectorLayer = new QgsVectorLayer( QStringLiteral( "Point" ),
QStringLiteral( "PointLayerForTin" ),
QStringLiteral( "memory" ) );
project.addMapLayer( vectorLayer );

QVariantList layerList;
QVariantMap layerMap;
layerMap["source"] = "PointLayerForTin";
layerMap["type"] = 0;
layerMap["attributeIndex"] = -1;
layerList.append( layerMap );

QVERIFY( definition.checkValueIsAcceptable( layerList, &context ) );
wrapper.setWidgetValue( layerList, context );
QCOMPARE( spy.count(), 1 );

QVariant value = wrapper.widgetValue();

QVERIFY( definition.checkValueIsAcceptable( value, &context ) );
QString valueAsPythonString = definition.valueAsPythonString( value, context );
QCOMPARE( valueAsPythonString, QStringLiteral( "[{'source': 'PointLayerForTin','type': 0,'attributeIndex': -1}]" ) );
}

void TestProcessingGui::testModelGraphicsView()
{
// test model

0 comments on commit 2446e7f

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