QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsfield.h"
20 
21 #include <QWidget>
22 
24  : QgsWidgetWrapper( vl, editor, parent )
25  , mValidConstraint( true )
26  , mFieldIdx( fieldIdx )
27 {
28 }
29 
31 {
32  return mFieldIdx;
33 }
34 
36 {
37  if ( mFieldIdx < layer()->fields().count() )
38  return layer()->fields().at( mFieldIdx );
39  else
40  return QgsField();
41 }
42 
44 {
45  if ( !mDefaultValue.isValid() )
46  {
47  QgsVectorLayer* lyr = layer();
48  mDefaultValue = lyr->dataProvider()->defaultValue( lyr->fields().fieldOriginIndex( mFieldIdx ) );
49  }
50 
51  return mDefaultValue;
52 }
53 
55 {
56  return qobject_cast<QgsEditorWidgetWrapper*>( widget->property( "EWV2Wrapper" ).value<QgsWidgetWrapper*>() );
57 }
58 
60 {
61  QWidget* wdg = widget();
62  if ( wdg )
63  {
64  wdg->setEnabled( enabled );
65  }
66 }
67 
69 {
70  mFeature = feature;
71  setValue( feature.attribute( mFieldIdx ) );
72 }
73 
75 {
76  emit valueChanged( QVariant( value ) );
77 }
78 
80 {
81  emit valueChanged( QVariant( value ) );
82 }
83 
85 {
86  emit valueChanged( QVariant( value ) );
87 }
88 
90 {
91  emit valueChanged( QVariant( value ) );
92 }
93 
95 {
96  emit valueChanged( QVariant( value ) );
97 }
98 
100 {
101  emit valueChanged( value() );
102 }
103 
105 {
106  if ( constraintValid )
107  widget()->setStyleSheet( QString() );
108  else
109  widget()->setStyleSheet( "background-color: #dd7777;" );
110 }
111 
113 {
114  bool toEmit( false );
115  QString errStr( tr( "predicate is True" ) );
116  QString expression = layer()->editFormConfig()->expression( mFieldIdx );
117  QString description;
118  QVariant value = ft.attribute( mFieldIdx );
119 
120  if ( ! expression.isEmpty() )
121  {
122  description = layer()->editFormConfig()->expressionDescription( mFieldIdx );
123 
127 
128  context.setFeature( ft );
129  QgsExpression expr( expression );
130 
131  mValidConstraint = expr.evaluate( &context ).toBool();
132 
133  if ( expr.hasParserError() )
134  errStr = expr.parserErrorString();
135  else if ( expr.hasEvalError() )
136  errStr = expr.evalErrorString();
137  else if ( ! mValidConstraint )
138  errStr = tr( "predicate is False" );
139 
140  toEmit = true;
141  }
142  else
143  mValidConstraint = true;
144 
145  if ( layer()->editFormConfig()->notNull( mFieldIdx ) )
146  {
147  if ( !expression.isEmpty() )
148  {
149  QString fieldName = layer()->attributeDisplayName( mFieldIdx );
150  expression = "( " + expression + " ) AND ( " + fieldName + " IS NOT NULL)";
151  description = "( " + description + " ) AND NotNull";
152  }
153  else
154  {
155  description = "NotNull";
156  expression = "NotNull";
157  }
158 
159  mValidConstraint = mValidConstraint && !value.isNull();
160 
161  if ( value.isNull() )
162  errStr = tr( "predicate is False" );
163 
164  toEmit = true;
165  }
166 
167  if ( toEmit )
168  {
169  updateConstraintWidgetStatus( mValidConstraint );
170  emit constraintStatusChanged( expression, description, errStr, mValidConstraint );
171  }
172 }
173 
175 {
176  return mValidConstraint;
177 }
Class for parsing and evaluation of expressions (formerly called "search strings").
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
void setStyleSheet(const QString &styleSheet)
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
void valueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Manages an editor widget Widget and wrapper share the same parent.
static QgsExpressionContext createFeatureBasedContext(const QgsFeature &feature, const QgsFields &fields)
Helper function for creating an expression context which contains just a feature and fields collectio...
virtual void updateConstraintWidgetStatus(bool constraintValid)
This should update the widget with a visual cue if a constraint status changed.
QString evalErrorString() const
Returns evaluation error.
T value() const
virtual void setEnabled(bool enabled) override
Is used to enable or disable the edit functionality of the managed widget.
QString expression(int idx) const
Returns the constraint expression of a specific field.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
QString parserErrorString() const
Returns parser error.
QString tr(const char *sourceText, const char *disambiguation, int n)
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:422
QgsEditFormConfig * editFormConfig() const
Get the configuration of the form used to represent this vector layer.
QgsFields fields() const
Returns the list of fields of this layer.
int fieldOriginIndex(int fieldIdx) const
Get field&#39;s origin index (its meaning is specific to each type of origin)
Definition: qgsfield.cpp:456
QVariant defaultValue() const
Access the default value of the field.
void setEnabled(bool)
const QgsFields * fields() const
Returns the field map associated with the feature.
Definition: qgsfeature.cpp:188
QVariant property(const char *name) const
bool isNull() const
void setFeature(const QgsFeature &feature) override
Will be called when the feature changes.
static QgsEditorWidgetWrapper * fromWidget(QWidget *widget)
Will return a wrapper for a given widget.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool isEmpty() const
QString expressionDescription(int idx) const
Returns the constraint expression description of a specific filed.
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:44
virtual QVariant defaultValue(int fieldId, bool forceLazyEval=false)
Returns the default value for field specified by fieldId.
const QgsAttributeEditorContext & context() const
Returns information about the context in which this widget is shown.
bool isValidConstraint() const
Get the current constraint status.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
void updateConstraint(const QgsFeature &featureContext)
Update constraint.
virtual QVariant value() const =0
Will be used to access the widget&#39;s value.
bool toBool() const
bool isValid() const
QWidget * widget()
Access the widget managed by this wrapper.
QgsVectorDataProvider * dataProvider()
Returns the data provider.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
int fieldIdx() const
Access the field index.
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
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:271
Manages an editor widget Widget and wrapper share the same parent.
QString attributeDisplayName(int attributeIndex) const
Convenience function that returns the attribute alias if defined or the field name else...
void constraintStatusChanged(const QString &constraint, const QString &desc, const QString &err, bool status)
Emit this signal when the constraint status changed.