@@ -30,12 +30,13 @@ QgsFeatureListModel::QgsFeatureListModel( QgsAttributeTableFilterModel *sourceMo
30
30
31
31
void QgsFeatureListModel::setSourceModel ( QgsAttributeTableFilterModel *sourceModel )
32
32
{
33
- QAbstractProxyModel ::setSourceModel ( sourceModel );
33
+ QSortFilterProxyModel ::setSourceModel ( sourceModel );
34
34
mExpressionContext = sourceModel->layer ()->createExpressionContext ();
35
35
mFilterModel = sourceModel;
36
36
37
37
if ( mFilterModel )
38
38
{
39
+ #if 0
39
40
// rewire (filter-)change events in the source model so this proxy reflects the changes
40
41
connect( mFilterModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &QgsFeatureListModel::onBeginRemoveRows );
41
42
connect( mFilterModel, &QAbstractItemModel::rowsRemoved, this, &QgsFeatureListModel::onEndRemoveRows );
@@ -46,6 +47,7 @@ void QgsFeatureListModel::setSourceModel( QgsAttributeTableFilterModel *sourceMo
46
47
connect( mFilterModel, &QAbstractItemModel::layoutChanged, this, &QAbstractItemModel::layoutChanged );
47
48
connect( mFilterModel, &QAbstractItemModel::modelAboutToBeReset, this, &QAbstractItemModel::modelAboutToBeReset );
48
49
connect( mFilterModel, &QAbstractItemModel::modelReset, this, &QAbstractItemModel::modelReset );
50
+ #endif
49
51
}
50
52
}
51
53
@@ -61,7 +63,7 @@ QgsFeatureId QgsFeatureListModel::idxToFid( const QModelIndex &index ) const
61
63
62
64
QModelIndex QgsFeatureListModel::fidToIdx ( const QgsFeatureId fid ) const
63
65
{
64
- return mFilterModel -> mapFromMaster ( mFilterModel ->masterModel ()->idToIndex ( fid ) );
66
+ return mapFromMaster ( mFilterModel ->masterModel ()->idToIndex ( fid ) );
65
67
}
66
68
67
69
QVariant QgsFeatureListModel::data ( const QModelIndex &index, int role ) const
@@ -192,12 +194,15 @@ Qt::ItemFlags QgsFeatureListModel::flags( const QModelIndex &index ) const
192
194
193
195
void QgsFeatureListModel::setInjectNull ( bool injectNull )
194
196
{
195
- if ( mInjectNull != injectNull )
196
- {
197
- beginResetModel ();
198
- mInjectNull = injectNull;
199
- endResetModel ();
200
- }
197
+ if ( mInjectNull == injectNull )
198
+ return ;
199
+
200
+ if ( injectNull )
201
+ setSortByDisplayExpression ( false );
202
+
203
+ beginResetModel ();
204
+ mInjectNull = injectNull;
205
+ endResetModel ();
201
206
}
202
207
203
208
bool QgsFeatureListModel::injectNull ()
@@ -224,7 +229,12 @@ bool QgsFeatureListModel::setDisplayExpression( const QString &expression )
224
229
225
230
mDisplayExpression = exp;
226
231
232
+ if ( mSortByDisplayExpression )
233
+ masterModel ()->prefetchSortData ( expression, 1 );
234
+
227
235
emit dataChanged ( index ( 0 , 0 ), index ( rowCount () - 1 , 0 ) );
236
+
237
+ invalidate ();
228
238
return true ;
229
239
}
230
240
@@ -269,24 +279,63 @@ void QgsFeatureListModel::onEndInsertRows( const QModelIndex &parent, int first,
269
279
endInsertRows ();
270
280
}
271
281
282
+ bool QgsFeatureListModel::sortByDisplayExpression () const
283
+ {
284
+ return mSortByDisplayExpression ;
285
+ }
286
+
287
+ void QgsFeatureListModel::setSortByDisplayExpression ( bool sortByDisplayExpression )
288
+ {
289
+ mSortByDisplayExpression = sortByDisplayExpression;
290
+
291
+ // If we are sorting by display expression, we do not support injected null
292
+ if ( sortByDisplayExpression )
293
+ setInjectNull ( false );
294
+
295
+ setSortRole ( QgsAttributeTableModel::SortRole + 1 );
296
+ setDynamicSortFilter ( mSortByDisplayExpression );
297
+ sort ( 0 );
298
+ }
299
+
272
300
QModelIndex QgsFeatureListModel::mapToMaster ( const QModelIndex &proxyIndex ) const
273
301
{
274
- if ( !proxyIndex.isValid () )
275
- return QModelIndex ();
302
+ QModelIndex masterIndex;
276
303
277
- int offset = mInjectNull ? 1 : 0 ;
304
+ if ( proxyIndex.isValid () )
305
+ {
306
+ if ( mSortByDisplayExpression )
307
+ {
308
+ masterIndex = mFilterModel ->mapToMaster ( mapToSource ( proxyIndex ) );
309
+ }
310
+ else
311
+ {
312
+ int offset = mInjectNull ? 1 : 0 ;
278
313
279
- return mFilterModel ->mapToMaster ( mFilterModel ->index ( proxyIndex.row () - offset, proxyIndex.column () ) );
314
+ masterIndex = mFilterModel ->mapToMaster ( mFilterModel ->index ( proxyIndex.row () - offset, proxyIndex.column () ) );
315
+ }
316
+ }
317
+ return masterIndex;
280
318
}
281
319
282
- QModelIndex QgsFeatureListModel::mapFromMaster ( const QModelIndex &sourceIndex ) const
320
+ QModelIndex QgsFeatureListModel::mapFromMaster ( const QModelIndex &masterIndex ) const
283
321
{
284
- if ( !sourceIndex.isValid () )
285
- return QModelIndex ();
322
+ QModelIndex proxyIndex;
286
323
287
- int offset = mInjectNull ? 1 : 0 ;
324
+ if ( masterIndex.isValid () )
325
+ {
326
+ if ( mSortByDisplayExpression )
327
+ {
328
+ proxyIndex = mapFromSource ( mFilterModel ->mapFromMaster ( masterIndex ) );
329
+ }
330
+ else
331
+ {
332
+ int offset = mInjectNull ? 1 : 0 ;
333
+
334
+ return createIndex ( mFilterModel ->mapFromMaster ( masterIndex ).row () + offset, 0 );
335
+ }
336
+ }
288
337
289
- return createIndex ( mFilterModel -> mapFromMaster ( sourceIndex ). row () + offset, 0 ) ;
338
+ return proxyIndex ;
290
339
}
291
340
292
341
QItemSelection QgsFeatureListModel::mapSelectionFromMaster ( const QItemSelection &selection ) const
@@ -303,27 +352,40 @@ QItemSelection QgsFeatureListModel::mapSelectionToMaster( const QItemSelection &
303
352
304
353
QModelIndex QgsFeatureListModel::mapToSource ( const QModelIndex &proxyIndex ) const
305
354
{
306
- if ( !proxyIndex.isValid () )
307
- return QModelIndex ();
355
+ QModelIndex sourceIndex;
308
356
309
- int offset = mInjectNull ? 1 : 0 ;
357
+ if ( mSortByDisplayExpression )
358
+ {
359
+ sourceIndex = QSortFilterProxyModel::mapToSource ( proxyIndex );
360
+ }
361
+ else
362
+ {
363
+ if ( !proxyIndex.isValid () )
364
+ return QModelIndex ();
310
365
311
- return sourceModel ()->index ( proxyIndex.row () - offset, proxyIndex.column () );
312
- }
366
+ int offset = mInjectNull ? 1 : 0 ;
313
367
314
- QModelIndex QgsFeatureListModel::mapFromSource ( const QModelIndex &sourceIndex ) const
315
- {
316
- if ( !sourceIndex.isValid () )
317
- return QModelIndex ();
368
+ sourceIndex = sourceModel ()->index ( proxyIndex.row () - offset, proxyIndex.column () );
369
+ }
318
370
319
- return createIndex ( sourceIndex. row (), 0 ) ;
371
+ return sourceIndex;
320
372
}
321
373
322
- QModelIndex QgsFeatureListModel::index ( int row, int column, const QModelIndex &parent ) const
374
+ QModelIndex QgsFeatureListModel::mapFromSource ( const QModelIndex &sourceIndex ) const
323
375
{
324
- Q_UNUSED ( parent )
376
+ QModelIndex proxyIndex;
377
+
378
+ if ( mSortByDisplayExpression )
379
+ {
380
+ proxyIndex = QSortFilterProxyModel::mapFromSource ( sourceIndex );
381
+ }
382
+ else
383
+ {
384
+ if ( sourceIndex.isValid () )
385
+ proxyIndex = createIndex ( sourceIndex.row (), 0 );
386
+ }
325
387
326
- return createIndex ( row, column ) ;
388
+ return proxyIndex ;
327
389
}
328
390
329
391
QModelIndex QgsFeatureListModel::parent ( const QModelIndex &child ) const
0 commit comments