QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
32 #include <QMessageBox>
33 #include <QPushButton>
49 setObjectName( QStringLiteral(
"referenced/" ) +
mRelation.
name() );
72 for (
auto it = transactionGroups.constBegin(); it != transactionGroups.constEnd(); ++it )
89 setObjectName( QStringLiteral(
"referenced/" ) +
mRelation.
name() );
247 addedFeatureIds.insert( finalFeature.
id() );
260 const int index = fields.
indexOf( fieldPair.first );
261 linkAttributes.insert( index, editingFeature.attribute( fieldPair.second ) );
267 const int index = fields.
indexOf( fieldPair.first );
268 linkAttributes.insert( index, finalFeature.
attribute( fieldPair.second ) );
280 keyAttrs.insert( fields.
indexFromName( fieldPair.referencingField() ),
mFeatureList.first().attribute( fieldPair.referencedField() ) );
307 return addedFeatureIds;
341 QStringList deletedFeaturesPks;
352 QString linkingFeaturesRequestExpression;
353 if ( !deletedFeaturesPks.empty() )
360 int relatedLinkingFeaturesCount = 0;
363 relatedLinkingFeaturesCount++;
366 if ( deletedFeaturesPks.size() == 1 && relatedLinkingFeaturesCount > 1 )
368 QMessageBox messageBox( QMessageBox::Question, tr(
"Really delete entry?" ), tr(
"The entry on %1 is still linked to %2 features on %3. Do you want to delete it?" ).arg(
mNmRelation.
referencedLayer()->
name(), QLocale().toString( relatedLinkingFeaturesCount ),
mRelation.
referencedLayer()->
name() ), QMessageBox::NoButton,
this );
369 messageBox.addButton( QMessageBox::Cancel );
370 QAbstractButton *deleteButton = messageBox.addButton( tr(
"Delete" ), QMessageBox::AcceptRole );
373 if ( messageBox.clickedButton() != deleteButton )
376 else if ( deletedFeaturesPks.size() > 1 && relatedLinkingFeaturesCount > deletedFeaturesPks.size() )
378 QMessageBox messageBox( QMessageBox::Question, tr(
"Really delete entries?" ), tr(
"The %1 entries on %2 are still linked to %3 features on %4. Do you want to delete them?" ).arg( QLocale().toString( deletedFeaturesPks.size() ),
mNmRelation.
referencedLayer()->
name(), QLocale().toString( relatedLinkingFeaturesCount ),
mRelation.
referencedLayer()->
name() ), QMessageBox::NoButton,
this );
379 messageBox.addButton( QMessageBox::Cancel );
380 QAbstractButton *deleteButton = messageBox.addButton( tr(
"Delete" ), QMessageBox::AcceptRole );
383 if ( messageBox.clickedButton() != deleteButton )
396 QString childrenInfo;
397 int childrenCount = 0;
398 const auto infoContextLayers = infoContext.
layers();
402 childrenInfo += ( tr(
"%n feature(s) on layer \"%1\", ",
nullptr, infoContext.
duplicatedFeatures( chl ).size() ).arg( chl->name() ) );
406 const int res = QMessageBox::question(
this, tr(
"Delete at least %1 feature(s) on other layer(s)" ).arg( childrenCount ),
407 tr(
"Delete %1 feature(s) on layer \"%2\", %3 as well\nand all of its other descendants.\nDelete these features?" ).arg( fids.count() ).arg( layer->
name() ).arg( childrenInfo ),
408 QMessageBox::Yes | QMessageBox::No );
409 if ( res != QMessageBox::Yes )
418 if ( contextLayers.size() > 1 )
420 int deletedCount = 0;
421 QString feedbackMessage;
424 feedbackMessage += tr(
"%1 on layer %2. " ).arg( context.
handledFeatures( contextLayer ).size() ).arg( contextLayer->name() );
451 QgsLogger::warning( tr(
"For 1:n relations is not possible to link to multiple features" ) );
458 QgsFeatureSelectionDlg *selectionDlg =
new QgsFeatureSelectionDlg( layer,
mEditorContext,
this );
459 selectionDlg->setAttribute( Qt::WA_DeleteOnClose );
462 selectionDlg->setWindowTitle( tr(
"Link existing child features for parent %1 \"%2\"" ).arg(
mRelation.
referencedLayer()->
name(), displayString ) );
465 selectionDlg->show();
470 QgsFeatureSelectionDlg *selectionDlg = qobject_cast<QgsFeatureSelectionDlg *>( sender() );
495 .setFilterFids( selectionDlg->selectedFeatures() )
505 const int index = fields.
indexOf( fieldPair.first );
506 linkAttributes.insert( index, editFeature.attribute( fieldPair.second ) );
513 const int index = fields.
indexOf( fieldPair.first );
514 linkAttributes.insert( index, relatedFeature.
attribute( fieldPair.second ) );
528 const auto constNewFeatures = linkFeaturesList;
529 for (
const QgsFeature &f : constNewFeatures )
537 QgsLogger::warning( tr(
"For 1:n relations is not possible to link to multiple features" ) );
541 QMap<int, QVariant> keys;
546 const QVariant val =
mFeatureList.first().attribute( fieldPair.referencedField() );
547 keys.insert( idx, val );
550 const auto constSelectedFeatures = selectionDlg->selectedFeatures();
565 QMapIterator<int, QVariant> it( keys );
566 while ( it.hasNext() )
593 .setFilterFids( fids )
605 QStringList featureFilters;
611 const QString filter = QStringLiteral(
"(%1) AND (%2)" ).arg(
612 featureFilters.join( QLatin1String(
" OR " ) ),
613 filters.join( QLatin1String(
" OR " ) ) );
617 .setFilterExpression( filter ) );
631 QMap<int, QgsField> keyFields;
638 QgsDebugMsg( QStringLiteral(
"referencing field %1 not found" ).arg( fieldPair.referencingField() ) );
642 keyFields.insert( idx, fld );
645 const auto constFeatureids = fids;
660 QMapIterator<int, QgsField> it( keyFields );
661 while ( it.hasNext() )
684 Q_UNUSED( newRelation )
685 Q_UNUSED( newFeature )
693 Q_UNUSED( newRelation )
694 Q_UNUSED( newNmRelation )
733 , mRelation( relation )
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsRelationManager relationManager
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a list of features to the sink.
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection)
Selects matching features using a list of feature IDs.
A polymorphic relation consists of the same properties like a normal relation except for the referenc...
@ Normal
A normal relation.
#define QgsDebugMsgLevel(str, level)
Context for cascade delete features.
Container of fields for a vector layer.
QMap< QPair< QString, QString >, QgsTransactionGroup * > transactionGroups()
Map of transaction groups.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
QgsAttributeList referencedFields() const
Returns a list of attributes used to form the referenced fields (most likely primary key) on the refe...
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Encapsulate geometry and attributes for new features, to be passed to createFeatures.
static QgsFeature duplicateFeature(QgsVectorLayer *layer, const QgsFeature &feature, QgsProject *project, QgsDuplicateFeatureContext &duplicateFeatureContext, const int maxDepth=0, int depth=0, QList< QgsVectorLayer * > referencedLayersBranch=QList< QgsVectorLayer * >())
Duplicates a feature and it's children (one level deep).
QgsPolymorphicRelation polymorphicRelation
RelationType type() const
Returns the type of the relation.
#define FID_TO_STRING(fid)
QList< int > QgsAttributeList
bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant(), bool skipDefaultValues=false)
Changes an attribute value for a feature (but does not immediately commit the changes).
QgsVectorLayer referencingLayer
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
static QString quotedValue(const QVariant &value)
Returns a string representation of a literal value, including appropriate quotations where required.
QgsFeatureIds handledFeatures(QgsVectorLayer *layer) const
Returns a list of feature IDs from the specified layer affected by the delete operation.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QList< QgsVectorLayer * > layers() const
Returns all the layers on which features have been duplicated.
QList< QgsVectorLayerUtils::QgsFeatureData > QgsFeaturesDataList
Alias for list of QgsFeatureData.
QgsVectorLayer referencedLayer
QgsExpression * filterExpression() const
Returns the filter expression (if set).
Contains mainly the QMap with QgsVectorLayer and QgsFeatureIds do list all the duplicated features.
QList< QgsFeature > QgsFeatureList
QMap< int, QVariant > QgsAttributeMap
static void warning(const QString &msg)
Goes to qWarning.
QString layerRepresentation(const QgsVectorLayer *layer) const
Returns layer representation as evaluated string.
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
QSet< QgsFeatureId > QgsFeatureIds
Defines a relation between matching fields of the two involved tables of a relation.
QgsField field(int fieldIdx) const
Returns the field at particular index (must be in range 0..N-1).
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names o...
QgsFeatureRequest getRelatedFeaturesRequest(const QgsFeature &feature) const
Creates a request to return all the features on the referencing (child) layer which have a foreign ke...
bool nextFeature(QgsFeature &f)
bool deleteFeatures(const QgsFeatureIds &fids, DeleteContext *context=nullptr)
Deletes a set of features from the layer (but does not commit it)
A geometry is the spatial representation of a feature.
Represents a vector layer which manages a vector based data sets.
QList< QgsVectorLayer * > handledLayers(bool includeAuxiliaryLayers=true) const
Returns a list of all layers affected by the delete operation.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
static bool impactsCascadeFeatures(const QgsVectorLayer *layer, const QgsFeatureIds &fids, const QgsProject *project, QgsDuplicateFeatureContext &context, QgsVectorLayerUtils::CascadedFeatureFlags flags=QgsVectorLayerUtils::CascadedFeatureFlags())
static QgsFeatureList createFeatures(const QgsVectorLayer *layer, const QgsFeaturesDataList &featuresData, QgsExpressionContext *context=nullptr)
Creates a set of new features ready for insertion into a layer.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
QString referencedLayerField
QgsFeatureIds duplicatedFeatures(QgsVectorLayer *layer) const
Returns the duplicated features in the given layer.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
This class contains context information for attribute editor widgets. It will be passed to embedded w...
Wrapper for iterator of features from vector data provider or vector layer.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a single feature to the sink.
QgsMessageBar * mainMessageBar()
Returns the main message bar.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
QString expression() const
Returns the original, unmodified expression string.
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
const QgsVectorLayerTools * vectorLayerTools() const
Returns the associated vector layer tools.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.
void setFormFeature(const QgsFeature &feature)
Set current feature for the currently edited form or table row.
@ Generated
A generated relation is a child of a polymorphic relation.
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
static QString getFeatureDisplayString(const QgsVectorLayer *layer, const QgsFeature &feature)
Encapsulate a field in an attribute table or data source.