Skip to content

Commit 8449e42

Browse files
committed
add constraint description
1 parent bffe308 commit 8449e42

14 files changed

Lines changed: 146 additions & 24 deletions

python/core/qgseditformconfig.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,22 @@ class QgsEditFormConfig : QObject
492492
*/
493493
void setExpression( int idx, const QString& str );
494494

495+
/**
496+
* Returns the constraint expression description of a specific filed.
497+
* @param idx The index of the field
498+
* @return the expression description
499+
* @note added in QGIS 2.16
500+
*/
501+
QString expressionDescription( int idx ) const;
502+
503+
/**
504+
* Set the constraint expression description for a specific field.
505+
* @param idx The index of the field
506+
* @param descr The description of the expression
507+
* @note added in QGIS 2.16
508+
*/
509+
void setExpressionDescription( int idx, const QString &descr );
510+
495511
/**
496512
* Returns if the field at fieldidx should be treated as NOT NULL value
497513
*/

python/gui/editorwidgets/core/qgseditorwidgetwrapper.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class QgsEditorWidgetWrapper : QgsWidgetWrapper
115115
* Emit this signal when the constraint status changed.
116116
* @brief constraintStatusChanged
117117
* @param constraint represented as a string
118+
* @param desc is the constraint description
118119
* @param err the error represented as a string. Empty if none.
119120
* @param status
120121
*/

src/app/qgsattributetypedialog.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ bool QgsAttributeTypeDialog::labelOnTop() const
180180
return labelOnTopCheckBox->isChecked();
181181
}
182182

