Skip to content

Commit 01516d7

Browse files
committedApr 29, 2020
reintroduce reload current feature
1 parent 82da0fd commit 01516d7

File tree

4 files changed

+139
-78
lines changed

4 files changed

+139
-78
lines changed
 

‎python/core/auto_generated/qgsfeaturepickermodel.sip.in‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ Indicator if the model is currently performing any feature iteration in the back
171171
void filterJobCompleted();
172172
%Docstring
173173
Indicates that a filter job has been completed and new data may be available.
174+
%End
175+
176+
void extraValueDoesNotExistChanged();
177+
%Docstring
178+
Flag indicating that the extraIdentifierValue does not exist in the data.
174179
%End
175180

176181
void beginUpdate();

‎src/core/qgsfeaturepickermodel.cpp‎

Lines changed: 125 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -211,97 +211,121 @@ void QgsFeaturePickerModel::updateCompleter()
211211
{
212212
setCurrentFeatureUnguarded( FID_NULL );
213213
}
214-
// We got strings for a filter selection
215-
std::sort( entries.begin(), entries.end(), []( const QgsFeatureExpressionValuesGatherer::Entry & a, const QgsFeatureExpressionValuesGatherer::Entry & b ) { return a.value.localeAwareCompare( b.value ) < 0; } );
216214

217-
if ( mAllowNull )
215+
// Only reloading the current entry?
216+
bool reloadCurrentFeatureOnly = mGatherer->data().toBool();
217+
if ( reloadCurrentFeatureOnly )
218218
{
219-
entries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry() );
219+
if ( !entries.isEmpty() )
220+
{
221+
mEntries.replace( mCurrentIndex, entries.at( 0 ) );
222+
emit dataChanged( index( mCurrentIndex, 0, QModelIndex() ), index( mCurrentIndex, 0, QModelIndex() ) );
223+
mShouldReloadCurrentFeature = 0;
224+
setExtraValueDoesNotExist( false );
225+
}
226+
else
227+
{
228+
setExtraValueDoesNotExist( true );
229+
}
230+
231+
mShouldReloadCurrentFeature = 0;
232+
233+
if ( mFilterValue.isEmpty() )
234+
reload();
220235
}
236+
else
237+
{
238+
// We got strings for a filter selection
239+
std::sort( entries.begin(), entries.end(), []( const QgsFeatureExpressionValuesGatherer::Entry & a, const QgsFeatureExpressionValuesGatherer::Entry & b ) { return a.value.localeAwareCompare( b.value ) < 0; } );
221240

222-
const int newEntriesSize = entries.size();
241+
if ( mAllowNull )
242+
{
243+
entries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry() );
244+
}
223245

224-
// fixed entry is either NULL or extra value
225-
const int nbFixedEntry = mAllowNull ? 1 : 0 ;
246+
const int newEntriesSize = entries.size();
226247

227-
// Find the index of the current entry in the new list
228-
int currentEntryInNewList = -1;
229-
if ( mCurrentIndex != -1 && mCurrentIndex < mEntries.count() )
230-
{
231-
for ( int i = 0; i < newEntriesSize; ++i )
248+
// fixed entry is either NULL or extra value
249+
const int nbFixedEntry = ( mExtraValueDoesNotExist ? 1 : 0 ) + ( mAllowNull ? 1 : 0 );
250+
251+
// Find the index of the current entry in the new list
252+
int currentEntryInNewList = -1;
253+
if ( mCurrentIndex != -1 && mCurrentIndex < mEntries.count() )
232254
{
233-
if ( entries.at( i ).feature.id() == mEntries.at( mCurrentIndex ).feature.id() )
255+
for ( int i = 0; i < newEntriesSize; ++i )
234256
{
235-
mEntries.replace( mCurrentIndex, entries.at( i ) );
236-
currentEntryInNewList = i;
237-
break;
257+
if ( entries.at( i ).feature.id() == mEntries.at( mCurrentIndex ).feature.id() )
258+
{
259+
mEntries.replace( mCurrentIndex, entries.at( i ) );
260+
currentEntryInNewList = i;
261+
break;
262+
}
238263
}
239264
}
240-
}
241265

