-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsclipboard.cpp
323 lines (269 loc) · 9 KB
/
qgsclipboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/***************************************************************************
qgsclipboard.cpp - QGIS internal clipboard for storage of features
--------------------------------------------------------------------
begin : 20 May, 2005
copyright : (C) 2005 by Brendan Morley
email : morb at ozemail dot com dot au
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <fstream>
#include <QApplication>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QClipboard>
#include <QMimeData>
#include <QTextCodec>
#include "qgsclipboard.h"
#include "qgsfeature.h"
#include "qgsfeaturestore.h"
#include "qgsfields.h"
#include "qgsgeometry.h"
#include "qgscoordinatereferencesystem.h"
#include "qgslogger.h"
#include "qgsvectorlayer.h"
#include "qgsogrutils.h"
#include "qgsjsonutils.h"
#include "qgssettings.h"
QgsClipboard::QgsClipboard()
: QObject()
, mFeatureClipboard()
, mFeatureFields()
, mUseSystemClipboard( false )
{
connect( QApplication::clipboard(), SIGNAL( dataChanged() ), this, SLOT( systemClipboardChanged() ) );
}
QgsClipboard::~QgsClipboard()
{
}
void QgsClipboard::replaceWithCopyOf( QgsVectorLayer *src )
{
if ( !src )
return;
// Replace the QGis clipboard.
mFeatureFields = src->fields();
mFeatureClipboard = src->selectedFeatures();
mCRS = src->crs();
QgsDebugMsg( "replaced QGis clipboard." );
setSystemClipboard();
mUseSystemClipboard = false;
emit changed();
}
void QgsClipboard::replaceWithCopyOf( QgsFeatureStore &featureStore )
{
QgsDebugMsg( QString( "features count = %1" ).arg( featureStore.features().size() ) );
mFeatureFields = featureStore.fields();
mFeatureClipboard = featureStore.features();
mCRS = featureStore.crs();
setSystemClipboard();
mUseSystemClipboard = false;
emit changed();
}
QString QgsClipboard::generateClipboardText() const
{
QgsSettings settings;
CopyFormat format = AttributesWithWKT;
if ( settings.contains( QStringLiteral( "/qgis/copyFeatureFormat" ) ) )
format = static_cast< CopyFormat >( settings.value( QStringLiteral( "qgis/copyFeatureFormat" ), true ).toInt() );
else
{
//old format setting
format = settings.value( QStringLiteral( "qgis/copyGeometryAsWKT" ), true ).toBool() ? AttributesWithWKT : AttributesOnly;
}
switch ( format )
{
case AttributesOnly:
case AttributesWithWKT:
{
QStringList textLines;
QStringList textFields;
// first do the field names
if ( format == AttributesWithWKT )
{
textFields += QStringLiteral( "wkt_geom" );
}
Q_FOREACH ( const QgsField &field, mFeatureFields )
{
textFields += field.name();
}
textLines += textFields.join( QStringLiteral( "\t" ) );
textFields.clear();
// then the field contents
for ( QgsFeatureList::const_iterator it = mFeatureClipboard.constBegin(); it != mFeatureClipboard.constEnd(); ++it )
{
QgsAttributes attributes = it->attributes();
// TODO: Set up Paste Transformations to specify the order in which fields are added.
if ( format == AttributesWithWKT )
{
if ( it->hasGeometry() )
textFields += it->geometry().exportToWkt();
else
{
textFields += QgsApplication::nullRepresentation();
}
}
// QgsDebugMsg("about to traverse fields.");
for ( int idx = 0; idx < attributes.count(); ++idx )
{
// QgsDebugMsg(QString("inspecting field '%1'.").arg(it2->toString()));
textFields += attributes.at( idx ).toString();
}
textLines += textFields.join( QStringLiteral( "\t" ) );
textFields.clear();
}
return textLines.join( QStringLiteral( "\n" ) );
}
case GeoJSON:
{
QgsJSONExporter exporter;
exporter.setSourceCrs( mCRS );
return exporter.exportFeatures( mFeatureClipboard );
}
}
return QString();
}
void QgsClipboard::setSystemClipboard()
{
QString textCopy = generateClipboardText();
QClipboard *cb = QApplication::clipboard();
// Copy text into the clipboard
// With qgis running under Linux, but with a Windows based X
// server (Xwin32), ::Selection was necessary to get the data into
// the Windows clipboard (which seems contrary to the Qt
// docs). With a Linux X server, ::Clipboard was required.
// The simple solution was to put the text into both clipboards.
#ifdef Q_OS_LINUX
cb->setText( textCopy, QClipboard::Selection );
#endif
cb->setText( textCopy, QClipboard::Clipboard );
QgsDebugMsgLevel( QString( "replaced system clipboard with: %1." ).arg( textCopy ), 4 );
}
QgsFeatureList QgsClipboard::stringToFeatureList( const QString &string, const QgsFields &fields ) const
{
//first try using OGR to read string
QgsFeatureList features = QgsOgrUtils::stringToFeatureList( string, fields, QTextCodec::codecForName( "System" ) );
if ( !features.isEmpty() )
return features;
// otherwise try to read in as WKT
QStringList values = string.split( '\n' );
if ( values.isEmpty() || string.isEmpty() )
return features;
Q_FOREACH ( const QString &row, values )
{
// Assume that it's just WKT for now.
QgsGeometry geometry = QgsGeometry::fromWkt( row );
if ( geometry.isNull() )
continue;
QgsFeature feature;
if ( !fields.isEmpty() )
feature.setFields( fields, true );
feature.setGeometry( geometry );
features.append( feature );
}
return features;
}
QgsFields QgsClipboard::retrieveFields() const
{
QClipboard *cb = QApplication::clipboard();
#ifdef Q_OS_LINUX
QString string = cb->text( QClipboard::Selection );
#else
QString string = cb->text( QClipboard::Clipboard );
#endif
return QgsOgrUtils::stringToFields( string, QTextCodec::codecForName( "System" ) );
}
QgsFeatureList QgsClipboard::copyOf( const QgsFields &fields ) const
{
QgsDebugMsg( "returning clipboard." );
if ( !mUseSystemClipboard )
return mFeatureClipboard;
QClipboard *cb = QApplication::clipboard();
#ifdef Q_OS_LINUX
QString text = cb->text( QClipboard::Selection );
#else
QString text = cb->text( QClipboard::Clipboard );
#endif
return stringToFeatureList( text, fields );
}
void QgsClipboard::clear()
{
mFeatureClipboard.clear();
QgsDebugMsg( "cleared clipboard." );
emit changed();
}
void QgsClipboard::insert( const QgsFeature &feature )
{
mFeatureClipboard.push_back( feature );
QgsDebugMsgLevel( "inserted " + feature.geometry().exportToWkt(), 4 );
mUseSystemClipboard = false;
emit changed();
}
bool QgsClipboard::isEmpty() const
{
QClipboard *cb = QApplication::clipboard();
#ifdef Q_OS_LINUX
QString text = cb->text( QClipboard::Selection );
#else
QString text = cb->text( QClipboard::Clipboard );
#endif
return text.isEmpty() && mFeatureClipboard.empty();
}
QgsFeatureList QgsClipboard::transformedCopyOf( const QgsCoordinateReferenceSystem &destCRS, const QgsFields &fields ) const
{
QgsFeatureList featureList = copyOf( fields );
QgsCoordinateTransform ct( crs(), destCRS );
QgsDebugMsg( "transforming clipboard." );
for ( QgsFeatureList::iterator iter = featureList.begin(); iter != featureList.end(); ++iter )
{
QgsGeometry g = iter->geometry();
g.transform( ct );
iter->setGeometry( g );
}
return featureList;
}
QgsCoordinateReferenceSystem QgsClipboard::crs() const
{
return mCRS;
}
void QgsClipboard::setData( const QString &mimeType, const QByteArray &data, const QString &text )
{
mUseSystemClipboard = true;
QMimeData *mdata = new QMimeData();
mdata->setData( mimeType, data );
if ( !text.isEmpty() )
{
mdata->setText( text );
}
// Transfers ownership to the clipboard object
#ifdef Q_OS_LINUX
QApplication::clipboard()->setMimeData( mdata, QClipboard::Selection );
#endif
QApplication::clipboard()->setMimeData( mdata, QClipboard::Clipboard );
}
void QgsClipboard::setText( const QString &text )
{
#ifdef Q_OS_LINUX
QApplication::clipboard()->setText( text, QClipboard::Selection );
#endif
QApplication::clipboard()->setText( text, QClipboard::Clipboard );
}
bool QgsClipboard::hasFormat( const QString &mimeType ) const
{
return QApplication::clipboard()->mimeData()->hasFormat( mimeType );
}
QByteArray QgsClipboard::data( const QString &mimeType ) const
{
return QApplication::clipboard()->mimeData()->data( mimeType );
}
void QgsClipboard::systemClipboardChanged()
{
mUseSystemClipboard = true;
emit changed();
}