Skip to content
Permalink
Browse files

Use QgsDefaultValue throughout the code

  • Loading branch information
m-kuhn committed Sep 29, 2017
1 parent cab3a38 commit 7b36287ff379490f250ee377fba3a4941f8e3f1e
@@ -70,6 +70,13 @@ class QgsDefaultValue
applied when a feature is updated or only when it's created.
%End

bool isValid() const;
%Docstring
Returns if this default value is should be applied.
:return: false if the expression is a null string.
:rtype: bool
%End

};

/************************************************************************
@@ -1406,27 +1406,30 @@ Draws a vertex symbol at (screen) coordinates x, y. (Useful to assist vertex edi
:rtype: QVariant
%End

void setDefaultValueExpression( int index, const QString &expression );
void setDefaultValueDefinition( int index, const QgsDefaultValue &definition );
%Docstring
Sets an expression to use when calculating the default value for a field.
Sets the definition of the expression to use when calculating the default value for a field.
\param index field index
\param expression expression to evaluate when calculating default values for field. Pass
\param definition default value definition to use and evaluate
when calculating default values for field. Pass
an empty expression to clear the default.

.. versionadded:: 3.0
.. seealso:: defaultValue()
.. seealso:: defaultValueExpression()
.. seealso:: defaultValueDefinition()
%End

QString defaultValueExpression( int index ) const;
QgsDefaultValue defaultValueDefinition( int index ) const;
%Docstring
Returns the expression used when calculating the default value for a field.
Returns the definition of the expression used when calculating the default value for a field.
\param index field index
:return: expression evaluated when calculating default values for field, or an
:return: definition of the default value with the expression evaluated
when calculating default values for field, or definition with an
empty string if no default is set
.. versionadded:: 3.0
.. seealso:: defaultValue()
.. seealso:: setDefaultValueExpression()
:rtype: str
.. seealso:: setDefaultValueDefinition()
:rtype: QgsDefaultValue
%End

QgsFieldConstraints::Constraints fieldConstraints( int fieldIndex ) const;
@@ -574,7 +574,7 @@ void QgsFieldsProperties::attributeTypeDialog()
attributeTypeDialog.setConstraintExpression( cfg.mConstraint );
attributeTypeDialog.setConstraintExpressionDescription( cfg.mConstraintDescription );
attributeTypeDialog.setConstraintExpressionEnforced( cfg.mConstraintStrength.value( QgsFieldConstraints::ConstraintExpression, QgsFieldConstraints::ConstraintStrengthHard ) == QgsFieldConstraints::ConstraintStrengthHard );
attributeTypeDialog.setDefaultValueExpression( mLayer->defaultValueExpression( index ) );
attributeTypeDialog.setDefaultValueExpression( mLayer->defaultValueDefinition( index ).expression() );

attributeTypeDialog.setEditorWidgetConfig( cfg.mEditorWidgetConfig );
attributeTypeDialog.setEditorWidgetType( cfg.mEditorWidgetType );
@@ -601,7 +601,7 @@ void QgsFieldsProperties::attributeTypeDialog()

cfg.mConstraintDescription = attributeTypeDialog.constraintExpressionDescription();
cfg.mConstraint = attributeTypeDialog.constraintExpression();
mLayer->setDefaultValueExpression( index, attributeTypeDialog.defaultValueExpression() );
mLayer->setDefaultValueDefinition( index, attributeTypeDialog.defaultValueExpression() );

cfg.mEditorWidgetType = attributeTypeDialog.editorWidgetType();
cfg.mEditorWidgetConfig = attributeTypeDialog.editorWidgetConfig();
@@ -47,3 +47,8 @@ void QgsDefaultValue::setApplyOnUpdate( bool applyOnUpdate )
{
mApplyOnUpdate = applyOnUpdate;
}

bool QgsDefaultValue::isValid() const
{
return mExpression.isEmpty();
}
@@ -83,6 +83,12 @@ class CORE_EXPORT QgsDefaultValue
*/
void setApplyOnUpdate( bool applyOnUpdate );

/**
* Returns if this default value is should be applied.
* \returns false if the expression is a null string.
*/
bool isValid() const;

