Navigation Menu

Skip to content

Commit

Permalink
Add UI for setting default field value expressions in the
Browse files Browse the repository at this point in the history
field property dialog

Sponsored by DB Fahrwegdienste GmbH
  • Loading branch information
nyalldawson committed Aug 30, 2016
1 parent 4e8594b commit 546cd67
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
28 changes: 19 additions & 9 deletions src/app/qgsattributetypedialog.cpp
Expand Up @@ -72,7 +72,7 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx
QSettings settings;
restoreGeometry( settings.value( "/Windows/QgsAttributeTypeDialog/geometry" ).toByteArray() );

constraintExpression->setLayer( vl );
constraintExpressionWidget->setLayer( vl );
}

QgsAttributeTypeDialog::~QgsAttributeTypeDialog()
Expand Down Expand Up @@ -180,29 +180,39 @@ bool QgsAttributeTypeDialog::labelOnTop() const
return labelOnTopCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setExpressionDescription( const QString &desc )
void QgsAttributeTypeDialog::setConstraintExpressionDescription( const QString &desc )
{
constraintExpressionDescription->setText( desc );
leConstraintExpressionDescription->setText( desc );
}

QString QgsAttributeTypeDialog::expressionDescription()
QString QgsAttributeTypeDialog::constraintExpressionDescription()
{
return constraintExpressionDescription->text();
return leConstraintExpressionDescription->text();
}

bool QgsAttributeTypeDialog::notNull() const
{
return notNullCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setExpression( const QString &str )
void QgsAttributeTypeDialog::setConstraintExpression( const QString &str )
{
constraintExpression->setField( str );
constraintExpressionWidget->setField( str );
}

QString QgsAttributeTypeDialog::expression() const
QString QgsAttributeTypeDialog::defaultValueExpression() const
{
return constraintExpression->asExpression();
return mExpressionWidget->expression();
}

void QgsAttributeTypeDialog::setDefaultValueExpression( const QString& expression )
{
mExpressionWidget->setExpression( expression );
}

QString QgsAttributeTypeDialog::constraintExpression() const
{
return constraintExpressionWidget->asExpression();
}

void QgsAttributeTypeDialog::setFieldEditable( bool editable )
Expand Down
19 changes: 15 additions & 4 deletions src/app/qgsattributetypedialog.h
Expand Up @@ -99,26 +99,37 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
* @param desc the expression description
* @note added in QGIS 2.16
**/
void setExpressionDescription( const QString &desc );
void setConstraintExpressionDescription( const QString &desc );

/**
* Getter for constraint expression description
* @return the expression description
* @note added in QGIS 2.16
**/
QString expressionDescription();
QString constraintExpressionDescription();

/**
* Getter for the constraint expression
* @note added in QGIS 2.16
*/
QString expression() const;
QString constraintExpression() const;

/**
* Setter for the constraint expression
* @note added in QGIS 2.16
*/
void setExpression( const QString &str );
void setConstraintExpression( const QString &str );

/**
* Returns the expression used for the field's default value, or
* an empty string if no default value expression is set.
*/
QString defaultValueExpression() const;

/**
* Sets the expression used for the field's default value
*/
void setDefaultValueExpression( const QString& expression );

private slots:
/**
Expand Down
10 changes: 10 additions & 0 deletions src/app/qgsfeatureaction.cpp
Expand Up @@ -143,6 +143,11 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes, boo
bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool();
QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) );

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );

// add the fields to the QgsFeature
const QgsFields& fields = mLayer->fields();
mFeature->initAttributes( fields.count() );
Expand All @@ -154,6 +159,11 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap& defaultAttributes, boo
{
v = defaultAttributes.value( idx );
}
else if ( !mLayer->defaultValueExpression( idx ).isEmpty() )
{
// client side default expression set - use this in preference to reusing last value
v = mLayer->defaultValue( idx, *mFeature, &context );
}
else if ( reuseLastValues && sLastUsedValues.contains( mLayer ) && sLastUsedValues[ mLayer ].contains( idx ) && !pkAttrList.contains( idx ) )
{
v = sLastUsedValues[ mLayer ][idx];
Expand Down
10 changes: 6 additions & 4 deletions src/app/qgsfieldsproperties.cpp
Expand Up @@ -553,8 +553,9 @@ void QgsFieldsProperties::attributeTypeDialog()
attributeTypeDialog.setFieldEditable( cfg.mEditable );
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
attributeTypeDialog.setNotNull( cfg.mNotNull );
attributeTypeDialog.setExpression( cfg.mConstraint );
attributeTypeDialog.setExpressionDescription( cfg.mConstraintDescription );
attributeTypeDialog.setConstraintExpression( cfg.mConstraint );
attributeTypeDialog.setConstraintExpressionDescription( cfg.mConstraintDescription );
attributeTypeDialog.setDefaultValueExpression( mLayer->defaultValueExpression( index ) );

attributeTypeDialog.setWidgetV2Config( cfg.mEditorWidgetV2Config );
attributeTypeDialog.setWidgetV2Type( cfg.mEditorWidgetV2Type );
Expand All @@ -565,8 +566,9 @@ void QgsFieldsProperties::attributeTypeDialog()
cfg.mEditable = attributeTypeDialog.fieldEditable();
cfg.mLabelOnTop = attributeTypeDialog.labelOnTop();
cfg.mNotNull = attributeTypeDialog.notNull();
cfg.mConstraintDescription = attributeTypeDialog.expressionDescription();
cfg.mConstraint = attributeTypeDialog.expression();
cfg.mConstraintDescription = attributeTypeDialog.constraintExpressionDescription();
cfg.mConstraint = attributeTypeDialog.constraintExpression();
mLayer->setDefaultValueExpression( index, attributeTypeDialog.defaultValueExpression() );

cfg.mEditorWidgetV2Type = attributeTypeDialog.editorWidgetV2Type();
cfg.mEditorWidgetV2Config = attributeTypeDialog.editorWidgetV2Config();
Expand Down
34 changes: 32 additions & 2 deletions src/ui/qgsattributetypeedit.ui
Expand Up @@ -51,6 +51,30 @@
<widget class="QStackedWidget" name="stackedWidget"/>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Default value</string>
</property>
</widget>
</item>
<item>
<widget class="QgsExpressionLineEdit" name="mExpressionWidget" native="true">
<property name="maximumSize">
<size>
<width>500</width>
<height>16777215</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="notNullCheckBox">
<property name="text">
<string>Not null</string>
Expand All @@ -70,7 +94,7 @@
</widget>
</item>
<item>
<widget class="QgsFieldExpressionWidget" name="constraintExpression" native="true"/>
<widget class="QgsFieldExpressionWidget" name="constraintExpressionWidget" native="true"/>
</item>
</layout>
</item>
Expand All @@ -87,13 +111,19 @@
</widget>
</item>
<item>
<widget class="QLineEdit" name="constraintExpressionDescription"/>
<widget class="QLineEdit" name="leConstraintExpressionDescription"/>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsExpressionLineEdit</class>
<extends>QWidget</extends>
<header>qgsexpressionlineedit.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFieldExpressionWidget</class>
<extends>QWidget</extends>
Expand Down

0 comments on commit 546cd67

Please sign in to comment.