Skip to content
Permalink
Browse files
Merge pull request #4530 from boundlessgeo/attributetable-duplicated-…
…rows-fix

Attributetable duplicated rows fix for 2.18
  • Loading branch information
elpaso committed May 10, 2017
2 parents d139119 + abf6acb commit 09ef9477db5b68c74ab0c8527533f9b594578399
@@ -268,6 +268,8 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
QVector<int> mColumnMapping;
int mapColumnToSource( int column ) const;

friend class TestQgsAttributeTable;

};

#endif
@@ -221,15 +221,16 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
mSortCache.insert( mFeat.id(), sortValue );
}

int n = mRowIdMap.size();
beginInsertRows( QModelIndex(), n, n );

mIdRowMap.insert( fid, n );
mRowIdMap.insert( n, fid );

endInsertRows();

reload( index( rowCount() - 1, 0 ), index( rowCount() - 1, columnCount() ) );
// Skip if the fid is already in the map (do not add twice)!
if ( ! mIdRowMap.contains( fid ) )
{
int n = mRowIdMap.size();
beginInsertRows( QModelIndex(), n, n );
mIdRowMap.insert( fid, n );
mRowIdMap.insert( n, fid );
endInsertRows();
reload( index( rowCount() - 1, 0 ), index( rowCount() - 1, columnCount() ) );
}
}
}

@@ -350,6 +350,7 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
QgsMapCanvas *mMapCanvas;

friend class TestQgsDualView;
friend class TestQgsAttributeTable;
};

/** \ingroup gui
@@ -253,18 +253,22 @@ void QgsLayerTreeView::updateExpandedStateFromNode( QgsLayerTreeNode* node )

QgsMapLayer* QgsLayerTreeView::layerForIndex( const QModelIndex& index ) const
{
QgsLayerTreeNode* node = layerTreeModel()->index2node( index );
if ( node )
// Check if model has been set and index is valid
if ( layerTreeModel() && index.isValid( ) )
{
if ( QgsLayerTree::isLayer( node ) )
return QgsLayerTree::toLayer( node )->layer();
}
else
{
// possibly a legend node
QgsLayerTreeModelLegendNode* legendNode = layerTreeModel()->index2legendNode( index );
if ( legendNode )
return legendNode->layerNode()->layer();
QgsLayerTreeNode* node = layerTreeModel()->index2node( index );
if ( node )
{
if ( QgsLayerTree::isLayer( node ) )
return QgsLayerTree::toLayer( node )->layer();
}
else
{
// possibly a legend node
QgsLayerTreeModelLegendNode* legendNode = layerTreeModel()->index2legendNode( index );
if ( legendNode )
return legendNode->layerNode()->layer();
}
}

return nullptr;
@@ -23,6 +23,7 @@
#include "qgsproject.h"
#include "qgsmapcanvas.h"
#include "qgsunittypes.h"
#include "qgsvectorfilewriter.h"

/** \ingroup UnitTests
* This is a unit test for the attribute table dialog
@@ -38,6 +39,7 @@ class TestQgsAttributeTable : public QObject
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {} // will be called before each testfunction is executed.
void cleanup() {} // will be called after every testfunction.
void testRegression15974();
void testFieldCalculation();
void testFieldCalculationArea();
void testNoGeom();
@@ -207,5 +209,41 @@ void TestQgsAttributeTable::testNoGeom()

}


void TestQgsAttributeTable::testRegression15974()
{
QString path = QDir::tempPath() + "/testshp15974.shp";
std::unique_ptr< QgsVectorLayer> tempLayer( new QgsVectorLayer( "polygon?crs=epsg:4326&field=id:integer" , "vl" , "memory" ) );
QVERIFY( tempLayer->isValid() );
QgsCoordinateReferenceSystem crs( 4326 );
QgsVectorFileWriter::writeAsVectorFormat( tempLayer.get( ), path, "system", &crs );
std::unique_ptr< QgsVectorLayer> shpLayer( new QgsVectorLayer( path, "test" , "ogr" ) );
QgsFeature f1( shpLayer->dataProvider()->fields(), 1 );
QgsGeometry* geom1;
geom1 = QgsGeometry().fromWkt( "polygon((0 0, 0 1, 1 1, 1 0, 0 0))" );
QVERIFY( geom1->isGeosValid( ) );
f1.setGeometry( geom1 );
QgsFeature f2( shpLayer->dataProvider()->fields(), 2 );
QgsGeometry* geom2;
geom2 = QgsGeometry().fromWkt( "polygon((0 0, 0 1, 1 1, 1 0, 0 0))" );
f2.setGeometry( geom2 );
QgsFeature f3( shpLayer->dataProvider()->fields(), 3 );
QgsGeometry* geom3;
geom3 = QgsGeometry().fromWkt( "polygon((0 0, 0 1, 1 1, 1 0, 0 0))" );
f3.setGeometry( geom3 );
QVERIFY( shpLayer->startEditing( ) );
QVERIFY( shpLayer->addFeatures( QgsFeatureList() << f1 << f2 << f3 ) );
std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( shpLayer.get() ) );
QCOMPARE( shpLayer->featureCount( ), 3L );
mQgisApp->saveEdits( shpLayer.get( ) );
QCOMPARE( shpLayer->featureCount( ), 3L );
QCOMPARE( dlg->mMainView->masterModel()->rowCount(), 3 );
QCOMPARE( dlg->mMainView->mLayerCache->cachedFeatureIds( ).count(), 3 );
QCOMPARE( dlg->mMainView->featureCount( ), 3 );
// The following passes locally but fails on Travis
// QCOMPARE( dlg->mMainView->mFilterModel->mFilteredFeatures.count(), 3 );
}


QTEST_MAIN( TestQgsAttributeTable )
#include "testqgsattributetable.moc"

0 comments on commit 09ef947

Please sign in to comment.