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