Merge pull request #33103 from elpaso/relation-reference-restore

Relation-reference widget: load dependencies
This commit is contained in:
Alessandro Pasotti 2019-11-28 10:25:55 +01:00 committed by GitHub
commit d89745aec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 234 additions and 0 deletions

View File

@ -169,6 +169,62 @@ Returns the current relation, which might be invalid
Set the current form feature (from the referencing layer)
.. versionadded:: 3.10
%End
QString referencedLayerDataSource() const;
%Docstring
Returns the public data source of the referenced layer
.. versionadded:: 3.12
%End
void setReferencedLayerDataSource( const QString &referencedLayerDataSource );
%Docstring
Set the public data source of the referenced layer to ``referencedLayerDataSource``
.. versionadded:: 3.12
%End
QString referencedLayerProviderKey() const;
%Docstring
Returns the data provider key of the referenced layer
.. versionadded:: 3.12
%End
void setReferencedLayerProviderKey( const QString &referencedLayerProviderKey );
%Docstring
Set the data provider key of the referenced layer to ``referencedLayerProviderKey``
.. versionadded:: 3.12
%End
QString referencedLayerId() const;
%Docstring
Returns the id of the referenced layer
.. versionadded:: 3.12
%End
void setReferencedLayerId( const QString &referencedLayerId );
%Docstring
Set the id of the referenced layer to ``referencedLayerId``
.. versionadded:: 3.12
%End
QString referencedLayerName() const;
%Docstring
Returns the name of the referenced layer
.. versionadded:: 3.12
%End
void setReferencedLayerName( const QString &referencedLayerName );
%Docstring
Set the name of the referenced layer to ``referencedLayerName``
.. versionadded:: 3.12
%End
public slots:

View File

@ -153,6 +153,11 @@ QVariantMap QgsRelationReferenceConfigDlg::config()
if ( mReferencedLayer )
{
// Store referenced layer data source and provider
myConfig.insert( QStringLiteral( "ReferencedLayerDataSource" ), mReferencedLayer->publicSource() );
myConfig.insert( QStringLiteral( "ReferencedLayerProviderKey" ), mReferencedLayer->providerType() );
myConfig.insert( QStringLiteral( "ReferencedLayerId" ), mReferencedLayer->id() );
myConfig.insert( QStringLiteral( "ReferencedLayerName" ), mReferencedLayer->name() );
mReferencedLayer->setDisplayExpression( mExpressionWidget->currentField() );
}

View File

