Skip to content
Permalink
Browse files

Moved form feature to private and added setters/getters

plus other minor style changes as suggested by m-kuhn
in his PR review
  • Loading branch information
elpaso committed May 14, 2018
1 parent 266f145 commit 541737681b791186243f1ac72b6edd5c79d74672
@@ -118,7 +118,7 @@ Return a list of variables required by the form context ``expression``

static bool expressionIsUsable( const QString &expression, const QgsFeature &feature );
%Docstring
Check wether the ``feature`` has all values required by the ``expression``
Check whether the ``feature`` has all values required by the ``expression``

@return True if the expression can be used

@@ -213,6 +213,7 @@ This will be disabled when the form is not editable.
.. versionadded:: 3.0
%End


signals:

void valueChanged( const QVariant &value );
@@ -279,8 +280,32 @@ change the visual cue.
.. versionadded:: 2.16
%End

protected:

QgsFeature formFeature() const;
%Docstring
The feature currently being edited, in its current state

:return: the feature currently being edited, in its current state

.. versionadded:: 3.2
%End

void setFormFeature( const QgsFeature &feature );
%Docstring
Set the feature currently being edited to ``feature``

.. versionadded:: 3.2
%End

bool setFormFeatureAttribute( const QString &attributeName, const QVariant &attributeValue );
%Docstring
Update the feature currently being edited by changing its
attribute ``attributeName`` to ``attributeValue``

:return: bool true on success

.. versionadded:: 3.2
%End


};
@@ -180,7 +180,7 @@ QGIS forms

const QgsAttributeEditorContext *parentContext() const;

const QgsFeature formFeature() const;
QgsFeature formFeature() const;
%Docstring
Return current feature from the currently edited form or table row

@@ -179,11 +179,11 @@ QSet<QString> QgsValueRelationFieldFormatter::expressionFormVariables( const QSt
const QStringList formVariables( QgsExpressionContextUtils::formScope()->variableNames() );
QSet<QString> variables;

for ( auto it = formVariables.constBegin(); it != formVariables.constEnd(); it++ )
for ( auto const &variable : formVariables )
{
if ( expression.contains( *it ) )
if ( expression.contains( variable ) )
{
variables.insert( *it );
variables.insert( variable );
}
}
return variables;
@@ -115,7 +115,7 @@ class CORE_EXPORT QgsValueRelationFieldFormatter : public QgsFieldFormatter
static QSet<QString> expressionFormVariables( const QString &expression );

/**
* Check wether the \a feature has all values required by the \a expression
* Check whether the \a feature has all values required by the \a expression
*
* @return True if the expression can be used
* \since QGIS 3.2
@@ -25,9 +25,9 @@

QgsEditorWidgetWrapper::QgsEditorWidgetWrapper( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent )
: QgsWidgetWrapper( vl, editor, parent )
, mFieldIdx( fieldIdx )
, mValidConstraint( true )
, mIsBlockingCommit( false )
, mFieldIdx( fieldIdx )
{
}

@@ -68,7 +68,7 @@ void QgsEditorWidgetWrapper::setEnabled( bool enabled )

void QgsEditorWidgetWrapper::setFeature( const QgsFeature &feature )
{
mFeature = feature;
mFormFeature = feature;
setValue( feature.attribute( mFieldIdx ) );
}

@@ -102,6 +102,11 @@ void QgsEditorWidgetWrapper::updateConstraintWidgetStatus()
}
}

bool QgsEditorWidgetWrapper::setFormFeatureAttribute( const QString &attributeName, const QVariant &attributeValue )
{
return mFormFeature.setAttribute( attributeName, attributeValue );
}

QgsEditorWidgetWrapper::ConstraintResult QgsEditorWidgetWrapper::constraintResult() const
{
return mConstraintResult;
@@ -213,6 +213,7 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
*/
void setConstraintResultVisible( bool constraintResultVisible );


signals:

/**
@@ -277,20 +278,44 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
*/
virtual void updateConstraintWidgetStatus();

protected:

/**
* mFieldIdx the widget feature field id
* The feature currently being edited, in its current state
*
* \return the feature currently being edited, in its current state
* \since QGIS 3.2
*/
int mFieldIdx;
QgsFeature formFeature() const { return mFormFeature; }

/**
* mFeature the current feature
* Set the feature currently being edited to \a feature
*
* \since QGIS 3.2
*/
void setFormFeature( const QgsFeature &feature ) { mFormFeature = feature; }

/**
* Update the feature currently being edited by changing its
* attribute \a attributeName to \a attributeValue
*
* \return bool true on success
* \since QGIS 3.2
*/
QgsFeature mFeature;
bool setFormFeatureAttribute( const QString &attributeName, const QVariant &attributeValue );


private:

/**
* mFieldIdx the widget feature field id
*/
int mFieldIdx;

/**
* The feature currently being edited, in its current state
*/
QgsFeature mFormFeature;

/**
* Boolean storing the current validity of the constraint for this widget.
*/
@@ -87,7 +87,7 @@ QVariant QgsValueRelationWidgetWrapper::value() const

QWidget *QgsValueRelationWidgetWrapper::createWidget( QWidget *parent )
{
QgsAttributeForm *form = dynamic_cast<QgsAttributeForm *>( parent );
QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
if ( form )
connect( form, &QgsAttributeForm::widgetValueChanged, this, &QgsValueRelationWidgetWrapper::widgetValueChanged );

@@ -195,7 +195,7 @@ void QgsValueRelationWidgetWrapper::widgetValueChanged( const QString &attribute
// Do nothing if the value has not changed
if ( attributeChanged )
{
mFeature.setAttribute( attribute, newValue );
setFormFeatureAttribute( attribute, newValue );
// Update combos if the value used in the filter expression has changed
if ( QgsValueRelationFieldFormatter::expressionRequiresFormScope( mExpression )
&& QgsValueRelationFieldFormatter::expressionFormAttributes( mExpression ).contains( attribute ) )
@@ -210,14 +210,14 @@ void QgsValueRelationWidgetWrapper::widgetValueChanged( const QString &attribute

void QgsValueRelationWidgetWrapper::setFeature( const QgsFeature &feature )
{
mFeature = feature;
setFormFeature( feature );
whileBlocking( this )->populate();
whileBlocking( this )->setValue( feature.attribute( mFieldIdx ) );
whileBlocking( this )->setValue( feature.attribute( fieldIdx() ) );
// A bit of logic to set the default value if AllowNull is false and this is a new feature
// Note that this needs to be here after the cache has been created/updated by populate()
// and signals unblocked (we want this to propagate to the feature itself)
if ( mFeature.isValid()
&& ! mFeature.attribute( mFieldIdx ).isValid()
if ( formFeature().isValid()
&& ! formFeature().attribute( fieldIdx() ).isValid()
&& mCache.size() > 0
&& ! config( QStringLiteral( "AllowNull" ) ).toBool( ) )
{
@@ -236,7 +236,7 @@ void QgsValueRelationWidgetWrapper::populate( )
// Initialize, note that signals are blocked, to avoid double signals on new features
if ( QgsValueRelationFieldFormatter::expressionRequiresFormScope( mExpression ) )
{
mCache = QgsValueRelationFieldFormatter::createCache( config( ), mFeature );
mCache = QgsValueRelationFieldFormatter::createCache( config( ), formFeature() );
}
else if ( mCache.isEmpty() )
{

0 comments on commit 5417376

Please sign in to comment.
You can’t perform that action at this time.