16 #include <QRegularExpression> 29 std::unique_ptr<QgsExpression> expression;
36 expression.reset(
new QgsExpression( fieldOrExpression ) );
39 if ( expression->hasParserError() || !expression->prepare( &context ) )
48 lst.insert( fieldOrExpression );
50 lst = expression->referencedColumns();
53 .
setFlags( ( expression && expression->needsGeometry() ) ?
71 QList<QVariant> values;
75 std::unique_ptr<QgsExpression> expression;
82 expression.reset(
new QgsExpression( fieldOrExpression ) );
92 QVariant v = expression->evaluate( &context );
111 QList<double> values;
116 QList<QVariant> variantValues =
getValues( layer, fieldOrExpression, ok, selectedOnly, feedback );
121 Q_FOREACH (
const QVariant &value, variantValues )
123 double val = value.toDouble( &convertOk );
126 else if ( value.isNull() )
147 if ( fieldIndex < 0 || fieldIndex >= fields.
count() )
150 QString fieldName = fields.
at( fieldIndex ).
name();
158 int limit = ignoreIds.size() + 1;
161 request.
setFilterExpression( QStringLiteral(
"%1=%2" ).arg( QgsExpression::quotedColumnRef( fieldName ),
162 QgsExpression::quotedValue( value ) ) );
168 if ( ignoreIds.contains( feat.
id() ) )
184 if ( fieldIndex < 0 || fieldIndex >= fields.
count() )
192 QVariant newVar( maxVal.toLongLong() + 1 );
200 switch ( field.
type() )
202 case QVariant::String:
205 if ( seed.isValid() )
206 base = seed.toString();
208 if ( !base.isEmpty() )
211 QRegularExpression rx( QStringLiteral(
"(.*)_\\d+" ) );
212 QRegularExpressionMatch match = rx.match( base );
213 if ( match.hasMatch() )
215 base = match.captured( 1 );
227 base = f.
attribute( fieldIndex ).toString();
234 if ( !base.isEmpty() && !vals.contains( base ) )
237 for (
int i = 1; i < 10000; ++i )
239 QString testVal = base +
'_' + QString::number( i );
240 if ( !vals.contains( testVal ) )
263 if ( attributeIndex < 0 || attributeIndex >= layer->
fields().
count() )
268 QVariant value = feature.
attribute( attributeIndex );
283 valid = expr.evaluate( &context ).toBool();
285 if ( expr.hasParserError() )
287 errors << QObject::tr(
"parser error: %1" ).arg( expr.parserErrorString() );
289 else if ( expr.hasEvalError() )
291 errors << QObject::tr(
"evaluation error: %1" ).arg( expr.evalErrorString() );
313 valid = valid && !value.isNull();
315 if ( value.isNull() )
317 errors << QObject::tr(
"value is NULL" );
337 valid = valid && !alreadyExists;
341 errors << QObject::tr(
"value is not unique" );
358 std::unique_ptr< QgsExpressionContext > tempContext;
363 evalContext = tempContext.get();
374 for (
int idx = 0; idx < fields.
count(); ++idx )
377 bool checkUnique =
true;
387 v = layer->
defaultValue( idx, newFeature, evalContext );
396 if ( !providerDefault.isEmpty() )
418 if ( !v.isValid() && attributes.contains( idx ) )
420 v = attributes.value( idx );
432 if ( uniqueValue.isValid() )
458 for (
int src = 0; src < srcAttr.count(); ++src )
460 dstAttr[ src ] = srcAttr.at( src );
477 while ( relatedFeaturesIt.
nextFeature( childFeature ) )
480 relation.referencingLayer()->startEditing();
482 const auto pairs = relation.fieldPairs();
488 childFeatureIds.insert(
duplicateFeature( relation.referencingLayer(), childFeature, project, depth, duplicateFeatureContext ).
id() );
492 duplicateFeatureContext.setDuplicatedFeatures( relation.referencingLayer(), childFeatureIds );
503 QList<QgsVectorLayer *> layers;
504 QMap<QgsVectorLayer *, QgsFeatureIds>::const_iterator i;
505 for ( i = mDuplicatedFeatures.begin(); i != mDuplicatedFeatures.end(); ++i )
506 layers.append( i.key() );
512 return mDuplicatedFeatures[layer];
517 mDuplicatedFeatures.insert( layer, ids );
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
Wrapper for iterator of features from vector data provider or vector layer.
static QgsFeature createFeature(QgsVectorLayer *layer, const QgsGeometry &geometry=QgsGeometry(), const QgsAttributeMap &attributes=QgsAttributeMap(), QgsExpressionContext *context=nullptr)
Creates a new feature ready for insertion into a layer.
static bool valueExists(const QgsVectorLayer *layer, int fieldIndex, const QVariant &value, const QgsFeatureIds &ignoreIds=QgsFeatureIds())
Returns true if the specified value already exists within a field.
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
ConstraintStrength constraintStrength(Constraint constraint) const
Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint is not pres...
bool isValid() const
Returns if this default value should be applied.
Contains mainly the QMap with QgsVectorLayer and QgsFeatureIds do list all the duplicated features...
QList< QgsVectorLayer * > layers() const
Returns all the layers on which features have been duplicated.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QSet< QgsFeatureId > QgsFeatureIds
ConstraintOrigin
Origin of constraints.
static QList< double > getDoubleValues(const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly=false, int *nullCount=nullptr, QgsFeedback *feedback=nullptr)
Fetches all double values from a specified field name or expression.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
static QList< QVariant > getValues(const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly=false, QgsFeedback *feedback=nullptr)
Fetches all values from a specified field name or expression.
Stores information about constraints which may be present on a field.
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
static bool validateAttribute(const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors, QgsFieldConstraints::ConstraintStrength strength=QgsFieldConstraints::ConstraintStrengthNotSet, QgsFieldConstraints::ConstraintOrigin origin=QgsFieldConstraints::ConstraintOriginNotSet)
Tests an attribute value to check whether it passes all constraints which are present on the correspo...
bool isEditable() const override
Returns true if the provider is in editing mode.
int count() const
Return number of items.
static QgsFeatureIterator getValuesIterator(const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly)
Create a feature iterator for a specified field name or expression.
Constraint was set at data provider.
Field has an expression constraint set. See constraintExpression().
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
Base class for feedback objects to be used for cancelation of something running in a worker thread...
virtual bool skipConstraintCheck(int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value=QVariant()) const
Returns true if a constraint check should be skipped for a specified field (e.g., if the value return...
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QStringList uniqueStringsMatching(int index, const QString &substring, int limit=-1, QgsFeedback *feedback=nullptr) const
Returns unique string values of an attribute which contain a specified subset string.
Defines a relation between matching fields of the two involved tables of a relation.
QgsFields fields() const override
Returns the list of fields of this layer.
QgsFeatureIterator getSelectedFeatures(QgsFeatureRequest request=QgsFeatureRequest()) const
Get an iterator of the selected features.
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void seed(uint32_t value)
QMap< int, QVariant > QgsAttributeMap
This class wraps a request for features to a vector layer (or directly its vector data provider)...
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index...
Reads and writes project states.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
QList< QgsRelation > referencedRelations(QgsVectorLayer *layer=nullptr) const
Get all relations where this layer is the referenced part (i.e.
Encapsulate a field in an attribute table or data source.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
QgsRelationManager relationManager
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static QVariant createUniqueValue(const QgsVectorLayer *layer, int fieldIndex, const QVariant &seed=QVariant())
Returns a new attribute value for the specified field index which is guaranteed to be unique...
void setValid(bool validity)
Sets the validity of the feature.
QgsDefaultValue defaultValueDefinition(int index) const
Returns the definition of the expression used when calculating the default value for a field...
QVariant maximumValue(int index) const override
Returns the maximum value for an attribute column or an invalid variant in case of error...
QgsFieldConstraints constraints
bool isCanceled() const
Tells whether the operation has been canceled already.
ConstraintStrength
Strength of constraints.
static QgsFeature duplicateFeature(QgsVectorLayer *layer, const QgsFeature &feature, QgsProject *project, int depth, QgsDuplicateFeatureContext &duplicateFeatureContext)
Duplicates a feature and it's children (one level deep).
QgsFeatureIds duplicatedFeatures(QgsVectorLayer *layer) const
Returns the duplicated features in the given layer.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
QgsVectorDataProvider * dataProvider() override
Returns the layer's data provider.
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
void appendScopes(const QList< QgsExpressionContextScope *> &scopes)
Appends a list of scopes to the end of the context.
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
QString constraintExpression() const
Returns the constraint expression for the field, if set.
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
Adds a single feature to the sink.
Fix relation, related elements are part of the parent and a parent copy will copy any children or del...
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Set flags that affect how features will be fetched.
QVariant defaultValue(int index, const QgsFeature &feature=QgsFeature(), QgsExpressionContext *context=nullptr) const
Returns the calculated default value for the specified field index.
Field must have a unique value.
virtual QVariant defaultValue(int fieldIndex) const
Returns any literal default values which are present at the provider for a specified field index...