@@ -185,6 +185,8 @@ QgsFeatureList QgsClipboard::stringToFeatureList( const QString& string, const Q
185
185
if ( values.isEmpty () || string.isEmpty () )
186
186
return features;
187
187
188
+ QgsFields sourceFields = retrieveFields ();
189
+
188
190
Q_FOREACH ( const QString& row, values )
189
191
{
190
192
// 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
197
199
if ( fieldValues.isEmpty () )
198
200
continue ;
199
201
200
- QgsGeometry *geometry = QgsGeometry::fromWkt ( fieldValues[0 ] );
201
- if ( !geometry )
202
- continue ;
203
-
204
202
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
+ }
207
211
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
+ }
209
221
features.append ( feature );
210
222
}
211
223
@@ -222,7 +234,35 @@ QgsFields QgsClipboard::retrieveFields() const
222
234
QString string = cb->text ( QClipboard::Clipboard );
223
235
#endif
224
236
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;
226
266
}
227
267
228
268
QgsFeatureList QgsClipboard::copyOf ( const QgsFields &fields ) const
0 commit comments