Skip to content

Commit ed34931

Browse files
mhugentnyalldawson
authored andcommitted
Fix copy/paste using text format between projects
1 parent 430649a commit ed34931

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/app/qgsclipboard.cpp

+48-8
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ QgsFeatureList QgsClipboard::stringToFeatureList( const QString& string, const Q
185185
if ( values.isEmpty() || string.isEmpty() )
186186
return features;
187187

188+
QgsFields sourceFields = retrieveFields();
189+
188190
Q_FOREACH ( const QString& row, values )
189191
{
190192
// Assume that it's just WKT for now. because GeoJSON is managed by
@@ -197,15 +199,25 @@ QgsFeatureList QgsClipboard::stringToFeatureList( const QString& string, const Q
197199
if ( fieldValues.isEmpty() )
198200
continue;
199201

200-
QgsGeometry *geometry = QgsGeometry::fromWkt( fieldValues[0] );
201-
if ( !geometry )
202-
continue;
203-
204202
QgsFeature feature;
205-
if ( !fields.isEmpty() )
206-
feature.setFields( fields, true );
203+
feature.setFields( sourceFields );
204+
feature.initAttributes( fieldValues.size() - 1 );
205+
206+
//skip header line
207+
if ( fieldValues.at( 0 ) == QLatin1String( "wkt_geom" ) )
208+
{
209+
continue;
210+
}
207211

208-
feature.setGeometry( geometry );
212+
for ( int i = 1; i < fieldValues.size(); ++i )
213+
{
214+
feature.setAttribute( i - 1, fieldValues.at( i ) );
215+
}
216+
QgsGeometry* geometry = QgsGeometry::fromWkt( fieldValues[0] );
217+
if ( geometry )
218+
{
219+
feature.setGeometry( geometry );
220+
}
209221
features.append( feature );
210222
}
211223

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

225-
return QgsOgrUtils::stringToFields( string, QTextCodec::codecForName( "System" ) );
237+
QgsFields f = QgsOgrUtils::stringToFields( string, QTextCodec::codecForName( "System" ) );
238+
if ( f.size() < 1 )
239+
{
240+
if ( string.isEmpty() )
241+
{
242+
return f;
243+
}
244+
//wkt?
245+
QStringList lines = string.split( '\n' );
246+
if ( !lines.empty() )
247+
{
248+
QStringList fieldNames = lines.at( 0 ).split( '\t' );
249+
//wkt / text always has wkt_geom as first attribute (however values can be NULL)
250+
if ( fieldNames.at( 0 ) != QLatin1String( "wkt_geom" ) )
251+
{
252+
return f;
253+
}
254+
for ( int i = 0; i < fieldNames.size(); ++i )
255+
{
256+
QString fieldName = fieldNames.at( i );
257+
if ( fieldName == QLatin1String( "wkt_geom" ) )
258+
{
259+
continue;
260+
}
261+
f.append( QgsField( fieldName, QVariant::String ) );
262+
}
263+
}
264+
}
265+
return f;
226266
}
227267

228268
QgsFeatureList QgsClipboard::copyOf( const QgsFields &fields ) const

tests/src/app/testqgisappclipboard.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ void TestQgisAppClipboard::pasteWkt()
236236
QCOMPARE( point->x(), 125.0 );
237237
QCOMPARE( point->y(), 10.0 );
238238

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

245245
void TestQgisAppClipboard::pasteGeoJson()

0 commit comments

Comments
 (0)