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 )
162 void QgsRelationReferenceConfigDlg::loadFields()
164 mAvailableFieldsList->clear();
165 mFilterFieldsList->clear();
167 if ( mReferencedLayer )
171 for (
int i = 0; i < flds.
count(); i++ )
174 mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.
at( i ).
name() );
179 void QgsRelationReferenceConfigDlg::addFilterField(
const QString &
field )
181 for (
int i = 0; i < mAvailableFieldsList->count(); i++ )
183 if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() ==
field )
185 addFilterField( mAvailableFieldsList->item( i ) );
191 void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
193 mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
194 mFilterFieldsList->addItem( item );
197 int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
199 QListWidget *lw = item->listWidget();
201 for (
int i = 0; i < lw->count(); i++ )
203 if ( lw->item( i ) == item )
Container of fields for a vector layer.
void setDisplayExpression(const QString &displayExpression)
Set the preview expression, used to create a human readable preview string.
int count() const
Returns number of items.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QString displayName() const
Returns the name to use when displaying this field.
QList< QgsRelation > referencingRelations(int idx) const
Returns the layer's relations, where the foreign key is on this layer.
QgsVectorLayer referencedLayer
QgsRelationManager relationManager
QString displayExpression
QVariantMap config() override
Create a configuration from the current GUI state.
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
Represents a vector layer which manages a vector based data sets.
QgsRelationReferenceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)