Skip to content

Commit

Permalink
Cleanup functions
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed Oct 28, 2021
1 parent fc8dc57 commit a7127f8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 67 deletions.
110 changes: 54 additions & 56 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -482,14 +482,18 @@ bool QgsAttributeForm::saveEdits( QString *error )
return success;
}

bool QgsAttributeForm::updateDefaultValues( const int originIdx )
void QgsAttributeForm::updateValuesDependencies( const int originIdx )
{
updateFieldDependencies();

// Synchronize
updateDefaultValueDependencies();
updateValuesDependenciesDefaultValues( originIdx );
updateValuesDependenciesVirtualFields( originIdx );
}

void QgsAttributeForm::updateValuesDependenciesDefaultValues( const int originIdx )
{
if ( !mDefaultValueDependencies.contains( originIdx ) )
return false;
return;

// create updated Feature
QgsFeature updatedFeature = QgsFeature( mFeature );
Expand Down Expand Up @@ -552,14 +556,10 @@ bool QgsAttributeForm::updateDefaultValues( const int originIdx )
}
}
}
return true;
}

void QgsAttributeForm::updateVirtualFields( const int originIdx )
void QgsAttributeForm::updateValuesDependenciesVirtualFields( const int originIdx )
{
// Synchronize
updateVirtualFieldsDependencies();

if ( !mVirtualFieldsDependencies.contains( originIdx ) )
return;

Expand Down Expand Up @@ -596,7 +596,7 @@ void QgsAttributeForm::updateVirtualFields( const int originIdx )
}
}

// go through depending fields and update the virtual field with its expression
// go through depending fields and update the virtual field with its expression
QList<QgsWidgetWrapper *> relevantWidgets = mVirtualFieldsDependencies.values( originIdx );
for ( QgsWidgetWrapper *ww : std::as_const( relevantWidgets ) )
{
Expand Down Expand Up @@ -1025,8 +1025,7 @@ void QgsAttributeForm::onAttributeChanged( const QVariant &value, const QVariant

//append field index here, so it's not updated recursive
mAlreadyUpdatedFields.append( eww->fieldIdx() );
updateDefaultValues( eww->fieldIdx() );
updateVirtualFields( eww->fieldIdx() );
updateValuesDependencies( eww->fieldIdx() );
mAlreadyUpdatedFields.removeAll( eww->fieldIdx() );

// Updates expression controlled labels
Expand Down Expand Up @@ -1834,8 +1833,7 @@ void QgsAttributeForm::init()
}
}

updateDefaultValueDependencies();
updateVirtualFieldsDependencies();
updateFieldDependencies();

if ( !mButtonBox )
{
Expand Down Expand Up @@ -2701,68 +2699,68 @@ bool QgsAttributeForm::fieldIsEditable( int fieldIndex ) const
return QgsVectorLayerUtils::fieldIsEditable( mLayer, fieldIndex, mFeature );
}

void QgsAttributeForm::updateDefaultValueDependencies()
void QgsAttributeForm::updateFieldDependencies()
{
mDefaultValueDependencies.clear();
mVirtualFieldsDependencies.clear();

//create defaultValueDependencies
for ( QgsWidgetWrapper *ww : std::as_const( mWidgets ) )
{
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
if ( eww )
if ( ! eww )
continue;

updateFieldDependenciesDefaultValue( eww );

updateFieldDependenciesVirtualFields( eww );
}
}

void QgsAttributeForm::updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww )
{
QgsExpression exp( eww->field().defaultValueDefinition().expression() );
const QSet<QString> referencedColumns = exp.referencedColumns();
for ( const QString &referencedColumn : referencedColumns )
{
if ( referencedColumn == QgsFeatureRequest::ALL_ATTRIBUTES )
{
QgsExpression exp( eww->field().defaultValueDefinition().expression() );
const QSet<QString> referencedColumns = exp.referencedColumns();
for ( const QString &referencedColumn : referencedColumns )
{
if ( referencedColumn == QgsFeatureRequest::ALL_ATTRIBUTES )
{
const QList<int> allAttributeIds( mLayer->fields().allAttributesList() );
const QList<int> allAttributeIds( mLayer->fields().allAttributesList() );

for ( const int id : allAttributeIds )
{
mDefaultValueDependencies.insertMulti( id, eww );
}
}
else
{
mDefaultValueDependencies.insertMulti( mLayer->fields().lookupField( referencedColumn ), eww );
}
for ( const int id : allAttributeIds )
{
mDefaultValueDependencies.insertMulti( id, eww );
}
}
else
{
mDefaultValueDependencies.insertMulti( mLayer->fields().lookupField( referencedColumn ), eww );
}
}
}

void QgsAttributeForm::updateVirtualFieldsDependencies()
void QgsAttributeForm::updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww )
{
mVirtualFieldsDependencies.clear();
QString expressionField = eww->layer()->expressionField( eww->fieldIdx() );
if ( expressionField.isEmpty() )
return;

for ( QgsWidgetWrapper *ww : std::as_const( mWidgets ) )
QgsExpression exp( expressionField );
const QSet<QString> referencedColumns = exp.referencedColumns();
for ( const QString &referencedColumn : referencedColumns )
{
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
if ( !eww )
continue;

QString expressionField = eww->layer()->expressionField( eww->fieldIdx() );
QgsExpression exp( expressionField );
if ( expressionField.isEmpty() )
continue;

const QSet<QString> referencedColumns = exp.referencedColumns();
for ( const QString &referencedColumn : referencedColumns )
if ( referencedColumn == QgsFeatureRequest::ALL_ATTRIBUTES )
{
if ( referencedColumn == QgsFeatureRequest::ALL_ATTRIBUTES )
const QList<int> allAttributeIds( mLayer->fields().allAttributesList() );
for ( const int id : allAttributeIds )
{
const QList<int> allAttributeIds( mLayer->fields().allAttributesList() );
for ( const int id : allAttributeIds )
{
mVirtualFieldsDependencies.insertMulti( id, eww );
}
}
else
{
mVirtualFieldsDependencies.insertMulti( mLayer->fields().lookupField( referencedColumn ), eww );
mVirtualFieldsDependencies.insertMulti( id, eww );
}
}
else
{
mVirtualFieldsDependencies.insertMulti( mLayer->fields().lookupField( referencedColumn ), eww );
}
}
}

Expand Down
18 changes: 7 additions & 11 deletions src/gui/qgsattributeform.h
Expand Up @@ -377,9 +377,9 @@ class GUI_EXPORT QgsAttributeForm : public QWidget

bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;

void updateDefaultValueDependencies();

void updateVirtualFieldsDependencies();
void updateFieldDependencies();
void updateFieldDependenciesDefaultValue( QgsEditorWidgetWrapper *eww );
void updateFieldDependenciesVirtualFields( QgsEditorWidgetWrapper *eww );

struct WidgetInfo
{
Expand Down Expand Up @@ -408,14 +408,10 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
//! Save single feature or add feature edits
bool saveEdits( QString *error );

//! fill up dependency map for default values
void createDefaultValueDependencies();

//! update the default values in the fields after a referenced field changed
bool updateDefaultValues( const int originIdx );

//! update the value in the virtual fields after a referenced field changed
void updateVirtualFields( const int originIdx );
//! update the default values and virtual fields in the fields after a referenced field changed
void updateValuesDependencies( const int originIdx );
void updateValuesDependenciesDefaultValues( const int originIdx );
void updateValuesDependenciesVirtualFields( const int originIdx );

void clearMultiEditMessages();
void pushSelectedFeaturesMessage();
Expand Down

0 comments on commit a7127f8

Please sign in to comment.