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

[Backport queued_ltr_backports] Copy CRS to clipboard when copying selected features #43532

Merged
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
2 changes: 2 additions & 0 deletions src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,8 @@ void QgsAttributeTableDialog::mActionCopySelectedRows_triggered()
featureStore.addFeature( featureMap[id] );
}

featureStore.setCrs( mLayer->crs() );

QgisApp::instance()->clipboard()->replaceWithCopyOf( featureStore );
}
else
Expand Down
41 changes: 41 additions & 0 deletions tests/src/app/testqgsattributetable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "qgssettings.h"
#include "qgsvectorfilewriter.h"
#include "qgsfeaturelistmodel.h"
#include "qgsclipboard.h"

#include "qgstest.h"

Expand Down Expand Up @@ -56,6 +57,8 @@ class TestQgsAttributeTable : public QObject
void testOrderColumn();
void testFilteredFeatures();
void testVisibleTemporal();
void testCopySelectedRows();


private:
QgisApp *mQgisApp = nullptr;
Expand Down Expand Up @@ -532,5 +535,43 @@ void TestQgsAttributeTable::testFilteredFeatures()
QCOMPARE( dlg->mMainView->filteredFeatureCount(), 3 );
}

void TestQgsAttributeTable::testCopySelectedRows()
{
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, 2 );

QgsFeature f2( tempLayer->dataProvider()->fields(), 2 );
f2.setAttribute( 0, 2 );
f2.setAttribute( 1, 4 );

QVERIFY( tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 ) );

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

tempLayer->selectByIds( QgsFeatureIds() << 1 << 2 );

dlg->mActionCopySelectedRows_triggered();

QgsClipboard *clipboard = QgisApp::instance()->clipboard();
QVERIFY( clipboard );
QVERIFY( !clipboard->isEmpty() );
QCOMPARE( clipboard->fields().names(), QStringList() << "pk" << "col1" << "col2" );

QgsFeatureList features = clipboard->copyOf();
QCOMPARE( features.count(), 2 );
QCOMPARE( features.at( 0 ).attribute( 0 ), 1 );
QCOMPARE( features.at( 0 ).attribute( "col1" ), 2 );
QCOMPARE( features.at( 0 ).attribute( "col2" ), QVariant() );
QCOMPARE( features.at( 1 ).attribute( "pk" ), 2 );
QCOMPARE( features.at( 1 ).attribute( "col1" ), 4 );
QCOMPARE( features.at( 1 ).attribute( 2 ), QVariant() );

QCOMPARE( clipboard->crs().authid(), "EPSG:3111" );
}

QGSTEST_MAIN( TestQgsAttributeTable )
#include "testqgsattributetable.moc"