Skip to content

Commit

Permalink
Move handling of not null constraint from edit form to layer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 2, 2016
1 parent bd9f672 commit 6bbd006
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 49 deletions.
2 changes: 1 addition & 1 deletion doc/api_break.dox
Expand Up @@ -740,7 +740,7 @@ QgsEditorWidgetRegistry::instance()->findBest() must be used instead.
- setExpression() has been renamed to setConstraintExpression()
- expressionDescription() has been renamed to constraintDescription()
- setExpressionDesctiption() has been renamed to setConstraintDescription()

- notNull() and setNotNull() have been removed. Use QgsVectorLayer.setFieldConstraints()/fieldConstraints(), or QgsField.constraints() instead.

QgsExpression {#qgis_api_break_3_0_QgsExpression}
-------------
Expand Down
9 changes: 0 additions & 9 deletions python/core/qgseditformconfig.sip
Expand Up @@ -243,15 +243,6 @@ class QgsEditFormConfig
*/
void setContraintDescription( int idx, const QString& description );

/**
* Returns if the field at fieldidx should be treated as NOT NULL value
*/
bool notNull( int fieldidx ) const;
/**
* Set if the field at fieldidx should be treated as NOT NULL value
*/
void setNotNull( int idx, bool notnull = true );

/**
* If this returns true, the widget at the given index will receive its label on the previous line
* while if it returns false, the widget will receive its label on the left hand side.
Expand Down
26 changes: 20 additions & 6 deletions src/app/qgsfieldsproperties.cpp
Expand Up @@ -558,12 +558,15 @@ void QgsFieldsProperties::attributeTypeDialog()

attributeTypeDialog.setFieldEditable( cfg.mEditable );
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
attributeTypeDialog.setNotNull( cfg.mNotNull );
attributeTypeDialog.setNotNull( cfg.mConstraints & QgsField::ConstraintNotNull );
attributeTypeDialog.setUnique( cfg.mConstraints & QgsField::ConstraintUnique );

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

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

cfg.mEditable = attributeTypeDialog.fieldEditable();
cfg.mLabelOnTop = attributeTypeDialog.labelOnTop();
cfg.mNotNull = attributeTypeDialog.notNull();

cfg.mConstraints = 0;
if ( attributeTypeDialog.notNull() && !( providerConstraints & QgsField::ConstraintNotNull ) )
{
cfg.mConstraints |= QgsField::ConstraintNotNull;
}
if ( attributeTypeDialog.unique() && !( providerConstraints & QgsField::ConstraintUnique ) )
{
cfg.mConstraints |= QgsField::ConstraintUnique;
}

cfg.mConstraintDescription = attributeTypeDialog.constraintExpressionDescription();
cfg.mConstraint = attributeTypeDialog.constraintExpression();
mLayer->setDefaultValueExpression( index, attributeTypeDialog.defaultValueExpression() );
Expand Down Expand Up @@ -962,13 +975,14 @@ void QgsFieldsProperties::apply()

editFormConfig.setReadOnly( i, !cfg.mEditable );
editFormConfig.setLabelOnTop( i, cfg.mLabelOnTop );
editFormConfig.setNotNull( i, cfg.mNotNull );
editFormConfig.setContraintDescription( i, cfg.mConstraintDescription );
editFormConfig.setConstraintExpression( i, cfg.mConstraint );

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

mLayer->setFieldConstraints( i, cfg.mConstraints );

if ( mFieldsList->item( i, attrWMSCol )->checkState() == Qt::Unchecked )
{
excludeAttributesWMS.insert( mFieldsList->item( i, attrNameCol )->text() );
Expand Down Expand Up @@ -1032,7 +1046,7 @@ QgsFieldsProperties::FieldConfig::FieldConfig()
: mEditable( true )
, mEditableEnabled( true )
, mLabelOnTop( false )
, mNotNull( false )
, mConstraints( 0 )
, mConstraintDescription( QString() )
, mButton( nullptr )
{
Expand All @@ -1045,7 +1059,7 @@ QgsFieldsProperties::FieldConfig::FieldConfig( QgsVectorLayer* layer, int idx )
mEditableEnabled = layer->fields().fieldOrigin( idx ) != QgsFields::OriginJoin
&& layer->fields().fieldOrigin( idx ) != QgsFields::OriginExpression;
mLabelOnTop = layer->editFormConfig().labelOnTop( idx );
mNotNull = layer->editFormConfig().notNull( idx );
mConstraints = layer->fieldConstraints( idx );
mConstraint = layer->editFormConfig().constraintExpression( idx );
mConstraintDescription = layer->editFormConfig().constraintDescription( idx );
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( layer, layer->fields().field( idx ).name() );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfieldsproperties.h
Expand Up @@ -122,7 +122,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
bool mEditable;
bool mEditableEnabled;
bool mLabelOnTop;
bool mNotNull;
QgsField::Constraints mConstraints;
QString mConstraint;
QString mConstraintDescription;
QPushButton* mButton;
Expand Down
17 changes: 0 additions & 17 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -220,14 +220,6 @@ void QgsEditFormConfig::setContraintDescription( int idx, const QString &descr )
}
}

bool QgsEditFormConfig::notNull( int idx ) const
{
if ( idx >= 0 && idx < d->mFields.count() )
return d->mNotNull.value( d->mFields.at( idx ).name(), false );
else
return false;
}

void QgsEditFormConfig::setReadOnly( int idx, bool readOnly )
{
if ( idx >= 0 && idx < d->mFields.count() )
Expand Down Expand Up @@ -301,15 +293,6 @@ void QgsEditFormConfig::setSuppress( QgsEditFormConfig::FeatureFormSuppress s )
d->mSuppressForm = s;
}

void QgsEditFormConfig::setNotNull( int idx, bool notnull )
{
if ( idx >= 0 && idx < d->mFields.count() )
{
d.detach();
d->mNotNull[ d->mFields.at( idx ).name()] = notnull;
}
}

void QgsEditFormConfig::readXml( const QDomNode& node )
{
d.detach();
Expand Down
9 changes: 0 additions & 9 deletions src/core/qgseditformconfig.h
Expand Up @@ -278,15 +278,6 @@ class CORE_EXPORT QgsEditFormConfig
*/
void setContraintDescription( int idx, const QString& description );

/**
* Returns if the field at fieldidx should be treated as NOT NULL value
*/
bool notNull( int fieldidx ) const;
/**
* Set if the field at fieldidx should be treated as NOT NULL value
*/
void setNotNull( int idx, bool notnull = true );

/**
* If this returns true, the widget at the given index will receive its label on the previous line
* while if it returns false, the widget will receive its label on the left hand side.
Expand Down
2 changes: 0 additions & 2 deletions src/core/qgseditformconfig_p.h
Expand Up @@ -42,7 +42,6 @@ class QgsEditFormConfigPrivate : public QSharedData
, mConstraintsDescription( o.mConstraintsDescription )
, mFieldEditables( o.mFieldEditables )
, mLabelOnTop( o.mLabelOnTop )
, mNotNull( o.mNotNull )
, mEditorWidgetTypes( o.mEditorWidgetTypes )
, mWidgetConfigs( o.mWidgetConfigs )
, mEditorLayout( o.mEditorLayout )
Expand All @@ -69,7 +68,6 @@ class QgsEditFormConfigPrivate : public QSharedData
QMap< QString, QString> mConstraintsDescription;
QMap< QString, bool> mFieldEditables;
QMap< QString, bool> mLabelOnTop;
QMap< QString, bool> mNotNull;

QMap<QString, QString> mEditorWidgetTypes;
QMap<QString, QgsEditorWidgetConfig > mWidgetConfigs;
Expand Down
6 changes: 5 additions & 1 deletion src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp
Expand Up @@ -264,6 +264,11 @@ void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomEle
formConfig.setReadOnly( idx, ewv2CfgElem.attribute( QStringLiteral( "fieldEditable" ), QStringLiteral( "1" ) ) != QLatin1String( "1" ) );
formConfig.setLabelOnTop( idx, ewv2CfgElem.attribute( QStringLiteral( "labelOnTop" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
formConfig.setNotNull( idx, ewv2CfgElem.attribute( QStringLiteral( "notNull" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
if ( ewv2CfgElem.attribute( QStringLiteral("notNull"), QStringLiteral("0") ) == QLatin1String( "1" ) )
{
// upgrade from older config
vectorLayer->setFieldConstraints( idx, vectorLayer->fieldConstraints( idx ) | QgsField::ConstraintNotNull );
}
formConfig.setConstraintExpression( idx, ewv2CfgElem.attribute( QStringLiteral( "constraint" ), QString() ) );
formConfig.setContraintDescription( idx, ewv2CfgElem.attribute( QStringLiteral( "constraintDescription" ), QString() ) );

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

Expand Down
4 changes: 2 additions & 2 deletions src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp
Expand Up @@ -136,7 +136,7 @@ void QgsEditorWidgetWrapper::updateConstraint( const QgsFeature &ft )
else
mValidConstraint = true;

if ( layer()->editFormConfig().notNull( mFieldIdx ) )
if ( layer()->fieldConstraints( mFieldIdx ) & QgsField::ConstraintNotNull )
{
if ( !expression.isEmpty() )
{
Expand Down Expand Up @@ -175,4 +175,4 @@ bool QgsEditorWidgetWrapper::isInTable( const QWidget* parent )
if ( !parent ) return false;
if ( qobject_cast<const QTableView*>( parent ) ) return true;
return isInTable( parent->parentWidget() );
}
}
2 changes: 1 addition & 1 deletion src/gui/qgsattributeform.cpp
Expand Up @@ -674,7 +674,7 @@ void QgsAttributeForm::onAttributeChanged( const QVariant& value )
break;
}

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

Expand Down

0 comments on commit 6bbd006

Please sign in to comment.