QGIS API Documentation  2.17.0-Master (eef6f05)
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  mDefaultValue = layer()->dataProvider()->defaultValue( mFieldIdx );
46 
47  return mDefaultValue;
48 }
49 
51 {
52  return qobject_cast<QgsEditorWidgetWrapper*>( widget->property( "EWV2Wrapper" ).value<QgsWidgetWrapper*>() );
53 }
54 
56 {
57  QWidget* wdg = widget();
58  if ( wdg )
59  {
60  wdg->setEnabled( enabled );
61  }
62 }
63 
65 {
66  mFeature = feature;
67  setValue( feature.attribute( mFieldIdx ) );
68 }
69 
71 {
72  emit valueChanged( QVariant( value ) );
73 }
74 
76 {
77  emit valueChanged( QVariant( value ) );
78 }
79 
81 {
82  emit valueChanged( QVariant( value ) );
83 }
84 
86 {
87  emit valueChanged( QVariant( value ) );
88 }
89 
91 {
92  emit valueChanged( QVariant( value ) );
93 }
94 
96 {
97  emit valueChanged( value() );
98 }
99 
101 {
102  if ( constraintValid )
103  widget()->setStyleSheet( QString() );
104  else
105  widget()->setStyleSheet( "background-color: #dd7777;" );
106 }
107 
109 {
110  bool toEmit( false );
111  QString errStr( tr( "predicate is True" ) );
112  QString expression = layer()->editFormConfig()->expression( mFieldIdx );
113  QString description;
114  QVariant value = ft.attribute( mFieldIdx );
115 
116  if ( ! expression.isEmpty() )
117  {
118  description = layer()->editFormConfig()->expressionDescription( mFieldIdx );
119 
123 
124  context.setFeature( ft );
125  QgsExpression expr( expression );
126 
127  mValidConstraint = expr.evaluate( &context ).toBool();
128 
129  if ( expr.hasParserError() )
130  errStr = expr.parserErrorString();
131  else if ( expr.hasEvalError() )
132  errStr = expr.evalErrorString();
133  else if ( ! mValidConstraint )
134  errStr = tr( "predicate is False" );
135 
136  toEmit = true;
137  }
138  else
139  mValidConstraint = true;
140 
141  if ( layer()->editFormConfig()->notNull( mFieldIdx ) )
142  {
143  if ( !expression.isEmpty() )
144  {
145  QString fieldName = layer()->attributeDisplayName( mFieldIdx );
146  expression = "( " + expression + " ) AND ( " + fieldName + " IS NOT NULL)";
147  description = "( " + description + " ) AND NotNull";
148  }
149  else
150  {
151  description = "NotNull";
152  expression = "NotNull";
153  }
154 
155  mValidConstraint = mValidConstraint && !value.isNull();
156 
157  if ( value.isNull() )
158  errStr = tr( "predicate is False" );
159 
160  toEmit = true;
161  }
162 
163  if ( toEmit )
164  {
165  updateConstraintWidgetStatus( mValidConstraint );
166  emit constraintStatusChanged( expression, description, errStr, mValidConstraint );
167  }
168 }
169 
171 {
172  return mValidConstraint;
173 }
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
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.
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
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.
virtual QVariant defaultValue(int fieldId)
Returns the default value for field specified by fieldId.
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
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.