Skip to content

Commit

Permalink
Fix attr table model field order when changed in the provider
Browse files Browse the repository at this point in the history
Fixes the GUI part of #45139
  • Loading branch information
elpaso committed Sep 21, 2021
1 parent 46c7c12 commit 0c4d0ea
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
Expand Up @@ -203,11 +203,13 @@ Returns -1 if none is defined.
virtual int columnCount( const QModelIndex &parent ) const;


void setAttributeTableConfig( const QgsAttributeTableConfig &config );
void setAttributeTableConfig( const QgsAttributeTableConfig &config);
%Docstring
Set the attribute table configuration to control which fields are shown,
in which order they are shown as well as if and where an action column
is shown.

:param config: attribute table config
%End

void setFilterExpression( const QgsExpression &expression, const QgsExpressionContext &context );
Expand Down
10 changes: 8 additions & 2 deletions src/core/qgsattributetableconfig.cpp
Expand Up @@ -86,8 +86,14 @@ void QgsAttributeTableConfig::update( const QgsFields &fields )
newColumn.hidden = false;
newColumn.type = Field;
newColumn.name = field.name();

mColumns.append( newColumn );
if ( containsActionColumn )
{
mColumns.insert( mColumns.size() - 1, newColumn );
}
else
{
mColumns.append( newColumn );
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/attributetable/qgsattributetablefiltermodel.cpp
Expand Up @@ -143,13 +143,13 @@ int QgsAttributeTableFilterModel::columnCount( const QModelIndex &parent ) const
return mColumnMapping.count();
}

void QgsAttributeTableFilterModel::setAttributeTableConfig( const QgsAttributeTableConfig &config )
void QgsAttributeTableFilterModel::setAttributeTableConfig( const QgsAttributeTableConfig &config, bool force )
{
const QgsAttributeTableConfig oldConfig = mConfig;
mConfig = config;
mConfig.update( layer()->fields() );

if ( mConfig.hasSameColumns( oldConfig ) )
if ( !force && mConfig.hasSameColumns( oldConfig ) )
{
return;
}
Expand Down
4 changes: 3 additions & 1 deletion src/gui/attributetable/qgsattributetablefiltermodel.h
Expand Up @@ -238,8 +238,10 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
* Set the attribute table configuration to control which fields are shown,
* in which order they are shown as well as if and where an action column
* is shown.
* \param config attribute table config
* \param force default FALSE, if TRUE the attribute table configuration will be reset even if it is not changed.
*/
void setAttributeTableConfig( const QgsAttributeTableConfig &config );
void setAttributeTableConfig( const QgsAttributeTableConfig &config, bool force SIP_PYARGREMOVE = false );

/**
* Set the \a expression and the \a context to be stored in case of the features
Expand Down
7 changes: 7 additions & 0 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -128,6 +128,13 @@ void QgsDualView::init( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas, const Qg
mAttributeForm = nullptr;

mLayer = layer;

// Keep fields order in sync: force config reset
connect( mLayer, &QgsVectorLayer::updatedFields, this, [ = ]
{
mFilterModel->setAttributeTableConfig( attributeTableConfig(), /* force */ true );
} );

mEditorContext = context;

// create an empty form to find out if it needs geometry or not
Expand Down

0 comments on commit 0c4d0ea

Please sign in to comment.