@ -211,6 +211,10 @@ void QgsRelationReferenceWidget::setRelation( const QgsRelation &relation, bool
if ( relation.isValid() )
{
mReferencedLayerId = relation.referencedLayerId();
mReferencedLayerName = relation.referencedLayer()->name();
setReferencedLayerDataSource( relation.referencedLayer()->publicSource() );
mReferencedLayerProviderKey = relation.referencedLayer()->providerType();
mInvalidLabel->hide();
mRelation = relation;
@ -1086,6 +1090,47 @@ void QgsRelationReferenceWidget::emitForeignKeysChanged( const QVariantList &for
emit foreignKeysChanged( foreignKeys );
}
QString QgsRelationReferenceWidget::referencedLayerName() const
{
return mReferencedLayerName;
}
void QgsRelationReferenceWidget::setReferencedLayerName( const QString &relationLayerName )
{
mReferencedLayerName = relationLayerName;
}
QString QgsRelationReferenceWidget::referencedLayerId() const
{
return mReferencedLayerId;
}
void QgsRelationReferenceWidget::setReferencedLayerId( const QString &relationLayerId )
{
mReferencedLayerId = relationLayerId;
}
QString QgsRelationReferenceWidget::referencedLayerProviderKey() const
{
return mReferencedLayerProviderKey;
}
void QgsRelationReferenceWidget::setReferencedLayerProviderKey( const QString &relationProviderKey )
{
mReferencedLayerProviderKey = relationProviderKey;
}
QString QgsRelationReferenceWidget::referencedLayerDataSource() const
{
return mReferencedLayerDataSource;
}
void QgsRelationReferenceWidget::setReferencedLayerDataSource( const QString &relationDataSource )
{
const QgsPathResolver resolver { QgsProject::instance()->pathResolver() };
mReferencedLayerDataSource = resolver.writePath( relationDataSource );
}
void QgsRelationReferenceWidget::setFormFeature( const QgsFeature &formFeature )
{
mFormFeature = formFeature;

View File

@ -191,6 +191,54 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
*/
void setFormFeature( const QgsFeature &formFeature );
/**
* Returns the public data source of the referenced layer
* \since QGIS 3.12
*/
QString referencedLayerDataSource() const;
/**
* Set the public data source of the referenced layer to \a referencedLayerDataSource
* \since QGIS 3.12
*/
void setReferencedLayerDataSource( const QString &referencedLayerDataSource );
/**
* Returns the data provider key of the referenced layer
* \since QGIS 3.12
*/
QString referencedLayerProviderKey() const;
/**
* Set the data provider key of the referenced layer to \a referencedLayerProviderKey
* \since QGIS 3.12
*/
void setReferencedLayerProviderKey( const QString &referencedLayerProviderKey );
/**
* Returns the id of the referenced layer
* \since QGIS 3.12
*/
QString referencedLayerId() const;
/**
* Set the id of the referenced layer to \a referencedLayerId
* \since QGIS 3.12
*/
void setReferencedLayerId( const QString &referencedLayerId );
/**
* Returns the name of the referenced layer
* \since QGIS 3.12
*/
QString referencedLayerName() const;
/**
* Set the name of the referenced layer to \a referencedLayerName
* \since QGIS 3.12
*/
void setReferencedLayerName( const QString &referencedLayerName );
public slots:
//! open the form of the related feature in a new dialog
void openForm();
@ -282,6 +330,10 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
bool mOpenFormButtonVisible = true;
bool mChainFilters = false;
bool mAllowAddFeatures = false;
QString mReferencedLayerId;
QString mReferencedLayerName;
QString mReferencedLayerDataSource;
QString mReferencedLayerProviderKey;
// UI
QVBoxLayout *mTopLayout = nullptr;

View File

@ -67,6 +67,12 @@ void QgsRelationReferenceWidgetWrapper::initWidget( QWidget *editor )
const QVariant relationName = config( QStringLiteral( "Relation" ) );
// Store relation data source and provider key
mWidget->setReferencedLayerDataSource( config( QStringLiteral( "ReferencedLayerDataSource" ) ).toString() );
mWidget->setReferencedLayerProviderKey( config( QStringLiteral( "ReferencedLayerProviderKey" ) ).toString() );
mWidget->setReferencedLayerId( config( QStringLiteral( "ReferencedLayerId" ) ).toString() );
mWidget->setReferencedLayerName( config( QStringLiteral( "ReferencedLayerName" ) ).toString() );
QgsRelation relation; // invalid relation by default
if ( relationName.isValid() )
relation = QgsProject::instance()->relationManager()->relation( relationName.toString() );
@ -176,6 +182,21 @@ QStringList QgsRelationReferenceWidgetWrapper::additionalFields() const
return fields;
}
QList<QgsVectorLayerRef> QgsRelationReferenceWidgetWrapper::layerDependencies() const
{
QList<QgsVectorLayerRef> result;
if ( mWidget )
{
result.append(
QgsVectorLayerRef(
mWidget->referencedLayerId(),
mWidget->referencedLayerName(),
mWidget->referencedLayerDataSource(),
mWidget->referencedLayerProviderKey() ) );
}
return result;
}
void QgsRelationReferenceWidgetWrapper::updateValues( const QVariant &val, const QVariantList &additionalValues )
{
if ( !mWidget || ( !mIndeterminateState && val == value() && val.isNull() == value().isNull() ) )

View File

@ -69,6 +69,7 @@ class GUI_EXPORT QgsRelationReferenceWidgetWrapper : public QgsEditorWidgetWrapp
protected:
void updateConstraintWidgetStatus() override;
QList<QgsVectorLayerRef> layerDependencies() const override SIP_SKIP;
private:
void updateValues( const QVariant &val, const QVariantList &additionalValues = QVariantList() ) override;
@ -77,6 +78,9 @@ class GUI_EXPORT QgsRelationReferenceWidgetWrapper : public QgsEditorWidgetWrapp
QgsMapCanvas *mCanvas = nullptr;
QgsMessageBar *mMessageBar = nullptr;
bool mIndeterminateState;
friend class TestQgsRelationReferenceWidget;
};
#endif // QGSRELATIONREFERENCEWIDGETWRAPPER_H

View File

@ -53,6 +53,7 @@ class TestQgsRelationReferenceWidget : public QObject
void testIdentifyOnMap();
void testAddEntry();
void testAddEntryNoGeom();
void testDependencies(); // Test relation datasource, id etc. config storage
private:
std::unique_ptr<QgsVectorLayer> mLayer1;
@ -472,5 +473,55 @@ void TestQgsRelationReferenceWidget::testAddEntryNoGeom()
QCOMPARE( w.mComboBox->identifierValues().at( 0 ).toInt(), 13 );
}
void TestQgsRelationReferenceWidget::testDependencies()
{
QgsVectorLayer mLayer1( QStringLiteral( "Point?crs=epsg:3111&field=pk:int&field=fk:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &mLayer1, false, false );
QgsVectorLayer mLayer2( QStringLiteral( "None?field=pk:int&field=material:string" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &mLayer2, false, false );
// create relation
QgsRelation mRelation;
mRelation.setId( QStringLiteral( "vl1.vl2" ) );
mRelation.setName( QStringLiteral( "vl1.vl2" ) );
mRelation.setReferencingLayer( mLayer1.id() );
mRelation.setReferencedLayer( mLayer2.id() );
mRelation.addFieldPair( QStringLiteral( "fk" ), QStringLiteral( "pk" ) );
QVERIFY( mRelation.isValid() );
QgsProject::instance()->relationManager()->addRelation( mRelation );
// check that a new added entry in referenced layer populate correctly
// widget config
QgsMapCanvas canvas;
QgsRelationReferenceWidget w( &canvas );
w.setRelation( mRelation, true );
w.init();
QCOMPARE( w.referencedLayerId(), mLayer2.id() );
QCOMPARE( w.referencedLayerName(), mLayer2.name() );
QCOMPARE( w.referencedLayerDataSource(), mLayer2.publicSource() );
QCOMPARE( w.referencedLayerProviderKey(), mLayer2.providerType() );
// Test dependencies
QgsRelationReferenceWidget editor( new QWidget() );
QgsRelationReferenceWidgetWrapper ww( &mLayer1, 10, &editor, &canvas, nullptr, nullptr );
ww.setConfig(
{
{ QStringLiteral( "ReferencedLayerDataSource" ), mLayer2.publicSource() },
{ QStringLiteral( "ReferencedLayerProviderKey" ), mLayer2.providerType() },
{ QStringLiteral( "ReferencedLayerId" ), mLayer2.id() },
{ QStringLiteral( "ReferencedLayerName" ), mLayer2.name() },
} );
ww.initWidget( &editor );
const QList<QgsVectorLayerRef> dependencies = ww.layerDependencies();
QVERIFY( dependencies.count() == 1 );
const QgsVectorLayerRef dependency = dependencies.first();
QCOMPARE( dependency.layerId, mLayer2.id() );
QCOMPARE( dependency.name, mLayer2.name() );
QCOMPARE( dependency.provider, mLayer2.providerType() );
QCOMPARE( dependency.source, mLayer2.publicSource() );
}
QGSTEST_MAIN( TestQgsRelationReferenceWidget )
#include "testqgsrelationreferencewidget.moc"