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 4d5bae2 commit 66cb422
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 44 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 @@ -83,26 +83,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 @@ -145,6 +145,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 @@ -156,6 +161,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 @@ -552,8 +552,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.setWidgetConfig( cfg.mEditorWidgetConfig );
attributeTypeDialog.setWidgetType( cfg.mEditorWidgetType );
Expand All @@ -564,8 +565,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.mEditorWidgetType = attributeTypeDialog.editorWidgetType();
cfg.mEditorWidgetConfig = attributeTypeDialog.editorWidgetConfig();
Expand Down
88 changes: 61 additions & 27 deletions src/ui/qgsattributetypeedit.ui
Expand Up @@ -14,7 +14,51 @@
<string>Edit Widget Properties</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QCheckBox" name="isFieldEditableCheckBox">
<property name="text">
<string>Editable</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="labelOnTopCheckBox">
<property name="text">
<string>Label on top</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</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="QgsCollapsibleGroupBox" name="groupBox">
<property name="title">
<string>Contraints</string>
Expand All @@ -40,7 +84,11 @@
</widget>
</item>
<item>
<widget class="QgsFieldExpressionWidget" name="constraintExpression" native="true"/>
<widget class="QgsFieldExpressionWidget" name="constraintExpressionWidget" native="true">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
</layout>
</item>
Expand All @@ -57,27 +105,17 @@
</widget>
</item>
<item>
<widget class="QLineEdit" name="constraintExpressionDescription"/>
<widget class="QLineEdit" name="leConstraintExpressionDescription"/>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="labelOnTopCheckBox">
<property name="text">
<string>Label on top</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item row="6" column="1">
<item row="4" column="1">
<widget class="QStackedWidget" name="stackedWidget"/>
</item>
<item row="10" column="1">
<item row="5" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -87,26 +125,16 @@
</property>
</widget>
</item>
<item row="0" column="0" rowspan="11">
<item row="0" column="0" rowspan="6">
<widget class="QListWidget" name="selectionListWidget"/>
</item>
<item row="0" column="1">
<widget class="QCheckBox" name="isFieldEditableCheckBox">
<property name="text">
<string>Editable</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsFieldExpressionWidget</class>
<class>QgsExpressionLineEdit</class>
<extends>QWidget</extends>
<header>qgsfieldexpressionwidget.h</header>
<header>qgsexpressionlineedit.h</header>
<container>1</container>
</customwidget>
<customwidget>
Expand All @@ -115,6 +143,12 @@
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFieldExpressionWidget</class>
<extends>QWidget</extends>
<header>qgsfieldexpressionwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>selectionListWidget</tabstop>
Expand Down

0 comments on commit 66cb422

Please sign in to comment.