Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #20673 : Correct mapFromSource method when reordering columns #8794

Merged
merged 1 commit into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/gui/attributetable/qgsattributetablefiltermodel.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"