Skip to content
Permalink
Browse files

Merge pull request #3079 from pblottiere/constraints

[FEATURE] widgets constraints
  • Loading branch information
m-kuhn committed Jun 1, 2016
2 parents 61e26fd + 8774ad2 commit 2b1560c5be3e6c0ff5045ffdff0fd6eec0f24a94
Showing with 1,113 additions and 45 deletions.
  1. +42 −1 python/core/qgseditformconfig.sip
  2. +1 −1 python/core/qgsfield.sip
  3. +38 −0 python/gui/editorwidgets/core/qgseditorwidgetwrapper.sip
  4. +14 −0 python/gui/editorwidgets/qgsrelationreferencewidgetwrapper.sip
  5. +34 −2 src/app/qgsattributetypedialog.cpp
  6. +39 −3 src/app/qgsattributetypedialog.h
  7. +14 −0 src/app/qgsfieldsproperties.cpp
  8. +3 −0 src/app/qgsfieldsproperties.h
  9. +47 −1 src/core/qgseditformconfig.cpp
  10. +50 −6 src/core/qgseditformconfig.h
  11. +5 −1 src/core/qgseditorwidgetconfig.h
  12. +6 −0 src/core/qgsfeature.cpp
  13. +2 −2 src/core/qgsvectorlayer.cpp
  14. +2 −2 src/core/qgsvectorlayer.h
  15. +7 −0 src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp
  16. +77 −0 src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp
  17. +45 −0 src/gui/editorwidgets/core/qgseditorwidgetwrapper.h
  18. +5 −0 src/gui/editorwidgets/qgscolorwidgetwrapper.cpp
  19. +2 −0 src/gui/editorwidgets/qgscolorwidgetwrapper.h
  20. +17 −1 src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp
  21. +2 −0 src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.h
  22. +13 −0 src/gui/editorwidgets/qgsfilenamewidgetwrapper.cpp
  23. +2 −0 src/gui/editorwidgets/qgsfilenamewidgetwrapper.h
  24. +13 −0 src/gui/editorwidgets/qgsphotowidgetwrapper.cpp
  25. +2 −0 src/gui/editorwidgets/qgsphotowidgetwrapper.h
  26. +1 −1 src/gui/editorwidgets/qgsrangewidgetfactory.cpp
  27. +1 −1 src/gui/editorwidgets/qgsrelationreferenceconfigdlg.cpp
  28. +1 −1 src/gui/editorwidgets/qgsrelationreferencefactory.cpp
  29. +11 −0 src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.cpp
  30. +14 −0 src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.h
  31. +13 −0 src/gui/editorwidgets/qgswebviewwidgetwrapper.cpp
  32. +2 −0 src/gui/editorwidgets/qgswebviewwidgetwrapper.h
  33. +258 −3 src/gui/qgsattributeform.cpp
  34. +19 −2 src/gui/qgsattributeform.h
  35. +62 −14 src/ui/qgsattributetypeedit.ui
  36. +4 −3 tests/src/gui/CMakeLists.txt
  37. +245 −0 tests/src/gui/testqgsattributeform.cpp
@@ -474,7 +474,48 @@ class QgsEditFormConfig : QObject
/**
* If set to false, the widget at the given index will be read-only.
*/
void setReadOnly(int idx, bool readOnly );
void setReadOnly( int idx, bool readOnly = true );

/**
* Returns the constraint expression of a specific field
* @param idx The index of the field
* @return the expression
* @note added in QGIS 2.16
*/
QString expression( int idx ) const;

/**
* Set the constraint expression for a specific field
* @param idx the field index
* @param str the constraint expression
* @note added in QGIS 2.16
*/
void setExpression( int idx, const QString& str );

/**
* Returns the constraint expression description of a specific filed.
* @param idx The index of the field
* @return the expression description
* @note added in QGIS 2.16
*/
QString expressionDescription( int idx ) const;

/**
* Set the constraint expression description for a specific field.
* @param idx The index of the field
* @param descr The description of the expression
* @note added in QGIS 2.16
*/
void setExpressionDescription( int idx, const QString &descr );

/**
* 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
@@ -165,7 +165,7 @@ class QgsField
/* Raise an exception if the arguments couldn't be parsed. */
sipNoMethod(sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible);

return NULL;
return nullptr;
%End

//! Allows direct construction of QVariants from fields.
@@ -88,6 +88,21 @@ class QgsEditorWidgetWrapper : QgsWidgetWrapper
*/
virtual void showIndeterminateState();

/**
* Update constraint.
* @param featureContext the feature to use to evaluate the constraint
* @note added in QGIS 2.16
*/
void updateConstraint( const QgsFeature &featureContext );

/**
* Get the current constraint status.
* @return true if the constraint is valid or if there's not constraint,
* false otherwise
* @note added in QGIS 2.16
*/
bool isValidConstraint() const;

