From 2e27474276811b8c5f9d402ce5651fddfcb08bf6 Mon Sep 17 00:00:00 2001 From: Gabriel Bolbotina Date: Fri, 27 Feb 2026 13:58:13 +0200 Subject: [PATCH 1/4] Initial draft --- app/attributes/attributecontroller.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/attributes/attributecontroller.cpp b/app/attributes/attributecontroller.cpp index 7415f731b..b5a3bafc2 100644 --- a/app/attributes/attributecontroller.cpp +++ b/app/attributes/attributecontroller.cpp @@ -127,18 +127,33 @@ void AttributeController::prefillRelationReferenceField() const QList fieldPairs = mLinkedRelation.fieldPairs(); for ( const QgsRelation::FieldPair &fieldPair : fieldPairs ) { + bool fieldFound = false; + QVariant fk = mParentController->featureLayerPair().feature().attribute( fieldPair.referencedField() ); + QString referenceField = fieldPair.referencingField(); QMap>::iterator formItemsIterator = mFormItems.begin(); while ( formItemsIterator != mFormItems.end() ) { std::shared_ptr itemData = formItemsIterator.value(); - if ( itemData->field().name() == fieldPair.referencingField() ) + if ( itemData->field().name() == referenceField ) { - QVariant fk = mParentController->featureLayerPair().feature().attribute( fieldPair.referencedField() ); setFormValue( itemData->id(), fk ); + fieldFound = true; break; } ++formItemsIterator; } + if ( ! fieldFound ) + { + QgsVectorLayer *childLayer = mLinkedRelation.referencingLayer(); + if ( childLayer ) + { + int fieldIndex = childLayer->fields().lookupField( referenceField ); + if ( fieldIndex != -1 ) + { + mFeatureLayerPair.featureRef().setAttribute( fieldIndex, fk ); + } + } + } } } From 303827f4fc17d29fe13baf9812b898ce8fe2ef65 Mon Sep 17 00:00:00 2001 From: Gabriel Bolbotina Date: Mon, 2 Mar 2026 10:08:13 +0200 Subject: [PATCH 2/4] Renamed variables Added comments --- app/attributes/attributecontroller.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/attributes/attributecontroller.cpp b/app/attributes/attributecontroller.cpp index b5a3bafc2..31b984799 100644 --- a/app/attributes/attributecontroller.cpp +++ b/app/attributes/attributecontroller.cpp @@ -128,29 +128,30 @@ void AttributeController::prefillRelationReferenceField() for ( const QgsRelation::FieldPair &fieldPair : fieldPairs ) { bool fieldFound = false; - QVariant fk = mParentController->featureLayerPair().feature().attribute( fieldPair.referencedField() ); - QString referenceField = fieldPair.referencingField(); + QVariant foreignKey = mParentController->featureLayerPair().feature().attribute( fieldPair.referencedField() ); + QString referencingField = fieldPair.referencingField(); QMap>::iterator formItemsIterator = mFormItems.begin(); while ( formItemsIterator != mFormItems.end() ) { std::shared_ptr itemData = formItemsIterator.value(); - if ( itemData->field().name() == referenceField ) + if ( itemData->field().name() == referencingField ) { - setFormValue( itemData->id(), fk ); + setFormValue( itemData->id(), foreignKey ); fieldFound = true; break; } ++formItemsIterator; } + // if the field is not visible on the child feature's form, look for it in the referincing layer if ( ! fieldFound ) { QgsVectorLayer *childLayer = mLinkedRelation.referencingLayer(); if ( childLayer ) { - int fieldIndex = childLayer->fields().lookupField( referenceField ); + int fieldIndex = childLayer->fields().lookupField( referencingField ); if ( fieldIndex != -1 ) { - mFeatureLayerPair.featureRef().setAttribute( fieldIndex, fk ); + mFeatureLayerPair.featureRef().setAttribute( fieldIndex, foreignKey ); } } } From 7471bfbe1a6318392e59efcf2e20b4f3af2f06a9 Mon Sep 17 00:00:00 2001 From: Gabriel Bolbotina Date: Tue, 3 Mar 2026 09:53:57 +0200 Subject: [PATCH 3/4] Simplified prefillRelationReferenceField function --- app/attributes/attributecontroller.cpp | 27 +++++--------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/app/attributes/attributecontroller.cpp b/app/attributes/attributecontroller.cpp index 31b984799..fec3e1cd9 100644 --- a/app/attributes/attributecontroller.cpp +++ b/app/attributes/attributecontroller.cpp @@ -127,32 +127,15 @@ void AttributeController::prefillRelationReferenceField() const QList fieldPairs = mLinkedRelation.fieldPairs(); for ( const QgsRelation::FieldPair &fieldPair : fieldPairs ) { - bool fieldFound = false; QVariant foreignKey = mParentController->featureLayerPair().feature().attribute( fieldPair.referencedField() ); QString referencingField = fieldPair.referencingField(); - QMap>::iterator formItemsIterator = mFormItems.begin(); - while ( formItemsIterator != mFormItems.end() ) - { - std::shared_ptr itemData = formItemsIterator.value(); - if ( itemData->field().name() == referencingField ) - { - setFormValue( itemData->id(), foreignKey ); - fieldFound = true; - break; - } - ++formItemsIterator; - } - // if the field is not visible on the child feature's form, look for it in the referincing layer - if ( ! fieldFound ) + QgsVectorLayer *childLayer = mLinkedRelation.referencingLayer(); + if ( childLayer ) { - QgsVectorLayer *childLayer = mLinkedRelation.referencingLayer(); - if ( childLayer ) + int fieldIndex = childLayer->fields().lookupField( referencingField ); + if ( fieldIndex != -1 ) { - int fieldIndex = childLayer->fields().lookupField( referencingField ); - if ( fieldIndex != -1 ) - { - mFeatureLayerPair.featureRef().setAttribute( fieldIndex, foreignKey ); - } + mFeatureLayerPair.featureRef().setAttribute( fieldIndex, foreignKey ); } } } From 0bc016f03b615422f4044859e6fd814ca3bf6c40 Mon Sep 17 00:00:00 2001 From: Gabriel Bolbotina Date: Tue, 3 Mar 2026 11:02:32 +0200 Subject: [PATCH 4/4] Fixed cppcheck code smells --- app/attributes/rememberattributescontroller.cpp | 2 +- app/attributes/rememberattributescontroller.h | 2 +- app/fieldsmodel.cpp | 2 +- app/fieldsmodel.h | 2 +- core/credentialstore.h | 2 +- core/credentialstoreplaintext.cpp | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/attributes/rememberattributescontroller.cpp b/app/attributes/rememberattributescontroller.cpp index 7d73a217f..37700fedf 100644 --- a/app/attributes/rememberattributescontroller.cpp +++ b/app/attributes/rememberattributescontroller.cpp @@ -79,7 +79,7 @@ bool RememberAttributesController::shouldRememberValue( const QgsVectorLayer *la return from.attributeFilter.at( fieldIndex ); } -bool RememberAttributesController::setShouldRememberValue( const QgsVectorLayer *layer, int fieldIndex, bool shouldRemember ) +bool RememberAttributesController::setShouldRememberValue( const QgsVectorLayer *layer, int fieldIndex, bool shouldRemember ) const { // global switch off of the functionality if ( !mRememberValuesAllowed ) diff --git a/app/attributes/rememberattributescontroller.h b/app/attributes/rememberattributescontroller.h index 1c8970412..20810fe9c 100644 --- a/app/attributes/rememberattributescontroller.h +++ b/app/attributes/rememberattributescontroller.h @@ -56,7 +56,7 @@ class RememberAttributesController : public QObject bool shouldRememberValue( const QgsVectorLayer *layer, int fieldIndex ) const; // Returns whether value was changed - bool setShouldRememberValue( const QgsVectorLayer *layer, int fieldIndex, bool shouldRemember ); + bool setShouldRememberValue( const QgsVectorLayer *layer, int fieldIndex, bool shouldRemember ) const; signals: void rememberValuesAllowedChanged(); diff --git a/app/fieldsmodel.cpp b/app/fieldsmodel.cpp index 49c78965e..5da8c7633 100644 --- a/app/fieldsmodel.cpp +++ b/app/fieldsmodel.cpp @@ -122,7 +122,7 @@ bool FieldsModel::setData( const QModelIndex &index, const QVariant &value, int return false; } -bool FieldsModel::contains( const QString &name ) +bool FieldsModel::contains( const QString &name ) const { for ( int i = 0; i < mFields.count(); ++i ) { diff --git a/app/fieldsmodel.h b/app/fieldsmodel.h index f33ee6135..3efd89277 100644 --- a/app/fieldsmodel.h +++ b/app/fieldsmodel.h @@ -62,7 +62,7 @@ class FieldsModel: public QAbstractListModel QList mFields; - bool contains( const QString &name ); + bool contains( const QString &name ) const; }; #endif // FIELDSMODEL_H diff --git a/core/credentialstore.h b/core/credentialstore.h index 50d7443fc..276f666b2 100644 --- a/core/credentialstore.h +++ b/core/credentialstore.h @@ -45,7 +45,7 @@ class CredentialStore : public QObject const QString &password, const QString &token, const QDateTime &tokenExpiration, - int method = 0 ); + int method = 0 ) const; //! Reads authentication data from keychain and emits a signal with all auth values void readAuthData(); diff --git a/core/credentialstoreplaintext.cpp b/core/credentialstoreplaintext.cpp index 7d8297f72..751a5d062 100644 --- a/core/credentialstoreplaintext.cpp +++ b/core/credentialstoreplaintext.cpp @@ -37,7 +37,7 @@ void CredentialStore::writeAuthData const QString &password, const QString &token, const QDateTime &tokenExpiration, - int method ) + int method ) const { QSettings settings; settings.beginGroup( KEYCHAIN_GROUP );