32 mFetchLimit->setMaximum( std::numeric_limits<int>::max() );
34 connect( mAddFilterButton, &QToolButton::clicked,
this, &QgsRelationReferenceConfigDlg::mAddFilterButton_clicked );
35 connect( mRemoveFilterButton, &QToolButton::clicked,
this, &QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked );
37 mExpressionWidget->registerExpressionContextGenerator( vl );
39 connect( mComboRelation,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsRelationReferenceConfigDlg::relationChanged );
42 for (
const QgsRelation &relation : constReferencingRelations )
44 if ( relation.name().isEmpty() )
45 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.id(), relation.referencedLayerId() ), relation.id() );
47 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.name(), relation.referencedLayerId() ), relation.id() );
49 QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mComboRelation->model() );
50 QStandardItem *item = model->item( model->rowCount() - 1 );
52 ? item->flags() & ~Qt::ItemIsEnabled
53 : item->flags() | Qt::ItemIsEnabled );
55 if (
auto *lReferencedLayer = relation.referencedLayer() )
57 mExpressionWidget->setField( lReferencedLayer->displayExpression() );
72 connect( mEditExpression, &QAbstractButton::clicked,
this, &QgsRelationReferenceConfigDlg::mEditExpression_clicked );
74 connect( mFetchLimitCheckBox, &QCheckBox::toggled, mFetchLimit, &QSpinBox::setEnabled );
77void QgsRelationReferenceConfigDlg::mEditExpression_clicked()
87 context.setHighlightedFunctions( QStringList() << QStringLiteral(
"current_value" ) << QStringLiteral(
"current_parent_value" ) );
88 context.setHighlightedVariables( QStringList() << QStringLiteral(
"current_geometry" )
89 << QStringLiteral(
"current_feature" )
90 << QStringLiteral(
"form_mode" )
91 << QStringLiteral(
"current_parent_geometry" )
92 << QStringLiteral(
"current_parent_feature" ) );
95 dlg.setWindowTitle( tr(
"Edit Filter Expression" ) );
97 if ( dlg.exec() == QDialog::Accepted )
99 mFilterExpression->setPlainText( dlg.expressionBuilder()->expressionText() );
107 mAllowNullWasSetByConfig =
config.contains( QStringLiteral(
"AllowNULL" ) );
108 if ( mAllowNullWasSetByConfig )
110 mCbxAllowNull->setChecked(
config.value( QStringLiteral(
"AllowNULL" ),
false ).toBool() );
112 mCbxShowForm->setChecked(
config.value( QStringLiteral(
"ShowForm" ),
false ).toBool() );
113 mCbxShowOpenFormButton->setChecked(
config.value( QStringLiteral(
"ShowOpenFormButton" ),
true ).toBool() );
115 if (
config.contains( QStringLiteral(
"Relation" ) ) )
117 mComboRelation->setCurrentIndex( mComboRelation->findData(
config.value( QStringLiteral(
"Relation" ) ).toString() ) );
118 relationChanged( mComboRelation->currentIndex() );
121 mCbxMapIdentification->setChecked(
config.value( QStringLiteral(
"MapIdentification" ),
false ).toBool() );
122 mCbxAllowAddFeatures->setChecked(
config.value( QStringLiteral(
"AllowAddFeatures" ),
false ).toBool() );
123 mCbxReadOnly->setChecked(
config.value( QStringLiteral(
"ReadOnly" ),
false ).toBool() );
124 mFetchLimitCheckBox->setChecked(
config.value( QStringLiteral(
"FetchLimitActive" ),
QgsSettings().value( QStringLiteral(
"maxEntriesRelationWidget" ), 100,
QgsSettings::Gui ).toInt() > 0 ).toBool() );
125 mFetchLimit->setValue(
config.value( QStringLiteral(
"FetchLimitNumber" ),
QgsSettings().value( QStringLiteral(
"maxEntriesRelationWidget" ), 100,
QgsSettings::Gui ) ).toInt() );
126 mFilterExpression->setPlainText(
config.value( QStringLiteral(
"FilterExpression" ) ).toString() );
128 if (
config.contains( QStringLiteral(
"FilterFields" ) ) )
130 mFilterGroupBox->setChecked(
true );
131 const auto constToStringList =
config.value(
"FilterFields" ).toStringList();
132 for (
const QString &fld : constToStringList )
134 addFilterField( fld );
137 mCbxChainFilters->setChecked(
config.value( QStringLiteral(
"ChainFilters" ) ).toBool() );
141void QgsRelationReferenceConfigDlg::relationChanged(
int idx )
143 const QString relName = mComboRelation->itemData( idx ).toString();
147 mExpressionWidget->setLayer( mReferencedLayer );
148 if ( mReferencedLayer )
151 mCbxMapIdentification->setEnabled( mReferencedLayer->
isSpatial() );
156 if ( ! mAllowNullWasSetByConfig )
164void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
166 const auto constSelectedItems = mAvailableFieldsList->selectedItems();
167 for ( QListWidgetItem *item : constSelectedItems )
169 addFilterField( item );
173void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
175 const auto constSelectedItems = mFilterFieldsList->selectedItems();
176 for ( QListWidgetItem *item : constSelectedItems )
178 mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
179 mAvailableFieldsList->addItem( item );
185 QVariantMap myConfig;
186 myConfig.insert( QStringLiteral(
"AllowNULL" ), mCbxAllowNull->isChecked() );
187 myConfig.insert( QStringLiteral(
"ShowForm" ), mCbxShowForm->isChecked() );
188 myConfig.insert( QStringLiteral(
"ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
189 myConfig.insert( QStringLiteral(
"MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
190 myConfig.insert( QStringLiteral(
"ReadOnly" ), mCbxReadOnly->isChecked() );
191 myConfig.insert( QStringLiteral(
"Relation" ), mComboRelation->currentData() );
192 myConfig.insert( QStringLiteral(
"AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
193 myConfig.insert( QStringLiteral(
"FetchLimitActive" ), mFetchLimitCheckBox->isChecked() );
194 myConfig.insert( QStringLiteral(
"FetchLimitNumber" ), mFetchLimit->value() );
196 if ( mFilterGroupBox->isChecked() )
198 QStringList filterFields;
199 filterFields.reserve( mFilterFieldsList->count() );
200 for (
int i = 0; i < mFilterFieldsList->count(); i++ )
202 filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
204 myConfig.insert( QStringLiteral(
"FilterFields" ), filterFields );
206 myConfig.insert( QStringLiteral(
"ChainFilters" ), mCbxChainFilters->isChecked() );
207 myConfig.insert( QStringLiteral(
"FilterExpression" ), mFilterExpression->toPlainText() );
210 if ( mReferencedLayer )
213 myConfig.insert( QStringLiteral(
"ReferencedLayerDataSource" ), mReferencedLayer->
publicSource() );
214 myConfig.insert( QStringLiteral(
"ReferencedLayerProviderKey" ), mReferencedLayer->
providerType() );
215 myConfig.insert( QStringLiteral(
"ReferencedLayerId" ), mReferencedLayer->
id() );
216 myConfig.insert( QStringLiteral(
"ReferencedLayerName" ), mReferencedLayer->
name() );
223void QgsRelationReferenceConfigDlg::loadFields()
225 mAvailableFieldsList->clear();
226 mFilterFieldsList->clear();
228 if ( mReferencedLayer )
232 for (
int i = 0; i < flds.
count(); i++ )
235 mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.
at( i ).
name() );
240void QgsRelationReferenceConfigDlg::addFilterField(
const QString &field )
242 for (
int i = 0; i < mAvailableFieldsList->count(); i++ )
244 if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() ==
field )
246 addFilterField( mAvailableFieldsList->item( i ) );
252void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
254 mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
255 mFilterFieldsList->addItem( item );
258int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
260 QListWidget *lw = item->listWidget();
262 for (
int i = 0; i < lw->count(); i++ )
264 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.
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(bool hidePassword=false) const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
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.
Represents a relationship between two vector layers.
QgsVectorLayer * referencedLayer
bool referencingFieldsAllowNull() const
Returns true if none of the referencing fields has a NOT NULL constraint.
This class is a composition of two QSettings instances:
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...
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