242-
int firstRow = 0;
266+
int firstRow = 0;
243267

244-
// Move current entry to the first position if this is a fixed entry or because
245-
// the entry exists in the new list
246-
if ( mCurrentIndex > -1 && ( mCurrentIndex < nbFixedEntry || currentEntryInNewList != -1 ) )
247-
{
248-
if ( mCurrentIndex != 0 )
268+
// Move current entry to the first position if this is a fixed entry or because
269+
// the entry exists in the new list
270+
if ( mCurrentIndex > -1 && ( mCurrentIndex < nbFixedEntry || currentEntryInNewList != -1 ) )
249271
{
250-
beginMoveRows( QModelIndex(), mCurrentIndex, mCurrentIndex, QModelIndex(), 0 );
251-
mEntries.move( mCurrentIndex, 0 );
252-
endMoveRows();
272+
if ( mCurrentIndex != 0 )
273+
{
274+
beginMoveRows( QModelIndex(), mCurrentIndex, mCurrentIndex, QModelIndex(), 0 );
275+
mEntries.move( mCurrentIndex, 0 );
276+
endMoveRows();
277+
}
278+
firstRow = 1;
253279
}
254-
firstRow = 1;
255-
}
256-
257-
// Remove all entries (except for extra entry if existent)
258-
beginRemoveRows( QModelIndex(), firstRow, mEntries.size() - firstRow );
259-
mEntries.remove( firstRow, mEntries.size() - firstRow );
260280

261-
// if we remove all rows before endRemoveRows, setExtraIdentifierValuesUnguarded will be called
262-
// and a null value will be added to mEntries, so we block setExtraIdentifierValuesUnguarded call
281+
// Remove all entries (except for extra entry if existent)
282+
beginRemoveRows( QModelIndex(), firstRow, mEntries.size() - firstRow );
283+
mEntries.remove( firstRow, mEntries.size() - firstRow );
263284

264-
endRemoveRows();
285+
// if we remove all rows before endRemoveRows, setExtraIdentifierValuesUnguarded will be called
286+
// and a null value will be added to mEntries, so we block setExtraIdentifierValuesUnguarded call
265287

288+
endRemoveRows();
266289

