Skip to content

Commit 546cd67

Browse files
committed
Add UI for setting default field value expressions in the
field property dialog Sponsored by DB Fahrwegdienste GmbH
1 parent 4e8594b commit 546cd67

5 files changed

+82
-19
lines changed

src/app/qgsattributetypedialog.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx
7272
QSettings settings;
7373
restoreGeometry( settings.value( "/Windows/QgsAttributeTypeDialog/geometry" ).toByteArray() );
7474

75-
constraintExpression->setLayer( vl );
75+
constraintExpressionWidget->setLayer( vl );
7676
}
7777

7878
QgsAttributeTypeDialog::~QgsAttributeTypeDialog()
@@ -180,29 +180,39 @@ bool QgsAttributeTypeDialog::labelOnTop() const
180180
return labelOnTopCheckBox->isChecked();
181181
}
182182

183-
void QgsAttributeTypeDialog::setExpressionDescription( const QString &desc )
183+
void QgsAttributeTypeDialog::setConstraintExpressionDescription( const QString &desc )
184184
{
185-
constraintExpressionDescription->setText( desc );
185+
leConstraintExpressionDescription->setText( desc );
186186
}
187187

188-
QString QgsAttributeTypeDialog::expressionDescription()
188+
QString QgsAttributeTypeDialog::constraintExpressionDescription()
189189
{
190-
return constraintExpressionDescription->text();
190+
return leConstraintExpressionDescription->text();
191191
}
192192

193193
bool QgsAttributeTypeDialog::notNull() const
194194
{
195195
return notNullCheckBox->isChecked();
196196
}
197197

198-
void QgsAttributeTypeDialog::setExpression( const QString &str )
198+
void QgsAttributeTypeDialog::setConstraintExpression( const QString &str )
199199
{
200-
constraintExpression->setField( str );
200+
constraintExpressionWidget->setField( str );
201201
}
202202

203-
QString QgsAttributeTypeDialog::expression() const
203+
QString QgsAttributeTypeDialog::defaultValueExpression() const
204204
{
205-
return constraintExpression->asExpression();
205+
return mExpressionWidget->expression();
206+
}
207+
208+
void QgsAttributeTypeDialog::setDefaultValueExpression( const QString& expression )
209+
{
210+
mExpressionWidget->setExpression( expression );
211+
}
212+
213+
QString QgsAttributeTypeDialog::constraintExpression() const
214+
{
215+
return constraintExpressionWidget->asExpression();
206216
}
207217

208218
void QgsAttributeTypeDialog::setFieldEditable( bool editable )

src/app/qgsattributetypedialog.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,37 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
9999
* @param desc the expression description
100100
* @note added in QGIS 2.16
101101
**/
102-
void setExpressionDescription( const QString &desc );
102+
void setConstraintExpressionDescription( const QString &desc );
103103

104104
/**
105105
* Getter for constraint expression description
106106
* @return the expression description
107107
* @note added in QGIS 2.16
108108
**/
109-
QString expressionDescription();
109+
QString constraintExpressionDescription();
110110

111111
/**
112112
* Getter for the constraint expression
113113
* @note added in QGIS 2.16
114114
*/
115-
QString expression() const;
115+
QString constraintExpression() const;
116116

117117
/**
118118
* Setter for the constraint expression
119119
* @note added in QGIS 2.16
120120
*/
121-
void setExpression( const QString &str );
121+
void setConstraintExpression( const QString &str );
122+
123+
/**
124+
* Returns the expression used for the field's default value, or
125+
* an empty string if no default value expression is set.
126+
*/
127+
QString defaultValueExpression() const;
128+
129+
/**
130+
* Sets the expression used for the field's default value
131+
*/
132+
void setDefaultValueExpression( const QString& expression );
122133

