Skip to content

Commit 8449e42

Browse files
committedMay 31, 2016
add constraint description
1 parent bffe308 commit 8449e42

14 files changed

+146
-24
lines changed
 

‎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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
137137
* Emit this signal when the constraint status changed.
138138
* @brief constraintStatusChanged
139139
* @param constraint represented as a string
140+
* @param desc is the constraint description
140141
* @param err the error represented as a string. Empty if none.
141142
* @param status
142143
*/
143-
void constraintStatusChanged( const QString& constraint, const QString& err, bool status );
144+
void constraintStatusChanged( const QString& constraint, const QString &desc, const QString& err, bool status );
144145

145146
public slots:
146147
/**

‎src/gui/qgsattributeform.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -795,17 +795,26 @@ void QgsAttributeForm::clearInvalidConstraintsMessage()
795795
}
796796
}
797797

798-
void QgsAttributeForm::displayInvalidConstraintMessage( const QStringList &f )
798+
void QgsAttributeForm::displayInvalidConstraintMessage( const QStringList &f,
799+
const QStringList &d )
799800
{
800801
clearInvalidConstraintsMessage();
801802

803+
// show only the third first error
804+
int max = 3;
805+
int size = f.size() > max ? max : f.size();
806+
QString descriptions;
807+
for ( int i = 0; i < size; i++ )
808+
descriptions += "<br />- " + f[i] + ": " + d[i];
809+
802810
mInvalidConstraintMessageBarItem =
803-
new QgsMessageBarItem( tr( "Invalid fields:" ),
804-
f.join( ", " ), QgsMessageBar::WARNING );
811+
new QgsMessageBarItem( tr( "Invalid fields: " ),
812+
descriptions, QgsMessageBar::WARNING );
805813
mMessageBar->pushItem( mInvalidConstraintMessageBarItem );
806814
}
807815

808-
bool QgsAttributeForm::currentFormValidConstraints( QStringList &invalidFields )
816+
bool QgsAttributeForm::currentFormValidConstraints( QStringList &invalidFields,
817+
QStringList &descriptions )
809818
{
810819
bool valid( true );
811820

@@ -817,6 +826,10 @@ bool QgsAttributeForm::currentFormValidConstraints( QStringList &invalidFields )
817826
if ( ! eww->isValidConstraint() )
818827
{
819828
invalidFields.append( eww->field().name() );
829+
830+
QString desc = eww->layer()->editFormConfig()->expressionDescription( eww->fieldIdx() );
831+
descriptions.append( desc );
832+
820833
valid = false; // continue to get all invalif fields
821834
}
822835
}
@@ -883,7 +896,7 @@ void QgsAttributeForm::onUpdatedFields()
883896
}
884897

885898
void QgsAttributeForm::onConstraintStatusChanged( const QString& constraint,
886-
const QString& err, bool ok )
899+
const QString &description, const QString& err, bool ok )
887900
{
888901
QgsEditorWidgetWrapper* eww = qobject_cast<QgsEditorWidgetWrapper*>( sender() );
889902
Q_ASSERT( eww );
@@ -892,7 +905,8 @@ void QgsAttributeForm::onConstraintStatusChanged( const QString& constraint,
892905

893906
if ( buddy )
894907
{
895-
QString tooltip = tr( "Expression: " ) + constraint + "\n" + tr( "Constraint: " ) + err;
908+
QString tooltip = tr( "Description: " ) + description + "\n" +
909+
tr( "Raw expression: " ) + constraint + "\n" + tr( "Constraint: " ) + err;
896910
buddy->setToolTip( tooltip );
897911

898912
if ( !buddy->property( "originalText" ).isValid() )
@@ -986,11 +1000,11 @@ void QgsAttributeForm::synchronizeEnabledState()
9861000
// push a message and disable the OK button if constraints are invalid
9871001
clearInvalidConstraintsMessage();
9881002

989-
QStringList invalidFields;
990-
bool validConstraint = currentFormValidConstraints( invalidFields );
1003+
QStringList invalidFields, descriptions;
1004+
bool validConstraint = currentFormValidConstraints( invalidFields, descriptions );
9911005

9921006
if ( ! validConstraint )
993-
displayInvalidConstraintMessage( invalidFields );
1007+
displayInvalidConstraintMessage( invalidFields, descriptions );
9941008

9951009
isEditable = isEditable & validConstraint;
9961010

@@ -1205,7 +1219,6 @@ void QgsAttributeForm::init()
12051219
}
12061220
mButtonBox->setVisible( buttonBoxVisible );
12071221

1208-
<<<<<<< HEAD
12091222
if ( !mSearchButtonBox )
12101223
{
12111224
mSearchButtonBox = new QWidget();
@@ -1618,7 +1631,8 @@ void QgsAttributeForm::afterWidgetInit()
16181631
}
16191632

16201633
connect( eww, SIGNAL( valueChanged( const QVariant& ) ), this, SLOT( onAttributeChanged( const QVariant& ) ) );
1621-
connect( eww, SIGNAL( constraintStatusChanged( QString, QString, bool ) ), this, SLOT( onConstraintStatusChanged( QString, QString, bool ) ) );
1634+
connect( eww, SIGNAL( constraintStatusChanged( QString, QString, QString, bool ) ),
1635+
this, SLOT( onConstraintStatusChanged( QString, QString, QString, bool ) ) );
16221636
}
16231637
}
16241638

‎src/gui/qgsattributeform.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
234234
void onAttributeAdded( int idx );
235235
void onAttributeDeleted( int idx );
236236
void onUpdatedFields();
237-
void onConstraintStatusChanged( const QString& constraint, const QString& err, bool ok );
238-
237+
void onConstraintStatusChanged( const QString& constraint,
238+
const QString &description, const QString& err, bool ok );
239239
void preventFeatureRefresh();
240240
void synchronizeEnabledState();
241241
void layerSelectionChanged();
@@ -302,10 +302,11 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
302302
void updateAllConstaints();
303303
void updateConstraints( QgsEditorWidgetWrapper *w );
304304
bool currentFormFeature( QgsFeature &feature );
305-
bool currentFormValidConstraints( QStringList &invalidFields );
305+
bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
306306
void constraintDependencies( QgsEditorWidgetWrapper *w, QList<QgsEditorWidgetWrapper*> &wDeps );
307307
void clearInvalidConstraintsMessage();
308-
void displayInvalidConstraintMessage( const QStringList &invalidFields );
308+
void displayInvalidConstraintMessage( const QStringList &invalidFields,
309+
const QStringList &description );
309310
void displayNullFieldsMessage();
310311
QgsMessageBarItem *mInvalidConstraintMessageBarItem;
311312
QgsMessageBarItem *mFieldNotInitializedMessageBarItem;

‎src/ui/qgsattributetypeedit.ui

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@
7474
</item>
7575
</layout>
7676
</item>
77+
<item row="5" column="1">
78+
<layout class="QHBoxLayout" name="horizontalLayout">
79+
<property name="bottomMargin">
80+
<number>0</number>
81+
</property>
82+
<item>
83+
<widget class="QLabel" name="label_2">
84+
<property name="text">
85+
<string>Constraint description</string>
86+
</property>
87+
</widget>
88+
</item>
89+
<item>
90+
<widget class="QLineEdit" name="constraintExpressionDescription"/>
91+
</item>
92+
</layout>
93+
</item>
7794
</layout>
7895
</widget>
7996
<customwidgets>

0 commit comments

Comments
 (0)
Please sign in to comment.