Skip to content

Commit ae9b306

Browse files
committed
Fix #10703 (selection reset after added/removed attribute)
1 parent 2016b30 commit ae9b306

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

src/gui/qgsfieldmodel.cpp

+52-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,59 @@ void QgsFieldModel::updateModel()
8686
{
8787
if ( mLayer )
8888
{
89-
if ( mFields.toList() != mLayer->pendingFields().toList() )
89+
QgsFields newFields = mLayer->pendingFields();
90+
if ( mFields.toList() != newFields.toList() )
9091
{
92+
// Try to handle two special cases: addition of a new field and removal of a field.
93+
// It would be better to listen directly to attributeAdded/attributeDeleted
94+
// so we would not have to check for addition/removal here.
95+
96+
if ( mFields.count() == newFields.count() - 1 )
97+
{
98+
QgsFields tmpNewFields = newFields;
99+
tmpNewFields.remove( tmpNewFields.count() - 1 );
100+
if ( mFields.toList() == tmpNewFields.toList() )
101+
{
102+
// the only change is a new field at the end
103+
beginInsertRows( QModelIndex(), mFields.count(), mFields.count() );
104+
mFields = newFields;
105+
endInsertRows();
106+
return;
107+
}
108+
}
109+
110+
if ( mFields.count() == newFields.count() + 1 )
111+
{
112+
QgsFields tmpOldFields = mFields;
113+
tmpOldFields.remove( tmpOldFields.count() - 1 );
114+
if ( tmpOldFields.toList() == newFields.toList() )
115+
{
116+
// the only change is a field removed at the end
117+
beginRemoveRows( QModelIndex(), mFields.count() - 1, mFields.count() - 1 );
118+
mFields = newFields;
119+
endRemoveRows();
120+
return;
121+
}
122+
123+
for ( int i = 0; i < newFields.count(); ++i )
124+
{
125+
if ( mFields.at( i ) != newFields.at( i ) )
126+
{
127+
QgsFields tmpOldFields = mFields;
128+
tmpOldFields.remove( i );
129+
if ( tmpOldFields.toList() != newFields.toList() )
130+
break; // the change is more complex - go with general case
131+
132+
// the only change is a field removed at index i
133+
beginRemoveRows( QModelIndex(), i, i );
134+
mFields = newFields;
135+
endRemoveRows();
136+
return;
137+
}
138+
}
139+
}
140+
141+
// general case with reset - not good - resets selections
91142
beginResetModel();
92143
mFields = mLayer->pendingFields();
93144
endResetModel();

0 commit comments

Comments
 (0)