Skip to content

Commit 6c1b5d2

Browse files
authoredJan 4, 2019
Merge pull request #8794 from troopa81/release-3_4
fixes #20673 : Correct mapFromSource method when reordering columns
2 parents 2df15a6 + 7179909 commit 6c1b5d2

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed
 

‎src/gui/attributetable/qgsattributetablefiltermodel.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,11 @@ void QgsAttributeTableFilterModel::setSelectedOnTop( bool selectedOnTop )
247247
Qt::SortOrder order = sortOrder();
248248

249249
// set default sort values if they are not correctly set
250-
if ( column < 0 )
251-
column = 0;
252-
253-
if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
254-
order = Qt::AscendingOrder;
255-
256-
sort( column, order );
257-
invalidate();
250+
if ( column < 0 || ( order != Qt::AscendingOrder && order != Qt::DescendingOrder ) )
251+
{
252+
sort( 0, Qt::AscendingOrder );
253+
invalidate();
254+
}
258255
}
259256
}
260257

@@ -382,7 +379,6 @@ void QgsAttributeTableFilterModel::selectionChanged()
382379
}
383380
else if ( mSelectedOnTop )
384381
{
385-
sort( sortColumn(), sortOrder() );
386382
invalidate();
387383
}
388384
}
@@ -402,6 +398,14 @@ int QgsAttributeTableFilterModel::mapColumnToSource( int column ) const
402398
return mColumnMapping.at( column );
403399
}
404400

401+
int QgsAttributeTableFilterModel::mapColumnFromSource( int column ) const
402+
{
403+
if ( mColumnMapping.isEmpty() )
404+
return column;
405+
else
406+
return mColumnMapping.indexOf( column );
407+
}
408+
405409
void QgsAttributeTableFilterModel::generateListOfVisibleFeatures()
406410
{
407411
if ( !layer() )
@@ -528,7 +532,8 @@ QModelIndex QgsAttributeTableFilterModel::mapFromSource( const QModelIndex &sour
528532
if ( proxyIndex.column() < 0 )
529533
return QModelIndex();
530534

531-
int col = mapColumnToSource( proxyIndex.column() );
535+
int col = mapColumnFromSource( proxyIndex.column() );
536+
532537
if ( col == -1 )
533538
col = 0;
534539

@@ -544,4 +549,3 @@ Qt::ItemFlags QgsAttributeTableFilterModel::flags( const QModelIndex &index ) co
544549
QModelIndex source_index = mapToSource( index );
545550
return masterModel()->flags( source_index );
546551
}
547-

‎src/gui/attributetable/qgsattributetablefiltermodel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
275275
QgsAttributeTableConfig mConfig;
276276
QVector<int> mColumnMapping;
277277
int mapColumnToSource( int column ) const;
278+
int mapColumnFromSource( int column ) const;
278279

279280
};
280281

‎tests/src/app/testqgsattributetable.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class TestQgsAttributeTable : public QObject
5151
void testNoGeom();
5252
void testSelected();
5353
void testSortByDisplayExpression();
54+
void testOrderColumn();
5455

5556
private:
5657
QgisApp *mQgisApp = nullptr;
@@ -306,6 +307,49 @@ void TestQgsAttributeTable::testRegression15974()
306307
QCOMPARE( dlg->mMainView->filteredFeatureCount(), 3 );
307308
}
308309

310+
void TestQgsAttributeTable::testOrderColumn()
311+
{
312+
std::unique_ptr< QgsVectorLayer> tempLayer( new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:int&field=col2:int" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
313+
QVERIFY( tempLayer->isValid() );
314+
315+
QgsFeature f1( tempLayer->dataProvider()->fields(), 1 );
316+
f1.setAttribute( 0, 1 );
317+
f1.setAttribute( 1, 13 );
318+
f1.setAttribute( 2, 7 );
319+
QVERIFY( tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 ) );
320+
321+
std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( tempLayer.get() ) );
322+
323+
// Issue https://issues.qgis.org/issues/20673
324+
// When we reorder column (last column becomes first column), and we select an entire row
325+
// the currentIndex is no longer the first column, and consequently it breaks edition
326+
327+
QgsAttributeTableConfig config = QgsAttributeTableConfig();
328+
config.update( tempLayer->dataProvider()->fields() );
329+
QVector<QgsAttributeTableConfig::ColumnConfig> columns = config.columns();
330+
331+
// move last column in first position
332+
columns.move( 2, 0 );
333+
config.setColumns( columns );
334+
335+
dlg->mMainView->setAttributeTableConfig( config );
336+
337+
QgsAttributeTableFilterModel *filterModel = static_cast<QgsAttributeTableFilterModel *>( dlg->mMainView->mTableView->model() );
338+
filterModel->sort( 0, Qt::AscendingOrder );
339+
340+
QModelIndex index = filterModel->mapToSource( filterModel->sourceModel()->index( 0, 0 ) );
341+
QCOMPARE( index.row(), 0 );
342+
QCOMPARE( index.column(), 2 );
343+
344+
index = filterModel->mapFromSource( filterModel->sourceModel()->index( 0, 0 ) );
345+
QCOMPARE( index.row(), 0 );
346+
QCOMPARE( index.column(), 1 );
347+
348+
qDebug() << filterModel->mapFromSource( filterModel->sourceModel()->index( 0, 0 ) );
349+
350+
// column 0 is indeed column 2 since we move it
351+
QCOMPARE( filterModel->sortColumn(), 2 );
352+
}
309353

310354
QGSTEST_MAIN( TestQgsAttributeTable )
311355
#include "testqgsattributetable.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.