Skip to content
Permalink
Browse files

[processing] Port file widget wrapper to new c++ API

  • Loading branch information
nyalldawson committed Feb 6, 2019
1 parent 4eaae77 commit 061882d9f7c077c2576980c42c28ff4d902c1089
@@ -35,6 +35,7 @@ QgsProcessingGuiRegistry::QgsProcessingGuiRegistry()
addParameterWidgetFactory( new QgsProcessingRangeWidgetWrapper() );
addParameterWidgetFactory( new QgsProcessingAuthConfigWidgetWrapper() );
addParameterWidgetFactory( new QgsProcessingMatrixWidgetWrapper() );
addParameterWidgetFactory( new QgsProcessingFileWidgetWrapper() );
}

QgsProcessingGuiRegistry::~QgsProcessingGuiRegistry()
@@ -25,6 +25,8 @@
#include "qgsprocessingcontext.h"
#include "qgsauthconfigselect.h"
#include "qgsapplication.h"
#include "qgsfilewidget.h"
#include "qgssettings.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QCheckBox>
@@ -1105,5 +1107,108 @@ QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingMatrixWidgetWrapper::c
}




//
// QgsProcessingFileWidgetWrapper
//

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

}

QWidget *QgsProcessingFileWidgetWrapper::createWidget()
{
const QgsProcessingParameterFile *fileParam = dynamic_cast< const QgsProcessingParameterFile *>( parameterDefinition() );
switch ( type() )
{
case QgsProcessingGui::Standard:
case QgsProcessingGui::Modeler:
case QgsProcessingGui::Batch:
{
mFileWidget = new QgsFileWidget();
mFileWidget->setToolTip( parameterDefinition()->toolTip() );
mFileWidget->setDialogTitle( parameterDefinition()->description() );

mFileWidget->setDefaultRoot( QgsSettings().value( QStringLiteral( "/Processing/LastInputPath" ), QDir::homePath() ).toString() );

switch ( fileParam->behavior() )
{
case QgsProcessingParameterFile::File:
mFileWidget->setStorageMode( QgsFileWidget::GetFile );
if ( !fileParam->extension().isEmpty() )
mFileWidget->setFilter( tr( "%1 files" ).arg( fileParam->extension().toUpper() ) + QStringLiteral( " (*." ) + fileParam->extension().toLower() + ')' );
break;

case QgsProcessingParameterFile::Folder:
mFileWidget->setStorageMode( QgsFileWidget::GetDirectory );
break;
}

connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
{
QgsSettings().setValue( QStringLiteral( "/Processing/LastInputPath" ), QFileInfo( path ).canonicalPath() );
emit widgetValueHasChanged( this );
} );
return mFileWidget;
};
}
return nullptr;
}

void QgsProcessingFileWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
{
const QString v = QgsProcessingParameters::parameterAsString( parameterDefinition(), value, context );
if ( mFileWidget )
mFileWidget->setFilePath( v );
}

QVariant QgsProcessingFileWidgetWrapper::widgetValue() const
{
if ( mFileWidget )
return mFileWidget->filePath();
else
return QVariant();
}

QStringList QgsProcessingFileWidgetWrapper::compatibleParameterTypes() const
{
return QStringList()
<< QgsProcessingParameterString::typeName()
<< QgsProcessingParameterFile::typeName();
}

QStringList QgsProcessingFileWidgetWrapper::compatibleOutputTypes() const
{
return QStringList() << QgsProcessingOutputFile::typeName()
<< QgsProcessingOutputString::typeName()
<< QgsProcessingOutputRasterLayer::typeName()
<< QgsProcessingOutputVectorLayer::typeName()
<< QgsProcessingOutputMapLayer::typeName();
}

QList<int> QgsProcessingFileWidgetWrapper::compatibleDataTypes() const
{
return QList< int >();
}

QString QgsProcessingFileWidgetWrapper::modelerExpressionFormatString() const
{
return tr( "string representing a path to a file or folder" );
}

QString QgsProcessingFileWidgetWrapper::parameterType() const
{
return QgsProcessingParameterFile::typeName();
}

QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingFileWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
{
return new QgsProcessingFileWidgetWrapper( parameter, type );
}


///@endcond PRIVATE

@@ -31,6 +31,7 @@ class QgsSpinBox;
class QgsDoubleSpinBox;
class QgsAuthConfigSelect;
class QgsProcessingMatrixParameterPanel;
class QgsFileWidget;

///@cond PRIVATE

@@ -324,6 +325,41 @@ class GUI_EXPORT QgsProcessingMatrixWidgetWrapper : public QgsAbstractProcessing
friend class TestProcessingGui;
};