signals:
/**
* Emit this signal, whenever the value changed.
@@ -96,6 +111,16 @@ class QgsEditorWidgetWrapper : QgsWidgetWrapper
*/
void valueChanged( const QVariant& value );

/**
* Emit this signal when the constraint status changed.
* @brief constraintStatusChanged
* @param constraint represented as a string
* @param desc is the constraint description
* @param err the error represented as a string. Empty if none.
* @param status
*/
void constraintStatusChanged( const QString& constraint, const QString& err, bool status );

public slots:
/**
* Will be called when the feature changes
@@ -162,4 +187,17 @@ class QgsEditorWidgetWrapper : QgsWidgetWrapper
* Will call the value() method to determine the emitted value
*/
void valueChanged();

protected:
/**
* This should update the widget with a visual cue if a constraint status
* changed.
*
* By default a stylesheet will be applied on the widget that changes the
* background color to red.
*
* This can be overwritten in subclasses to allow individual widgets to
* change the visual cue.
*/
virtual void updateConstraintWidgetStatus();
};
@@ -21,4 +21,18 @@ class QgsRelationReferenceWidgetWrapper : QgsEditorWidgetWrapper
public slots:
virtual void setValue( const QVariant& value );
virtual void setEnabled( bool enabled );

protected:
/**
* This should update the widget with a visual cue if a constraint status
* changed.
*
* By default a stylesheet will be applied on the widget that changes the
* background color to red.
*
* This can be overwritten in subclasses to allow individual widgets to
* change the visual cue.
* @note added in QGIS 2.16
*/
void updateConstraintWidgetStatus();
};
@@ -71,6 +71,8 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx

QSettings settings;
restoreGeometry( settings.value( "/Windows/QgsAttributeTypeDialog/geometry" ).toByteArray() );

constraintExpression->setLayer( vl );
}

QgsAttributeTypeDialog::~QgsAttributeTypeDialog()
@@ -163,16 +165,46 @@ void QgsAttributeTypeDialog::setWidgetV2Config( const QgsEditorWidgetConfig& con
mWidgetV2Config = config;
}

bool QgsAttributeTypeDialog::fieldEditable()
bool QgsAttributeTypeDialog::fieldEditable() const
{
return isFieldEditableCheckBox->isChecked();
}

bool QgsAttributeTypeDialog::labelOnTop()
void QgsAttributeTypeDialog::setNotNull( bool notNull )
{
notNullCheckBox->setChecked( notNull );
}

