Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Relation-reference widget: load dependencies
Same behavior of value-relation widget.

Funded by: ARPA Piemonte
  • Loading branch information
elpaso committed Nov 27, 2019
1 parent d953f9e commit 37c42c3
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 0 deletions.
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferenceconfigdlg.cpp
Expand Up @@ -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() );
}

Expand Down
44 changes: 44 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -211,6 +211,10 @@ void QgsRelationReferenceWidget::setRelation( const QgsRelation &relation, bool

if ( relation.isValid() )
{
mReferencedLayerId = relation.referencedLayerId();
mReferencedLayerName = relation.referencedLayer()->name();
mReferencedLayerDataSource = relation.referencedLayer()->publicSource();
mReferencedLayerProviderKey = relation.referencedLayer()->providerType();
mInvalidLabel->hide();

mRelation = relation;
Expand Down Expand Up @@ -1086,6 +1090,46 @@ 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 )
{
mReferencedLayerDataSource = relationDataSource;
}

void QgsRelationReferenceWidget::setFormFeature( const QgsFeature &formFeature )
{
mFormFeature = formFeature;
Expand Down
52 changes: 52 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidget.h
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.cpp
Expand Up @@ -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() );
Expand Down Expand Up @@ -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() ) )
Expand Down
4 changes: 4 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.h
Expand Up @@ -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;
Expand All @@ -77,6 +78,9 @@ class GUI_EXPORT QgsRelationReferenceWidgetWrapper : public QgsEditorWidgetWrapp
QgsMapCanvas *mCanvas = nullptr;
QgsMessageBar *mMessageBar = nullptr;
bool mIndeterminateState;

friend class TestQgsRelationReferenceWidget;

};

#endif // QGSRELATIONREFERENCEWIDGETWRAPPER_H
51 changes: 51 additions & 0 deletions tests/src/gui/testqgsrelationreferencewidget.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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"

0 comments on commit 37c42c3

Please sign in to comment.