30 connect( mAddFilterButton, &QToolButton::clicked,
this, &QgsRelationReferenceConfigDlg::mAddFilterButton_clicked );
31 connect( mRemoveFilterButton, &QToolButton::clicked,
this, &QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked );
33 mExpressionWidget->registerExpressionContextGenerator( vl );
35 connect( mComboRelation,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsRelationReferenceConfigDlg::relationChanged );
38 for (
const QgsRelation &relation : constReferencingRelations )
40 if ( relation.name().isEmpty() )
41 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.id(), relation.referencedLayerId() ), relation.id() );
43 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.name(), relation.referencedLayerId() ), relation.id() );
44 if ( relation.referencedLayer() )
46 mExpressionWidget->setField( relation.referencedLayer()->displayExpression() );
66 mCbxAllowNull->setChecked(
config.value( QStringLiteral(
"AllowNULL" ),
false ).toBool() );
67 mCbxOrderByValue->setChecked(
config.value( QStringLiteral(
"OrderByValue" ),
false ).toBool() );
68 mCbxShowForm->setChecked(
config.value( QStringLiteral(
"ShowForm" ),
false ).toBool() );
69 mCbxShowOpenFormButton->setChecked(
config.value( QStringLiteral(
"ShowOpenFormButton" ),
true ).toBool() );
71 if (
config.contains( QStringLiteral(
"Relation" ) ) )
73 mComboRelation->setCurrentIndex( mComboRelation->findData(
config.value( QStringLiteral(
"Relation" ) ).toString() ) );
74 relationChanged( mComboRelation->currentIndex() );
77 mCbxMapIdentification->setChecked(
config.value( QStringLiteral(
"MapIdentification" ),
false ).toBool() );
78 mCbxAllowAddFeatures->setChecked(
config.value( QStringLiteral(
"AllowAddFeatures" ),
false ).toBool() );
79 mCbxReadOnly->setChecked(
config.value( QStringLiteral(
"ReadOnly" ),
false ).toBool() );
81 if (
config.contains( QStringLiteral(
"FilterFields" ) ) )
83 mFilterGroupBox->setChecked(
true );
84 const auto constToStringList =
config.value(
"FilterFields" ).toStringList();
85 for (
const QString &fld : constToStringList )
87 addFilterField( fld );
90 mCbxChainFilters->setChecked(
config.value( QStringLiteral(
"ChainFilters" ) ).toBool() );
94 void QgsRelationReferenceConfigDlg::relationChanged(
int idx )
96 QString relName = mComboRelation->itemData( idx ).toString();
100 mExpressionWidget->setLayer( mReferencedLayer );
101 if ( mReferencedLayer )
104 mCbxMapIdentification->setEnabled( mReferencedLayer->
isSpatial() );
110 void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
112 const auto constSelectedItems = mAvailableFieldsList->selectedItems();
113 for ( QListWidgetItem *item : constSelectedItems )
115 addFilterField( item );
119 void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
121 const auto constSelectedItems = mFilterFieldsList->selectedItems();
122 for ( QListWidgetItem *item : constSelectedItems )
124 mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
125 mAvailableFieldsList->addItem( item );
131 QVariantMap myConfig;
132 myConfig.insert( QStringLiteral(
"AllowNULL" ), mCbxAllowNull->isChecked() );
133 myConfig.insert( QStringLiteral(
"OrderByValue" ), mCbxOrderByValue->isChecked() );
134 myConfig.insert( QStringLiteral(
"ShowForm" ), mCbxShowForm->isChecked() );
135 myConfig.insert( QStringLiteral(
"ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
136 myConfig.insert( QStringLiteral(
"MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
137 myConfig.insert( QStringLiteral(
"ReadOnly" ), mCbxReadOnly->isChecked() );
138 myConfig.insert( QStringLiteral(
"Relation" ), mComboRelation->currentData() );
139 myConfig.insert( QStringLiteral(
"AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
141 if ( mFilterGroupBox->isChecked() )
143 QStringList filterFields;
144 filterFields.reserve( mFilterFieldsList->count() );
145 for (
int i = 0; i < mFilterFieldsList->count(); i++ )
147 filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
149 myConfig.insert( QStringLiteral(
"FilterFields" ), filterFields );
151 myConfig.insert( QStringLiteral(
"ChainFilters" ), mCbxChainFilters->isChecked() );
154 if ( mReferencedLayer )
157 myConfig.insert( QStringLiteral(
"ReferencedLayerDataSource" ), mReferencedLayer->
publicSource() );
158 myConfig.insert( QStringLiteral(
"ReferencedLayerProviderKey" ), mReferencedLayer->
providerType() );
159 myConfig.insert( QStringLiteral(
"ReferencedLayerId" ), mReferencedLayer->
id() );
160 myConfig.insert( QStringLiteral(
"ReferencedLayerName" ), mReferencedLayer->
name() );
167 void QgsRelationReferenceConfigDlg::loadFields()
169 mAvailableFieldsList->clear();
170 mFilterFieldsList->clear();
172 if ( mReferencedLayer )
176 for (
int i = 0; i < flds.
count(); i++ )
179 mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.
at( i ).
name() );
184 void QgsRelationReferenceConfigDlg::addFilterField(
const QString &field )
186 for (
int i = 0; i < mAvailableFieldsList->count(); i++ )
188 if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() ==
field )
190 addFilterField( mAvailableFieldsList->item( i ) );
196 void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
198 mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
199 mFilterFieldsList->addItem( item );
202 int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
204 QListWidget *lw = item->listWidget();
206 for (
int i = 0; i < lw->count(); i++ )
208 if ( lw->item( i ) == item )