@@ -211,97 +211,121 @@ void QgsFeaturePickerModel::updateCompleter()
211
211
{
212
212
setCurrentFeatureUnguarded ( FID_NULL );
213
213
}
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 ; } );
216
214
217
- if ( mAllowNull )
215
+ // Only reloading the current entry?
216
+ bool reloadCurrentFeatureOnly = mGatherer ->data ().toBool ();
217
+ if ( reloadCurrentFeatureOnly )
218
218
{
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 ();
220
235
}
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 ; } );
221
240
222
- const int newEntriesSize = entries.size ();
241
+ if ( mAllowNull )
242
+ {
243
+ entries.prepend ( QgsFeatureExpressionValuesGatherer::nullEntry () );
244
+ }
223
245
224
- // fixed entry is either NULL or extra value
225
- const int nbFixedEntry = mAllowNull ? 1 : 0 ;
246
+ const int newEntriesSize = entries.size ();
226
247
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 () )
232
254
{
233
- if ( entries. at ( i ). feature . id () == mEntries . at ( mCurrentIndex ). feature . id () )
255
+ for ( int i = 0 ; i < newEntriesSize; ++i )
234
256
{
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
+ }
238
263
}
239
264
}
240
- }
241
265
242
- int firstRow = 0 ;
266
+ int firstRow = 0 ;
243
267
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 ) )
249
271
{
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 ;
253
279
}
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 );
260
280
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 );
263
284
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
265
287
288
+ endRemoveRows ();
266
289
267
- if ( currentEntryInNewList == -1 )
268
- {
269
- beginInsertRows ( QModelIndex (), firstRow, entries.size () + 1 );
270
- mEntries += entries;
271
- endInsertRows ();
272
290
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 )
281
292
{
282
- beginInsertRows ( QModelIndex (), 0 , currentEntryInNewList - 1 );
283
- mEntries = entries. mid ( 0 , currentEntryInNewList ) + mEntries ;
293
+ beginInsertRows ( QModelIndex (), firstRow, entries. size () + 1 );
294
+ mEntries + = entries;
284
295
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 );
285
301
}
286
302
else
287
303
{
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
+ }
290
314
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 );
295
325
}
296
326
297
- beginInsertRows ( QModelIndex (), currentEntryInNewList + 1 , newEntriesSize - currentEntryInNewList - 1 );
298
- mEntries += entries.mid ( currentEntryInNewList + 1 );
299
- endInsertRows ();
300
- setCurrentIndex ( currentEntryInNewList );
327
+ emit filterJobCompleted ();
301
328
}
302
-
303
- emit filterJobCompleted ();
304
-
305
329
emit endUpdate ();
306
330
307
331
@@ -328,20 +352,28 @@ void QgsFeaturePickerModel::scheduledReload()
328
352
329
353
QgsFeatureRequest request;
330
354
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
+ }
341
372
342
373
request.setLimit ( QgsSettings ().value ( QStringLiteral ( " maxEntriesRelationWidget" ), 100 , QgsSettings::Gui ).toInt () );
343
374
344
375
mGatherer = new QgsFeatureExpressionValuesGatherer ( mSourceLayer , mDisplayExpression , request );
376
+ mGatherer ->setData ( mShouldReloadCurrentFeature );
345
377
connect ( mGatherer , &QgsFeatureExpressionValuesGatherer::finished, this , &QgsFeaturePickerModel::updateCompleter );
346
378
347
379
mGatherer ->start ();
@@ -390,15 +422,32 @@ void QgsFeaturePickerModel::setCurrentFeatureUnguarded( const QgsFeatureId &feat
390
422
}
391
423
392
424
// Value not found in current entries
393
- if ( mCurrentIndex != index && ( mAllowNull || featureId = = 0 ) )
425
+ if ( mCurrentIndex != index && ( mAllowNull || featureId ! = 0 ) )
394
426
{
395
427
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
+ }
397
437
endInsertRows ();
398
438
setCurrentIndex ( 0 , true );
399
439
}
400
440
}
401
441
442
+ void QgsFeaturePickerModel::setExtraValueDoesNotExist ( bool extraValueDoesNotExist )
443
+ {
444
+ if ( mExtraValueDoesNotExist == extraValueDoesNotExist )
445
+ return ;
446
+
447
+ mExtraValueDoesNotExist = extraValueDoesNotExist;
448
+ emit extraValueDoesNotExistChanged ();
449
+ }
450
+
402
451
QgsConditionalStyle QgsFeaturePickerModel::featureStyle ( const QgsFeature &feature ) const
403
452
{
404
453
if ( !mSourceLayer )
@@ -445,7 +494,7 @@ void QgsFeaturePickerModel::setAllowNull( bool allowNull )
445
494
446
495
QgsFeature QgsFeaturePickerModel::currentFeature () const
447
496
{
448
- if ( mCurrentIndex < mEntries .count () )
497
+ if ( mCurrentIndex >= 0 && mCurrentIndex < mEntries .count () )
449
498
return mEntries .at ( mCurrentIndex ).feature ;
450
499
else
451
500
return QgsFeature ();
0 commit comments