QGIS API Documentation 3.39.0-Master (d85f3c2a281)
Loading...
Searching...
No Matches
qgsrelationreferenceconfigdlg.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrelationreferenceconfigdlg.cpp
3 --------------------------------------
4 Date : 21.4.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 "qgsfields.h"
19#include "qgsproject.h"
20#include "qgsrelationmanager.h"
21#include "qgsvectorlayer.h"
24#include "qgsfieldconstraints.h"
25
27 : QgsEditorConfigWidget( vl, fieldIdx, parent )
28
29{
30 setupUi( this );
31
32 mFetchLimit->setMaximum( std::numeric_limits<int>::max() );
33
34 connect( mAddFilterButton, &QToolButton::clicked, this, &QgsRelationReferenceConfigDlg::mAddFilterButton_clicked );
35 connect( mRemoveFilterButton, &QToolButton::clicked, this, &QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked );
36
37 mExpressionWidget->registerExpressionContextGenerator( vl );
38
39 connect( mComboRelation, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRelationReferenceConfigDlg::relationChanged );
40
41 const auto constReferencingRelations = vl->referencingRelations( fieldIdx );
42 for ( const QgsRelation &relation : constReferencingRelations )
43 {
44 if ( relation.name().isEmpty() )
45 mComboRelation->addItem( QStringLiteral( "%1 (%2)" ).arg( relation.id(), relation.referencedLayerId() ), relation.id() );
46 else
47 mComboRelation->addItem( QStringLiteral( "%1 (%2)" ).arg( relation.name(), relation.referencedLayerId() ), relation.id() );
48
49 QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mComboRelation->model() );
50 QStandardItem *item = model->item( model->rowCount() - 1 );
51 item->setFlags( relation.type() == Qgis::RelationshipType::Generated
52 ? item->flags() & ~Qt::ItemIsEnabled
53 : item->flags() | Qt::ItemIsEnabled );
54
55 if ( auto *lReferencedLayer = relation.referencedLayer() )
56 {
57 mExpressionWidget->setField( lReferencedLayer->displayExpression() );
58 }
59 }
60
61 connect( mCbxAllowNull, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
62 connect( mCbxShowForm, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
63 connect( mCbxShowOpenFormButton, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
64 connect( mCbxMapIdentification, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
65 connect( mCbxReadOnly, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
66 connect( mComboRelation, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
67 connect( mCbxAllowAddFeatures, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
68 connect( mFilterGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
69 connect( mFilterFieldsList, &QListWidget::itemChanged, this, &QgsEditorConfigWidget::changed );
70 connect( mCbxChainFilters, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
71 connect( mExpressionWidget, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsEditorConfigWidget::changed );
72 connect( mEditExpression, &QAbstractButton::clicked, this, &QgsRelationReferenceConfigDlg::mEditExpression_clicked );
73 connect( mFilterExpression, &QTextEdit::textChanged, this, &QgsEditorConfigWidget::changed );
74 connect( mFetchLimitCheckBox, &QCheckBox::toggled, mFetchLimit, &QSpinBox::setEnabled );
75}
76
77void QgsRelationReferenceConfigDlg::mEditExpression_clicked()
78{
79 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer() );
80 if ( !vl )
81 return;
82
86
87 context.setHighlightedFunctions( QStringList() << QStringLiteral( "current_value" ) << QStringLiteral( "current_parent_value" ) );
88 context.setHighlightedVariables( QStringList() << QStringLiteral( "current_geometry" )
89 << QStringLiteral( "current_feature" )
90 << QStringLiteral( "form_mode" )
91 << QStringLiteral( "current_parent_geometry" )
92 << QStringLiteral( "current_parent_feature" ) );
93
94 QgsExpressionBuilderDialog dlg( vl, mFilterExpression->toPlainText(), this, QStringLiteral( "generic" ), context );
95 dlg.setWindowTitle( tr( "Edit Filter Expression" ) );
96
97 if ( dlg.exec() == QDialog::Accepted )
98 {
99 mFilterExpression->setPlainText( dlg.expressionBuilder()->expressionText() );
100 }
101}
102
103void QgsRelationReferenceConfigDlg::setConfig( const QVariantMap &config )
104{
105 // Only unset allowNull if it was in the config or the default value that was
106 // calculated from the field constraints when the widget was created will be overridden
107 mAllowNullWasSetByConfig = config.contains( QStringLiteral( "AllowNULL" ) );
108 if ( mAllowNullWasSetByConfig )
109 {
110 mCbxAllowNull->setChecked( config.value( QStringLiteral( "AllowNULL" ), false ).toBool() );
111 }
112 mCbxShowForm->setChecked( config.value( QStringLiteral( "ShowForm" ), false ).toBool() );
113 mCbxShowOpenFormButton->setChecked( config.value( QStringLiteral( "ShowOpenFormButton" ), true ).toBool() );
114
115 if ( config.contains( QStringLiteral( "Relation" ) ) )
116 {
117 mComboRelation->setCurrentIndex( mComboRelation->findData( config.value( QStringLiteral( "Relation" ) ).toString() ) );
118 relationChanged( mComboRelation->currentIndex() );
119 }
120
121 mCbxMapIdentification->setChecked( config.value( QStringLiteral( "MapIdentification" ), false ).toBool() );
122 mCbxAllowAddFeatures->setChecked( config.value( QStringLiteral( "AllowAddFeatures" ), false ).toBool() );
123 mCbxReadOnly->setChecked( config.value( QStringLiteral( "ReadOnly" ), false ).toBool() );
124 mFetchLimitCheckBox->setChecked( config.value( QStringLiteral( "FetchLimitActive" ), QgsSettings().value( QStringLiteral( "maxEntriesRelationWidget" ), 100, QgsSettings::Gui ).toInt() > 0 ).toBool() );
125 mFetchLimit->setValue( config.value( QStringLiteral( "FetchLimitNumber" ), QgsSettings().value( QStringLiteral( "maxEntriesRelationWidget" ), 100, QgsSettings::Gui ) ).toInt() );
126 mFilterExpression->setPlainText( config.value( QStringLiteral( "FilterExpression" ) ).toString() );
127
128 if ( config.contains( QStringLiteral( "FilterFields" ) ) )
129 {
130 mFilterGroupBox->setChecked( true );
131 const auto constToStringList = config.value( "FilterFields" ).toStringList();
132 for ( const QString &fld : constToStringList )
133 {
134 addFilterField( fld );
135 }
136
137 mCbxChainFilters->setChecked( config.value( QStringLiteral( "ChainFilters" ) ).toBool() );
138 }
139}
140
141void QgsRelationReferenceConfigDlg::relationChanged( int idx )
142{
143 const QString relName = mComboRelation->itemData( idx ).toString();
144 const QgsRelation rel = QgsProject::instance()->relationManager()->relation( relName );
145
146 mReferencedLayer = rel.referencedLayer();
147 mExpressionWidget->setLayer( mReferencedLayer ); // set even if 0
148 if ( mReferencedLayer )
149 {
150 mExpressionWidget->setField( mReferencedLayer->displayExpression() );
151 mCbxMapIdentification->setEnabled( mReferencedLayer->isSpatial() );
152 }
153
154 // If AllowNULL is not set in the config, provide a default value based on the
155 // constraints of the referencing fields
156 if ( ! mAllowNullWasSetByConfig )
157 {
158 mCbxAllowNull->setChecked( rel.referencingFieldsAllowNull() );
159 }
160
161 loadFields();
162}
163
164void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
165{
166 const auto constSelectedItems = mAvailableFieldsList->selectedItems();
167 for ( QListWidgetItem *item : constSelectedItems )
168 {
169 addFilterField( item );
170 }
171}
172
173void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
174{
175 const auto constSelectedItems = mFilterFieldsList->selectedItems();
176 for ( QListWidgetItem *item : constSelectedItems )
177 {
178 mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
179 mAvailableFieldsList->addItem( item );
180 }
181}
182
184{
185 QVariantMap myConfig;
186 myConfig.insert( QStringLiteral( "AllowNULL" ), mCbxAllowNull->isChecked() );
187 myConfig.insert( QStringLiteral( "ShowForm" ), mCbxShowForm->isChecked() );
188 myConfig.insert( QStringLiteral( "ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
189 myConfig.insert( QStringLiteral( "MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
190 myConfig.insert( QStringLiteral( "ReadOnly" ), mCbxReadOnly->isChecked() );
191 myConfig.insert( QStringLiteral( "Relation" ), mComboRelation->currentData() );
192 myConfig.insert( QStringLiteral( "AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
193 myConfig.insert( QStringLiteral( "FetchLimitActive" ), mFetchLimitCheckBox->isChecked() );
194 myConfig.insert( QStringLiteral( "FetchLimitNumber" ), mFetchLimit->value() );
195
196 if ( mFilterGroupBox->isChecked() )
197 {
198 QStringList filterFields;
199 filterFields.reserve( mFilterFieldsList->count() );
200 for ( int i = 0; i < mFilterFieldsList->count(); i++ )
201 {
202 filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
203 }
204 myConfig.insert( QStringLiteral( "FilterFields" ), filterFields );
205
206 myConfig.insert( QStringLiteral( "ChainFilters" ), mCbxChainFilters->isChecked() );
207 myConfig.insert( QStringLiteral( "FilterExpression" ), mFilterExpression->toPlainText() );
208 }
209
210 if ( mReferencedLayer )
211 {
212 // Store referenced layer data source and provider
213 myConfig.insert( QStringLiteral( "ReferencedLayerDataSource" ), mReferencedLayer->publicSource() );
214 myConfig.insert( QStringLiteral( "ReferencedLayerProviderKey" ), mReferencedLayer->providerType() );
215 myConfig.insert( QStringLiteral( "ReferencedLayerId" ), mReferencedLayer->id() );
216 myConfig.insert( QStringLiteral( "ReferencedLayerName" ), mReferencedLayer->name() );
217 mReferencedLayer->setDisplayExpression( mExpressionWidget->currentField() );
218 }
219
220 return myConfig;
221}
222
223void QgsRelationReferenceConfigDlg::loadFields()
224{
225 mAvailableFieldsList->clear();
226 mFilterFieldsList->clear();
227
228 if ( mReferencedLayer )
229 {
230 QgsVectorLayer *l = mReferencedLayer;
231 const QgsFields &flds = l->fields();
232 for ( int i = 0; i < flds.count(); i++ )
233 {
234 mAvailableFieldsList->addItem( flds.at( i ).displayName() );
235 mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.at( i ).name() );
236 }
237 }
238}
239
240void QgsRelationReferenceConfigDlg::addFilterField( const QString &field )
241{
242 for ( int i = 0; i < mAvailableFieldsList->count(); i++ )
243 {
244 if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() == field )
245 {
246 addFilterField( mAvailableFieldsList->item( i ) );
247 break;
248 }
249 }
250}
251
252void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
253{
254 mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
255 mFilterFieldsList->addItem( item );
256}
257
258int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
259{
260 QListWidget *lw = item->listWidget();
261
262 for ( int i = 0; i < lw->count(); i++ )
263 {
264 if ( lw->item( i ) == item )
265 return i;
266 }
267
268 return -1;
269}
@ Generated
A generated relation is a child of a polymorphic relation.
This class should be subclassed for every configurable editor widget type.
int field()
Returns the field for which this configuration widget applies.
QgsVectorLayer * layer()
Returns the layer for which this configuration widget applies.
void changed()
Emitted when the configuration of the widget is changed.
A generic dialog for building expression strings.
static QgsExpressionContextScope * parentFormScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current parent attribute form/tab...
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table form...
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The QgsFieldExpressionWidget class creates a widget to choose fields and edit expressions It contains...
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
QString name
Definition qgsfield.h:62
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:94
Container of fields for a vector layer.
Definition qgsfields.h:46
int count
Definition qgsfields.h:50
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
QString name
Definition qgsmaplayer.h:80
QString providerType() const
Returns the provider type (provider key) for this layer.
QString id
Definition qgsmaplayer.h:79
QString publicSource(bool hidePassword=false) const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
QgsRelationManager * relationManager
Definition qgsproject.h:117
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
QgsRelationReferenceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
QVariantMap config() override
Create a configuration from the current GUI state.
Represents a relationship between two vector layers.
Definition qgsrelation.h:44
QgsVectorLayer * referencedLayer
Definition qgsrelation.h:49
bool referencingFieldsAllowNull() const
Returns true if none of the referencing fields has a NOT NULL constraint.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
Represents a vector layer which manages a vector based data sets.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QList< QgsRelation > referencingRelations(int idx) const
Returns the layer's relations, where the foreign key is on this layer.
void setDisplayExpression(const QString &displayExpression)
Set the preview expression, used to create a human readable preview string.
QString displayExpression