class GUI_EXPORT QgsProcessingFileWidgetWrapper : public QgsAbstractProcessingParameterWidgetWrapper, public QgsProcessingParameterWidgetFactoryInterface
{
Q_OBJECT

public:

QgsProcessingFileWidgetWrapper( const QgsProcessingParameterDefinition *parameter = nullptr,
QgsProcessingGui::WidgetType type = QgsProcessingGui::Standard, QWidget *parent = nullptr );

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

// QgsProcessingParameterWidgetWrapper interface
QWidget *createWidget() override SIP_FACTORY;

protected:

void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
QVariant widgetValue() const override;

QStringList compatibleParameterTypes() const override;

QStringList compatibleOutputTypes() const override;

QList< int > compatibleDataTypes() const override;
QString modelerExpressionFormatString() const override;

private:

QgsFileWidget *mFileWidget = nullptr;

friend class TestProcessingGui;
};

///@endcond PRIVATE

#endif // QGSPROCESSINGWIDGETWRAPPERIMPL_H
@@ -46,6 +46,7 @@
#include "qgsauthmanager.h"
#include "qgsprocessingmatrixparameterdialog.h"
#include "models/qgsprocessingmodelalgorithm.h"
#include "qgsfilewidget.h"

class TestParamType : public QgsProcessingParameterDefinition
{
@@ -150,6 +151,7 @@ class TestProcessingGui : public QObject
void testModelerWrapper();
void testBooleanWrapper();
void testStringWrapper();
void testFileWrapper();
void testAuthCfgWrapper();
void testCrsWrapper();
void testNumericWrapperDouble();
@@ -826,6 +828,74 @@ void TestProcessingGui::testStringWrapper()
delete l;
}

void TestProcessingGui::testFileWrapper()
{
auto testWrapper = []( QgsProcessingGui::WidgetType type )
{
QgsProcessingParameterFile param( QStringLiteral( "file" ), QStringLiteral( "file" ) );

QgsProcessingFileWidgetWrapper wrapper( &param, type );

QgsProcessingContext context;
QWidget *w = wrapper.createWrappedWidget( context );

QSignalSpy spy( &wrapper, &QgsProcessingFileWidgetWrapper::widgetValueHasChanged );
wrapper.setWidgetValue( TEST_DATA_DIR + QStringLiteral( "/points.shp" ), context );
QCOMPARE( spy.count(), 1 );
QCOMPARE( wrapper.widgetValue().toString(), TEST_DATA_DIR + QStringLiteral( "/points.shp" ) );
QCOMPARE( static_cast< QgsFileWidget * >( wrapper.wrappedWidget() )->filePath(), TEST_DATA_DIR + QStringLiteral( "/points.shp" ) );
QVERIFY( static_cast< QgsFileWidget * >( wrapper.wrappedWidget() )->filter().isEmpty() );
QCOMPARE( static_cast< QgsFileWidget * >( wrapper.wrappedWidget() )->storageMode(), QgsFileWidget::GetFile );
wrapper.setWidgetValue( QString(), context );
QCOMPARE( spy.count(), 2 );
QVERIFY( wrapper.widgetValue().toString().isEmpty() );
QVERIFY( static_cast< QgsFileWidget * >( wrapper.wrappedWidget() )->filePath().isEmpty() );

QLabel *l = wrapper.createWrappedLabel();
if ( wrapper.type() != QgsProcessingGui::Batch )
{
QVERIFY( l );
QCOMPARE( l->text(), QStringLiteral( "file" ) );
QCOMPARE( l->toolTip(), param.toolTip() );
delete l;
}
else
{
QVERIFY( !l );
}

// check signal
static_cast< QgsFileWidget * >( wrapper.wrappedWidget() )->setFilePath( TEST_DATA_DIR + QStringLiteral( "/polys.shp" ) );
QCOMPARE( spy.count(), 3 );

delete w;

// with filter
QgsProcessingParameterFile param2( QStringLiteral( "file" ), QStringLiteral( "file" ), QgsProcessingParameterFile::File, QStringLiteral( "qml" ) );

QgsProcessingFileWidgetWrapper wrapper2( &param2, type );
w = wrapper2.createWrappedWidget( context );
QCOMPARE( static_cast< QgsFileWidget * >( wrapper2.wrappedWidget() )->filter(), QStringLiteral( "QML files (*.qml)" ) );
QCOMPARE( static_cast< QgsFileWidget * >( wrapper2.wrappedWidget() )->storageMode(), QgsFileWidget::GetFile );

// folder mode
QgsProcessingParameterFile param3( QStringLiteral( "folder" ), QStringLiteral( "folder" ), QgsProcessingParameterFile::Folder );

QgsProcessingFileWidgetWrapper wrapper3( &param3, type );
w = wrapper3.createWrappedWidget( context );
QCOMPARE( static_cast< QgsFileWidget * >( wrapper3.wrappedWidget() )->storageMode(), QgsFileWidget::GetDirectory );
};

// standard wrapper
testWrapper( QgsProcessingGui::Standard );

// batch wrapper
testWrapper( QgsProcessingGui::Batch );

// modeler wrapper
testWrapper( QgsProcessingGui::Modeler );
}

void TestProcessingGui::testAuthCfgWrapper()
{
QList<QgsAuthMethodConfig> configs;

0 comments on commit 061882d

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