private:
QString mExpression;
bool mApplyOnUpdate = false;
@@ -244,7 +244,7 @@ QgsVectorLayer *QgsVectorLayer::clone() const
layer->setFieldAlias( i, attributeAlias( i ) );
layer->setEditorWidgetSetup( i, editorWidgetSetup( i ) );
layer->setConstraintExpression( i, constraintExpression( i ), constraintDescription( i ) );
layer->setDefaultValueExpression( i, defaultValueExpression( i ) );
layer->setDefaultValueDefinition( i, defaultValueDefinition( i ) );

QMap< QgsFieldConstraints::Constraint, QgsFieldConstraints::ConstraintStrength> constraints = fieldConstraintsAndStrength( i );
auto constraintIt = constraints.constBegin();
@@ -3047,24 +3047,23 @@ QVariant QgsVectorLayer::defaultValue( int index, const QgsFeature &feature, Qgs
return val;
}

void QgsVectorLayer::setDefaultValueExpression( int index, const QString &expression )
void QgsVectorLayer::setDefaultValueDefinition( int index, const QgsDefaultValue &definition )
{
if ( index < 0 || index >= mFields.count() )
return;

if ( expression.isEmpty() )
if ( definition.isValid() )
{
mDefaultExpressionMap.remove( mFields.at( index ).name() );
}
else
{
mDefaultExpressionMap.insert( mFields.at( index ).name(), expression );
mDefaultExpressionMap.insert( mFields.at( index ).name(), definition );
}
updateFields();
}

// TODO to QgsDefaultValue defaultValueDenfinition( int index )
QString QgsVectorLayer::defaultValueExpression( int index ) const
QgsDefaultValue QgsVectorLayer::defaultValueDefinition( int index ) const
{
if ( index < 0 || index >= mFields.count() )
return QString();
@@ -1319,7 +1319,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*/
void updateFields();

/** Returns the calculated default value for the specified field index. The default
/**
* Returns the calculated default value for the specified field index. The default
* value may be taken from a client side default value expression (see setDefaultValueExpression())
* or taken from the underlying data provider.
* \param index field index
@@ -1335,25 +1336,30 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
QVariant defaultValue( int index, const QgsFeature &feature = QgsFeature(),
QgsExpressionContext *context = nullptr ) const;

/** Sets an expression to use when calculating the default value for a field.
/**
* Sets the definition of the expression to use when calculating the default value for a field.
* \param index field index
* \param expression expression to evaluate when calculating default values for field. Pass
* \param definition default value definition to use and evaluate
* when calculating default values for field. Pass
* an empty expression to clear the default.
*
* \since QGIS 3.0
* \see defaultValue()
* \see defaultValueExpression()
* \see defaultValueDefinition()
*/
void setDefaultValueExpression( int index, const QString &expression );
void setDefaultValueDefinition( int index, const QgsDefaultValue &definition );

/** Returns the expression used when calculating the default value for a field.
/**
* Returns the definition of the expression used when calculating the default value for a field.
* \param index field index
* \returns expression evaluated when calculating default values for field, or an
* \returns definition of the default value with the expression evaluated
* when calculating default values for field, or definition with an
* empty string if no default is set
* \since QGIS 3.0
* \see defaultValue()
* \see setDefaultValueExpression()
* \see setDefaultValueDefinition()
*/
QString defaultValueExpression( int index ) const;
QgsDefaultValue defaultValueDefinition( int index ) const;

/**
* Returns any constraints which are present for a specified
@@ -259,7 +259,7 @@ QgsFeature QgsVectorLayerUtils::createFeature( QgsVectorLayer *layer, const QgsG
// in order of priority:

// 1. client side default expression
if ( !layer->defaultValueExpression( idx ).isEmpty() )
if ( !layer->defaultValueDefinition( idx ).isValid() )
{
// client side default expression set - takes precedence over all. Why? Well, this is the only default
// which QGIS users have control over, so we assume that they're deliberately overriding any

0 comments on commit 7b36287

Please sign in to comment.
You can’t perform that action at this time.