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() );
45 QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mComboRelation->model() );
46 QStandardItem *item = model->item( model->rowCount() - 1 );
48 ? item->flags() & ~Qt::ItemIsEnabled
49 : item->flags() | Qt::ItemIsEnabled );
51 if (
auto *lReferencedLayer = relation.referencedLayer() )
53 mExpressionWidget->setField( lReferencedLayer->displayExpression() );
69 connect( mEditExpression, &QAbstractButton::clicked,
this, &QgsRelationReferenceConfigDlg::mEditExpression_clicked );
73void QgsRelationReferenceConfigDlg::mEditExpression_clicked()
83 context.setHighlightedFunctions( QStringList() << QStringLiteral(
"current_value" ) << QStringLiteral(
"current_parent_value" ) );
84 context.setHighlightedVariables( QStringList() << QStringLiteral(
"current_geometry" )
85 << QStringLiteral(
"current_feature" )
86 << QStringLiteral(
"form_mode" )
87 << QStringLiteral(
"current_parent_geometry" )
88 << QStringLiteral(
"current_parent_feature" ) );
91 dlg.setWindowTitle( tr(
"Edit Filter Expression" ) );
93 if ( dlg.exec() == QDialog::Accepted )
95 mFilterExpression->setPlainText( dlg.expressionBuilder()->expressionText() );
101 mCbxAllowNull->setChecked(
config.value( QStringLiteral(
"AllowNULL" ),
false ).toBool() );
102 mCbxOrderByValue->setChecked(
config.value( QStringLiteral(
"OrderByValue" ),
false ).toBool() );
103 mCbxShowForm->setChecked(
config.value( QStringLiteral(
"ShowForm" ),
false ).toBool() );
104 mCbxShowOpenFormButton->setChecked(
config.value( QStringLiteral(
"ShowOpenFormButton" ),
true ).toBool() );
106 if (
config.contains( QStringLiteral(
"Relation" ) ) )
108 mComboRelation->setCurrentIndex( mComboRelation->findData(
config.value( QStringLiteral(
"Relation" ) ).toString() ) );
109 relationChanged( mComboRelation->currentIndex() );
112 mCbxMapIdentification->setChecked(
config.value( QStringLiteral(
"MapIdentification" ),
false ).toBool() );
113 mCbxAllowAddFeatures->setChecked(
config.value( QStringLiteral(
"AllowAddFeatures" ),
false ).toBool() );
114 mCbxReadOnly->setChecked(
config.value( QStringLiteral(
"ReadOnly" ),
false ).toBool() );
115 mFilterExpression->setPlainText(
config.value( QStringLiteral(
"FilterExpression" ) ).toString() );
117 if (
config.contains( QStringLiteral(
"FilterFields" ) ) )
119 mFilterGroupBox->setChecked(
true );
120 const auto constToStringList =
config.value(
"FilterFields" ).toStringList();
121 for (
const QString &fld : constToStringList )
123 addFilterField( fld );
126 mCbxChainFilters->setChecked(
config.value( QStringLiteral(
"ChainFilters" ) ).toBool() );
130void QgsRelationReferenceConfigDlg::relationChanged(
int idx )
132 const QString relName = mComboRelation->itemData( idx ).toString();
136 mExpressionWidget->setLayer( mReferencedLayer );
137 if ( mReferencedLayer )
140 mCbxMapIdentification->setEnabled( mReferencedLayer->
isSpatial() );
146void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
148 const auto constSelectedItems = mAvailableFieldsList->selectedItems();
149 for ( QListWidgetItem *item : constSelectedItems )
151 addFilterField( item );
155void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
157 const auto constSelectedItems = mFilterFieldsList->selectedItems();
158 for ( QListWidgetItem *item : constSelectedItems )
160 mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
161 mAvailableFieldsList->addItem( item );
167 QVariantMap myConfig;
168 myConfig.insert( QStringLiteral(
"AllowNULL" ), mCbxAllowNull->isChecked() );
169 myConfig.insert( QStringLiteral(
"OrderByValue" ), mCbxOrderByValue->isChecked() );
170 myConfig.insert( QStringLiteral(
"ShowForm" ), mCbxShowForm->isChecked() );
171 myConfig.insert( QStringLiteral(
"ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
172 myConfig.insert( QStringLiteral(
"MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
173 myConfig.insert( QStringLiteral(
"ReadOnly" ), mCbxReadOnly->isChecked() );
174 myConfig.insert( QStringLiteral(
"Relation" ), mComboRelation->currentData() );
175 myConfig.insert( QStringLiteral(
"AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
177 if ( mFilterGroupBox->isChecked() )
179 QStringList filterFields;
180 filterFields.reserve( mFilterFieldsList->count() );
181 for (
int i = 0; i < mFilterFieldsList->count(); i++ )
183 filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
185 myConfig.insert( QStringLiteral(
"FilterFields" ), filterFields );
187 myConfig.insert( QStringLiteral(
"ChainFilters" ), mCbxChainFilters->isChecked() );
188 myConfig.insert( QStringLiteral(
"FilterExpression" ), mFilterExpression->toPlainText() );
191 if ( mReferencedLayer )
194 myConfig.insert( QStringLiteral(
"ReferencedLayerDataSource" ), mReferencedLayer->
publicSource() );
195 myConfig.insert( QStringLiteral(
"ReferencedLayerProviderKey" ), mReferencedLayer->
providerType() );
196 myConfig.insert( QStringLiteral(
"ReferencedLayerId" ), mReferencedLayer->
id() );
197 myConfig.insert( QStringLiteral(
"ReferencedLayerName" ), mReferencedLayer->
name() );
204void QgsRelationReferenceConfigDlg::loadFields()
206 mAvailableFieldsList->clear();
207 mFilterFieldsList->clear();
209 if ( mReferencedLayer )
213 for (
int i = 0; i < flds.
count(); i++ )
216 mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.
at( i ).
name() );
221void QgsRelationReferenceConfigDlg::addFilterField(
const QString &
field )
223 for (
int i = 0; i < mAvailableFieldsList->count(); i++ )
225 if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() ==
field )
227 addFilterField( mAvailableFieldsList->item( i ) );
233void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
235 mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
236 mFilterFieldsList->addItem( item );
239int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
241 QListWidget *lw = item->listWidget();
243 for (
int i = 0; i < lw->count(); i++ )
245 if ( lw->item( i ) == item )
@ Generated
A generated relation is a child of a polymorphic relation.
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...
QString displayName() const
Returns the name to use when displaying this field.
Container of fields for a vector layer.
int count() const
Returns number of items.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
QString providerType() const
Returns the provider type (provider key) for this layer.
QString publicSource() const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QgsRelationManager * relationManager
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.
QgsVectorLayer * referencedLayer
Represents a vector layer which manages a vector based data sets.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QList< QgsRelation > referencingRelations(int idx) const
Returns the layer's relations, where the foreign key is on this layer.
void setDisplayExpression(const QString &displayExpression)
Set the preview expression, used to create a human readable preview string.
QString displayExpression