183+
void QgsAttributeTypeDialog::setExpressionDescription( const QString &desc )
184+
{
185+
constraintExpressionDescription->setText( desc );
186+
}
187+
188+
QString QgsAttributeTypeDialog::expressionDescription()
189+
{
190+
return constraintExpressionDescription->text();
191+
}
192+
183193
bool QgsAttributeTypeDialog::notNull() const
184194
{
185195
return notNullCheckBox->isChecked();

src/app/qgsattributetypedialog.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
8585
bool fieldEditable() const;
8686

8787
/**
88-
* Getter for checkbox for not null
88+
* Setter for checkbox for not null
8989
*/
9090
void setNotNull( bool notNull );
9191

@@ -94,6 +94,20 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
9494
*/
9595
bool notNull() const;
9696

97+
/*
98+
* Setter for constraint expression description
99+
* @param desc the expression description
100+
* @note added in QGIS 2.16
101+
**/
102+
void setExpressionDescription( const QString &desc );
103+
104+
/*
105+
* Getter for constraint expression description
106+
* @return the expression description
107+
* @note added in QGIS 2.16
108+
**/
109+
QString expressionDescription();
110+
97111
/**
98112
* Getter for the constraint expression
99113
* @note added in QGIS 2.16

src/app/qgsfieldsproperties.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ void QgsFieldsProperties::attributeTypeDialog()
529529
attributeTypeDialog.setLabelOnTop( cfg.mLabelOnTop );
530530
attributeTypeDialog.setNotNull( cfg.mNotNull );
531531
attributeTypeDialog.setExpression( cfg.mConstraint );
532+
attributeTypeDialog.setExpressionDescription( cfg.mConstraintDescription );
532533

533534
attributeTypeDialog.setWidgetV2Config( cfg.mEditorWidgetV2Config );
534535
attributeTypeDialog.setWidgetV2Type( cfg.mEditorWidgetV2Type );
@@ -539,6 +540,7 @@ void QgsFieldsProperties::attributeTypeDialog()
539540
cfg.mEditable = attributeTypeDialog.fieldEditable();
540541
cfg.mLabelOnTop = attributeTypeDialog.labelOnTop();
541542
cfg.mNotNull = attributeTypeDialog.notNull();
543+
cfg.mConstraintDescription = attributeTypeDialog.expressionDescription();
542544
cfg.mConstraint = attributeTypeDialog.expression();
543545

544546
cfg.mEditorWidgetV2Type = attributeTypeDialog.editorWidgetV2Type();
@@ -913,6 +915,7 @@ void QgsFieldsProperties::apply()
913915
mLayer->editFormConfig()->setReadOnly( i, !cfg.mEditable );
914916
mLayer->editFormConfig()->setLabelOnTop( i, cfg.mLabelOnTop );
915917
mLayer->editFormConfig()->setNotNull( i, cfg.mNotNull );
918+
mLayer->editFormConfig()->setExpressionDescription( i, cfg.mConstraintDescription );
916919
mLayer->editFormConfig()->setExpression( i, cfg.mConstraint );
917920

918921
mLayer->editFormConfig()->setWidgetType( idx, cfg.mEditorWidgetV2Type );
@@ -981,6 +984,7 @@ QgsFieldsProperties::FieldConfig::FieldConfig()
981984
, mEditableEnabled( true )
982985
, mLabelOnTop( false )
983986
, mNotNull( false )
987+
, mConstraintDescription( QString() )
984988
, mButton( nullptr )
985989
{
986990
}
@@ -994,6 +998,7 @@ QgsFieldsProperties::FieldConfig::FieldConfig( QgsVectorLayer* layer, int idx )
994998
mLabelOnTop = layer->editFormConfig()->labelOnTop( idx );
995999
mNotNull = layer->editFormConfig()->notNull( idx );
9961000
mConstraint = layer->editFormConfig()->expression( idx );
1001+
mConstraintDescription = layer->editFormConfig()->expressionDescription( idx );
9971002
mEditorWidgetV2Type = layer->editFormConfig()->widgetType( idx );
9981003
mEditorWidgetV2Config = layer->editFormConfig()->widgetConfig( idx );
9991004

src/app/qgsfieldsproperties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
9494
bool mLabelOnTop;
9595
bool mNotNull;
9696
QString mConstraint;
97+
QString mConstraintDescription;
9798
QPushButton* mButton;
9899
QString mEditorWidgetV2Type;
99100
QMap<QString, QVariant> mEditorWidgetV2Config;

src/core/qgseditformconfig.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ void QgsEditFormConfig::setExpression( int idx, const QString& str )
135135
mConstraints[ mFields.at( idx ).name()] = str;
136136
}
137137

138+
QString QgsEditFormConfig::expressionDescription( int idx ) const
139+
{
140+
QString description;
141+
142+
if ( idx >= 0 && idx < mFields.count() )
143+
description = mConstraintsDescription[ mFields.at( idx ).name()];
144+
145+
return description;
146+
}
147+
148+
void QgsEditFormConfig::setExpressionDescription( int idx, const QString &descr )
149+
{
150+
if ( idx >= 0 && idx < mFields.count() )
151+
mConstraintsDescription[ mFields.at( idx ).name()] = descr;
152+
}
153+
138154
bool QgsEditFormConfig::notNull( int idx ) const
139155
{
140156
if ( idx >= 0 && idx < mFields.count() )

src/core/qgseditformconfig.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,12 @@ class CORE_EXPORT QgsEditFormConfig : public QObject
484484
QgsEditorWidgetConfig widgetConfig( const QString& widgetName ) const;
485485

486486
/**
487-
* Remove the configuration for the editor widget used to represent the field at the given index
488-
*
489-
* @param fieldIdx The index of the field
490-
*
491-
* @return true if successful, false if the field does not exist
492-
*/
487+
* Remove the configuration for the editor widget used to represent the field at the given index
488+
*
489+
* @param fieldIdx The index of the field
490+
*
491+
* @return true if successful, false if the field does not exist
492+
*/
493493
bool removeWidgetConfig( int fieldIdx );
494494

495495
/**
@@ -528,6 +528,22 @@ class CORE_EXPORT QgsEditFormConfig : public QObject
528528
*/
529529
void setExpression( int idx, const QString& str );
530530

531+
/**
532+
* Returns the constraint expression description of a specific filed.
533+
* @param idx The index of the field
534+
* @return the expression description
535+
* @note added in QGIS 2.16
536+
*/
537+
QString expressionDescription( int idx ) const;
538+
539+
/**
540+
* Set the constraint expression description for a specific field.
541+
* @param idx The index of the field
542+
* @param descr The description of the expression
543+
* @note added in QGIS 2.16
544+
*/
545+
void setExpressionDescription( int idx, const QString &descr );
546+
531547
/**
532548
* Returns if the field at fieldidx should be treated as NOT NULL value
533549
*/
@@ -657,6 +673,7 @@ class CORE_EXPORT QgsEditFormConfig : public QObject
657673
QList< TabData > mTabs;
658674

659675
QMap< QString, QString> mConstraints;
676+
QMap< QString, QString> mConstraintsDescription;
660677
QMap< QString, bool> mFieldEditables;
661678
QMap< QString, bool> mLabelOnTop;
662679
QMap< QString, bool> mNotNull;

src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomEle
253253
vectorLayer->editFormConfig()->setLabelOnTop( idx, ewv2CfgElem.attribute( "labelOnTop", "0" ) == "1" );
254254
vectorLayer->editFormConfig()->setNotNull( idx, ewv2CfgElem.attribute( "notNull", "0" ) == "1" );
255255
vectorLayer->editFormConfig()->setExpression( idx, ewv2CfgElem.attribute( "constraint", QString() ) );
256+
vectorLayer->editFormConfig()->setExpressionDescription( idx, ewv2CfgElem.attribute( "constraintDescription", QString() ) );
256257

257258
vectorLayer->editFormConfig()->setWidgetConfig( idx, cfg );
258259
}
@@ -312,6 +313,7 @@ void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement&
312313
ewv2CfgElem.setAttribute( "labelOnTop", vectorLayer->editFormConfig()->labelOnTop( idx ) );
313314
ewv2CfgElem.setAttribute( "notNull", vectorLayer->editFormConfig()->notNull( idx ) );
314315
ewv2CfgElem.setAttribute( "constraint", vectorLayer->editFormConfig()->expression( idx ) );
316+
ewv2CfgElem.setAttribute( "constraintDescription", vectorLayer->editFormConfig()->expressionDescription( idx ) );
315317