267-
if ( currentEntryInNewList == -1 )
268-
{
269-
beginInsertRows( QModelIndex(), firstRow, entries.size() + 1 );
270-
mEntries += entries;
271-
endInsertRows();
272290

273-
// if all entries have been cleaned (firstRow == 0)
274-
// and there is a value in entries, prefer this value over NULL
275-
// else chose the first one (the previous one)
276-
setCurrentIndex( firstRow == 0 && mAllowNull && !entries.isEmpty() ? 1 : 0, firstRow == 0 );
277-
}
278-
else
279-
{
280-
if ( currentEntryInNewList != 0 )
291+
if ( currentEntryInNewList == -1 )
281292
{
282-
beginInsertRows( QModelIndex(), 0, currentEntryInNewList - 1 );
283-
mEntries = entries.mid( 0, currentEntryInNewList ) + mEntries;
293+
beginInsertRows( QModelIndex(), firstRow, entries.size() + 1 );
294+
mEntries += entries;
284295
endInsertRows();
296+
297+
// if all entries have been cleaned (firstRow == 0)
298+
// and there is a value in entries, prefer this value over NULL
299+
// else chose the first one (the previous one)
300+
setCurrentIndex( firstRow == 0 && mAllowNull && !entries.isEmpty() ? 1 : 0, firstRow == 0 );
285301
}
286302
else
287303
{
288-
mEntries.replace( 0, entries.at( 0 ) );
289-
}
304+
if ( currentEntryInNewList != 0 )
305+
{
306+
beginInsertRows( QModelIndex(), 0, currentEntryInNewList - 1 );
307+
mEntries = entries.mid( 0, currentEntryInNewList ) + mEntries;
308+
endInsertRows();
309+
}
310+
else
311+
{
312+
mEntries.replace( 0, entries.at( 0 ) );
313+
}
290314

291-
// don't notify for a change if it's a fixed entry
292-
if ( currentEntryInNewList >= nbFixedEntry )
293-
{
294-
emit dataChanged( index( currentEntryInNewList, 0, QModelIndex() ), index( currentEntryInNewList, 0, QModelIndex() ) );
315+
// don't notify for a change if it's a fixed entry
316+
if ( currentEntryInNewList >= nbFixedEntry )
317+
{
318+
emit dataChanged( index( currentEntryInNewList, 0, QModelIndex() ), index( currentEntryInNewList, 0, QModelIndex() ) );
319+
}
320+
321+
beginInsertRows( QModelIndex(), currentEntryInNewList + 1, newEntriesSize - currentEntryInNewList - 1 );
322+
mEntries += entries.mid( currentEntryInNewList + 1 );
323+
endInsertRows();
324+
setCurrentIndex( currentEntryInNewList );
295325
}
296326

297-
beginInsertRows( QModelIndex(), currentEntryInNewList + 1, newEntriesSize - currentEntryInNewList - 1 );
298-
mEntries += entries.mid( currentEntryInNewList + 1 );
299-
endInsertRows();
300-
setCurrentIndex( currentEntryInNewList );
327+
emit filterJobCompleted();
301328
}
302-
303-
emit filterJobCompleted();
304-
305329
emit endUpdate();
306330

307331

@@ -328,20 +352,28 @@ void QgsFeaturePickerModel::scheduledReload()
328352

329353
QgsFeatureRequest request;
330354

