Skip to content

Commit

Permalink
Fix copy/paste using text format between projects
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent authored and nyalldawson committed Aug 14, 2018
1 parent 430649a commit ed34931
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
56 changes: 48 additions & 8 deletions src/app/qgsclipboard.cpp
Expand Up @@ -185,6 +185,8 @@ QgsFeatureList QgsClipboard::stringToFeatureList( const QString& string, const Q
if ( values.isEmpty() || string.isEmpty() )
return features;

QgsFields sourceFields = retrieveFields();

Q_FOREACH ( const QString& row, values )
{
// Assume that it's just WKT for now. because GeoJSON is managed by
Expand All @@ -197,15 +199,25 @@ QgsFeatureList QgsClipboard::stringToFeatureList( const QString& string, const Q
if ( fieldValues.isEmpty() )
continue;

QgsGeometry *geometry = QgsGeometry::fromWkt( fieldValues[0] );
if ( !geometry )
continue;

QgsFeature feature;
if ( !fields.isEmpty() )
feature.setFields( fields, true );
feature.setFields( sourceFields );
feature.initAttributes( fieldValues.size() - 1 );

//skip header line
if ( fieldValues.at( 0 ) == QLatin1String( "wkt_geom" ) )
{
continue;
}

feature.setGeometry( geometry );
for ( int i = 1; i < fieldValues.size(); ++i )
{
feature.setAttribute( i - 1, fieldValues.at( i ) );
}
QgsGeometry* geometry = QgsGeometry::fromWkt( fieldValues[0] );
if ( geometry )
{
feature.setGeometry( geometry );
}
features.append( feature );
}

Expand All @@ -222,7 +234,35 @@ QgsFields QgsClipboard::retrieveFields() const
QString string = cb->text( QClipboard::Clipboard );
#endif

return QgsOgrUtils::stringToFields( string, QTextCodec::codecForName( "System" ) );
QgsFields f = QgsOgrUtils::stringToFields( string, QTextCodec::codecForName( "System" ) );
if ( f.size() < 1 )
{
if ( string.isEmpty() )
{
return f;
}
//wkt?
QStringList lines = string.split( '\n' );
if ( !lines.empty() )
{
QStringList fieldNames = lines.at( 0 ).split( '\t' );
//wkt / text always has wkt_geom as first attribute (however values can be NULL)
if ( fieldNames.at( 0 ) != QLatin1String( "wkt_geom" ) )
{
return f;
}
for ( int i = 0; i < fieldNames.size(); ++i )
{
QString fieldName = fieldNames.at( i );
if ( fieldName == QLatin1String( "wkt_geom" ) )
{
continue;
}
f.append( QgsField( fieldName, QVariant::String ) );
}
}
}
return f;
}

QgsFeatureList QgsClipboard::copyOf( const QgsFields &fields ) const
Expand Down
4 changes: 2 additions & 2 deletions tests/src/app/testqgisappclipboard.cpp
Expand Up @@ -236,10 +236,10 @@ void TestQgisAppClipboard::pasteWkt()
QCOMPARE( point->x(), 125.0 );
QCOMPARE( point->y(), 10.0 );

// only fields => no geom so no feature list is returned
// clipboard now supports features without geometry
mQgisApp->clipboard()->setText( "MNL\t11\t282\tkm\t\nMNL\t11\t347.80000000000001\tkm\t" );
features = mQgisApp->clipboard()->copyOf();
QCOMPARE( features.length(), 0 );
QCOMPARE( features.length(), 2 );
}

void TestQgisAppClipboard::pasteGeoJson()
Expand Down

0 comments on commit ed34931

Please sign in to comment.