Skip to content

Commit 6bbd006

Browse files
committedNov 2, 2016
Move handling of not null constraint from edit form to layer
1 parent bd9f672 commit 6bbd006

10 files changed

+30
-49
lines changed
 

‎doc/api_break.dox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ QgsEditorWidgetRegistry::instance()->findBest() must be used instead.
740740
- setExpression() has been renamed to setConstraintExpression()
741741
- expressionDescription() has been renamed to constraintDescription()
742742
- setExpressionDesctiption() has been renamed to setConstraintDescription()
743-
743+
- notNull() and setNotNull() have been removed. Use QgsVectorLayer.setFieldConstraints()/fieldConstraints(), or QgsField.constraints() instead.
744744

745745
QgsExpression {#qgis_api_break_3_0_QgsExpression}
746746
-------------

‎python/core/qgseditformconfig.sip

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,6 @@ class QgsEditFormConfig
243243
*/
244244
void setContraintDescription( int idx, const QString& description );
245245

246-
/**
247-
* Returns if the field at fieldidx should be treated as NOT NULL value
248-
*/
249-
bool notNull( int fieldidx ) const;
250-
/**
251-
* Set if the field at fieldidx should be treated as NOT NULL value
252-
*/
253-
void setNotNull( int idx, bool notnull = true );
254-
255246
/**
256247
* If this returns true, the widget at the given index will receive its label on the previous line
257248
* while if it returns false, the widget will receive its label on the left hand side.

‎src/app/qgsfieldsproperties.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,15 @@ void QgsFieldsProperties::attributeTypeDialog()
558558

559559
attributeTypeDialog.setFieldEditable( cfg.mEditable );
560560
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
561-
attributeTypeDialog.setNotNull( cfg.mNotNull );
561+
attributeTypeDialog.setNotNull( cfg.mConstraints & QgsField::ConstraintNotNull );
562+
attributeTypeDialog.setUnique( cfg.mConstraints & QgsField::ConstraintUnique );
562563

564+
QgsField::Constraints providerConstraints = 0;
563565
if ( mLayer->fields().fieldOrigin( index ) == QgsFields::OriginProvider )
564566
{
565-
attributeTypeDialog.setProviderConstraints( mLayer->dataProvider()->fieldConstraints( mLayer->fields().fieldOriginIndex( index ) ) );
567+
providerConstraints = mLayer->dataProvider()->fieldConstraints( mLayer->fields().fieldOriginIndex( index ) );
566568
}
569+
attributeTypeDialog.setProviderConstraints( providerConstraints );
567570

568571
attributeTypeDialog.setConstraintExpression( cfg.mConstraint );
569572
attributeTypeDialog.setConstraintExpressionDescription( cfg.mConstraintDescription );
@@ -577,7 +580,17 @@ void QgsFieldsProperties::attributeTypeDialog()
577580

578581
cfg.mEditable = attributeTypeDialog.fieldEditable();
579582
cfg.mLabelOnTop = attributeTypeDialog.labelOnTop();
580-
cfg.mNotNull = attributeTypeDialog.notNull();
583+
584+
cfg.mConstraints = 0;
585+
if ( attributeTypeDialog.notNull() && !( providerConstraints & QgsField::ConstraintNotNull ) )
586+
{
587+
cfg.mConstraints |= QgsField::ConstraintNotNull;
588+
}
589+
if ( attributeTypeDialog.unique() && !( providerConstraints & QgsField::ConstraintUnique ) )
590+
{
591+
cfg.mConstraints |= QgsField::ConstraintUnique;
592+
}
593+
581594
cfg.mConstraintDescription = attributeTypeDialog.constraintExpressionDescription();
582595
cfg.mConstraint = attributeTypeDialog.constraintExpression();
583596
mLayer->setDefaultValueExpression( index, attributeTypeDialog.defaultValueExpression() );
@@ -962,13 +975,14 @@ void QgsFieldsProperties::apply()
962975

963976
editFormConfig.setReadOnly( i, !cfg.mEditable );
964977
editFormConfig.setLabelOnTop( i, cfg.mLabelOnTop );
965-
editFormConfig.setNotNull( i, cfg.mNotNull );
966978
editFormConfig.setContraintDescription( i, cfg.mConstraintDescription );
967979
editFormConfig.setConstraintExpression( i, cfg.mConstraint );
968980

969981
editFormConfig.setWidgetType( name, cfg.mEditorWidgetType );
970982
editFormConfig.setWidgetConfig( name, cfg.mEditorWidgetConfig );
971983

984+
mLayer->setFieldConstraints( i, cfg.mConstraints );
985+
972986
if ( mFieldsList->item( i, attrWMSCol )->checkState() == Qt::Unchecked )
973987
{
974988
excludeAttributesWMS.insert( mFieldsList->item( i, attrNameCol )->text() );
@@ -1032,7 +1046,7 @@ QgsFieldsProperties::FieldConfig::FieldConfig()
10321046
: mEditable( true )
10331047
, mEditableEnabled( true )
10341048
, mLabelOnTop( false )
1035-
, mNotNull( false )
1049+
, mConstraints( 0 )
10361050
, mConstraintDescription( QString() )
10371051
, mButton( nullptr )
10381052
{
@@ -1045,7 +1059,7 @@ QgsFieldsProperties::FieldConfig::FieldConfig( QgsVectorLayer* layer, int idx )
10451059
mEditableEnabled = layer->fields().fieldOrigin( idx ) != QgsFields::OriginJoin
10461060
&& layer->fields().fieldOrigin( idx ) != QgsFields::OriginExpression;
10471061
mLabelOnTop = layer->editFormConfig().labelOnTop( idx );
1048-
mNotNull = layer->editFormConfig().notNull( idx );
1062+
mConstraints = layer->fieldConstraints( idx );
10491063
mConstraint = layer->editFormConfig().constraintExpression( idx );
10501064
mConstraintDescription = layer->editFormConfig().constraintDescription( idx );
10511065
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( layer, layer->fields().field( idx ).name() );

‎src/app/qgsfieldsproperties.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
122122
bool mEditable;
123123
bool mEditableEnabled;
124124
bool mLabelOnTop;
125-
bool mNotNull;
125+
QgsField::Constraints mConstraints;
126126
QString mConstraint;
127127
QString mConstraintDescription;
128128
QPushButton* mButton;

‎src/core/qgseditformconfig.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,6 @@ void QgsEditFormConfig::setContraintDescription( int idx, const QString &descr )
220220
}
221221
}
222222

223-
bool QgsEditFormConfig::notNull( int idx ) const
224-
{
225-
if ( idx >= 0 && idx < d->mFields.count() )
226-
return d->mNotNull.value( d->mFields.at( idx ).name(), false );
227-
else
228-
return false;
229-
}
230-
231223
void QgsEditFormConfig::setReadOnly( int idx, bool readOnly )
232224
{
233225
if ( idx >= 0 && idx < d->mFields.count() )
@@ -301,15 +293,6 @@ void QgsEditFormConfig::setSuppress( QgsEditFormConfig::FeatureFormSuppress s )
301293
d->mSuppressForm = s;
302294
}
303295

304-
void QgsEditFormConfig::setNotNull( int idx, bool notnull )
305-
{
306-
if ( idx >= 0 && idx < d->mFields.count() )
307-
{
308-
d.detach();
309-
d->mNotNull[ d->mFields.at( idx ).name()] = notnull;
310-
}
311-
}
312-
313296
void QgsEditFormConfig::readXml( const QDomNode& node )
314297
{
315298
d.detach();

‎src/core/qgseditformconfig.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,6 @@ class CORE_EXPORT QgsEditFormConfig
278278
*/
279279
void setContraintDescription( int idx, const QString& description );
280280

281-
/**
282-
* Returns if the field at fieldidx should be treated as NOT NULL value
283-
*/
284-
bool notNull( int fieldidx ) const;
285-
/**
286-
* Set if the field at fieldidx should be treated as NOT NULL value
287-
*/
288-
void setNotNull( int idx, bool notnull = true );
289-
290281
/**
291282
* If this returns true, the widget at the given index will receive its label on the previous line
292283
* while if it returns false, the widget will receive its label on the left hand side.

‎src/core/qgseditformconfig_p.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class QgsEditFormConfigPrivate : public QSharedData
4242
, mConstraintsDescription( o.mConstraintsDescription )
4343
, mFieldEditables( o.mFieldEditables )
4444
, mLabelOnTop( o.mLabelOnTop )
45-
, mNotNull( o.mNotNull )
4645
, mEditorWidgetTypes( o.mEditorWidgetTypes )
4746
, mWidgetConfigs( o.mWidgetConfigs )
4847
, mEditorLayout( o.mEditorLayout )
@@ -69,7 +68,6 @@ class QgsEditFormConfigPrivate : public QSharedData
6968
QMap< QString, QString> mConstraintsDescription;
7069
QMap< QString, bool> mFieldEditables;
7170
QMap< QString, bool> mLabelOnTop;
72-
QMap< QString, bool> mNotNull;
7371

7472
QMap<QString, QString> mEditorWidgetTypes;
7573
QMap<QString, QgsEditorWidgetConfig > mWidgetConfigs;

‎src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomEle
264264
formConfig.setReadOnly( idx, ewv2CfgElem.attribute( QStringLiteral( "fieldEditable" ), QStringLiteral( "1" ) ) != QLatin1String( "1" ) );
265265
formConfig.setLabelOnTop( idx, ewv2CfgElem.attribute( QStringLiteral( "labelOnTop" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
266266
formConfig.setNotNull( idx, ewv2CfgElem.attribute( QStringLiteral( "notNull" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
267+
if ( ewv2CfgElem.attribute( QStringLiteral("notNull"), QStringLiteral("0") ) == QLatin1String( "1" ) )
268+
{
269+
// upgrade from older config
270+
vectorLayer->setFieldConstraints( idx, vectorLayer->fieldConstraints( idx ) | QgsField::ConstraintNotNull );
271+
}
267272
formConfig.setConstraintExpression( idx, ewv2CfgElem.attribute( QStringLiteral( "constraint" ), QString() ) );
268273
formConfig.setContraintDescription( idx, ewv2CfgElem.attribute( QStringLiteral( "constraintDescription" ), QString() ) );
269274

@@ -319,7 +324,6 @@ void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement&
319324
QDomElement ewv2CfgElem = doc.createElement( QStringLiteral( "widgetv2config" ) );
320325
ewv2CfgElem.setAttribute( QStringLiteral( "fieldEditable" ), !vectorLayer->editFormConfig().readOnly( idx ) );
321326
ewv2CfgElem.setAttribute( QStringLiteral( "labelOnTop" ), vectorLayer->editFormConfig().labelOnTop( idx ) );
322-
ewv2CfgElem.setAttribute( QStringLiteral( "notNull" ), vectorLayer->editFormConfig().notNull( idx ) );
323327
ewv2CfgElem.setAttribute( QStringLiteral( "constraint" ), vectorLayer->editFormConfig().constraintExpression( idx ) );
324328
ewv2CfgElem.setAttribute( QStringLiteral( "constraintDescription" ), vectorLayer->editFormConfig().constraintDescription( idx ) );
325329

‎src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void QgsEditorWidgetWrapper::updateConstraint( const QgsFeature &ft )
136136
else
137137
mValidConstraint = true;
138138

139-
if ( layer()->editFormConfig().notNull( mFieldIdx ) )
139+
if ( layer()->fieldConstraints( mFieldIdx ) & QgsField::ConstraintNotNull )
140140
{
141141
if ( !expression.isEmpty() )
142142
{
@@ -175,4 +175,4 @@ bool QgsEditorWidgetWrapper::isInTable( const QWidget* parent )
175175
if ( !parent ) return false;
176176
if ( qobject_cast<const QTableView*>( parent ) ) return true;
177177
return isInTable( parent->parentWidget() );
178-
}
178+
}

‎src/gui/qgsattributeform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ void QgsAttributeForm::onAttributeChanged( const QVariant& value )
674674
break;
675675
}
676676

677-
if ( eww->layer()->editFormConfig().notNull( eww->fieldIdx() ) )
677+
if ( eww->layer()->fieldConstraints( eww->fieldIdx() ) & QgsField::ConstraintNotNull )
678678
{
679679
QLabel* buddy = mBuddyMap.value( eww->widget() );
680680

0 commit comments

Comments
 (0)
Please sign in to comment.