QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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
17
18#include "qgsfields.h"
20#include "qgsvectorlayer.h"
23#include "qgsvectorlayerutils.h"
24
25#include <QTableView>
26
27#include "moc_qgseditorwidgetwrapper.cpp"
28
29QgsEditorWidgetWrapper::QgsEditorWidgetWrapper( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent )
30 : QgsWidgetWrapper( vl, editor, parent )
31 , mFieldIdx( fieldIdx )
32 , mValidConstraint( true )
33 , mIsBlockingCommit( false )
34{
35}
36
38{
39 return mFieldIdx;
40}
41
43{
44 QgsVectorLayer *vl = layer();
45 if ( vl && mFieldIdx < vl->fields().count() )
46 return vl->fields().at( mFieldIdx );
47 else
48 return QgsField();
49}
50
52{
53 mDefaultValue = layer()->dataProvider()->defaultValueClause( mFieldIdx );
54
55 return mDefaultValue;
56}
57
58QgsEditorWidgetWrapper *QgsEditorWidgetWrapper::fromWidget( QWidget *widget ) // cppcheck-suppress duplInheritedMember
59{
60 if ( !widget )
61 return nullptr;
62
63 return qobject_cast<QgsEditorWidgetWrapper *>( widget->property( "EWV2Wrapper" ).value<QgsWidgetWrapper *>() );
64}
65
67{
68 QWidget *wdg = widget();
69 if ( wdg )
70 {
71 wdg->setEnabled( enabled );
72 }
73}
74
76{
77 setFormFeature( feature );
78 QVariantList newAdditionalFieldValues;
79 const QStringList constAdditionalFields = additionalFields();
80 for ( const QString &fieldName : constAdditionalFields )
81 newAdditionalFieldValues << feature.attribute( fieldName );
82 setValues( feature.attribute( mFieldIdx ), newAdditionalFieldValues );
83}
84
86{
87 isRunningDeprecatedSetValue = true;
88 updateValues( value, QVariantList() );
89 isRunningDeprecatedSetValue = false;
90}
91
92void QgsEditorWidgetWrapper::setValues( const QVariant &value, const QVariantList &additionalValues )
93{
94 updateValues( value, additionalValues );
95}
96
104
105void QgsEditorWidgetWrapper::parentFormValueChanged( const QString &attribute, const QVariant &value )
106{
107 Q_UNUSED( attribute )
108 Q_UNUSED( value )
109}
110
112{
113 if ( !mConstraintResultVisible )
114 {
115 widget()->setStyleSheet( QString() );
116 }
117 else
118 {
119 switch ( mConstraintResult )
120 {
122 widget()->setStyleSheet( QString() );
123 break;
124
126 widget()->setStyleSheet( QStringLiteral( "QWidget { background-color: rgba(255, 150, 0, 0.3); } QCalendarWidget QWidget#qt_calendar_calendarview, QCalendarWidget QWidget#qt_calendar_navigationbar QWidget { color: rgb(0, 0, 0); background-color: rgba(255, 150, 0, 1); }" ) );
127 break;
128
130 widget()->setStyleSheet( QStringLiteral( "QWidget { background-color: rgba(255, 200, 45, 0.3); } QCalendarWidget QWidget#qt_calendar_calendarview, QCalendarWidget QWidget#qt_calendar_navigationbar QWidget { color: rgb(0, 0, 0); background-color: rgba(255, 200, 45, 1); }" ) );
131 break;
132 }
133 }
134}
135
136bool QgsEditorWidgetWrapper::setFormFeatureAttribute( const QString &attributeName, const QVariant &attributeValue )
137{
138 return mFormFeature.setAttribute( attributeName, attributeValue );
139}
140
141void QgsEditorWidgetWrapper::updateValues( const QVariant &value, const QVariantList &additionalValues )
142{
143 // this method should be made pure virtual in QGIS 4
144 Q_UNUSED( additionalValues );
146 // avoid infinite recursive loop
147 if ( !isRunningDeprecatedSetValue )
148 setValue( value );
150}
151
156
158{
159 return mConstraintResultVisible;
160}
161
163{
164 if ( mConstraintResultVisible == constraintResultVisible )
165 return;
166
167 mConstraintResultVisible = constraintResultVisible;
168
170
171 emit constraintResultVisibleChanged( mConstraintResultVisible );
172}
173
175{
176 updateConstraint( layer(), mFieldIdx, ft, constraintOrigin );
177}
178
180{
181 QStringList errors;
182 QStringList softErrors;
183 QStringList expressions;
184 QStringList descriptions;
185 bool toEmit( false );
186 bool hardConstraintsOk( true );
187 bool softConstraintsOk( true );
188
189 const QgsField field = layer->fields().at( index );
190 const QString expression = field.constraints().constraintExpression();
191
192 if ( ft.isValid() )
193 {
194 if ( !expression.isEmpty() )
195 {
196 expressions << expression;
197 descriptions << field.constraints().constraintDescription();
198 toEmit = true;
199 }
200
201 if ( field.constraints().constraints() & QgsFieldConstraints::ConstraintNotNull )
202 {
203 descriptions << tr( "Not NULL" );
204 if ( !expression.isEmpty() )
205 {
206 expressions << field.name() + QStringLiteral( " IS NOT NULL" );
207 }
208 else
209 {
210 expressions << QStringLiteral( "IS NOT NULL" );
211 }
212 toEmit = true;
213 }
214
215 if ( field.constraints().constraints() & QgsFieldConstraints::ConstraintUnique )
216 {
217 descriptions << tr( "Unique" );
218 if ( !expression.isEmpty() )
219 {
220 expressions << field.name() + QStringLiteral( " IS UNIQUE" );
221 }
222 else
223 {
224 expressions << QStringLiteral( "IS UNIQUE" );
225 }
226 toEmit = true;
227 }
228
229 hardConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer, ft, index, errors, QgsFieldConstraints::ConstraintStrengthHard, constraintOrigin );
230
231 softConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer, ft, index, softErrors, QgsFieldConstraints::ConstraintStrengthSoft, constraintOrigin );
232 errors << softErrors;
233 }
234 else // invalid feature
235 {
236 if ( !expression.isEmpty() )
237 {
238 hardConstraintsOk = true;
239 softConstraintsOk = false;
240
241 errors << QStringLiteral( "Invalid feature" );
242
243 toEmit = true;
244 }
245 }
246
247 mValidConstraint = hardConstraintsOk && softConstraintsOk;
248 mIsBlockingCommit = !hardConstraintsOk;
249
250 mConstraintFailureReason = errors.join( QLatin1String( ", " ) );
251
252 if ( toEmit )
253 {
254 const QString errStr = errors.isEmpty() ? tr( "Constraint checks passed" ) : mConstraintFailureReason;
255
256 const QString description = descriptions.join( QLatin1String( ", " ) );
257 QString expressionDesc;
258 if ( expressions.size() > 1 )
259 expressionDesc = "( " + expressions.join( QLatin1String( " ) AND ( " ) ) + " )";
260 else if ( !expressions.isEmpty() )
261 expressionDesc = expressions.at( 0 );
262
263 const ConstraintResult result = !hardConstraintsOk ? ConstraintResultFailHard
264 : ( !softConstraintsOk ? ConstraintResultFailSoft : ConstraintResultPass );
265 //set the constraint result
266 mConstraintResult = result;
268 emit constraintStatusChanged( expressionDesc, description, errStr, result );
269 }
270}
271
280
282{
283 return mValidConstraint;
284}
285
287{
288 return mIsBlockingCommit;
289}
290
291
293{
294 return mConstraintFailureReason;
295}
296
297bool QgsEditorWidgetWrapper::isInTable( const QWidget *parent )
298{
299 if ( !parent )
300 return false;
301 if ( qobject_cast<const QTableView *>( parent ) )
302 return true;
303 return isInTable( parent->parentWidget() );
304}
305
306void QgsEditorWidgetWrapper::setHint( const QString &hintText )
307{
308 if ( QWidget *w = widget() )
309 w->setToolTip( hintText );
310}
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emit this signal, whenever the value changed.
virtual void updateConstraintWidgetStatus()
This should update the widget with a visual cue if a constraint status changed.
void setFormFeature(const QgsFeature &feature)
Set the feature currently being edited to feature.
virtual QVariant value() const =0
Will be used to access the widget's value.
virtual QVariantList additionalFieldValues() const
Will be used to access the widget's values for potential additional fields handled by the widget.
int fieldIdx() const
Access the field index.
virtual void parentFormValueChanged(const QString &attribute, const QVariant &value)
Is called in embedded form widgets when an attribute value in the parent form has changed.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
void setFeature(const QgsFeature &feature) override
Will be called when the feature changes.
virtual QStringList additionalFields() const
Returns the list of additional fields which the editor handles.
void constraintResultVisibleChanged(bool visible)
Emit this signal when the constraint result visibility changed.
QString constraintFailureReason() const
Returns the reason why a constraint check has failed (or an empty string if constraint check was succ...
QVariant defaultValue() const
Access the default value of the field.
void setEnabled(bool enabled) override
Is used to enable or disable the edit functionality of the managed widget.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
bool isValidConstraint() const
Gets the current constraint status.
void updateConstraint(const QgsFeature &featureContext, QgsFieldConstraints::ConstraintOrigin constraintOrigin=QgsFieldConstraints::ConstraintOriginNotSet)
Update constraint.
static bool isInTable(const QWidget *parent)
Check if the given widget or one of its parent is a QTableView.
void setValues(const QVariant &value, const QVariantList &additionalValues)
Is called when the value of the widget or additional field values needs to be changed.
bool setFormFeatureAttribute(const QString &attributeName, const QVariant &attributeValue)
Update the feature currently being edited by changing its attribute attributeName to attributeValue.
void emitValueChanged()
Will call the value() method to determine the emitted value.
virtual void setHint(const QString &hintText)
Add a hint text on the widget.
QgsField field() const
Access the field.
ConstraintResult
Result of constraint checks.
@ ConstraintResultFailSoft
Widget failed at least one soft (non-enforced) constraint.
@ ConstraintResultPass
Widget passed constraints successfully.
@ ConstraintResultFailHard
Widget failed at least one hard (enforced) constraint.
void constraintStatusChanged(const QString &constraint, const QString &desc, const QString &err, QgsEditorWidgetWrapper::ConstraintResult status)
Emit this signal when the constraint status changed.
virtual void setValue(const QVariant &value)
Is called when the value of the widget needs to be changed.
bool isBlockingCommit() const
Returns true if the widget is preventing the feature from being committed.
void setConstraintResultVisible(bool constraintResultVisible)
Sets whether the constraint result is visible.
static QgsEditorWidgetWrapper * fromWidget(QWidget *widget)
Will return a wrapper for a given widget.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
bool isValid() const
Returns the validity of this feature.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
@ ConstraintStrengthSoft
User is warned if constraint is violated but feature can still be accepted.
@ ConstraintStrengthHard
Constraint must be honored before feature can be accepted.
ConstraintOrigin
Origin of constraints.
@ ConstraintNotNull
Field may not be null.
@ ConstraintUnique
Field must have a unique value.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index.
static bool validateAttribute(const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors, QgsFieldConstraints::ConstraintStrength strength=QgsFieldConstraints::ConstraintStrengthNotSet, QgsFieldConstraints::ConstraintOrigin origin=QgsFieldConstraints::ConstraintOriginNotSet)
Tests a feature attribute value to check whether it passes all constraints which are present on the c...
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
QWidget * widget()
Access the widget managed by this wrapper.
QgsWidgetWrapper(QgsVectorLayer *vl, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169