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 );
39 if ( relation.
name().isEmpty() )
40 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.
id(), relation.
referencedLayerId() ), relation.
id() );
42 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.
name(), relation.
referencedLayerId() ), relation.
id() );
65 mCbxAllowNull->setChecked( config.value( QStringLiteral(
"AllowNULL" ),
false ).toBool() );
66 mCbxOrderByValue->setChecked( config.value( QStringLiteral(
"OrderByValue" ),
false ).toBool() );
67 mCbxShowForm->setChecked( config.value( QStringLiteral(
"ShowForm" ),
false ).toBool() );
68 mCbxShowOpenFormButton->setChecked( config.value( QStringLiteral(
"ShowOpenFormButton" ),
true ).toBool() );
70 if ( config.contains( QStringLiteral(
"Relation" ) ) )
72 mComboRelation->setCurrentIndex( mComboRelation->findData( config.value( QStringLiteral(
"Relation" ) ).toString() ) );
73 relationChanged( mComboRelation->currentIndex() );
76 mCbxMapIdentification->setChecked( config.value( QStringLiteral(
"MapIdentification" ),
false ).toBool() );
77 mCbxAllowAddFeatures->setChecked( config.value( QStringLiteral(
"AllowAddFeatures" ),
false ).toBool() );
78 mCbxReadOnly->setChecked( config.value( QStringLiteral(
"ReadOnly" ),
false ).toBool() );
80 if ( config.contains( QStringLiteral(
"FilterFields" ) ) )
82 mFilterGroupBox->setChecked(
true );
83 Q_FOREACH (
const QString &fld, config.value(
"FilterFields" ).toStringList() )
85 addFilterField( fld );
88 mCbxChainFilters->setChecked( config.value( QStringLiteral(
"ChainFilters" ) ).toBool() );
92 void QgsRelationReferenceConfigDlg::relationChanged(
int idx )
94 QString relName = mComboRelation->itemData( idx ).toString();
98 mExpressionWidget->setLayer( mReferencedLayer );
99 if ( mReferencedLayer )
102 mCbxMapIdentification->setEnabled( mReferencedLayer->
isSpatial() );
108 void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
110 Q_FOREACH ( QListWidgetItem *item, mAvailableFieldsList->selectedItems() )
112 addFilterField( item );
116 void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
118 Q_FOREACH ( QListWidgetItem *item, mFilterFieldsList->selectedItems() )
120 mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
121 mAvailableFieldsList->addItem( item );
127 QVariantMap myConfig;
128 myConfig.insert( QStringLiteral(
"AllowNULL" ), mCbxAllowNull->isChecked() );
129 myConfig.insert( QStringLiteral(
"OrderByValue" ), mCbxOrderByValue->isChecked() );
130 myConfig.insert( QStringLiteral(
"ShowForm" ), mCbxShowForm->isChecked() );
131 myConfig.insert( QStringLiteral(
"ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
132 myConfig.insert( QStringLiteral(
"MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
133 myConfig.insert( QStringLiteral(
"ReadOnly" ), mCbxReadOnly->isChecked() );
134 myConfig.insert( QStringLiteral(
"Relation" ), mComboRelation->currentData() );
135 myConfig.insert( QStringLiteral(
"AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
137 if ( mFilterGroupBox->isChecked() )
139 QStringList filterFields;
140 filterFields.reserve( mFilterFieldsList->count() );
141 for (
int i = 0; i < mFilterFieldsList->count(); i++ )
143 filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
145 myConfig.insert( QStringLiteral(
"FilterFields" ), filterFields );
147 myConfig.insert( QStringLiteral(
"ChainFilters" ), mCbxChainFilters->isChecked() );
150 if ( mReferencedLayer )
158 void QgsRelationReferenceConfigDlg::loadFields()
160 mAvailableFieldsList->clear();
161 mFilterFieldsList->clear();
163 if ( mReferencedLayer )
167 for (
int i = 0; i < flds.
count(); i++ )
170 mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.
at( i ).
name() );
175 void QgsRelationReferenceConfigDlg::addFilterField(
const QString &
field )
177 for (
int i = 0; i < mAvailableFieldsList->count(); i++ )
179 if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() ==
field )
181 addFilterField( mAvailableFieldsList->item( i ) );
187 void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
189 mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
190 mFilterFieldsList->addItem( item );
193 int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
195 QListWidget *lw = item->listWidget();
197 for (
int i = 0; i < lw->count(); i++ )
199 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
Return number of items.
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
QgsFields fields() const override
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
Get relations, where the foreign key is on this layer.
bool isSpatial() const override
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
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
Get access to a relation by its id.
Represents a vector layer which manages a vector based data sets.
QString referencedLayerId() const
Access the referenced (parent) layer's id.
QgsRelationReferenceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)