Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] Port file widget wrapper to new c++ API
  • Loading branch information
nyalldawson committed Feb 8, 2019
1 parent 4eaae77 commit 061882d
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/gui/processing/qgsprocessingguiregistry.cpp
Expand Up @@ -35,6 +35,7 @@ QgsProcessingGuiRegistry::QgsProcessingGuiRegistry()
addParameterWidgetFactory( new QgsProcessingRangeWidgetWrapper() );
addParameterWidgetFactory( new QgsProcessingAuthConfigWidgetWrapper() );
addParameterWidgetFactory( new QgsProcessingMatrixWidgetWrapper() );
addParameterWidgetFactory( new QgsProcessingFileWidgetWrapper() );
}

QgsProcessingGuiRegistry::~QgsProcessingGuiRegistry()
Expand Down
105 changes: 105 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -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

36 changes: 36 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.h
Expand Up @@ -31,6 +31,7 @@ class QgsSpinBox;
class QgsDoubleSpinBox;
class QgsAuthConfigSelect;
class QgsProcessingMatrixParameterPanel;
class QgsFileWidget;

///@cond PRIVATE

Expand Down Expand Up @@ -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
70 changes: 70 additions & 0 deletions tests/src/gui/testprocessinggui.cpp
Expand Up @@ -46,6 +46,7 @@
#include "qgsauthmanager.h"
#include "qgsprocessingmatrixparameterdialog.h"
#include "models/qgsprocessingmodelalgorithm.h"
#include "qgsfilewidget.h"

class TestParamType : public QgsProcessingParameterDefinition
{
Expand Down Expand Up @@ -150,6 +151,7 @@ class TestProcessingGui : public QObject
void testModelerWrapper();
void testBooleanWrapper();
void testStringWrapper();
void testFileWrapper();
void testAuthCfgWrapper();
void testCrsWrapper();
void testNumericWrapperDouble();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 061882d

Please sign in to comment.