Skip to content

Commit

Permalink
Add gui for setting/exposing constraints in field properties
Browse files Browse the repository at this point in the history
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 f6c1bf7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
27 changes: 27 additions & 0 deletions src/app/qgsattributetypedialog.cpp
Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down
16 changes: 16 additions & 0 deletions src/app/qgsattributetypedialog.h
Expand Up @@ -21,6 +21,7 @@

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

class QDialog;

Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsfieldsproperties.cpp
Expand Up @@ -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 ) );
Expand Down
20 changes: 14 additions & 6 deletions src/ui/qgsattributetypeedit.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand All @@ -169,6 +176,7 @@
<tabstop>labelOnTopCheckBox</tabstop>
<tabstop>mExpressionWidget</tabstop>
<tabstop>notNullCheckBox</tabstop>
<tabstop>mUniqueCheckBox</tabstop>
<tabstop>constraintExpressionWidget</tabstop>
<tabstop>leConstraintExpressionDescription</tabstop>
</tabstops>
Expand Down

0 comments on commit f6c1bf7

Please sign in to comment.