Skip to content
Permalink
Browse files

[feature] Editor widget auto config for foreign keys (#3699)

Automatically pick RelationReference widgets for foreign keys.
  • Loading branch information
pvalsecc authored and m-kuhn committed Nov 1, 2016
1 parent 4f8ee78 commit 667718bc89e0bf54f84d0d66d117ae7052cd5c0c
@@ -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;
}
@@ -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;
@@ -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();
@@ -16,6 +16,9 @@

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


class TestQgsEditorWidgetRegistry: public QObject
@@ -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" ) );

0 comments on commit 667718b

Please sign in to comment.
You can’t perform that action at this time.