Skip to content
Permalink
Browse files
Remember just added features for multiple selection
  • Loading branch information
domi4484 committed Nov 8, 2021
1 parent bab8618 commit 37bc7d1b087b62f4cabff4909874045eff29f66c
Showing with 41 additions and 32 deletions.
  1. +22 −22 src/gui/qgsabstractrelationeditorwidget.cpp
  2. +18 −10 src/gui/qgsrelationeditorwidget.cpp
  3. +1 −0 src/gui/qgsrelationeditorwidget.h
@@ -242,7 +242,8 @@ QgsFeatureIds QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &ge
QgsAttributeMap linkAttributes = keyAttrs;
const auto constFieldPairs = mRelation.fieldPairs();

for ( const QgsFeature &editingFeature : mFeatureList )
QgsVectorLayerUtils::QgsFeaturesDataList linkFeatureDataList;
for ( const QgsFeature &editingFeature : std::as_const( mFeatureList ) )
{
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
{
@@ -257,10 +258,10 @@ QgsFeatureIds QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &ge
linkAttributes.insert( index, finalFeature.attribute( fieldPair.second ) );
}

QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context );

mRelation.referencingLayer()->addFeature( linkFeature );
linkFeatureDataList.append( QgsVectorLayerUtils::QgsFeatureData( QgsGeometry(), linkAttributes ) );
}
QgsFeatureList linkFeatureList = QgsVectorLayerUtils::createFeatures( mRelation.referencingLayer(), linkFeatureDataList, &context );
mRelation.referencingLayer()->addFeatures( linkFeatureList );
}
else
{
@@ -275,7 +276,7 @@ QgsFeatureIds QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &ge
addedFeatureIds.insert( linkFeature.id() );

// In multiedit add to other features to but whitout dialog
for ( const QgsFeature &feature : mFeatureList )
for ( const QgsFeature &feature : std::as_const( mFeatureList ) )
{
// First feature already added
if ( mFeatureList.first() == feature )
@@ -457,21 +458,9 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
// only normal relations support m:n relation
Q_ASSERT( mNmRelation.type() == QgsRelation::Normal );

QgsFeatureIterator it = mNmRelation.referencedLayer()->getFeatures(
QgsFeatureRequest()
.setFilterFids( selectionDlg->selectedFeatures() )
.setSubsetOfAttributes( mNmRelation.referencedFields() ) );

QgsFeature relatedFeature;

QgsFeatureList newFeatures;

// Fields of the linking table
const QgsFields fields = mRelation.referencingLayer()->fields();

// Expression context for the linking table
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();

QgsAttributeMap linkAttributes;

if ( mRelation.type() == QgsRelation::Generated )
@@ -483,9 +472,15 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
polyRel.layerRepresentation( mRelation.referencedLayer() ) );
}

QgsVectorLayerUtils::QgsFeaturesDataList linkFeatureDataList;
QgsFeature relatedFeature;
QgsFeatureIterator it = mNmRelation.referencedLayer()->getFeatures(
QgsFeatureRequest()
.setFilterFids( selectionDlg->selectedFeatures() )
.setSubsetOfAttributes( mNmRelation.referencedFields() ) );
while ( it.nextFeature( relatedFeature ) )
{
for ( const QgsFeature &editFeature : mFeatureList )
for ( const QgsFeature &editFeature : std::as_const( mFeatureList ) )
{
{
const auto constFieldPairs = mRelation.fieldPairs();
@@ -502,14 +497,19 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
const int index = fields.indexOf( fieldPair.first );
linkAttributes.insert( index, relatedFeature.attribute( fieldPair.second ) );
}
const QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context );
newFeatures << linkFeature;

linkFeatureDataList.append( QgsVectorLayerUtils::QgsFeatureData( QgsGeometry(), linkAttributes ) );
}
}

mRelation.referencingLayer()->addFeatures( newFeatures );
// Expression context for the linking table
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();

QgsFeatureList linkFeaturesList = QgsVectorLayerUtils::createFeatures( mRelation.referencingLayer(), linkFeatureDataList, &context );

mRelation.referencingLayer()->addFeatures( linkFeaturesList );
QgsFeatureIds ids;
const auto constNewFeatures = newFeatures;
const auto constNewFeatures = linkFeaturesList;
for ( const QgsFeature &f : constNewFeatures )
ids << f.id();
mRelation.referencingLayer()->selectByIds( ids );
@@ -387,6 +387,7 @@ void QgsRelationEditorWidget::addFeature()
return;

mMultiEditTreeWidget->blockSignals( true );
mMultiEdit1NJustAddedIds = addedFeatures;
QTreeWidgetItemIterator treeWidgetItemIterator( mMultiEditTreeWidget );
while ( *treeWidgetItemIterator )
{
@@ -452,22 +453,17 @@ void QgsRelationEditorWidget::multiEditItemSelectionChanged()
{
const QList<QTreeWidgetItem *> selectedItems = mMultiEditTreeWidget->selectedItems();

if ( ! mNmRelation.isValid() )
{
updateButtons();
return;
}

// Select all items pointing to the same feature
// but only if we are not deselecting.
if ( selectedItems.size() == 1
&& mMultiEditPreviousSelectedItems.size() <= 1 )
{
if ( selectedItems.first()->data( 0, static_cast<int>( MultiEditTreeWidgetRole::FeatureType ) ).toInt() == static_cast<int>( MultiEditFeatureType::Child ) )
{
QgsFeatureId featureId = selectedItems.first()->data( 0, static_cast<int>( MultiEditTreeWidgetRole::FeatureId ) ).toInt();

mMultiEditTreeWidget->blockSignals( true );

const QgsFeatureId featureIdSelectedItem = selectedItems.first()->data( 0, static_cast<int>( MultiEditTreeWidgetRole::FeatureId ) ).toInt();

QTreeWidgetItemIterator treeWidgetItemIterator( mMultiEditTreeWidget );
while ( *treeWidgetItemIterator )
{
@@ -477,8 +473,20 @@ void QgsRelationEditorWidget::multiEditItemSelectionChanged()
continue;
}

if ( featureId == ( *treeWidgetItemIterator )->data( 0, static_cast<int>( MultiEditTreeWidgetRole::FeatureId ) ).toInt() )
( *treeWidgetItemIterator )->setSelected( true );
const QgsFeatureId featureIdCurrentItem = ( *treeWidgetItemIterator )->data( 0, static_cast<int>( MultiEditTreeWidgetRole::FeatureId ) ).toInt();
if ( mNmRelation.isValid() )
{
if ( featureIdSelectedItem == featureIdCurrentItem )
( *treeWidgetItemIterator )->setSelected( true );
}
else
{
if ( ! mMultiEdit1NJustAddedIds.contains( featureIdSelectedItem ) )
break;

if ( mMultiEdit1NJustAddedIds.contains( featureIdCurrentItem ) )
( *treeWidgetItemIterator )->setSelected( true );
}

++treeWidgetItemIterator;
}
@@ -256,6 +256,7 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsAbstractRelationEditorWidge
bool mShowFirstFeature = true;

QList<QTreeWidgetItem *> mMultiEditPreviousSelectedItems;
QgsFeatureIds mMultiEdit1NJustAddedIds;
};


0 comments on commit 37bc7d1

Please sign in to comment.