Skip to content

Commit f6c1bf7

Browse files
committedNov 2, 2016
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.
1 parent d1fd588 commit f6c1bf7

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed
 

‎src/app/qgsattributetypedialog.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,23 @@ bool QgsAttributeTypeDialog::fieldEditable() const
176176
return isFieldEditableCheckBox->isChecked();
177177
}
178178

179+
void QgsAttributeTypeDialog::setProviderConstraints( QgsVectorDataProvider::Constraints constraints )
180+
{
181+
if ( constraints & QgsVectorDataProvider::ConstraintNotNull )
182+
{
183+
notNullCheckBox->setChecked( true );
184+
notNullCheckBox->setEnabled( false );
185+
notNullCheckBox->setToolTip( tr( "The provider for this layer has a NOT NULL constraint set on the field." ) );
186+
}
187+
188+
if ( constraints & QgsVectorDataProvider::ConstraintUnique )
189+
{
190+
mUniqueCheckBox->setChecked( true );
191+
mUniqueCheckBox->setEnabled( false );
192+
mUniqueCheckBox->setToolTip( tr( "The provider for this layer has a UNIQUE constraint set on the field." ) );
193+
}
194+
}
195+
179196
void QgsAttributeTypeDialog::setNotNull( bool notNull )
180197
{
181198
notNullCheckBox->setChecked( notNull );
@@ -201,6 +218,16 @@ bool QgsAttributeTypeDialog::notNull() const
201218
return notNullCheckBox->isChecked();
202219
}
203220

221+
void QgsAttributeTypeDialog::setUnique( bool unique )
222+
{
223+
mUniqueCheckBox->setChecked( unique );
224+
}
225+
226+
bool QgsAttributeTypeDialog::unique() const
227+
{
228+
return mUniqueCheckBox->isChecked();
229+
}
230+
204231
void QgsAttributeTypeDialog::setConstraintExpression( const QString &str )
205232
{
206233
constraintExpressionWidget->setField( str );

‎src/app/qgsattributetypedialog.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "qgseditorconfigwidget.h"
2323
#include "qgsfeature.h"
24+
#include "qgsvectordataprovider.h"
2425

2526
class QDialog;
2627

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

73+
/**
74+
* Sets any provider side constraints which may affect this field's behaviour.
75+
*/
76+
void setProviderConstraints( QgsVectorDataProvider::Constraints constraints );
77+
7278
/**
7379
* Setter for checkbox for not null
7480
*/
@@ -79,6 +85,16 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
7985
*/
8086
bool notNull() const;
8187

88+
/**
89+
* Setter for unique constraint checkbox
90+
*/
91+
void setUnique( bool unique );
92+
93+
/**
94+
* Getter for unique constraint checkbox state
95+
*/
96+
bool unique() const;
97+
8298
/**
8399
* Setter for constraint expression description
84100
* @param desc the expression description

‎src/app/qgsfieldsproperties.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,12 @@ void QgsFieldsProperties::attributeTypeDialog()
559559
attributeTypeDialog.setFieldEditable( cfg.mEditable );
560560
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
561561
attributeTypeDialog.setNotNull( cfg.mNotNull );
562+
563+
if ( mLayer->fields().fieldOrigin( index ) == QgsFields::OriginProvider )
564+
{
565+
attributeTypeDialog.setProviderConstraints( mLayer->dataProvider()->fieldConstraints( mLayer->fields().fieldOriginIndex( index ) ) );
566+
}
567+
562568
attributeTypeDialog.setConstraintExpression( cfg.mConstraint );
563569
attributeTypeDialog.setConstraintExpressionDescription( cfg.mConstraintDescription );
564570
attributeTypeDialog.setDefaultValueExpression( mLayer->defaultValueExpression( index ) );

‎src/ui/qgsattributetypeedit.ui

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
</property>
3131
</widget>
3232
</item>
33+
<item>
34+
<widget class="QCheckBox" name="mUniqueCheckBox">
35+
<property name="text">
36+
<string>Unique</string>
37+
</property>
38+
</widget>
39+
</item>
3340
<item>
3441
<layout class="QHBoxLayout" name="horizontalLayout_2">
3542
<property name="topMargin">
@@ -144,18 +151,18 @@
144151
</layout>
145152
</widget>
146153
<customwidgets>
147-
<customwidget>
148-
<class>QgsExpressionLineEdit</class>
149-
<extends>QWidget</extends>
150-
<header>qgsexpressionlineedit.h</header>
151-
<container>1</container>
152-
</customwidget>
153154
<customwidget>
154155
<class>QgsCollapsibleGroupBox</class>
155156
<extends>QGroupBox</extends>
156157
<header>qgscollapsiblegroupbox.h</header>
157158
<container>1</container>
158159
</customwidget>
160+
<customwidget>
161+
<class>QgsExpressionLineEdit</class>
162+
<extends>QWidget</extends>
163+
<header>qgsexpressionlineedit.h</header>
164+
<container>1</container>
165+
</customwidget>
159166
<customwidget>
160167
<class>QgsFieldExpressionWidget</class>
161168
<extends>QWidget</extends>
@@ -169,6 +176,7 @@
169176
<tabstop>labelOnTopCheckBox</tabstop>
170177
<tabstop>mExpressionWidget</tabstop>
171178
<tabstop>notNullCheckBox</tabstop>
179+
<tabstop>mUniqueCheckBox</tabstop>
172180
<tabstop>constraintExpressionWidget</tabstop>
173181
<tabstop>leConstraintExpressionDescription</tabstop>
174182
</tabstops>

0 commit comments

Comments
 (0)
Please sign in to comment.