|
@@ -213,6 +213,16 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry ) |
|
|
|
|
|
const QgsVectorLayerTools *vlTools = mEditorContext.vectorLayerTools(); |
|
|
|
|
|
// Fields of the linking table |
|
|
const QgsFields fields = mRelation.referencingLayer()->fields(); |
|
|
|
|
|
// For generated relations insert the referenced layer field |
|
|
if ( mRelation.type() == QgsRelation::Generated ) |
|
|
{ |
|
|
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation(); |
|
|
keyAttrs.insert( fields.indexFromName( polyRel.referencedLayerField() ), polyRel.layerRepresentation( mRelation.referencedLayer() ) ); |
|
|
} |
|
|
|
|
|
if ( mNmRelation.isValid() ) |
|
|
{ |
|
|
// only normal relations support m:n relation |
|
@@ -221,52 +231,43 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry ) |
|
|
// n:m Relation: first let the user create a new feature on the other table |
|
|
// and autocreate a new linking feature. |
|
|
QgsFeature f; |
|
|
if ( vlTools->addFeature( mNmRelation.referencedLayer(), QgsAttributeMap(), geometry, &f ) ) |
|
|
{ |
|
|
// Fields of the linking table |
|
|
const QgsFields fields = mRelation.referencingLayer()->fields(); |
|
|
|
|
|
// Expression context for the linking table |
|
|
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext(); |
|
|
|
|
|
QgsAttributeMap linkAttributes; |
|
|
const auto constFieldPairs = mRelation.fieldPairs(); |
|
|
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs ) |
|
|
{ |
|
|
int index = fields.indexOf( fieldPair.first ); |
|
|
linkAttributes.insert( index, mFeature.attribute( fieldPair.second ) ); |
|
|
} |
|
|
if ( !vlTools->addFeature( mNmRelation.referencedLayer(), QgsAttributeMap(), geometry, &f ) ) |
|
|
return; |
|
|
|
|
|
const auto constNmFieldPairs = mNmRelation.fieldPairs(); |
|
|
for ( const QgsRelation::FieldPair &fieldPair : constNmFieldPairs ) |
|
|
{ |
|
|
int index = fields.indexOf( fieldPair.first ); |
|
|
linkAttributes.insert( index, f.attribute( fieldPair.second ) ); |
|
|
} |
|
|
QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context ); |
|
|
// Expression context for the linking table |
|
|
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext(); |
|
|
|
|
|
mRelation.referencingLayer()->addFeature( linkFeature ); |
|
|
QgsAttributeMap linkAttributes = keyAttrs; |
|
|
const auto constFieldPairs = mRelation.fieldPairs(); |
|
|
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs ) |
|
|
{ |
|
|
int index = fields.indexOf( fieldPair.first ); |
|
|
linkAttributes.insert( index, mFeature.attribute( fieldPair.second ) ); |
|
|
} |
|
|
|
|
|
updateUi(); |
|
|
const auto constNmFieldPairs = mNmRelation.fieldPairs(); |
|
|
for ( const QgsRelation::FieldPair &fieldPair : constNmFieldPairs ) |
|
|
{ |
|
|
int index = fields.indexOf( fieldPair.first ); |
|
|
linkAttributes.insert( index, f.attribute( fieldPair.second ) ); |
|
|
} |
|
|
QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context ); |
|
|
|
|
|
mRelation.referencingLayer()->addFeature( linkFeature ); |
|
|
} |
|
|
else |
|
|
{ |
|
|
QgsFields fields = mRelation.referencingLayer()->fields(); |
|
|
if ( mRelation.type() == QgsRelation::Generated ) |
|
|
{ |
|
|
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation(); |
|
|
keyAttrs.insert( fields.indexFromName( polyRel.referencedLayerField() ), polyRel.layerRepresentation( mRelation.referencedLayer() ) ); |
|
|
} |
|
|
|
|
|
const auto constFieldPairs = mRelation.fieldPairs(); |
|
|
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs ) |
|
|
{ |
|
|
keyAttrs.insert( fields.indexFromName( fieldPair.referencingField() ), mFeature.attribute( fieldPair.referencedField() ) ); |
|
|
} |
|
|
|
|
|
vlTools->addFeature( mRelation.referencingLayer(), keyAttrs, geometry ); |
|
|
if ( !vlTools->addFeature( mRelation.referencingLayer(), keyAttrs, geometry ) ) |
|
|
return; |
|
|
} |
|
|
|
|
|
updateUi(); |
|
|
} |
|
|
|
|
|
void QgsAbstractRelationEditorWidget::deleteFeature( const QgsFeatureId fid ) |
|
@@ -439,6 +440,16 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted() |
|
|
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext(); |
|
|
|
|
|
QgsAttributeMap linkAttributes; |
|
|
|
|
|
if ( mRelation.type() == QgsRelation::Generated ) |
|
|
{ |
|
|
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation(); |
|
|
Q_ASSERT( polyRel.isValid() ); |
|
|
|
|
|
linkAttributes.insert( fields.indexFromName( polyRel.referencedLayerField() ), |
|
|
polyRel.layerRepresentation( mRelation.referencedLayer() ) ); |
|
|
} |
|
|
|
|
|
const auto constFieldPairs = mRelation.fieldPairs(); |
|
|
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs ) |
|
|
{ |
|
@@ -485,7 +496,7 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted() |
|
|
{ |
|
|
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation(); |
|
|
|
|
|
Q_ASSERT( mRelation.polymorphicRelation().isValid() ); |
|
|
Q_ASSERT( polyRel.isValid() ); |
|
|
|
|
|
mRelation.referencingLayer()->changeAttributeValue( fid, |
|
|
referencingLayer->fields().indexFromName( polyRel.referencedLayerField() ), |
|
@@ -547,8 +558,6 @@ void QgsAbstractRelationEditorWidget::unlinkFeatures( const QgsFeatureIds &fids |
|
|
} |
|
|
|
|
|
mRelation.referencingLayer()->deleteFeatures( fids ); |
|
|
|
|
|
updateUi(); |
|
|
} |
|
|
else |
|
|
{ |
|
@@ -589,6 +598,8 @@ void QgsAbstractRelationEditorWidget::unlinkFeatures( const QgsFeatureIds &fids |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
updateUi(); |
|
|
} |
|
|
|
|
|
void QgsAbstractRelationEditorWidget::updateUi() |
|
|