QGIS API Documentation  2.99.0-Master (6a61179)
qgseditorwidgetwrapper.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgseditorwidgetwrapper.cpp
3  --------------------------------------
4  Date : 20.4.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias at opengis dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgseditorwidgetwrapper.h"
17 #include "qgsvectorlayer.h"
18 #include "qgsvectordataprovider.h"
19 #include "qgsfields.h"
20 #include "qgsvectorlayerutils.h"
21 
22 #include <QTableView>
23 
24 QgsEditorWidgetWrapper::QgsEditorWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent )
25  : QgsWidgetWrapper( vl, editor, parent )
26  , mValidConstraint( true )
27  , mIsBlockingCommit( false )
28  , mFieldIdx( fieldIdx )
29 {
30 }
31 
33 {
34  return mFieldIdx;
35 }
36 
38 {
39  if ( mFieldIdx < layer()->fields().count() )
40  return layer()->fields().at( mFieldIdx );
41  else
42  return QgsField();
43 }
44 
46 {
47  mDefaultValue = layer()->dataProvider()->defaultValueClause( mFieldIdx );
48 
49  return mDefaultValue;
50 }
51 
53 {
54  return qobject_cast<QgsEditorWidgetWrapper*>( widget->property( "EWV2Wrapper" ).value<QgsWidgetWrapper*>() );
55 }
56 
58 {
59  QWidget* wdg = widget();
60  if ( wdg )
61  {
62  wdg->setEnabled( enabled );
63  }
64 }
65 
67 {
68  mFeature = feature;
69  setValue( feature.attribute( mFieldIdx ) );
70 }
71 
73 {
74  emit valueChanged( QVariant( value ) );
75 }
76 
78 {
79  emit valueChanged( QVariant( value ) );
80 }
81 
83 {
84  emit valueChanged( QVariant( value ) );
85 }
86 
88 {
89  emit valueChanged( QVariant( value ) );
90 }
91 
93 {
94  emit valueChanged( QVariant( value ) );
95 }
96 
98 {
99  emit valueChanged( value() );
100 }
101 
103 {
104  switch ( constraintResult )
105  {
107  widget()->setStyleSheet( QString() );
108  break;
109 
111  widget()->setStyleSheet( QStringLiteral( "background-color: #dd7777;" ) );
112  break;
113 
115  widget()->setStyleSheet( QStringLiteral( "background-color: #ffd85d;" ) );
116  break;
117  }
118 }
119 
121 {
122  bool toEmit( false );
123  QgsField field = layer()->fields().at( mFieldIdx );
124 
125  QString expression = field.constraints().constraintExpression();
126  QStringList expressions, descriptions;
127 
128  if ( ! expression.isEmpty() )
129  {
130  expressions << expression;
131  descriptions << field.constraints().constraintDescription();
132  toEmit = true;
133  }
134 
136  {
137  descriptions << tr( "Not NULL" );
138  if ( !expression.isEmpty() )
139  {
140  expressions << field.name() + QStringLiteral( " IS NOT NULL" );
141  }
142  else
143  {
144  expressions << QStringLiteral( "IS NOT NULL" );
145  }
146  toEmit = true;
147  }
148 
150  {
151  descriptions << tr( "Unique" );
152  if ( !expression.isEmpty() )
153  {
154  expressions << field.name() + QStringLiteral( " IS UNIQUE" );
155  }
156  else
157  {
158  expressions << QStringLiteral( "IS UNIQUE" );
159  }
160  toEmit = true;
161  }
162 
163  QStringList errors;
164  bool hardConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer(), ft, mFieldIdx, errors, QgsFieldConstraints::ConstraintStrengthHard, constraintOrigin );
165 
166  QStringList softErrors;
167  bool softConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer(), ft, mFieldIdx, softErrors, QgsFieldConstraints::ConstraintStrengthSoft, constraintOrigin );
168  errors << softErrors;
169 
170  mValidConstraint = hardConstraintsOk && softConstraintsOk;
171  mIsBlockingCommit = !hardConstraintsOk;
172 
173  mConstraintFailureReason = errors.join( ", " );
174 
175  if ( toEmit )
176  {
177  QString errStr = errors.isEmpty() ? tr( "Constraint checks passed" ) : mConstraintFailureReason;
178 
179  QString description = descriptions.join( ", " );
180  QString expressionDesc;
181  if ( expressions.size() > 1 )
182  expressionDesc = "( " + expressions.join( " ) AND ( " ) + " )";
183  else if ( !expressions.isEmpty() )
184  expressionDesc = expressions.at( 0 );
185 
186  ConstraintResult result = !hardConstraintsOk ? ConstraintResultFailHard
187  : ( !softConstraintsOk ? ConstraintResultFailSoft : ConstraintResultPass );
189  emit constraintStatusChanged( expressionDesc, description, errStr, result );
190  }
191 }
192 
194 {
195  return mValidConstraint;
196 }
197 
199 {
200  return mIsBlockingCommit;
201 }
202 
204 {
205  return mConstraintFailureReason;
206 }
207 
208 bool QgsEditorWidgetWrapper::isInTable( const QWidget* parent )
209 {
210  if ( !parent ) return false;
211  if ( qobject_cast<const QTableView*>( parent ) ) return true;
212  return isInTable( parent->parentWidget() );
213 }
Widget failed at least one soft (non-enforced) constraint.
QString constraintFailureReason() const
Returns the reason why a constraint check has failed (or an empty string if constraint check was succ...
QString name
Definition: qgsfield.h:55
void valueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
Manages an editor widget Widget and wrapper share the same parent.
ConstraintOrigin
Origin of constraints.
Widget failed at least one hard (enforced) constraint.
void setEnabled(bool enabled) override
Is used to enable or disable the edit functionality of the managed widget.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
static bool validateAttribute(const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors, QgsFieldConstraints::ConstraintStrength strength=QgsFieldConstraints::ConstraintStrengthNotSet, QgsFieldConstraints::ConstraintOrigin origin=QgsFieldConstraints::ConstraintOriginNotSet)
Tests an attribute value to check whether it passes all constraints which are present on the correspo...
QgsFields fields() const
Returns the list of fields of this layer.
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:137
void updateConstraint(const QgsFeature &featureContext, QgsFieldConstraints::ConstraintOrigin constraintOrigin=QgsFieldConstraints::ConstraintOriginNotSet)
Update constraint.
QVariant defaultValue() const
Access the default value of the field.
static bool isInTable(const QWidget *parent)
Check if the given widget or one of its parent is a QTableView.
bool isBlockingCommit() const
Returns true if the widget is preventing the feature from being committed.
void setFeature(const QgsFeature &feature) override
Will be called when the feature changes.
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
static QgsEditorWidgetWrapper * fromWidget(QWidget *widget)
Will return a wrapper for a given widget.
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index...
virtual void setValue(const QVariant &value)=0
Is called, when the value of the widget needs to be changed.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:47
User is warned if constraint is violated but feature can still be accepted.
bool isValidConstraint() const
Get the current constraint status.
Widget passed constraints successfully.
QgsFieldConstraints constraints
Definition: qgsfield.h:58
ConstraintResult
Result of constraint checks.
void constraintStatusChanged(const QString &constraint, const QString &desc, const QString &err, ConstraintResult status)
Emit this signal when the constraint status changed.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
virtual void updateConstraintWidgetStatus(ConstraintResult status)
This should update the widget with a visual cue if a constraint status changed.
virtual QVariant value() const =0
Will be used to access the widget&#39;s value.
QWidget * widget()
Access the widget managed by this wrapper.
QgsVectorDataProvider * dataProvider()
Returns the data provider.
int fieldIdx() const
Access the field index.
QString constraintExpression() const
Returns the constraint expression for the field, if set.
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:277
Manages an editor widget Widget and wrapper share the same parent.
Constraint must be honored before feature can be accepted.
Field must have a unique value.