Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixes #20673 : Correct mapFromSource method when reordering columns
  • Loading branch information
troopa81 committed Jan 4, 2019
1 parent 2df15a6 commit 7179909
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/gui/attributetable/qgsattributetablefiltermodel.cpp
Expand Up @@ -247,14 +247,11 @@ void QgsAttributeTableFilterModel::setSelectedOnTop( bool selectedOnTop )
Qt::SortOrder order = sortOrder();

// set default sort values if they are not correctly set
if ( column < 0 )
column = 0;

if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
order = Qt::AscendingOrder;

sort( column, order );
invalidate();
if ( column < 0 || ( order != Qt::AscendingOrder && order != Qt::DescendingOrder ) )
{
sort( 0, Qt::AscendingOrder );
invalidate();
}
}
}

Expand Down Expand Up @@ -382,7 +379,6 @@ void QgsAttributeTableFilterModel::selectionChanged()
}
else if ( mSelectedOnTop )
{
sort( sortColumn(), sortOrder() );
invalidate();
}
}
Expand All @@ -402,6 +398,14 @@ int QgsAttributeTableFilterModel::mapColumnToSource( int column ) const
return mColumnMapping.at( column );
}

int QgsAttributeTableFilterModel::mapColumnFromSource( int column ) const
{
if ( mColumnMapping.isEmpty() )
return column;
else
return mColumnMapping.indexOf( column );
}

void QgsAttributeTableFilterModel::generateListOfVisibleFeatures()
{
if ( !layer() )
Expand Down Expand Up @@ -528,7 +532,8 @@ QModelIndex QgsAttributeTableFilterModel::mapFromSource( const QModelIndex &sour
if ( proxyIndex.column() < 0 )
return QModelIndex();

int col = mapColumnToSource( proxyIndex.column() );
int col = mapColumnFromSource( proxyIndex.column() );

if ( col == -1 )
col = 0;

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

1 change: 1 addition & 0 deletions src/gui/attributetable/qgsattributetablefiltermodel.h
Expand Up @@ -275,6 +275,7 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
QgsAttributeTableConfig mConfig;
QVector<int> mColumnMapping;
int mapColumnToSource( int column ) const;
int mapColumnFromSource( int column ) const;

};

Expand Down
44 changes: 44 additions & 0 deletions tests/src/app/testqgsattributetable.cpp
Expand Up @@ -51,6 +51,7 @@ class TestQgsAttributeTable : public QObject
void testNoGeom();
void testSelected();
void testSortByDisplayExpression();
void testOrderColumn();

private:
QgisApp *mQgisApp = nullptr;
Expand Down Expand Up @@ -306,6 +307,49 @@ void TestQgsAttributeTable::testRegression15974()
QCOMPARE( dlg->mMainView->filteredFeatureCount(), 3 );
}

void TestQgsAttributeTable::testOrderColumn()
{
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" ) ) );
QVERIFY( tempLayer->isValid() );

QgsFeature f1( tempLayer->dataProvider()->fields(), 1 );
f1.setAttribute( 0, 1 );
f1.setAttribute( 1, 13 );
f1.setAttribute( 2, 7 );
QVERIFY( tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 ) );

std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( tempLayer.get() ) );

// Issue https://issues.qgis.org/issues/20673
// When we reorder column (last column becomes first column), and we select an entire row
// the currentIndex is no longer the first column, and consequently it breaks edition

QgsAttributeTableConfig config = QgsAttributeTableConfig();
config.update( tempLayer->dataProvider()->fields() );
QVector<QgsAttributeTableConfig::ColumnConfig> columns = config.columns();

// move last column in first position
columns.move( 2, 0 );
config.setColumns( columns );

dlg->mMainView->setAttributeTableConfig( config );

QgsAttributeTableFilterModel *filterModel = static_cast<QgsAttributeTableFilterModel *>( dlg->mMainView->mTableView->model() );
filterModel->sort( 0, Qt::AscendingOrder );

QModelIndex index = filterModel->mapToSource( filterModel->sourceModel()->index( 0, 0 ) );
QCOMPARE( index.row(), 0 );
QCOMPARE( index.column(), 2 );

index = filterModel->mapFromSource( filterModel->sourceModel()->index( 0, 0 ) );
QCOMPARE( index.row(), 0 );
QCOMPARE( index.column(), 1 );

qDebug() << filterModel->mapFromSource( filterModel->sourceModel()->index( 0, 0 ) );

// column 0 is indeed column 2 since we move it
QCOMPARE( filterModel->sortColumn(), 2 );
}

QGSTEST_MAIN( TestQgsAttributeTable )
#include "testqgsattributetable.moc"

0 comments on commit 7179909

Please sign in to comment.