Skip to content

Commit

Permalink
[feature] Editor widget auto config for foreign keys (#3699)
Browse files Browse the repository at this point in the history
Automatically pick RelationReference widgets for foreign keys.
  • Loading branch information
pvalsecc authored and m-kuhn committed Nov 1, 2016
1 parent 4f8ee78 commit 667718b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencefactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,9 @@ QVariant QgsRelationReferenceFactory::sortValue( QgsVectorLayer* vl, int fieldId
{
return representValue( vl, fieldIdx, config, cache, value );
}

unsigned int QgsRelationReferenceFactory::fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const
{
const QList<QgsRelation> relations = vl->referencingRelations( fieldIdx );
return !relations.isEmpty() ? 21 /*A bit stronger than the range widget*/ : 5;
}
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencefactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class GUI_EXPORT QgsRelationReferenceFactory : public QgsEditorWidgetFactory

virtual QHash<const char *, int> supportedWidgetTypes() override;

virtual unsigned int fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const override;

private:
QgsAttributeEditorContext mEditorContext;
QgsMapCanvas* mCanvas;
Expand Down
5 changes: 4 additions & 1 deletion src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ void QgsRelationReferenceWidgetWrapper::initWidget( QWidget* editor )
}
mWidget->setAllowAddFeatures( config( QStringLiteral( "AllowAddFeatures" ), false ).toBool() );

QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( QStringLiteral( "Relation" ) ).toString() );
const QVariant relationName = config( QStringLiteral( "Relation" ) );
QgsRelation relation = relationName.isValid() ?
QgsProject::instance()->relationManager()->relation( relationName.toString() ) :
layer()->referencingRelations( fieldIdx() )[0];

// If this widget is already embedded by the same relation, reduce functionality
const QgsAttributeEditorContext* ctx = &context();
Expand Down
27 changes: 27 additions & 0 deletions tests/src/gui/testqgseditorwidgetregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#include "qgseditorwidgetregistry.h"
#include "qgseditorwidgetautoconf.h"
#include "qgsproject.h"
#include "qgsrelationmanager.h"
#include "qgsmaplayerregistry.h"


class TestQgsEditorWidgetRegistry: public QObject
Expand Down Expand Up @@ -109,6 +112,30 @@ class TestQgsEditorWidgetRegistry: public QObject
QCOMPARE( setup.config().count(), 0 );
}

void referencedLayers()
{
//build two layers
QgsVectorLayer vl1( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=name:string&field=fk:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
QgsVectorLayer vl2( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:string" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
QgsMapLayerRegistry::instance()->addMapLayer( &vl1, false, false );
QgsMapLayerRegistry::instance()->addMapLayer( &vl2, false, false );

//create a relation between them
QgsRelation relation;
relation.setRelationId( QStringLiteral( "vl1->vl2" ) );
relation.setRelationName( QStringLiteral( "vl1->vl2" ) );
relation.setReferencingLayer( vl1.id() );
relation.setReferencedLayer( vl2.id() );
relation.addFieldPair( "fk", "pk" );
QVERIFY( relation.isValid() );
QgsProject::instance()->relationManager()->addRelation( relation );

//check the guessed editor widget type for vl1.fk is RelationReference
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( &vl1, QStringLiteral( "fk" ) );
QCOMPARE( setup.type(), QString( "RelationReference" ) );
QCOMPARE( setup.config(), QgsEditorWidgetConfig() );
}

void typeFromPlugin()
{
const QgsVectorLayer vl( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=special:string" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
Expand Down

0 comments on commit 667718b

Please sign in to comment.