316318
mWidgetFactories[widgetType]->writeConfig( vectorLayer->editFormConfig()->widgetConfig( idx ), ewv2CfgElem, doc, vectorLayer, idx );
317319

src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,13 @@ void QgsEditorWidgetWrapper::updateConstraint( const QgsFeature &ft )
108108
bool toEmit( false );
109109
QString errStr( tr( "predicate is True" ) );
110110
QString expression = layer()->editFormConfig()->expression( mFieldIdx );
111+
QString description;
111112
QVariant value = ft.attribute( mFieldIdx );
112113

113114
if ( ! expression.isEmpty() )
114115
{
116+
description = layer()->editFormConfig()->expressionDescription( mFieldIdx );
117+
115118
QgsExpressionContext context =
116119
QgsExpressionContextUtils::createFeatureBasedContext( ft, *ft.fields() );
117120
context << QgsExpressionContextUtils::layerScope( layer() );
@@ -139,9 +142,13 @@ void QgsEditorWidgetWrapper::updateConstraint( const QgsFeature &ft )
139142
{
140143
QString fieldName = ft.fields()->field( mFieldIdx ).name();
141144
expression = "( " + expression + " ) AND ( " + fieldName + " IS NOT NULL)";
145+
description = "( " + description + " ) AND NotNull";
142146
}
143147
else
148+
{
149+
description = "NotNull";
144150
expression = "NotNull";
151+
}
145152

146153
mValidConstraint = mValidConstraint && !value.isNull();
147154

@@ -154,7 +161,7 @@ void QgsEditorWidgetWrapper::updateConstraint( const QgsFeature &ft )
154161
if ( toEmit )
155162
{
156163
updateConstraintWidgetStatus();
157-
emit constraintStatusChanged( expression, errStr, mValidConstraint );
164+
emit constraintStatusChanged( expression, description, errStr, mValidConstraint );
158165
}
159166
}
160167

0 commit comments

Comments
 (0)