QGIS API Documentation 3.99.0-Master (0c964c3d988)
Loading...
Searching...
No Matches
qgsrelationreferencefactory.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrelationreferencefactory.cpp
3 --------------------------------------
4 Date : 29.5.2013
5 Copyright : (C) 2013 Matthias Kuhn
6 Email : matthias at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgsproject.h"
19#include "qgsrelation.h"
24
27 , mCanvas( canvas )
28 , mMessageBar( messageBar )
29{
30}
31
32QgsEditorWidgetWrapper *QgsRelationReferenceFactory::create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const
33{
34 return new QgsRelationReferenceWidgetWrapper( vl, fieldIdx, editor, mCanvas, mMessageBar, parent );
35}
36
38{
39 return new QgsRelationReferenceSearchWidgetWrapper( vl, fieldIdx, mCanvas, parent );
40}
41
43{
44 return new QgsRelationReferenceConfigDlg( vl, fieldIdx, parent );
45}
46
48{
49 QHash<const char *, int> map = QHash<const char *, int>();
50 map.insert( QgsRelationReferenceWidget::staticMetaObject.className(), 10 );
51 return map;
52}
53
54unsigned int QgsRelationReferenceFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
55{
56 bool isFirstFieldInRelation = false;
57 const QList<QgsRelation> relations = vl->referencingRelations( fieldIdx );
58 for ( const QgsRelation &rel : relations )
59 {
60 switch ( rel.type() )
61 {
63 {
64 // For composite foreign keys, only the first referencing field should
65 // get a RelationReference widget. The other fields are managed by the
66 // widget internally and should not show a separate RelationReference
67 // widget (see https://github.com/qgis/QGIS/issues/32048).
68 const QList<QgsRelation::FieldPair> fieldPairs = rel.fieldPairs();
69 if ( !fieldPairs.isEmpty() )
70 {
71 const int firstReferencingFieldIdx = vl->fields().lookupField( fieldPairs.at( 0 ).referencingField() );
72 if ( firstReferencingFieldIdx == fieldIdx )
73 {
74 isFirstFieldInRelation = true;
75 }
76 }
77 break;
78 }
79
81 break;
82 }
83 }
84 // generated relations should not be used for relation reference widget
85 // For composite foreign keys, only the first field gets the RelationReference widget
86 return isFirstFieldInRelation ? 21 /*A bit stronger than the range widget*/ : 5;
87}
@ Generated
A generated relation is a child of a polymorphic relation.
Definition qgis.h:4502
@ Normal
A normal relation.
Definition qgis.h:4501
Base class for widgets which configure editor widget types.
QgsEditorWidgetFactory(const QString &name, const QIcon &icon=QIcon())
Constructor.
QIcon icon() const
Returns the icon of this widget type.
QString name() const
Returns the human readable identifier name of this widget type.
Manages an editor widget.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Map canvas is a class for displaying all GIS data types on a canvas.
A bar for displaying non-blocking messages to the user.
Configuration widget for relation reference widgets.
QgsEditorConfigWidget * configWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
Override this in your implementation.
QgsRelationReferenceFactory(const QString &name, QgsMapCanvas *canvas, QgsMessageBar *messageBar, const QIcon &icon=QIcon())
Constructor for QgsRelationReferenceFactory, where name is a human-readable name for the factory and ...
QgsEditorWidgetWrapper * create(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent) const override
Override this in your implementation.
QgsSearchWidgetWrapper * createSearchWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
By default a simple QgsFilterLineEdit is returned as search widget.
unsigned int fieldScore(const QgsVectorLayer *vl, int fieldIdx) const override
This method allows disabling this editor widget type for a certain field.
QHash< const char *, int > supportedWidgetTypes() override
Returns a list of widget types which this editor widget supports.
Represents a relationship between two vector layers.
Definition qgsrelation.h:42
Shows a search widget on a filter form.
Represents a vector layer which manages a vector based dataset.
QList< QgsRelation > referencingRelations(int idx) const
Returns the layer's relations, where the foreign key is on this layer.