Skip to content

Commit 71a90aa

Browse files
committedMar 14, 2020
Widget wrapper for schema param
1 parent cf310c5 commit 71a90aa

File tree

6 files changed

+530
-10
lines changed

6 files changed

+530
-10
lines changed
 

‎src/gui/processing/qgsprocessingguiregistry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ QgsProcessingGuiRegistry::QgsProcessingGuiRegistry()
4848
addParameterWidgetFactory( new QgsProcessingMapThemeWidgetWrapper() );
4949
addParameterWidgetFactory( new QgsProcessingDateTimeWidgetWrapper() );
5050
addParameterWidgetFactory( new QgsProcessingProviderConnectionWidgetWrapper() );
51+
addParameterWidgetFactory( new QgsProcessingDatabaseSchemaWidgetWrapper() );
5152
}
5253

5354
QgsProcessingGuiRegistry::~QgsProcessingGuiRegistry()

‎src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp

Lines changed: 243 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "qgsmapthemecollection.h"
5050
#include "qgsdatetimeedit.h"
5151
#include "qgsproviderconnectioncombobox.h"
52+
#include "qgsdatabaseschemacombobox.h"
5253
#include <QToolButton>
5354
#include <QLabel>
5455
#include <QHBoxLayout>
@@ -2863,7 +2864,7 @@ QgsProcessingCoordinateOperationParameterDefinitionWidget::QgsProcessingCoordina
28632864
const QMap<QString, QgsProcessingModelParameter> components = widgetContext.model()->parameterComponents();
28642865
for ( auto it = components.constBegin(); it != components.constEnd(); ++it )
28652866
{
2866-
if ( it->parameterName() == definition->name() )
2867+
if ( definition && it->parameterName() == definition->name() )
28672868
continue;
28682869

28692870
// TODO - we should probably filter this list?
@@ -4043,5 +4044,246 @@ QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingProviderConnectionWidg
40434044

40444045

40454046

4047+
//
4048+
// QgsProcessingDatabaseSchemaWidgetWrapper
4049+
//
4050+
4051+
QgsProcessingDatabaseSchemaParameterDefinitionWidget::QgsProcessingDatabaseSchemaParameterDefinitionWidget( QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition, const QgsProcessingAlgorithm *algorithm, QWidget *parent )
4052+
: QgsProcessingAbstractParameterDefinitionWidget( context, widgetContext, definition, algorithm, parent )
4053+
{
4054+
const QgsProcessingParameterDatabaseSchema *schemaParam = dynamic_cast< const QgsProcessingParameterDatabaseSchema *>( definition );
4055+
4056+
QVBoxLayout *vlayout = new QVBoxLayout();
4057+
vlayout->setMargin( 0 );
4058+
vlayout->setContentsMargins( 0, 0, 0, 0 );
4059+
4060+
mConnectionParamComboBox = new QComboBox();
4061+
QString initialConnection;
4062+
if ( schemaParam )
4063+
{
4064+
initialConnection = schemaParam->parentConnectionParameterName();
4065+
}
4066+
4067+
if ( widgetContext.model() )
4068+
{
4069+
// populate combo box with other model input choices
4070+
const QMap<QString, QgsProcessingModelParameter> components = widgetContext.model()->parameterComponents();
4071+
for ( auto it = components.constBegin(); it != components.constEnd(); ++it )
4072+
{
4073+
if ( definition && it->parameterName() == definition->name() )
4074+
continue;
4075+
4076+
if ( !dynamic_cast< const QgsProcessingParameterProviderConnection * >( widgetContext.model()->parameterDefinition( it->parameterName() ) ) )
4077+
continue;
4078+
4079+
mConnectionParamComboBox->addItem( it->parameterName(), it->parameterName() );
4080+
if ( !initialConnection.isEmpty() && initialConnection == it->parameterName() )
4081+
{
4082+
mConnectionParamComboBox->setCurrentIndex( mConnectionParamComboBox->count() - 1 );
4083+
}
4084+
}
4085+
}
4086+
4087+
if ( mConnectionParamComboBox->count() == 0 && !initialConnection.isEmpty() )
4088+
{
4089+
// if no candidates found, we just add the existing one as a placeholder
4090+
mConnectionParamComboBox->addItem( initialConnection, initialConnection );
4091+
mConnectionParamComboBox->setCurrentIndex( mConnectionParamComboBox->count() - 1 );
4092+
}
4093+
4094+
vlayout->addWidget( new QLabel( tr( "Provider connection parameter" ) ) );
4095+
vlayout->addWidget( mConnectionParamComboBox );
4096+
4097+
vlayout->addWidget( new QLabel( tr( "Default value" ) ) );
4098+
4099+
mDefaultEdit = new QLineEdit();
4100+
vlayout->addWidget( mDefaultEdit );
4101+
setLayout( vlayout );
4102+
4103+
if ( schemaParam )
4104+
{
4105+
mDefaultEdit->setText( schemaParam->defaultValue().toString() );
4106+
}
4107+
}
4108+
4109+
QgsProcessingParameterDefinition *QgsProcessingDatabaseSchemaParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
4110+
{
4111+
QVariant defaultVal;
4112+
if ( mDefaultEdit->text().isEmpty() )
4113+
defaultVal = QVariant();
4114+
else
4115+
defaultVal = mDefaultEdit->text();
4116+
auto param = qgis::make_unique< QgsProcessingParameterDatabaseSchema>( name, description, mConnectionParamComboBox->currentData().toString(), defaultVal );
4117+
param->setFlags( flags );
4118+
return param.release();
4119+
}
4120+
4121+
4122+
QgsProcessingDatabaseSchemaWidgetWrapper::QgsProcessingDatabaseSchemaWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QWidget *parent )
4123+
: QgsAbstractProcessingParameterWidgetWrapper( parameter, type, parent )
4124+
{
4125+
4126+
}
4127+
4128+
QWidget *QgsProcessingDatabaseSchemaWidgetWrapper::createWidget()
4129+
{
4130+
const QgsProcessingParameterDatabaseSchema *schemaParam = dynamic_cast< const QgsProcessingParameterDatabaseSchema *>( parameterDefinition() );
4131+
4132+
mSchemaComboBox = new QgsDatabaseSchemaComboBox( QString(), QString() );
4133+
if ( schemaParam->flags() & QgsProcessingParameterDefinition::FlagOptional )
4134+
mSchemaComboBox->setAllowEmptySchema( true );
4135+
4136+
switch ( type() )
4137+
{
4138+
case QgsProcessingGui::Standard:
4139+
case QgsProcessingGui::Batch:
4140+
break;
4141+
case QgsProcessingGui::Modeler:
4142+
mSchemaComboBox->comboBox()->setEditable( true );
4143+
break;
4144+
}
4145+
4146+
mSchemaComboBox->setToolTip( parameterDefinition()->toolTip() );
4147+
connect( mSchemaComboBox->comboBox(), &QComboBox::currentTextChanged, this, [ = ]( const QString & )
4148+
{
4149+
if ( mBlockSignals )
4150+
return;
4151+
4152+
emit widgetValueHasChanged( this );
4153+
} );
4154+
4155+
return mSchemaComboBox;
4156+
}
4157+
4158+
QgsProcessingAbstractParameterDefinitionWidget *QgsProcessingDatabaseSchemaWidgetWrapper::createParameterDefinitionWidget( QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition, const QgsProcessingAlgorithm *algorithm )
4159+
{
4160+
return new QgsProcessingDatabaseSchemaParameterDefinitionWidget( context, widgetContext, definition, algorithm );
4161+
}
4162+
4163+
void QgsProcessingDatabaseSchemaWidgetWrapper::setParentConnectionWrapperValue( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper )
4164+
{
4165+
// evaluate value to connection
4166+
QgsProcessingContext *context = nullptr;
4167+
std::unique_ptr< QgsProcessingContext > tmpContext;
4168+
if ( mProcessingContextGenerator )
4169+
context = mProcessingContextGenerator->processingContext();
4170+
4171+
if ( !context )
4172+
{
4173+
tmpContext = qgis::make_unique< QgsProcessingContext >();
4174+
context = tmpContext.get();
4175+
}
4176+
4177+
QVariant value = parentWrapper->parameterValue();
4178+
const QString connection = QgsProcessingParameters::parameterAsConnectionName( parentWrapper->parameterDefinition(), value, *context );
4179+
4180+
if ( mSchemaComboBox )
4181+
mSchemaComboBox->setConnectionName( connection, dynamic_cast< const QgsProcessingParameterProviderConnection * >( parentWrapper->parameterDefinition() )->providerId() );
4182+
4183+
const QgsProcessingParameterDatabaseSchema *schemaParam = static_cast< const QgsProcessingParameterDatabaseSchema * >( parameterDefinition() );
4184+
if ( schemaParam->defaultValue().isValid() )
4185+
setWidgetValue( parameterDefinition()->defaultValue(), *context );
4186+
}
4187+
4188+
void QgsProcessingDatabaseSchemaWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )
4189+
{
4190+
const QString v = QgsProcessingParameters::parameterAsSchema( parameterDefinition(), value, context );
4191+
4192+
if ( !value.isValid() )
4193+
mSchemaComboBox->comboBox()->setCurrentIndex( -1 );
4194+
else
4195+
{
4196+
if ( mSchemaComboBox->comboBox()->isEditable() )
4197+
{
4198+
const QString prev = mSchemaComboBox->comboBox()->currentText();
4199+
mBlockSignals++;
4200+
mSchemaComboBox->setSchema( v );
4201+
mSchemaComboBox->comboBox()->setCurrentText( v );
4202+
4203+
mBlockSignals--;
4204+
if ( prev != v )
4205+
emit widgetValueHasChanged( this );
4206+
}
4207+
else
4208+
mSchemaComboBox->setSchema( v );
4209+
}
4210+
}
4211+
4212+
QVariant QgsProcessingDatabaseSchemaWidgetWrapper::widgetValue() const
4213+
{
4214+
if ( mSchemaComboBox )
4215+
if ( mSchemaComboBox->comboBox()->isEditable() )
4216+
return mSchemaComboBox->comboBox()->currentText().isEmpty() ? QVariant() : QVariant( mSchemaComboBox->comboBox()->currentText() );
4217+
else
4218+
return mSchemaComboBox->currentSchema().isEmpty() ? QVariant() : QVariant( mSchemaComboBox->currentSchema() );
4219+
else
4220+
return QVariant();
4221+
}
4222+
4223+
QStringList QgsProcessingDatabaseSchemaWidgetWrapper::compatibleParameterTypes() const
4224+
{
4225+
return QStringList()
4226+
<< QgsProcessingParameterProviderConnection::typeName()
4227+
<< QgsProcessingParameterString::typeName()
4228+
<< QgsProcessingParameterExpression::typeName();
4229+
}
4230+
4231+
QStringList QgsProcessingDatabaseSchemaWidgetWrapper::compatibleOutputTypes() const
4232+
{
4233+
return QStringList()
4234+
<< QgsProcessingOutputString::typeName();
4235+
}
4236+
4237+
QList<int> QgsProcessingDatabaseSchemaWidgetWrapper::compatibleDataTypes() const
4238+
{
4239+
return QList< int >();
4240+
}
4241+
4242+
QString QgsProcessingDatabaseSchemaWidgetWrapper::modelerExpressionFormatString() const
4243+
{
4244+
return tr( "database schema name as a string value" );
4245+
}
4246+
4247+
QString QgsProcessingDatabaseSchemaWidgetWrapper::parameterType() const
4248+
{
4249+
return QgsProcessingParameterDatabaseSchema::typeName();
4250+
}
4251+
4252+
QgsAbstractProcessingParameterWidgetWrapper *QgsProcessingDatabaseSchemaWidgetWrapper::createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type )
4253+
{
4254+
return new QgsProcessingDatabaseSchemaWidgetWrapper( parameter, type );
4255+
}
4256+
4257+
void QgsProcessingDatabaseSchemaWidgetWrapper::postInitialize( const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
4258+
{
4259+
QgsAbstractProcessingParameterWidgetWrapper::postInitialize( wrappers );
4260+
switch ( type() )
4261+
{
4262+
case QgsProcessingGui::Standard:
4263+
case QgsProcessingGui::Batch:
4264+
{
4265+
for ( const QgsAbstractProcessingParameterWidgetWrapper *wrapper : wrappers )
4266+
{
4267+
if ( wrapper->parameterDefinition()->name() == static_cast< const QgsProcessingParameterDatabaseSchema * >( parameterDefinition() )->parentConnectionParameterName() )
4268+
{
4269+
setParentConnectionWrapperValue( wrapper );
4270+
connect( wrapper, &QgsAbstractProcessingParameterWidgetWrapper::widgetValueHasChanged, this, [ = ]
4271+
{
4272+
setParentConnectionWrapperValue( wrapper );
4273+
} );
4274+
break;
4275+
}
4276+
}
4277+
break;
4278+
}
4279+
4280+
case QgsProcessingGui::Modeler:
4281+
break;
4282+
}
4283+
}
4284+
4285+
4286+
4287+
40464288

40474289
///@endcond PRIVATE

‎src/gui/processing/qgsprocessingwidgetwrapperimpl.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class QgsDateTimeEdit;
5555
class QgsDateEdit;
5656
class QgsTimeEdit;
5757
class QgsProviderConnectionComboBox;
58+
class QgsDatabaseSchemaComboBox;
5859

5960
///@cond PRIVATE
6061

@@ -1219,6 +1220,71 @@ class GUI_EXPORT QgsProcessingProviderConnectionWidgetWrapper : public QgsAbstra
12191220
friend class TestProcessingGui;
12201221
};
12211222