123134
private slots:
124135
/**

src/app/qgsfeatureaction.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes, boo
143143
bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool();
144144
QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );
145145

146+
QgsExpressionContext context;
147+
context << QgsExpressionContextUtils::globalScope()
148+
<< QgsExpressionContextUtils::projectScope()
149+
<< QgsExpressionContextUtils::layerScope( mLayer );
150+
146151
// add the fields to the QgsFeature
147152
const QgsFields& fields = mLayer->fields();
148153
mFeature->initAttributes( fields.count() );
@@ -154,6 +159,11 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes, boo
154159
{
155160
v = defaultAttributes.value( idx );
156161
}
162+
else if ( !mLayer->defaultValueExpression( idx ).isEmpty() )
163+
{
164+
// client side default expression set - use this in preference to reusing last value
165+
v = mLayer->defaultValue( idx, *mFeature, &context );
166+
}
157167
else if ( reuseLastValues && sLastUsedValues.contains( mLayer ) && sLastUsedValues[ mLayer ].contains( idx ) && !pkAttrList.contains( idx ) )
158168
{
159169
v = sLastUsedValues[ mLayer ][idx];

src/app/qgsfieldsproperties.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,9 @@ void QgsFieldsProperties::attributeTypeDialog()
553553
attributeTypeDialog.setFieldEditable( cfg.mEditable );
554554
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
555555
attributeTypeDialog.setNotNull( cfg.mNotNull );
556-
attributeTypeDialog.setExpression( cfg.mConstraint );
557-
attributeTypeDialog.setExpressionDescription( cfg.mConstraintDescription );
556+
attributeTypeDialog.setConstraintExpression( cfg.mConstraint );
557+
attributeTypeDialog.setConstraintExpressionDescription( cfg.mConstraintDescription );
558+
attributeTypeDialog.setDefaultValueExpression( mLayer->defaultValueExpression( index ) );
558559

559560
attributeTypeDialog.setWidgetV2Config( cfg.mEditorWidgetV2Config );
560561
attributeTypeDialog.setWidgetV2Type( cfg.mEditorWidgetV2Type );
@@ -565,8 +566,9 @@ void QgsFieldsProperties::attributeTypeDialog()
565566
cfg.mEditable = attributeTypeDialog.fieldEditable();
566567
cfg.mLabelOnTop = attributeTypeDialog.labelOnTop();
567568
cfg.mNotNull = attributeTypeDialog.notNull();
568-
cfg.mConstraintDescription = attributeTypeDialog.expressionDescription();
569-
cfg.mConstraint = attributeTypeDialog.expression();
569+
cfg.mConstraintDescription = attributeTypeDialog.constraintExpressionDescription();
570+
cfg.mConstraint = attributeTypeDialog.constraintExpression();
571+
mLayer->setDefaultValueExpression( index, attributeTypeDialog.defaultValueExpression() );
570572

571573
cfg.mEditorWidgetV2Type = attributeTypeDialog.editorWidgetV2Type();
572574
cfg.mEditorWidgetV2Config = attributeTypeDialog.editorWidgetV2Config();

src/ui/qgsattributetypeedit.ui

+32-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@
5151
<widget class="QStackedWidget" name="stackedWidget"/>
5252
</item>
5353
<item row="2" column="1">
54+
<layout class="QHBoxLayout" name="horizontalLayout_3">
55+
<item>
56+
<widget class="QLabel" name="label_3">
57+
<property name="text">
58+
<string>Default value</string>
59+
</property>
60+
</widget>
61+
</item>
62+
<item>
63+
<widget class="QgsExpressionLineEdit" name="mExpressionWidget" native="true">
64+
<property name="maximumSize">
65+
<size>
66+
<width>500</width>
67+
<height>16777215</height>
68+
</size>
69+
</property>
70+
<property name="focusPolicy">
71+
<enum>Qt::StrongFocus</enum>
72+
</property>
73+
</widget>
74+
</item>
75+
</layout>
76+
</item>
77+
<item row="3" column="1">
5478
<widget class="QCheckBox" name="notNullCheckBox">
5579
<property name="text">
5680
<string>Not null</string>
@@ -70,7 +94,7 @@
7094
</widget>
7195
</item>
7296
<item>
73-
<widget class="QgsFieldExpressionWidget" name="constraintExpression" native="true"/>
97+
<widget class="QgsFieldExpressionWidget" name="constraintExpressionWidget" native="true"/>
7498
</item>
7599
</layout>
76100
</item>
@@ -87,13 +111,19 @@
87111
</widget>
88112
</item>
89113
<item>
90-
<widget class="QLineEdit" name="constraintExpressionDescription"/>
114+
<widget class="QLineEdit" name="leConstraintExpressionDescription"/>
91115
</item>
92116
</layout>
93117
</item>
94118
</layout>
95119
</widget>
96120
<customwidgets>
121+
<customwidget>
122+
<class>QgsExpressionLineEdit</class>
123+
<extends>QWidget</extends>
124+
<header>qgsexpressionlineedit.h</header>
125+
<container>1</container>
126+
</customwidget>
97127
<customwidget>
98128
<class>QgsFieldExpressionWidget</class>
99129
<extends>QWidget</extends>

0 commit comments

Comments
 (0)