Skip to content

Commit

Permalink
Use QgsDefaultValue throughout the code
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Sep 29, 2017
1 parent cab3a38 commit 7b36287
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 27 deletions.
7 changes: 7 additions & 0 deletions python/core/qgsdefaultvalue.sip
Expand Up @@ -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

};

/************************************************************************
Expand Down
21 changes: 12 additions & 9 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsfieldsproperties.cpp
Expand Up @@ -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 );
Expand All @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsdefaultvalue.cpp
Expand Up @@ -47,3 +47,8 @@ void QgsDefaultValue::setApplyOnUpdate( bool applyOnUpdate )
{
mApplyOnUpdate = applyOnUpdate;
}

bool QgsDefaultValue::isValid() const
{
return mExpression.isEmpty();
}
6 changes: 6 additions & 0 deletions src/core/qgsdefaultvalue.h
Expand Up @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 15 additions & 9 deletions src/core/qgsvectorlayer.h
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerutils.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 7b36287

Please sign in to comment.