1223+
1224+
1225+
class GUI_EXPORT QgsProcessingDatabaseSchemaParameterDefinitionWidget : public QgsProcessingAbstractParameterDefinitionWidget
1226+
{
1227+
Q_OBJECT
1228+
public:
1229+
1230+
QgsProcessingDatabaseSchemaParameterDefinitionWidget( QgsProcessingContext &context,
1231+
const QgsProcessingParameterWidgetContext &widgetContext,
1232+
const QgsProcessingParameterDefinition *definition = nullptr,
1233+
const QgsProcessingAlgorithm *algorithm = nullptr, QWidget *parent SIP_TRANSFERTHIS = nullptr );
1234+
QgsProcessingParameterDefinition *createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const override;
1235+
1236+
private:
1237+
1238+
QComboBox *mConnectionParamComboBox = nullptr;
1239+
QLineEdit *mDefaultEdit = nullptr;
1240+
1241+
};
1242+
1243+
class GUI_EXPORT QgsProcessingDatabaseSchemaWidgetWrapper : public QgsAbstractProcessingParameterWidgetWrapper, public QgsProcessingParameterWidgetFactoryInterface
1244+
{
1245+
Q_OBJECT
1246+
1247+
public:
1248+
1249+
QgsProcessingDatabaseSchemaWidgetWrapper( const QgsProcessingParameterDefinition *parameter = nullptr,
1250+
QgsProcessingGui::WidgetType type = QgsProcessingGui::Standard, QWidget *parent = nullptr );
1251+
1252+
// QgsProcessingParameterWidgetFactoryInterface
1253+
QString parameterType() const override;
1254+
QgsAbstractProcessingParameterWidgetWrapper *createWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type ) override;
1255+
void postInitialize( const QList< QgsAbstractProcessingParameterWidgetWrapper * > &wrappers ) override;
1256+
1257+
1258+
// QgsProcessingParameterWidgetWrapper interface
1259+
QWidget *createWidget() override SIP_FACTORY;
1260+
QgsProcessingAbstractParameterDefinitionWidget *createParameterDefinitionWidget(
1261+
QgsProcessingContext &context,
1262+
const QgsProcessingParameterWidgetContext &widgetContext,
1263+
const QgsProcessingParameterDefinition *definition = nullptr,
1264+
const QgsProcessingAlgorithm *algorithm = nullptr ) override;
1265+
1266+
public slots:
1267+
void setParentConnectionWrapperValue( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper );
1268+
1269+
protected:
1270+
1271+
void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
1272+
QVariant widgetValue() const override;
1273+
1274+
QStringList compatibleParameterTypes() const override;
1275+
1276+
QStringList compatibleOutputTypes() const override;
1277+
1278+
QList< int > compatibleDataTypes() const override;
1279+
QString modelerExpressionFormatString() const override;
1280+
1281+
private:
1282+
1283+
QgsDatabaseSchemaComboBox *mSchemaComboBox = nullptr;
1284+
int mBlockSignals = 0;
1285+
1286+
friend class TestProcessingGui;
1287+
};
12221288
///@endcond PRIVATE
12231289

