QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
31QgsEditorWidgetWrapper *QgsRelationReferenceFactory::create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const
32{
33 return new QgsRelationReferenceWidgetWrapper( vl, fieldIdx, editor, mCanvas, mMessageBar, parent );
34}
35
37{
38 return new QgsRelationReferenceSearchWidgetWrapper( vl, fieldIdx, mCanvas, parent );
39}
40
42{
43 return new QgsRelationReferenceConfigDlg( vl, fieldIdx, parent );
44}
45
47{
48 QHash<const char *, int> map = QHash<const char *, int>();
49 map.insert( QgsRelationReferenceWidget::staticMetaObject.className(), 10 );
50 return map;
51}
52
53unsigned int QgsRelationReferenceFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
54{
55 bool isFirstFieldInRelation = false;
56 const QList<QgsRelation> relations = vl->referencingRelations( fieldIdx );
57 for ( const QgsRelation &rel : relations )
58 {
59 switch ( rel.type() )
60 {
62 {
63 // For composite foreign keys, only the first referencing field should
64 // get a RelationReference widget. The other fields are managed by the
65 // widget internally and should not show a separate RelationReference
66 // widget (see https://github.com/qgis/QGIS/issues/32048).
67 const QList<QgsRelation::FieldPair> fieldPairs = rel.fieldPairs();
68 if ( !fieldPairs.isEmpty() )
69 {
70 const int firstReferencingFieldIdx = vl->fields().lookupField( fieldPairs.at( 0 ).referencingField() );
71 if ( firstReferencingFieldIdx == fieldIdx )
72 {
73 isFirstFieldInRelation = true;
74 }
75 }
76 break;
77 }
78
80 break;
81 }
82 }
83 // generated relations should not be used for relation reference widget
84 // For composite foreign keys, only the first field gets the RelationReference widget
85 return isFirstFieldInRelation ? 21 /*A bit stronger than the range widget*/ : 5;
86}
@ Generated
A generated relation is a child of a polymorphic relation.
Definition qgis.h:4537
@ Normal
A normal relation.
Definition qgis.h:4536
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.