17#include "moc_qgsrelationreferenceconfigdlg.cpp"
33 mFetchLimit->setMaximum( std::numeric_limits<int>::max() );
35 connect( mAddFilterButton, &QToolButton::clicked,
this, &QgsRelationReferenceConfigDlg::mAddFilterButton_clicked );
36 connect( mRemoveFilterButton, &QToolButton::clicked,
this, &QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked );
38 mExpressionWidget->registerExpressionContextGenerator( vl );
40 connect( mComboRelation,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsRelationReferenceConfigDlg::relationChanged );
43 for (
const QgsRelation &relation : constReferencingRelations )
45 if ( relation.name().isEmpty() )
46 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.id(), relation.referencedLayerId() ), relation.id() );
48 mComboRelation->addItem( QStringLiteral(
"%1 (%2)" ).arg( relation.name(), relation.referencedLayerId() ), relation.id() );
50 QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mComboRelation->model() );
51 QStandardItem *item = model->item( model->rowCount() - 1 );
54 if (
auto *lReferencedLayer = relation.referencedLayer() )
56 mExpressionWidget->setField( lReferencedLayer->displayExpression() );
71 connect( mEditExpression, &QAbstractButton::clicked,
this, &QgsRelationReferenceConfigDlg::mEditExpression_clicked );
73 connect( mFetchLimitCheckBox, &QCheckBox::toggled, mFetchLimit, &QSpinBox::setEnabled );
76void QgsRelationReferenceConfigDlg::mEditExpression_clicked()
86 context.setHighlightedFunctions( QStringList() << QStringLiteral(
"current_value" ) << QStringLiteral(
"current_parent_value" ) );
87 context.setHighlightedVariables( QStringList() << QStringLiteral(
"current_geometry" ) << QStringLiteral(
"current_feature" ) << QStringLiteral(
"form_mode" ) << QStringLiteral(
"current_parent_geometry" ) << QStringLiteral(
"current_parent_feature" ) );
90 dlg.setWindowTitle( tr(
"Edit Filter Expression" ) );
92 if ( dlg.exec() == QDialog::Accepted )
94 mFilterExpression->setPlainText( dlg.expressionBuilder()->expressionText() );
102 mAllowNullWasSetByConfig =
config.contains( QStringLiteral(
"AllowNULL" ) );
103 if ( mAllowNullWasSetByConfig )
105 mCbxAllowNull->setChecked(
config.value( QStringLiteral(
"AllowNULL" ),
false ).toBool() );
107 mCbxShowForm->setChecked(
config.value( QStringLiteral(
"ShowForm" ),
false ).toBool() );
108 mCbxShowOpenFormButton->setChecked(
config.value( QStringLiteral(
"ShowOpenFormButton" ),
true ).toBool() );
110 if (
config.contains( QStringLiteral(
"Relation" ) ) )
112 mComboRelation->setCurrentIndex( mComboRelation->findData(
config.value( QStringLiteral(
"Relation" ) ).toString() ) );
113 relationChanged( mComboRelation->currentIndex() );
116 mCbxMapIdentification->setChecked(
config.value( QStringLiteral(
"MapIdentification" ),
false ).toBool() );
117 mCbxAllowAddFeatures->setChecked(
config.value( QStringLiteral(
"AllowAddFeatures" ),
false ).toBool() );
118 mCbxReadOnly->setChecked(
config.value( QStringLiteral(
"ReadOnly" ),
false ).toBool() );
119 mFetchLimitCheckBox->setChecked(
config.value( QStringLiteral(
"FetchLimitActive" ),
QgsSettings().value( QStringLiteral(
"maxEntriesRelationWidget" ), 100,
QgsSettings::Gui ).toInt() > 0 ).toBool() );
120 mFetchLimit->setValue(
config.value( QStringLiteral(
"FetchLimitNumber" ),
QgsSettings().value( QStringLiteral(
"maxEntriesRelationWidget" ), 100,
QgsSettings::Gui ) ).toInt() );
121 mFilterExpression->setPlainText(
config.value( QStringLiteral(
"FilterExpression" ) ).toString() );
123 if (
config.contains( QStringLiteral(
"FilterFields" ) ) )
125 mFilterGroupBox->setChecked(
true );
126 const auto constToStringList =
config.value(
"FilterFields" ).toStringList();
127 for (
const QString &fld : constToStringList )
129 addFilterField( fld );
132 mCbxChainFilters->setChecked(
config.value( QStringLiteral(
"ChainFilters" ) ).toBool() );
136void QgsRelationReferenceConfigDlg::relationChanged(
int idx )
138 const QString relName = mComboRelation->itemData( idx ).toString();
142 mExpressionWidget->setLayer( mReferencedLayer );
143 if ( mReferencedLayer )
146 mCbxMapIdentification->setEnabled( mReferencedLayer->
isSpatial() );
151 if ( !mAllowNullWasSetByConfig )
159void QgsRelationReferenceConfigDlg::mAddFilterButton_clicked()
161 const auto constSelectedItems = mAvailableFieldsList->selectedItems();
162 for ( QListWidgetItem *item : constSelectedItems )
164 addFilterField( item );
168void QgsRelationReferenceConfigDlg::mRemoveFilterButton_clicked()
170 const auto constSelectedItems = mFilterFieldsList->selectedItems();
171 for ( QListWidgetItem *item : constSelectedItems )
173 mFilterFieldsList->takeItem( indexFromListWidgetItem( item ) );
174 mAvailableFieldsList->addItem( item );
180 QVariantMap myConfig;
181 myConfig.insert( QStringLiteral(
"AllowNULL" ), mCbxAllowNull->isChecked() );
182 myConfig.insert( QStringLiteral(
"ShowForm" ), mCbxShowForm->isChecked() );
183 myConfig.insert( QStringLiteral(
"ShowOpenFormButton" ), mCbxShowOpenFormButton->isChecked() );
184 myConfig.insert( QStringLiteral(
"MapIdentification" ), mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
185 myConfig.insert( QStringLiteral(
"ReadOnly" ), mCbxReadOnly->isChecked() );
186 myConfig.insert( QStringLiteral(
"Relation" ), mComboRelation->currentData() );
187 myConfig.insert( QStringLiteral(
"AllowAddFeatures" ), mCbxAllowAddFeatures->isChecked() );
188 myConfig.insert( QStringLiteral(
"FetchLimitActive" ), mFetchLimitCheckBox->isChecked() );
189 myConfig.insert( QStringLiteral(
"FetchLimitNumber" ), mFetchLimit->value() );
191 if ( mFilterGroupBox->isChecked() )
193 QStringList filterFields;
194 filterFields.reserve( mFilterFieldsList->count() );
195 for (
int i = 0; i < mFilterFieldsList->count(); i++ )
197 filterFields << mFilterFieldsList->item( i )->data( Qt::UserRole ).toString();
199 myConfig.insert( QStringLiteral(
"FilterFields" ), filterFields );
201 myConfig.insert( QStringLiteral(
"ChainFilters" ), mCbxChainFilters->isChecked() );
202 myConfig.insert( QStringLiteral(
"FilterExpression" ), mFilterExpression->toPlainText() );
205 if ( mReferencedLayer )
208 myConfig.insert( QStringLiteral(
"ReferencedLayerDataSource" ), mReferencedLayer->
publicSource() );
209 myConfig.insert( QStringLiteral(
"ReferencedLayerProviderKey" ), mReferencedLayer->
providerType() );
210 myConfig.insert( QStringLiteral(
"ReferencedLayerId" ), mReferencedLayer->
id() );
211 myConfig.insert( QStringLiteral(
"ReferencedLayerName" ), mReferencedLayer->
name() );
218void QgsRelationReferenceConfigDlg::loadFields()
220 mAvailableFieldsList->clear();
221 mFilterFieldsList->clear();
223 if ( mReferencedLayer )
227 for (
int i = 0; i < flds.
count(); i++ )
230 mAvailableFieldsList->item( mAvailableFieldsList->count() - 1 )->setData( Qt::UserRole, flds.
at( i ).
name() );
235void QgsRelationReferenceConfigDlg::addFilterField(
const QString &field )
237 for (
int i = 0; i < mAvailableFieldsList->count(); i++ )
239 if ( mAvailableFieldsList->item( i )->data( Qt::UserRole ).toString() ==
field )
241 addFilterField( mAvailableFieldsList->item( i ) );
247void QgsRelationReferenceConfigDlg::addFilterField( QListWidgetItem *item )
249 mAvailableFieldsList->takeItem( indexFromListWidgetItem( item ) );
250 mFilterFieldsList->addItem( item );
253int QgsRelationReferenceConfigDlg::indexFromListWidgetItem( QListWidgetItem *item )
255 QListWidget *lw = item->listWidget();
257 for (
int i = 0; i < lw->count(); i++ )
259 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