12241290
#endif // QGSPROCESSINGWIDGETWRAPPERIMPL_H

‎src/gui/qgsdatabaseschemacombobox.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ QgsDatabaseSchemaComboBox::QgsDatabaseSchemaComboBox( const QString &provider, c
2424
: QWidget( parent )
2525
, mProvider( provider )
2626
{
27-
mModel = new QgsDatabaseSchemaModel( provider, connection, this );
27+
if ( !provider.isEmpty() && !connection.isEmpty() )
28+
mModel = new QgsDatabaseSchemaModel( provider, connection, this );
2829
init();
2930
}
3031

@@ -37,20 +38,25 @@ QgsDatabaseSchemaComboBox::QgsDatabaseSchemaComboBox( QgsAbstractDatabaseProvide
3738

3839
void QgsDatabaseSchemaComboBox::setAllowEmptySchema( bool allowEmpty )
3940
{
40-
mModel->setAllowEmptySchema( allowEmpty );
41+
mAllowEmpty = allowEmpty;
42+
if ( mModel )
43+
mModel->setAllowEmptySchema( mAllowEmpty );
4144
}
4245

4346
bool QgsDatabaseSchemaComboBox::allowEmptySchema() const
4447
{
45-
return mModel->allowEmptySchema();
48+
return mAllowEmpty;
4649
}
4750

4851
void QgsDatabaseSchemaComboBox::init()
4952
{
5053
mComboBox = new QComboBox();
5154

5255
mSortModel = new QgsDatabaseSchemaComboBoxSortModel( this );
53-
mSortModel->setSourceModel( mModel );
56+
57+
if ( mModel )
58+
mSortModel->setSourceModel( mModel );
59+
5460
mSortModel->setSortRole( Qt::DisplayRole );
5561
mSortModel->setSortLocaleAware( true );
5662
mSortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
@@ -84,7 +90,7 @@ void QgsDatabaseSchemaComboBox::setSchema( const QString &schema )
8490

8591
if ( schema.isEmpty() )
8692
{
87-
if ( mModel->allowEmptySchema() )
93+
if ( mAllowEmpty )
8894
mComboBox->setCurrentIndex( 0 );
8995
else
9096
mComboBox->setCurrentIndex( -1 );
@@ -116,17 +122,20 @@ void QgsDatabaseSchemaComboBox::setConnectionName( const QString &connection, co
116122
const QString oldSchema = currentSchema();
117123
QgsDatabaseSchemaModel *oldModel = mModel;
118124
mModel = new QgsDatabaseSchemaModel( mProvider, connection, this );
119-
mModel->setAllowEmptySchema( oldModel->allowEmptySchema() );
125+
mModel->setAllowEmptySchema( mAllowEmpty );
120126
mSortModel->setSourceModel( mModel );
121-
oldModel->deleteLater();
127+
if ( oldModel )
128+
oldModel->deleteLater();
129+
122130
if ( currentSchema() != oldSchema )
123131
setSchema( oldSchema );
124132
}
125133

126134
void QgsDatabaseSchemaComboBox::refreshSchemas()
127135
{
128136
const QString oldSchema = currentSchema();
129-
mModel->refresh();
137+
if ( mModel )
138+
mModel->refresh();
130139
setSchema( oldSchema );
131140
}
132141

@@ -149,7 +158,7 @@ void QgsDatabaseSchemaComboBox::indexChanged( int i )
149158

150159
void QgsDatabaseSchemaComboBox::rowsChanged()
151160
{
152-
if ( mComboBox->count() == 1 || ( mModel->allowEmptySchema() && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
161+
if ( mComboBox->count() == 1 || ( mAllowEmpty && mComboBox->count() == 2 && mComboBox->currentIndex() == 1 ) )
153162
{
154163
//currently selected connection item has changed
155164
emit schemaChanged( currentSchema() );

‎src/gui/qgsdatabaseschemacombobox.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class GUI_EXPORT QgsDatabaseSchemaComboBox : public QWidget
120120
private:
121121
void init();
122122

123+
bool mAllowEmpty = false;
123124
QString mProvider;
124125
QgsDatabaseSchemaModel *mModel = nullptr;
125126
QSortFilterProxyModel *mSortModel = nullptr;

‎tests/src/gui/testprocessinggui.cpp

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "qgsproviderregistry.h"
7373
#include "qgsprovidermetadata.h"
7474
#include "qgsproviderconnectioncombobox.h"
75+
#include "qgsdatabaseschemacombobox.h"
7576

7677
class TestParamType : public QgsProcessingParameterDefinition
7778
{
@@ -205,6 +206,7 @@ class TestProcessingGui : public QObject
205206
void testMapThemeWrapper();
206207
void testDateTimeWrapper();
207208
void testProviderConnectionWrapper();
209+
void testDatabaseSchemaWrapper();
208210

209211
private:
210212

@@ -4743,6 +4745,205 @@ void TestProcessingGui::testProviderConnectionWrapper()
47434745
QVERIFY( !static_cast< QgsProcessingParameterProviderConnection * >( def.get() )->defaultValue().isValid() );
47444746
}
47454747

4748+
void TestProcessingGui::testDatabaseSchemaWrapper()
4749+
{
4750+
#ifdef ENABLE_PGTEST
4751+
// register some connections
4752+
QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "postgres" ) );
4753+
4754+
QString dbConn = getenv( "QGIS_PGTEST_DB" );
4755+
if ( dbConn.isEmpty() )
4756+
{
4757+
dbConn = "service=\"qgis_test\"";
4758+
}
4759+
QgsAbstractProviderConnection *conn = md->createConnection( QStringLiteral( "%1 sslmode=disable" ).arg( dbConn ), QVariantMap() );
4760+
md->saveConnection( conn, QStringLiteral( "aa" ) );
4761+
4762+
const QStringList schemas = dynamic_cast<QgsAbstractDatabaseProviderConnection *>( conn )->schemas();
4763+
QVERIFY( !schemas.isEmpty() );
4764+
4765+
auto testWrapper = [&schemas]( QgsProcessingGui::WidgetType type )
4766+
{
4767+
QgsProcessingParameterProviderConnection connParam( QStringLiteral( "conn" ), QStringLiteral( "connection" ), QStringLiteral( "postgres" ), true );
4768+
TestLayerWrapper connWrapper( &connParam );
4769+
4770+
QgsProcessingParameterDatabaseSchema param( QStringLiteral( "schema" ), QStringLiteral( "schema" ), QStringLiteral( "conn" ), QVariant(), false );
4771+
4772+
QgsProcessingDatabaseSchemaWidgetWrapper wrapper( &param, type );
4773+
4774+
QgsProcessingContext context;
4775+
QWidget *w = wrapper.createWrappedWidget( context );
4776+
// no connection associated yet
4777+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->count(), 0 );
4778+
4779+
// Set the parent widget connection value
4780+
connWrapper.setWidgetValue( QStringLiteral( "aa" ), context );
4781+
wrapper.setParentConnectionWrapperValue( &connWrapper );
4782+
4783+
// now we should have schemas available
4784+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->count(), schemas.count() );
4785+
4786+
QSignalSpy spy( &wrapper, &QgsProcessingDatabaseSchemaWidgetWrapper::widgetValueHasChanged );
4787+
wrapper.setWidgetValue( QStringLiteral( "qgis_test" ), context );
4788+
QCOMPARE( spy.count(), 1 );
4789+
QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "qgis_test" ) );
4790+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->currentSchema(), QStringLiteral( "qgis_test" ) );
4791+
wrapper.setWidgetValue( QStringLiteral( "public" ), context );
4792+
QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "public" ) );
4793+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->currentSchema(), QStringLiteral( "public" ) );
4794+
QCOMPARE( spy.count(), 2 );
4795+
wrapper.setWidgetValue( QStringLiteral( "public" ), context );
4796+
QCOMPARE( spy.count(), 2 );
4797+
4798+
switch ( type )
4799+
{
4800+
case QgsProcessingGui::Standard:
4801+
case QgsProcessingGui::Batch:
4802+
{
4803+
// batch or standard mode, only valid schemas can be set!
4804+
// not valid
4805+
wrapper.setWidgetValue( QStringLiteral( "cc" ), context );
4806+
QCOMPARE( spy.count(), 3 );
4807+
QVERIFY( !wrapper.widgetValue().isValid() );
4808+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->currentIndex(), -1 );
4809+
break;
4810+
4811+
}
4812+
case QgsProcessingGui::Modeler:
4813+
// invalid schemas permitted
4814+
wrapper.setWidgetValue( QStringLiteral( "cc" ), context );
4815+
QCOMPARE( spy.count(), 3 );
4816+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "cc" ) );
4817+
QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "cc" ) );
4818+
wrapper.setWidgetValue( QStringLiteral( "aa" ), context );
4819+
QCOMPARE( spy.count(), 4 );
4820+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "aa" ) );
4821+
QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "aa" ) );
4822+
break;
4823+
}
4824+
4825+
// make sure things are ok if connection is changed back to nothing
4826+
connWrapper.setWidgetValue( QVariant(), context );
4827+
wrapper.setParentConnectionWrapperValue( &connWrapper );
4828+
4829+
switch ( type )
4830+
{
4831+
case QgsProcessingGui::Standard:
4832+
case QgsProcessingGui::Batch:
4833+
{
4834+
QCOMPARE( spy.count(), 3 );
4835+
break;
4836+
}
4837+
4838+
case QgsProcessingGui::Modeler:
4839+
QCOMPARE( spy.count(), 5 );
4840+
break;
4841+
}
4842+
QVERIFY( !wrapper.widgetValue().isValid() );
4843+
4844+
wrapper.setWidgetValue( QStringLiteral( "qgis_test" ), context );
4845+
switch ( type )
4846+
{
4847+
case QgsProcessingGui::Standard:
4848+
case QgsProcessingGui::Batch:
4849+
{
4850+
QVERIFY( !wrapper.widgetValue().isValid() );
4851+
break;
4852+
}
4853+
4854+
case QgsProcessingGui::Modeler:
4855+
// invalid schemas permitted
4856+
QCOMPARE( spy.count(), 6 );
4857+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "qgis_test" ) );
4858+
QCOMPARE( wrapper.widgetValue().toString(), QStringLiteral( "qgis_test" ) );
4859+
4860+
break;
4861+
}
4862+
delete w;
4863+
4864+
connWrapper.setWidgetValue( QStringLiteral( "aa" ), context );
4865+
4866+
// optional
4867+
QgsProcessingParameterDatabaseSchema param2( QStringLiteral( "schema" ), QStringLiteral( "schema" ), QStringLiteral( "conn" ), QVariant(), true );
4868+
QgsProcessingDatabaseSchemaWidgetWrapper wrapper3( &param2, type );
4869+
w = wrapper3.createWrappedWidget( context );
4870+
4871+
wrapper3.setParentConnectionWrapperValue( &connWrapper );
4872+
4873+
QSignalSpy spy3( &wrapper3, &QgsProcessingDatabaseSchemaWidgetWrapper::widgetValueHasChanged );
4874+
wrapper3.setWidgetValue( QStringLiteral( "qgis_test" ), context );
4875+
QCOMPARE( spy3.count(), 1 );
4876+
QCOMPARE( wrapper3.widgetValue().toString(), QStringLiteral( "qgis_test" ) );
4877+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper3.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "qgis_test" ) );
4878+
wrapper3.setWidgetValue( QStringLiteral( "public" ), context );
4879+
QCOMPARE( spy3.count(), 2 );
4880+
QCOMPARE( wrapper3.widgetValue().toString(), QStringLiteral( "public" ) );
4881+
QCOMPARE( static_cast< QgsDatabaseSchemaComboBox * >( wrapper3.wrappedWidget() )->comboBox()->currentText(), QStringLiteral( "public" ) );
4882+
wrapper3.setWidgetValue( QVariant(), context );
4883+
QCOMPARE( spy3.count(), 3 );
4884+
QVERIFY( !wrapper3.widgetValue().isValid() );
4885+
4886+
delete w;
4887+
QLabel *l = wrapper.createWrappedLabel();
4888+
if ( wrapper.type() != QgsProcessingGui::Batch )
4889+
{
4890+
QVERIFY( l );
4891+
QCOMPARE( l->text(), QStringLiteral( "schema" ) );
4892+
QCOMPARE( l->toolTip(), param.toolTip() );
4893+
delete l;
4894+
}
4895+
else
4896+
{
4897+
QVERIFY( !l );
4898+
}
4899+
4900+
};
4901+
4902+
// standard wrapper
4903+
testWrapper( QgsProcessingGui::Standard );
4904+
4905+
// batch wrapper
4906+
testWrapper( QgsProcessingGui::Batch );
4907+
4908+
// modeler wrapper
4909+
testWrapper( QgsProcessingGui::Modeler );
4910+
4911+
// config widget
4912+
QgsProcessingParameterWidgetContext widgetContext;
4913+
QgsProcessingContext context;
4914+
std::unique_ptr< QgsProcessingParameterDefinitionWidget > widget = qgis::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "databaseschema" ), context, widgetContext );
4915+
std::unique_ptr< QgsProcessingParameterDefinition > def( widget->createParameter( QStringLiteral( "param_name" ) ) );
4916+
QCOMPARE( def->name(), QStringLiteral( "param_name" ) );
4917+
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagOptional ) ); // should default to mandatory
4918+
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced ) );
4919+
QVERIFY( !static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->defaultValue().isValid() );
4920+
4921+
// using a parameter definition as initial values
4922+
QgsProcessingParameterDatabaseSchema schemaParam( QStringLiteral( "n" ), QStringLiteral( "test desc" ), QStringLiteral( "connparam" ), QStringLiteral( "aaa" ), false );
4923+
widget = qgis::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "databaseschema" ), context, widgetContext, &schemaParam );
4924+
def.reset( widget->createParameter( QStringLiteral( "param_name" ) ) );
4925+
QCOMPARE( def->name(), QStringLiteral( "param_name" ) );
4926+
QCOMPARE( def->description(), QStringLiteral( "test desc" ) );
4927+
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagOptional ) );
4928+
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced ) );
4929+
QCOMPARE( static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->defaultValue().toString(), QStringLiteral( "aaa" ) );
4930+
QCOMPARE( static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->parentConnectionParameterName(), QStringLiteral( "connparam" ) );
4931+
schemaParam.setFlags( QgsProcessingParameterDefinition::FlagAdvanced | QgsProcessingParameterDefinition::FlagOptional );
4932+
schemaParam.setDefaultValue( QStringLiteral( "xxx" ) );
4933+
widget = qgis::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "databaseschema" ), context, widgetContext, &schemaParam );
4934+
def.reset( widget->createParameter( QStringLiteral( "param_name" ) ) );
4935+
QCOMPARE( def->name(), QStringLiteral( "param_name" ) );
4936+
QCOMPARE( def->description(), QStringLiteral( "test desc" ) );
4937+
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagOptional );
4938+
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced );
4939+
QCOMPARE( static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->defaultValue().toString(), QStringLiteral( "xxx" ) );
4940+
schemaParam.setDefaultValue( QVariant() );
4941+
widget = qgis::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "databaseschema" ), context, widgetContext, &schemaParam );
4942+
def.reset( widget->createParameter( QStringLiteral( "param_name" ) ) );
4943+
QVERIFY( !static_cast< QgsProcessingParameterDatabaseSchema * >( def.get() )->defaultValue().isValid() );
4944+
#endif
4945+
}
4946+
47464947
void TestProcessingGui::cleanupTempDir()
47474948
{
47484949
QDir tmpDir = QDir( mTempDir );

0 commit comments

Comments
 (0)
Please sign in to comment.