Skip to content

Commit

Permalink
Moved form feature to private and added setters/getters
Browse files Browse the repository at this point in the history
plus other minor style changes as suggested by m-kuhn
in his PR review
  • Loading branch information
elpaso committed May 15, 2018
1 parent 266f145 commit 5417376
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 21 deletions.
Expand Up @@ -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

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


signals:

void valueChanged( const QVariant &value );
Expand Down Expand Up @@ -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


};
Expand Down
2 changes: 1 addition & 1 deletion python/gui/auto_generated/qgsattributeeditorcontext.sip.in
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/fieldformatter/qgsvaluerelationfieldformatter.h
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp
Expand Up @@ -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 )
{
}

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

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

Expand Down Expand Up @@ -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;
Expand Down
35 changes: 30 additions & 5 deletions src/gui/editorwidgets/core/qgseditorwidgetwrapper.h
Expand Up @@ -213,6 +213,7 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
*/
void setConstraintResultVisible( bool constraintResultVisible );


signals:

/**
Expand Down Expand Up @@ -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.
*/
Expand Down
14 changes: 7 additions & 7 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 ) )
Expand All @@ -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( ) )
{
Expand All @@ -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() )
{
Expand Down

0 comments on commit 5417376

Please sign in to comment.