Skip to content

Commit 667718b

Browse files
pvalseccm-kuhn
authored andcommitted
[feature] Editor widget auto config for foreign keys (#3699)
Automatically pick RelationReference widgets for foreign keys.
1 parent 4f8ee78 commit 667718b

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

src/gui/editorwidgets/qgsrelationreferencefactory.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,9 @@ QVariant QgsRelationReferenceFactory::sortValue( QgsVectorLayer* vl, int fieldId
181181
{
182182
return representValue( vl, fieldIdx, config, cache, value );
183183
}
184+
185+
unsigned int QgsRelationReferenceFactory::fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const
186+
{
187+
const QList<QgsRelation> relations = vl->referencingRelations( fieldIdx );
188+
return !relations.isEmpty() ? 21 /*A bit stronger than the range widget*/ : 5;
189+
}

src/gui/editorwidgets/qgsrelationreferencefactory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class GUI_EXPORT QgsRelationReferenceFactory : public QgsEditorWidgetFactory
8888

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

91+
virtual unsigned int fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const override;
92+
9193
private:
9294
QgsAttributeEditorContext mEditorContext;
9395
QgsMapCanvas* mCanvas;

src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ void QgsRelationReferenceWidgetWrapper::initWidget( QWidget* editor )
6262
}
6363
mWidget->setAllowAddFeatures( config( QStringLiteral( "AllowAddFeatures" ), false ).toBool() );
6464

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

6770
// If this widget is already embedded by the same relation, reduce functionality
6871
const QgsAttributeEditorContext* ctx = &context();

tests/src/gui/testqgseditorwidgetregistry.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
#include "qgseditorwidgetregistry.h"
1818
#include "qgseditorwidgetautoconf.h"
19+
#include "qgsproject.h"
20+
#include "qgsrelationmanager.h"
21+
#include "qgsmaplayerregistry.h"
1922

2023

2124
class TestQgsEditorWidgetRegistry: public QObject
@@ -109,6 +112,30 @@ class TestQgsEditorWidgetRegistry: public QObject
109112
QCOMPARE( setup.config().count(), 0 );
110113
}
111114

115+
void referencedLayers()
116+
{
117+
//build two layers
118+
QgsVectorLayer vl1( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=name:string&field=fk:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
119+
QgsVectorLayer vl2( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:string" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
120+
QgsMapLayerRegistry::instance()->addMapLayer( &vl1, false, false );
121+
QgsMapLayerRegistry::instance()->addMapLayer( &vl2, false, false );
122+
123+
//create a relation between them
124+
QgsRelation relation;
125+
relation.setRelationId( QStringLiteral( "vl1->vl2" ) );
126+
relation.setRelationName( QStringLiteral( "vl1->vl2" ) );
127+
relation.setReferencingLayer( vl1.id() );
128+
relation.setReferencedLayer( vl2.id() );
129+
relation.addFieldPair( "fk", "pk" );
130+
QVERIFY( relation.isValid() );
131+
QgsProject::instance()->relationManager()->addRelation( relation );
132+
133+
//check the guessed editor widget type for vl1.fk is RelationReference
134+
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( &vl1, QStringLiteral( "fk" ) );
135+
QCOMPARE( setup.type(), QString( "RelationReference" ) );
136+
QCOMPARE( setup.config(), QgsEditorWidgetConfig() );
137+
}
138+
112139
void typeFromPlugin()
113140
{
114141
const QgsVectorLayer vl( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=special:string" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );

0 commit comments

Comments
 (0)