331-
QString filterClause;
332-
if ( mFilterValue.isEmpty() && !mFilterExpression.isEmpty() )
333-
filterClause = mFilterExpression;
334-
else if ( mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
335-
filterClause = QStringLiteral( "(%1) ILIKE '%%2%'" ).arg( mDisplayExpression, mFilterValue );
336-
else if ( !mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
337-
filterClause = QStringLiteral( "(%1) AND ((%2) ILIKE '%%3%')" ).arg( mFilterExpression, mDisplayExpression, mFilterValue );
338-
339-
if ( !filterClause.isEmpty() )
340-
request.setFilterExpression( filterClause );
355+
if ( mShouldReloadCurrentFeature != 0 )
356+
{
357+
request.setFilterFid( mShouldReloadCurrentFeature );
358+
}
359+
else
360+
{
361+
QString filterClause;
362+
if ( mFilterValue.isEmpty() && !mFilterExpression.isEmpty() )
363+
filterClause = mFilterExpression;
364+
else if ( mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
365+
filterClause = QStringLiteral( "(%1) ILIKE '%%2%'" ).arg( mDisplayExpression, mFilterValue );
366+
else if ( !mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
367+
filterClause = QStringLiteral( "(%1) AND ((%2) ILIKE '%%3%')" ).arg( mFilterExpression, mDisplayExpression, mFilterValue );
368+
369+
if ( !filterClause.isEmpty() )
370+
request.setFilterExpression( filterClause );
371+
}
341372

342373
request.setLimit( QgsSettings().value( QStringLiteral( "maxEntriesRelationWidget" ), 100, QgsSettings::Gui ).toInt() );
343374

344375
mGatherer = new QgsFeatureExpressionValuesGatherer( mSourceLayer, mDisplayExpression, request );
376+
mGatherer->setData( mShouldReloadCurrentFeature );
345377
connect( mGatherer, &QgsFeatureExpressionValuesGatherer::finished, this, &QgsFeaturePickerModel::updateCompleter );
346378

347379
mGatherer->start();
@@ -390,15 +422,32 @@ void QgsFeaturePickerModel::setCurrentFeatureUnguarded( const QgsFeatureId &feat
390422
}
391423

392424
// Value not found in current entries
393-
if ( mCurrentIndex != index && ( mAllowNull || featureId == 0 ) )
425+
if ( mCurrentIndex != index && ( mAllowNull || featureId != 0 ) )
394426
{
395427
beginInsertRows( QModelIndex(), 0, 0 );
396-
mEntries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry() );
428+
if ( featureId != 0 )
429+
{
430+
mShouldReloadCurrentFeature = featureId;
431+
mReloadTimer.start();
432+
}
433+
else
434+
{
435+
mEntries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry() );
436+
}
397437
endInsertRows();
398438
setCurrentIndex( 0, true );
399439
}
400440
}
401441

442+
void QgsFeaturePickerModel::setExtraValueDoesNotExist( bool extraValueDoesNotExist )
443+
{
444+
if ( mExtraValueDoesNotExist == extraValueDoesNotExist )
445+
return;
446+
447+
mExtraValueDoesNotExist = extraValueDoesNotExist;
448+
emit extraValueDoesNotExistChanged();
449+
}
450+
402451
QgsConditionalStyle QgsFeaturePickerModel::featureStyle( const QgsFeature &feature ) const
403452
{
404453
if ( !mSourceLayer )
@@ -445,7 +494,7 @@ void QgsFeaturePickerModel::setAllowNull( bool allowNull )
445494

446495
QgsFeature QgsFeaturePickerModel::currentFeature() const
447496
{
448-
if ( mCurrentIndex < mEntries.count() )
497+
if ( mCurrentIndex >= 0 && mCurrentIndex < mEntries.count() )
449498
return mEntries.at( mCurrentIndex ).feature;
450499
else
451500
return QgsFeature();

‎src/core/qgsfeaturepickermodel.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ class CORE_EXPORT QgsFeaturePickerModel : public QAbstractItemModel
189189
*/
190190
void filterJobCompleted();
191191

192+
/**
193+
* Flag indicating that the extraIdentifierValue does not exist in the data.
194+
*/
195+
void extraValueDoesNotExistChanged();
196+
192197
/**
193198
* Notification that the model is about to be changed because a job was completed.
194199
*/
@@ -212,6 +217,7 @@ class CORE_EXPORT QgsFeaturePickerModel : public QAbstractItemModel
212217
void setCurrentIndex( int index, bool force = false );
213218
void reload();
214219
void setCurrentFeatureUnguarded( const QgsFeatureId &featureId );
220+
void setExtraValueDoesNotExist( bool extraValueDoesNotExist );
215221
QgsConditionalStyle featureStyle( const QgsFeature &feature ) const;
216222

217223
QgsVectorLayer *mSourceLayer = nullptr;
@@ -224,11 +230,12 @@ class CORE_EXPORT QgsFeaturePickerModel : public QAbstractItemModel
224230
QVector<QgsFeatureExpressionValuesGatherer::Entry> mEntries;
225231
QgsFeatureExpressionValuesGatherer *mGatherer = nullptr;
226232
QTimer mReloadTimer;
233+
QgsFeatureId mShouldReloadCurrentFeature = 0;
234+
bool mExtraValueDoesNotExist = false;
227235
bool mAllowNull = false;
228236

229237
int mCurrentIndex = -1;
230238
bool mIsSettingCurrentFeature = false;
231-
232239
};
233240

234241
#endif // QGSFEATUREPICKERMODEL_H

‎src/gui/qgsfeaturepickerwidget.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void QgsFeaturePickerWidget::setLayer( QgsVectorLayer *sourceLayer )
7575
mModel->setSourceLayer( sourceLayer );
7676
}
7777

78-
void QgsFeaturePickerWidget::setCurrentFeature(QgsFeatureId featureId )
78+
void QgsFeaturePickerWidget::setCurrentFeature( QgsFeatureId featureId )
7979
{
8080
mModel->setCurrentFeature( featureId );
8181
}

0 commit comments

Comments
 (0)
Please sign in to comment.