bool QgsAttributeTypeDialog::labelOnTop() const
{
return labelOnTopCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setExpressionDescription( const QString &desc )
{
constraintExpressionDescription->setText( desc );
}

QString QgsAttributeTypeDialog::expressionDescription()
{
return constraintExpressionDescription->text();
}

bool QgsAttributeTypeDialog::notNull() const
{
return notNullCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setExpression( const QString &str )
{
constraintExpression->setField( str );
}

QString QgsAttributeTypeDialog::expression() const
{
return constraintExpression->asExpression();
}

void QgsAttributeTypeDialog::setFieldEditable( bool editable )
{
isFieldEditableCheckBox->setChecked( editable );
@@ -69,6 +69,11 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
*/
void setLabelOnTop( bool onTop );

/**
* Getter for checkbox for label on top of field
*/
bool labelOnTop() const;

/**
* Setter for checkbox for editable state of field
*/
@@ -77,12 +82,43 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
/**
* Getter for checkbox for editable state of field
*/
bool fieldEditable();
bool fieldEditable() const;

/**
* Getter for checkbox for label on top of field
* Setter for checkbox for not null
*/
void setNotNull( bool notNull );

/**
* Getter for checkbox for not null
*/
bool notNull() const;

/*
* Setter for constraint expression description
* @param desc the expression description
* @note added in QGIS 2.16
**/
void setExpressionDescription( const QString &desc );

/*
* Getter for constraint expression description
* @return the expression description
* @note added in QGIS 2.16
**/
QString expressionDescription();

/**
* Getter for the constraint expression
* @note added in QGIS 2.16
*/
QString expression() const;

/**
* Setter for the constraint expression
* @note added in QGIS 2.16
*/
bool labelOnTop();
void setExpression( const QString &str );

private slots:
/**
@@ -527,6 +527,9 @@ void QgsFieldsProperties::attributeTypeDialog()

attributeTypeDialog.setFieldEditable( cfg.mEditable );
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
attributeTypeDialog.setNotNull( cfg.mNotNull );
attributeTypeDialog.setExpression( cfg.mConstraint );
attributeTypeDialog.setExpressionDescription( cfg.mConstraintDescription );

attributeTypeDialog.setWidgetV2Config( cfg.mEditorWidgetV2Config );
attributeTypeDialog.setWidgetV2Type( cfg.mEditorWidgetV2Type );
@@ -536,6 +539,9 @@ void QgsFieldsProperties::attributeTypeDialog()

cfg.mEditable = attributeTypeDialog.fieldEditable();
cfg.mLabelOnTop = attributeTypeDialog.labelOnTop();
cfg.mNotNull = attributeTypeDialog.notNull();
cfg.mConstraintDescription = attributeTypeDialog.expressionDescription();
cfg.mConstraint = attributeTypeDialog.expression();

cfg.mEditorWidgetV2Type = attributeTypeDialog.editorWidgetV2Type();
cfg.mEditorWidgetV2Config = attributeTypeDialog.editorWidgetV2Config();
@@ -908,6 +914,9 @@ void QgsFieldsProperties::apply()

mLayer->editFormConfig()->setReadOnly( i, !cfg.mEditable );
mLayer->editFormConfig()->setLabelOnTop( i, cfg.mLabelOnTop );
mLayer->editFormConfig()->setNotNull( i, cfg.mNotNull );
mLayer->editFormConfig()->setExpressionDescription( i, cfg.mConstraintDescription );
mLayer->editFormConfig()->setExpression( i, cfg.mConstraint );

mLayer->editFormConfig()->setWidgetType( idx, cfg.mEditorWidgetV2Type );
mLayer->editFormConfig()->setWidgetConfig( idx, cfg.mEditorWidgetV2Config );
@@ -974,6 +983,8 @@ QgsFieldsProperties::FieldConfig::FieldConfig()
: mEditable( true )
, mEditableEnabled( true )
, mLabelOnTop( false )
, mNotNull( false )
, mConstraintDescription( QString() )
, mButton( nullptr )
{
}
@@ -985,6 +996,9 @@ 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 );
mConstraint = layer->editFormConfig()->expression( idx );
mConstraintDescription = layer->editFormConfig()->expressionDescription( idx );
mEditorWidgetV2Type = layer->editFormConfig()->widgetType( idx );
mEditorWidgetV2Config = layer->editFormConfig()->widgetConfig( idx );

@@ -92,6 +92,9 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
bool mEditable;
bool mEditableEnabled;
bool mLabelOnTop;
bool mNotNull;
QString mConstraint;
QString mConstraintDescription;
QPushButton* mButton;
QString mEditorWidgetV2Type;
QMap<QString, QVariant> mEditorWidgetV2Config;
@@ -119,6 +119,46 @@ bool QgsEditFormConfig::labelOnTop( int idx ) const
return false;
}

QString QgsEditFormConfig::expression( int idx ) const
{
QString expr;

if ( idx >= 0 && idx < mFields.count() )
expr = mConstraints.value( mFields.at( idx ).name(), QString() );

return expr;
}

void QgsEditFormConfig::setExpression( int idx, const QString& str )
{
if ( idx >= 0 && idx < mFields.count() )
mConstraints[ mFields.at( idx ).name()] = str;
}

QString QgsEditFormConfig::expressionDescription( int idx ) const
{
QString description;

if ( idx >= 0 && idx < mFields.count() )
description = mConstraintsDescription[ mFields.at( idx ).name()];

return description;
}

void QgsEditFormConfig::setExpressionDescription( int idx, const QString &descr )
{
if ( idx >= 0 && idx < mFields.count() )
mConstraintsDescription[ mFields.at( idx ).name()] = descr;
}

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

void QgsEditFormConfig::setReadOnly( int idx, bool readOnly )
{
if ( idx >= 0 && idx < mFields.count() )
@@ -131,6 +171,12 @@ void QgsEditFormConfig::setLabelOnTop( int idx, bool onTop )
mLabelOnTop[ mFields.at( idx ).name()] = onTop;
}

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

void QgsEditFormConfig::readXml( const QDomNode& node )
{
QDomNode editFormNode = node.namedItem( "editform" );
@@ -280,7 +326,6 @@ void QgsEditFormConfig::writeXml( QDomNode& node ) const
efifpField.appendChild( doc.createTextNode( QgsProject::instance()->writePath( initFilePath() ) ) );
node.appendChild( efifpField );


QDomElement eficField = doc.createElement( "editforminitcode" );
eficField.appendChild( doc.createCDATASection( initCode() ) );
node.appendChild( eficField );
@@ -337,6 +382,7 @@ void QgsEditFormConfig::writeXml( QDomNode& node ) const
{
QDomElement widgetElem = doc.createElement( "widget" );
widgetElem.setAttribute( "name", configIt.key() );
// widgetElem.setAttribute( "notNull", );

QDomElement configElem = doc.createElement( "config" );
widgetElem.appendChild( configElem );

0 comments on commit 2b1560c

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