Skip to content
Permalink
Browse files

Add gui for setting/exposing constraints in field properties

Provider set constraints cannot be changed by users, ie
if a not null constraint is set on the field at the database
then QGIS users cannot clear this constraint.
  • Loading branch information
nyalldawson committed Nov 2, 2016
1 parent d1fd588 commit f6c1bf7f5b3d2e3277a0bf8510665f09bfccd3e8
@@ -176,6 +176,23 @@ bool QgsAttributeTypeDialog::fieldEditable() const
return isFieldEditableCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setProviderConstraints( QgsVectorDataProvider::Constraints constraints )
{
if ( constraints & QgsVectorDataProvider::ConstraintNotNull )
{
notNullCheckBox->setChecked( true );
notNullCheckBox->setEnabled( false );
notNullCheckBox->setToolTip( tr( "The provider for this layer has a NOT NULL constraint set on the field." ) );
}

if ( constraints & QgsVectorDataProvider::ConstraintUnique )
{
mUniqueCheckBox->setChecked( true );
mUniqueCheckBox->setEnabled( false );
mUniqueCheckBox->setToolTip( tr( "The provider for this layer has a UNIQUE constraint set on the field." ) );
}
}

void QgsAttributeTypeDialog::setNotNull( bool notNull )
{
notNullCheckBox->setChecked( notNull );
@@ -201,6 +218,16 @@ bool QgsAttributeTypeDialog::notNull() const
return notNullCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setUnique( bool unique )
{
mUniqueCheckBox->setChecked( unique );
}

bool QgsAttributeTypeDialog::unique() const
{
return mUniqueCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setConstraintExpression( const QString &str )
{
constraintExpressionWidget->setField( str );
@@ -21,6 +21,7 @@

#include "qgseditorconfigwidget.h"
#include "qgsfeature.h"
#include "qgsvectordataprovider.h"

class QDialog;

@@ -69,6 +70,11 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
*/
bool fieldEditable() const;

/**
* Sets any provider side constraints which may affect this field's behaviour.
*/
void setProviderConstraints( QgsVectorDataProvider::Constraints constraints );

/**
* Setter for checkbox for not null
*/
@@ -79,6 +85,16 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
*/
bool notNull() const;

/**
* Setter for unique constraint checkbox
*/
void setUnique( bool unique );

/**
* Getter for unique constraint checkbox state
*/
bool unique() const;

/**
* Setter for constraint expression description
* @param desc the expression description
@@ -559,6 +559,12 @@ void QgsFieldsProperties::attributeTypeDialog()
attributeTypeDialog.setFieldEditable( cfg.mEditable );
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
attributeTypeDialog.setNotNull( cfg.mNotNull );

if ( mLayer->fields().fieldOrigin( index ) == QgsFields::OriginProvider )
{
attributeTypeDialog.setProviderConstraints( mLayer->dataProvider()->fieldConstraints( mLayer->fields().fieldOriginIndex( index ) ) );
}

attributeTypeDialog.setConstraintExpression( cfg.mConstraint );
attributeTypeDialog.setConstraintExpressionDescription( cfg.mConstraintDescription );
attributeTypeDialog.setDefaultValueExpression( mLayer->defaultValueExpression( index ) );
@@ -30,6 +30,13 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="mUniqueCheckBox">
<property name="text">
<string>Unique</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
@@ -144,18 +151,18 @@
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsExpressionLineEdit</class>
<extends>QWidget</extends>
<header>qgsexpressionlineedit.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsExpressionLineEdit</class>
<extends>QWidget</extends>
<header>qgsexpressionlineedit.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFieldExpressionWidget</class>
<extends>QWidget</extends>
@@ -169,6 +176,7 @@
<tabstop>labelOnTopCheckBox</tabstop>
<tabstop>mExpressionWidget</tabstop>
<tabstop>notNullCheckBox</tabstop>
<tabstop>mUniqueCheckBox</tabstop>
<tabstop>constraintExpressionWidget</tabstop>
<tabstop>leConstraintExpressionDescription</tabstop>
</tabstops>

0 comments on commit f6c1bf7

Please sign in to comment.
You can’t perform that action at this time.