Skip to content
Permalink
Browse files

Move handling of not null constraint from edit form to layer

  • Loading branch information
nyalldawson committed Nov 2, 2016
1 parent bd9f672 commit 6bbd0061f3e7cb4f739c3ec94a0711381b39119d
@@ -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}
-------------
@@ -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.
@@ -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 );
@@ -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() );
@@ -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() );
@@ -1032,7 +1046,7 @@ QgsFieldsProperties::FieldConfig::FieldConfig()
: mEditable( true )
, mEditableEnabled( true )
, mLabelOnTop( false )
, mNotNull( false )
, mConstraints( 0 )
, mConstraintDescription( QString() )
, mButton( nullptr )
{
@@ -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() );
@@ -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;
@@ -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() )
@@ -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();
@@ -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.
@@ -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 )
@@ -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;
@@ -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() ) );

@@ -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 ) );

@@ -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() )
{
@@ -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() );
}
}
@@ -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() );

0 comments on commit 6bbd006

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