QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#include "moc_qgsrelationreferenceconfigdlg.cpp"
18
19#include "qgsfields.h"
20#include "qgsproject.h"
21#include "qgsrelationmanager.h"
22#include "qgsvectorlayer.h"
25#include "qgsfieldconstraints.h"
26
28 : QgsEditorConfigWidget( vl, fieldIdx, parent )
29
30{
31 setupUi( this );
32
33 mFetchLimit->setMaximum( std::numeric_limits<int>::max() );
34
35 connect( mAddFilterButton, &QToolButton::clicked, this, &QgsRelationReferenceConfigDlg::mAddFilterButton_clicked );
36 connect( mRemoveFilterButton, &QToolButton::clicked, this, &QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked );
37
38 mExpressionWidget->registerExpressionContextGenerator( vl );
39
40 connect( mComboRelation, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsRelationReferenceConfigDlg::relationChanged );
41
42 const auto constReferencingRelations = vl->referencingRelations( fieldIdx );
43 for ( const QgsRelation &relation : constReferencingRelations )
44 {
45 if ( relation.name().isEmpty() )
46 mComboRelation->addItem( QStringLiteral( "%1 (%2)" ).arg( relation.id(), relation.referencedLayerId() ), relation.id() );
47 else
48 mComboRelation->addItem( QStringLiteral( "%1 (%2)" ).arg( relation.name(), relation.referencedLayerId() ), relation.id() );
49
50 QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mComboRelation->model() );
51 QStandardItem *item = model->item( model->rowCount() - 1 );
52 item->setFlags( relation.type() == Qgis::RelationshipType::Generated ? item->flags() & ~Qt::ItemIsEnabled : item->flags() | Qt::ItemIsEnabled );
53
54 if ( auto *lReferencedLayer = relation.referencedLayer() )
55 {
56 mExpressionWidget->setField( lReferencedLayer->displayExpression() );
57 }
58 }
59
60 connect( mCbxAllowNull, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
61 connect( mCbxShowForm, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
62 connect( mCbxShowOpenFormButton, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
63 connect( mCbxMapIdentification, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
64 connect( mCbxReadOnly, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
65 connect( mComboRelation, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
66 connect( mCbxAllowAddFeatures, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
67 connect( mFilterGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
68 connect( mFilterFieldsList, &QListWidget::itemChanged, this, &QgsEditorConfigWidget::changed );
69 connect( mCbxChainFilters, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
70 connect( mExpressionWidget, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsEditorConfigWidget::changed );
71 connect( mEditExpression, &QAbstractButton::clicked, this, &QgsRelationReferenceConfigDlg::mEditExpression_clicked );
72 connect( mFilterExpression, &QTextEdit::textChanged, this, &QgsEditorConfigWidget::changed );
73 connect( mFetchLimitCheckBox, &QCheckBox::toggled, mFetchLimit, &QSpinBox::setEnabled );
74}
75
76void QgsRelationReferenceConfigDlg::mEditExpression_clicked()
77{
78 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer() );
79 if ( !vl )
80 return;
81
85
86 context.setHighlightedFunctions( QStringList() << QStringLiteral( "current_value" ) << QStringLiteral( "current_parent_value" ) );
87 context.setHighlightedVariables( QStringList() << QStringLiteral( "current_geometry" ) << QStringLiteral( "current_feature" ) << QStringLiteral( "form_mode" ) << QStringLiteral( "current_parent_geometry" ) << QStringLiteral( "current_parent_feature" ) );
88
89 QgsExpressionBuilderDialog dlg( vl, mFilterExpression->toPlainText(), this, QStringLiteral( "generic" ), context );
90 dlg.setWindowTitle( tr( "Edit Filter Expression" ) );
91
92 if ( dlg.exec() == QDialog::Accepted )
93 {
94 mFilterExpression->setPlainText( dlg.expressionBuilder()->expressionText() );
95 }
96}
97
98void QgsRelationReferenceConfigDlg::setConfig( const QVariantMap &config )
99{
100 // Only unset allowNull if it was in the config or the default value that was
101 // calculated from the field constraints when the widget was created will be overridden
102 mAllowNullWasSetByConfig = config.contains( QStringLiteral( "AllowNULL" ) );
103 if ( mAllowNullWasSetByConfig )
104 {
105 mCbxAllowNull->setChecked( config.value( QStringLiteral( "AllowNULL" ), false ).toBool() );
106 }
107 mCbxShowForm->setChecked( config.value( QStringLiteral( "ShowForm" ), false ).toBool() );
108 mCbxShowOpenFormButton->setChecked( config.value( QStringLiteral( "ShowOpenFormButton" ), true ).toBool() );
109
110 if ( config.contains( QStringLiteral( "Relation" ) ) )
111 {
112 mComboRelation->setCurrentIndex( mComboRelation->findData( config.value( QStringLiteral( "Relation" ) ).toString() ) );
113 relationChanged( mComboRelation->currentIndex() );
114 }
115
116 mCbxMapIdentification->setChecked( config.value( QStringLiteral( "MapIdentification" ), false ).toBool() );
117 mCbxAllowAddFeatures->setChecked( config.value( QStringLiteral( "AllowAddFeatures" ), false ).toBool() );
118 mCbxReadOnly->setChecked( config.value( QStringLiteral( "ReadOnly" ), false ).toBool() );
119 mFetchLimitCheckBox->setChecked( config.value( QStringLiteral( "FetchLimitActive" ), QgsSettings().value( QStringLiteral( "maxEntriesRelationWidget" ), 100, QgsSettings::Gui ).toInt() > 0 ).toBool() );
120 mFetchLimit->setValue( config.value( QStringLiteral( "FetchLimitNumber" ), QgsSettings().value( QStringLiteral( "maxEntriesRelationWidget" ), 100, QgsSettings::Gui ) ).toInt() );
121 mFilterExpression->setPlainText( config.value( QStringLiteral( "FilterExpression" ) ).toString() );
122
123 if ( config.contains( QStringLiteral( "FilterFields" ) ) )
124 {
125 mFilterGroupBox->setChecked( true );
126 const auto constToStringList = config.value( "FilterFields" ).toStringList();
127 for ( const QString &fld : constToStringList )
128 {
129 addFilterField( fld );
130 }
131
132 mCbxChainFilters->setChecked( config.value( QStringLiteral( "ChainFilters" ) ).toBool() );
133 }
134}
135
136void QgsRelationReferenceConfigDlg::relationChanged( int idx )
137{
138 const QString relName = mComboRelation->itemData( idx ).toString();
139 const QgsRelation rel = QgsProject::instance()->relationManager()->relation( relName );
140
141 mReferencedLayer = rel.referencedLayer();
142 mExpressionWidget->setLayer( mReferencedLayer ); // set even if 0
143 if ( mReferencedLayer )
144 {
145 mExpressionWidget->setField( mReferencedLayer->displayExpression() );
146 mCbxMapIdentification->setEnabled( mReferencedLayer->isSpatial() );
147 }
148
149 // If AllowNULL is not set in the config, provide a default value based on the
150 // constraints of the referencing fields
151 if ( !mAllowNullWasSetByConfig )
152 {
153 mCbxAllowNull->setChecked( rel.referencingFieldsAllowNull() );
154 }
155
156 loadFields();
157}
158
159void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
160{
161 const auto constSelectedItems = mAvailableFieldsList->selectedItems();
162 for ( QListWidgetItem *item : constSelectedItems )
163 {
164 addFilterField( item );
165 }
166}
167
168void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
169{
170 const auto constSelectedItems = mFilterFieldsList->selectedItems();
171 for ( QListWidgetItem *item : constSelectedItems )
172 {
173 mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
174 mAvailableFieldsList->addItem( item );
175 }
176}
177
179{
180 QVariantMap myConfig;
181 myConfig.insert( QStringLiteral( "AllowNULL" ), mCbxAllowNull->isChecked() );
182 myConfig.insert( QStringLiteral( "ShowForm" ), mCbxShowForm->isChecked() );
183 myConfig.insert( QStringLiteral( "ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
184 myConfig.insert( QStringLiteral( "MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
185 myConfig.insert( QStringLiteral( "ReadOnly" ), mCbxReadOnly->isChecked() );
186 myConfig.insert( QStringLiteral( "Relation" ), mComboRelation->currentData() );
187 myConfig.insert( QStringLiteral( "AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
188 myConfig.insert( QStringLiteral( "FetchLimitActive" ), mFetchLimitCheckBox->isChecked() );
189 myConfig.insert( QStringLiteral( "FetchLimitNumber" ), mFetchLimit->value() );
190
191 if ( mFilterGroupBox->isChecked() )
192 {
193 QStringList filterFields;
194 filterFields.reserve( mFilterFieldsList->count() );
195 for ( int i = 0; i < mFilterFieldsList->count(); i++ )
196 {
197 filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
198 }
199 myConfig.insert( QStringLiteral( "FilterFields" ), filterFields );
200
201 myConfig.insert( QStringLiteral( "ChainFilters" ), mCbxChainFilters->isChecked() );
202 myConfig.insert( QStringLiteral( "FilterExpression" ), mFilterExpression->toPlainText() );
203 }
204
205 if ( mReferencedLayer )
206 {
207 // Store referenced layer data source and provider
208 myConfig.insert( QStringLiteral( "ReferencedLayerDataSource" ), mReferencedLayer->publicSource() );
209 myConfig.insert( QStringLiteral( "ReferencedLayerProviderKey" ), mReferencedLayer->providerType() );
210 myConfig.insert( QStringLiteral( "ReferencedLayerId" ), mReferencedLayer->id() );
211 myConfig.insert( QStringLiteral( "ReferencedLayerName" ), mReferencedLayer->name() );
212 mReferencedLayer->setDisplayExpression( mExpressionWidget->currentField() );
213 }
214
215 return myConfig;
216}
217
218void QgsRelationReferenceConfigDlg::loadFields()
219{
220 mAvailableFieldsList->clear();
221 mFilterFieldsList->clear();
222
223 if ( mReferencedLayer )
224 {
225 QgsVectorLayer *l = mReferencedLayer;
226 const QgsFields &flds = l->fields();
227 for ( int i = 0; i < flds.count(); i++ )
228 {
229 mAvailableFieldsList->addItem( flds.at( i ).displayName() );
230 mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.at( i ).name() );
231 }
232 }
233}
234
235void QgsRelationReferenceConfigDlg::addFilterField( const QString &field )
236{
237 for ( int i = 0; i < mAvailableFieldsList->count(); i++ )
238 {
239 if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() == field )
240 {
241 addFilterField( mAvailableFieldsList->item( i ) );
242 break;
243 }
244 }
245}
246
247void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
248{
249 mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
250 mFilterFieldsList->addItem( item );
251}
252
253int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
254{
255 QListWidget *lw = item->listWidget();
256
257 for ( int i = 0; i < lw->count(); i++ )
258 {
259 if ( lw->item( i ) == item )
260 return i;
261 }
262
263 return -1;
264}
@ 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:95
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