31 connect( mAddFilterButton, &QToolButton::clicked,
this, &QgsRelationReferenceConfigDlg::mAddFilterButton_clicked );
32 connect( mRemoveFilterButton, &QToolButton::clicked,
this, &QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked );
34 mExpressionWidget->registerExpressionContextGenerator( vl );
36 connect( mComboRelation,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsRelationReferenceConfigDlg::relationChanged );
39 for (
const QgsRelation &relation : constReferencingRelations )
41 if ( relation.name().isEmpty() )
42 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.id(), relation.referencedLayerId() ), relation.id() );
44 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.name(), relation.referencedLayerId() ), relation.id() );
46 QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mComboRelation->model() );
47 QStandardItem *item = model->item( model->rowCount() - 1 );
49 ? item->flags() & ~Qt::ItemIsEnabled
50 : item->flags() | Qt::ItemIsEnabled );
52 if (
auto *lReferencedLayer = relation.referencedLayer() )
54 mExpressionWidget->setField( lReferencedLayer->displayExpression() );
70 connect( mEditExpression, &QAbstractButton::clicked,
this, &QgsRelationReferenceConfigDlg::mEditExpression_clicked );
74 void QgsRelationReferenceConfigDlg::mEditExpression_clicked()
84 context.setHighlightedFunctions( QStringList() << QStringLiteral(
"current_value" ) << QStringLiteral(
"current_parent_value" ) );
85 context.setHighlightedVariables( QStringList() << QStringLiteral(
"current_geometry" )
86 << QStringLiteral(
"current_feature" )
87 << QStringLiteral(
"form_mode" )
88 << QStringLiteral(
"current_parent_geometry" )
89 << QStringLiteral(
"current_parent_feature" ) );
92 dlg.setWindowTitle( tr(
"Edit Filter Expression" ) );
94 if ( dlg.exec() == QDialog::Accepted )
96 mFilterExpression->setPlainText( dlg.expressionBuilder()->expressionText() );
102 mCbxAllowNull->setChecked(
config.value( QStringLiteral(
"AllowNULL" ),
false ).toBool() );
103 mCbxOrderByValue->setChecked(
config.value( QStringLiteral(
"OrderByValue" ),
false ).toBool() );
104 mCbxShowForm->setChecked(
config.value( QStringLiteral(
"ShowForm" ),
false ).toBool() );
105 mCbxShowOpenFormButton->setChecked(
config.value( QStringLiteral(
"ShowOpenFormButton" ),
true ).toBool() );
107 if (
config.contains( QStringLiteral(
"Relation" ) ) )
109 mComboRelation->setCurrentIndex( mComboRelation->findData(
config.value( QStringLiteral(
"Relation" ) ).toString() ) );
110 relationChanged( mComboRelation->currentIndex() );
113 mCbxMapIdentification->setChecked(
config.value( QStringLiteral(
"MapIdentification" ),
false ).toBool() );
114 mCbxAllowAddFeatures->setChecked(
config.value( QStringLiteral(
"AllowAddFeatures" ),
false ).toBool() );
115 mCbxReadOnly->setChecked(
config.value( QStringLiteral(
"ReadOnly" ),
false ).toBool() );
116 mFilterExpression->setPlainText(
config.value( QStringLiteral(
"FilterExpression" ) ).toString() );
118 if (
config.contains( QStringLiteral(
"FilterFields" ) ) )
120 mFilterGroupBox->setChecked(
true );
121 const auto constToStringList =
config.value(
"FilterFields" ).toStringList();
122 for (
const QString &fld : constToStringList )
124 addFilterField( fld );
127 mCbxChainFilters->setChecked(
config.value( QStringLiteral(
"ChainFilters" ) ).toBool() );
131 void QgsRelationReferenceConfigDlg::relationChanged(
int idx )
133 const QString relName = mComboRelation->itemData( idx ).toString();
137 mExpressionWidget->setLayer( mReferencedLayer );
138 if ( mReferencedLayer )
141 mCbxMapIdentification->setEnabled( mReferencedLayer->
isSpatial() );
147 void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
149 const auto constSelectedItems = mAvailableFieldsList->selectedItems();
150 for ( QListWidgetItem *item : constSelectedItems )
152 addFilterField( item );
156 void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
158 const auto constSelectedItems = mFilterFieldsList->selectedItems();
159 for ( QListWidgetItem *item : constSelectedItems )
161 mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
162 mAvailableFieldsList->addItem( item );
168 QVariantMap myConfig;
169 myConfig.insert( QStringLiteral(
"AllowNULL" ), mCbxAllowNull->isChecked() );
170 myConfig.insert( QStringLiteral(
"OrderByValue" ), mCbxOrderByValue->isChecked() );
171 myConfig.insert( QStringLiteral(
"ShowForm" ), mCbxShowForm->isChecked() );
172 myConfig.insert( QStringLiteral(
"ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
173 myConfig.insert( QStringLiteral(
"MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
174 myConfig.insert( QStringLiteral(
"ReadOnly" ), mCbxReadOnly->isChecked() );
175 myConfig.insert( QStringLiteral(
"Relation" ), mComboRelation->currentData() );
176 myConfig.insert( QStringLiteral(
"AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
178 if ( mFilterGroupBox->isChecked() )
180 QStringList filterFields;
181 filterFields.reserve( mFilterFieldsList->count() );
182 for (
int i = 0; i < mFilterFieldsList->count(); i++ )
184 filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
186 myConfig.insert( QStringLiteral(
"FilterFields" ), filterFields );
188 myConfig.insert( QStringLiteral(
"ChainFilters" ), mCbxChainFilters->isChecked() );
189 myConfig.insert( QStringLiteral(
"FilterExpression" ), mFilterExpression->toPlainText() );
192 if ( mReferencedLayer )
195 myConfig.insert( QStringLiteral(
"ReferencedLayerDataSource" ), mReferencedLayer->
publicSource() );
196 myConfig.insert( QStringLiteral(
"ReferencedLayerProviderKey" ), mReferencedLayer->
providerType() );
197 myConfig.insert( QStringLiteral(
"ReferencedLayerId" ), mReferencedLayer->
id() );
198 myConfig.insert( QStringLiteral(
"ReferencedLayerName" ), mReferencedLayer->
name() );
205 void QgsRelationReferenceConfigDlg::loadFields()
207 mAvailableFieldsList->clear();
208 mFilterFieldsList->clear();
210 if ( mReferencedLayer )
214 for (
int i = 0; i < flds.
count(); i++ )
217 mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.
at( i ).
name() );
222 void QgsRelationReferenceConfigDlg::addFilterField(
const QString &
field )
224 for (
int i = 0; i < mAvailableFieldsList->count(); i++ )
226 if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() ==
field )
228 addFilterField( mAvailableFieldsList->item( i ) );
234 void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
236 mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
237 mFilterFieldsList->addItem( item );
240 int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
242 QListWidget *lw = item->listWidget();
244 for (
int i = 0; i < lw->count(); i++ )
246 if ( lw->item( i ) == item )