Skip to content

Commit d6eb7ba

Browse files
committedSep 29, 2017
Add "apply on update" option to default values
1 parent cb8ae89 commit d6eb7ba

File tree

10 files changed

+131
-28
lines changed

10 files changed

+131
-28
lines changed
 

‎python/core/qgsfield.sip

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,17 @@ Gets variant type of the field as it will be retrieved from data source
178178
Set the field comment
179179
%End
180180

181-
QString defaultValueExpression() const;
181+
QgsDefaultValue defaultValue() const;
182182
%Docstring
183183
Returns the expression used when calculating the default value for the field.
184184
:return: expression evaluated when calculating default values for field, or an
185185
empty string if no default is set
186186
.. versionadded:: 3.0
187187
.. seealso:: setDefaultValueExpression()
188-
:rtype: str
188+
:rtype: QgsDefaultValue
189189
%End
190190

191-
void setDefaultValueExpression( const QString &expression );
191+
void setDefaultValue( const QgsDefaultValue &defaultValue );
192192
%Docstring
193193
Sets an expression to use when calculating the default value for the field.
194194
\param expression expression to evaluate when calculating default values for field. Pass

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ SET(QGIS_CORE_SRCS
160160
qgsdatetimestatisticalsummary.cpp
161161
qgsdatumtransformstore.cpp
162162
qgsdbfilterproxymodel.cpp
163+
qgsdefaultvalue.cpp
163164
qgsdiagramrenderer.cpp
164165
qgsdistancearea.cpp
165166
qgseditformconfig.cpp
@@ -805,6 +806,7 @@ SET(QGIS_CORE_HDRS
805806
qgsdatetimestatisticalsummary.h
806807
qgsdatumtransformstore.h
807808
qgsdbfilterproxymodel.h
809+
qgsdefaultvalue.h
808810
qgsdiagramrenderer.h
809811
qgsdistancearea.h
810812
qgseditformconfig.h

‎src/core/qgsdefaultvalue.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/***************************************************************************
2+
qgsdefaultvalue.cpp
3+
4+
---------------------
5+
begin : 19.9.2017
6+
copyright : (C) 2017 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#include "qgsdefaultvalue.h"
17+
18+
QgsDefaultValue::QgsDefaultValue( const QString &expression, bool applyOnUpdate )
19+
: mExpression( expression )
20+
, mApplyOnUpdate( applyOnUpdate )
21+
{
22+
23+
}
24+
25+
bool QgsDefaultValue::operator==( const QgsDefaultValue &other ) const
26+
{
27+
return mExpression == other.mExpression
28+
&& mApplyOnUpdate == other.mApplyOnUpdate;
29+
}
30+
31+
QString QgsDefaultValue::expression() const
32+
{
33+
return mExpression;
34+
}
35+
36+
void QgsDefaultValue::setExpression( const QString &expression )
37+
{
38+
mExpression = expression;
39+
}
40+
41+
bool QgsDefaultValue::applyOnUpdate() const
42+
{
43+
return mApplyOnUpdate;
44+
}
45+
46+
void QgsDefaultValue::setApplyOnUpdate( bool applyOnUpdate )
47+
{
48+
mApplyOnUpdate = applyOnUpdate;
49+
}

‎src/core/qgsdefaultvalue.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/***************************************************************************
2+
qgsdefaultvalue.h
3+
4+
---------------------
5+
begin : 19.9.2017
6+
copyright : (C) 2017 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSDEFAULTVALUE_H
17+
#define QGSDEFAULTVALUE_H
18+
19+
#include "qgis_core.h"
20+
21+
#include <QString>
22+
#include <QObject>
23+
24+
class CORE_EXPORT QgsDefaultValue
25+
{
26+
Q_GADGET
27+
28+
Q_PROPERTY( QString expression READ expression WRITE setExpression )
29+
Q_PROPERTY( bool applyOnUpdate READ applyOnUpdate WRITE setApplyOnUpdate )
30+
31+
public:
32+
QgsDefaultValue( const QString &expression = QString(), bool applyOnUpdate = false );
33+
bool operator==( const QgsDefaultValue &other ) const;
34+
35+
QString expression() const;
36+
void setExpression( const QString &expression );
37+
38+
bool applyOnUpdate() const;
39+
void setApplyOnUpdate( bool applyOnUpdate );
40+
41+
private:
42+
QString mExpression;
43+
bool mApplyOnUpdate = false;
44+
};
45+
46+
#endif // QGSDEFAULTVALUE_H

‎src/core/qgsfield.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ void QgsField::setComment( const QString &comment )
165165
d->comment = comment;
166166
}
167167

168-
QString QgsField::defaultValueExpression() const
168+
QgsDefaultValue QgsField::defaultValue() const
169169
{
170-
return d->defaultValueExpression;
170+
return d->defaultValue;
171171
}
172172

173-
void QgsField::setDefaultValueExpression( const QString &expression )
173+
void QgsField::setDefaultValue( const QgsDefaultValue &defaultValue )
174174
{
175-
d->defaultValueExpression = expression;
175+
d->defaultValue = defaultValue;
176176
}
177177

178178
void QgsField::setConstraints( const QgsFieldConstraints &constraints )
@@ -307,7 +307,8 @@ QDataStream &operator<<( QDataStream &out, const QgsField &field )
307307
out << field.precision();
308308
out << field.comment();
309309
out << field.alias();
310-
out << field.defaultValueExpression();
310+
out << field.defaultValue().expression();
311+
out << field.defaultValue().applyOnUpdate();
311312
out << field.constraints().constraints();
312313
out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) );
313314
out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ) );
@@ -335,7 +336,7 @@ QDataStream &operator>>( QDataStream &in, QgsField &field )
335336
field.setPrecision( static_cast< int >( precision ) );
336337
field.setComment( comment );
337338
field.setAlias( alias );
338-
field.setDefaultValueExpression( defaultValueExpression );
339+
field.setDefaultValue( defaultValueExpression );
339340
QgsFieldConstraints fieldConstraints;
340341
if ( constraints & QgsFieldConstraints::ConstraintNotNull )
341342
{

‎src/core/qgsfield.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ typedef QList<int> QgsAttributeList SIP_SKIP;
3434

3535
#include "qgseditorwidgetsetup.h"
3636
#include "qgsfieldconstraints.h"
37+
#include "qgsdefaultvalue.h"
3738

3839
/** \class QgsField
3940
* \ingroup core
@@ -53,7 +54,7 @@ class CORE_EXPORT QgsField
5354
Q_PROPERTY( QString comment READ comment WRITE setComment )
5455
Q_PROPERTY( QString name READ name WRITE setName )
5556
Q_PROPERTY( QString alias READ alias WRITE setAlias )
56-
Q_PROPERTY( QString defaultValueExpression READ defaultValueExpression WRITE setDefaultValueExpression )
57+
Q_PROPERTY( QgsDefaultValue defaultValue READ defaultValue WRITE setDefaultValue )
5758
Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
5859

5960
public:
@@ -199,15 +200,15 @@ class CORE_EXPORT QgsField
199200
* \since QGIS 3.0
200201
* \see setDefaultValueExpression()
201202
*/
202-
QString defaultValueExpression() const;
203+
QgsDefaultValue defaultValue() const;
203204

204205
/** Sets an expression to use when calculating the default value for the field.
205206
* \param expression expression to evaluate when calculating default values for field. Pass
206207
* an empty expression to clear the default.
207208
* \since QGIS 3.0
208209
* \see defaultValueExpression()
209210
*/
210-
void setDefaultValueExpression( const QString &expression );
211+
void setDefaultValue( const QgsDefaultValue &defaultValue );
211212

212213
/**
213214
* Returns constraints which are present for the field.

‎src/core/qgsfield_p.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "qgsfieldconstraints.h"
3333
#include "qgseditorwidgetsetup.h"
34+
#include "qgsdefaultvalue.h"
3435
#include <QString>
3536
#include <QVariant>
3637
#include <QSharedData>
@@ -72,7 +73,7 @@ class QgsFieldPrivate : public QSharedData
7273
, precision( other.precision )
7374
, comment( other.comment )
7475
, alias( other.alias )
75-
, defaultValueExpression( other.defaultValueExpression )
76+
, defaultValue( other.defaultValue )
7677
, constraints( other.constraints )
7778
{
7879
}
@@ -83,7 +84,7 @@ class QgsFieldPrivate : public QSharedData
8384
{
8485
return ( ( name == other.name ) && ( type == other.type ) && ( subType == other.subType )
8586
&& ( length == other.length ) && ( precision == other.precision )
86-
&& ( alias == other.alias ) && ( defaultValueExpression == other.defaultValueExpression )
87+
&& ( alias == other.alias ) && ( defaultValue == other.defaultValue )
8788
&& ( constraints == other.constraints ) );
8889
}
8990

@@ -111,8 +112,8 @@ class QgsFieldPrivate : public QSharedData
111112
//! Alias for field name (friendly name shown to users)
112113
QString alias;
113114

114-
//! Default value expression
115-
QString defaultValueExpression;
115+
//! Default value
116+
QgsDefaultValue defaultValue;
116117

117118
//! Field constraints
118119
QgsFieldConstraints constraints;

‎src/core/qgsvectorlayer.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,10 +1725,11 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
17251725

17261726
QString field = defaultElem.attribute( QStringLiteral( "field" ), QString() );
17271727
QString expression = defaultElem.attribute( QStringLiteral( "expression" ), QString() );
1728+
bool applyOnUpdate = defaultElem.attribute( QStringLiteral( "applyOnUpdate" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
17281729
if ( field.isEmpty() || expression.isEmpty() )
17291730
continue;
17301731

1731-
mDefaultExpressionMap.insert( field, expression );
1732+
mDefaultExpressionMap.insert( field, QgsDefaultValue( expression, applyOnUpdate ) );
17321733
}
17331734
}
17341735

@@ -2074,7 +2075,8 @@ bool QgsVectorLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString
20742075
{
20752076
QDomElement defaultElem = doc.createElement( QStringLiteral( "default" ) );
20762077
defaultElem.setAttribute( QStringLiteral( "field" ), field.name() );
2077-
defaultElem.setAttribute( QStringLiteral( "expression" ), field.defaultValueExpression() );
2078+
defaultElem.setAttribute( QStringLiteral( "expression" ), field.defaultValue().expression() );
2079+
defaultElem.setAttribute( QStringLiteral( "applyOnUpdate" ), field.defaultValue().applyOnUpdate() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
20782080
defaultsElem.appendChild( defaultElem );
20792081
}
20802082
node.appendChild( defaultsElem );
@@ -2918,14 +2920,14 @@ void QgsVectorLayer::updateFields()
29182920

29192921
mFields[ index ].setAlias( aliasIt.value() );
29202922
}
2921-
QMap< QString, QString >::const_iterator defaultIt = mDefaultExpressionMap.constBegin();
2923+
QMap< QString, QgsDefaultValue >::const_iterator defaultIt = mDefaultExpressionMap.constBegin();
29222924
for ( ; defaultIt != mDefaultExpressionMap.constEnd(); ++defaultIt )
29232925
{
29242926
int index = mFields.lookupField( defaultIt.key() );
29252927
if ( index < 0 )
29262928
continue;
29272929

2928-
mFields[ index ].setDefaultValueExpression( defaultIt.value() );
2930+
mFields[ index ].setDefaultValue( defaultIt.value() );
29292931
}
29302932

29312933
QMap< QString, QgsFieldConstraints::Constraints >::const_iterator constraintIt = mFieldConstraints.constBegin();
@@ -3004,7 +3006,7 @@ QVariant QgsVectorLayer::defaultValue( int index, const QgsFeature &feature, Qgs
30043006
if ( index < 0 || index >= mFields.count() )
30053007
return QVariant();
30063008

3007-
QString expression = mFields.at( index ).defaultValueExpression();
3009+
QString expression = mFields.at( index ).defaultValue().expression();
30083010
if ( expression.isEmpty() )
30093011
return mDataProvider->defaultValue( index );
30103012

@@ -3061,12 +3063,13 @@ void QgsVectorLayer::setDefaultValueExpression( int index, const QString &expres
30613063
updateFields();
30623064
}
30633065

3066+
// TODO to QgsDefaultValue defaultValueDenfinition( int index )
30643067
QString QgsVectorLayer::defaultValueExpression( int index ) const
30653068
{
30663069
if ( index < 0 || index >= mFields.count() )
30673070
return QString();
30683071
else
3069-
return mFields.at( index ).defaultValueExpression();
3072+
return mFields.at( index ).defaultValue().expression();
30703073
}
30713074

30723075
QSet<QVariant> QgsVectorLayer::uniqueValues( int index, int limit ) const

‎src/core/qgsvectorlayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
19991999
QgsStringMap mAttributeAliasMap;
20002000

20012001
//! Map which stores default value expressions for fields
2002-
QgsStringMap mDefaultExpressionMap;
2002+
QMap<QString, QgsDefaultValue> mDefaultExpressionMap;
20032003

20042004
//! Map which stores constraints for fields
20052005
QMap< QString, QgsFieldConstraints::Constraints > mFieldConstraints;

‎tests/src/core/testqgsfield.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ void TestQgsField::gettersSetters()
135135
QCOMPARE( field.comment(), QString( "comment" ) );
136136
field.setAlias( QStringLiteral( "alias" ) );
137137
QCOMPARE( field.alias(), QString( "alias" ) );
138-
field.setDefaultValueExpression( QStringLiteral( "1+2" ) );
139-
QCOMPARE( field.defaultValueExpression(), QString( "1+2" ) );
138+
field.setDefaultValue( QStringLiteral( "1+2" ) );
139+
QCOMPARE( field.defaultValue(), QString( "1+2" ) );
140140
QgsFieldConstraints constraints;
141141
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
142142
field.setConstraints( constraints );
@@ -244,10 +244,10 @@ void TestQgsField::equality()
244244
QVERIFY( !( field1 == field2 ) );
245245
QVERIFY( field1 != field2 );
246246
field2.setAlias( QString() );
247-
field2.setDefaultValueExpression( QStringLiteral( "1+2" ) );
247+
field2.setDefaultValue( QStringLiteral( "1+2" ) );
248248
QVERIFY( !( field1 == field2 ) );
249249
QVERIFY( field1 != field2 );
250-
field2.setDefaultValueExpression( QString() );
250+
field2.setDefaultValue( QString() );
251251
constraints = field2.constraints();
252252
constraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull );
253253
field2.setConstraints( constraints );
@@ -461,7 +461,7 @@ void TestQgsField::dataStream()
461461
original.setTypeName( QStringLiteral( "typename1" ) );
462462
original.setComment( QStringLiteral( "comment1" ) );
463463
original.setAlias( QStringLiteral( "alias" ) );
464-
original.setDefaultValueExpression( QStringLiteral( "default" ) );
464+
original.setDefaultValue( QStringLiteral( "default" ) );
465465
QgsFieldConstraints constraints;
466466
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
467467
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginLayer );

0 commit comments

Comments
 (0)
Please sign in to comment.