Skip to content

Commit a66a4db

Browse files
authored
use a QMimeData clipboard object to paste features as HTML table (#6243)
1 parent 0a93674 commit a66a4db

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/app/qgsclipboard.cpp

+33-5
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,50 @@ void QgsClipboard::setSystemClipboard()
154154
// that just the next call to systemClipboardChanged() should be ignored
155155
mIgnoreNextSystemClipboardChange = true;
156156

157-
QString textCopy = generateClipboardText();
158-
159157
QClipboard *cb = QApplication::clipboard();
160158

161159
// Copy text into the clipboard
160+
QString textCopy = generateClipboardText();
161+
QMimeData *m = new QMimeData();
162+
m->setText( textCopy );
163+
164+
if ( mFeatureClipboard.count() < 1000 )
165+
{
166+
QgsSettings settings;
167+
CopyFormat format = AttributesWithWKT;
168+
if ( settings.contains( QStringLiteral( "/qgis/copyFeatureFormat" ) ) )
169+
{
170+
format = static_cast< CopyFormat >( settings.value( QStringLiteral( "qgis/copyFeatureFormat" ), true ).toInt() );
171+
}
172+
173+
QString htmlCopy;
174+
switch ( format )
175+
{
176+
case AttributesOnly:
177+
case AttributesWithWKT:
178+
htmlCopy = textCopy;
179+
htmlCopy.replace( '\n', QStringLiteral( "</td></tr><tr><td>" ) );
180+
htmlCopy.replace( '\t', QStringLiteral( "</td><td>" ) );
181+
htmlCopy = QStringLiteral( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/></head><body><table border=\"1\"><tr><td>" ) + htmlCopy + QStringLiteral( "</td></tr></table></body></html>" );
182+
break;
183+
case GeoJSON:
184+
break;
185+
}
186+
if ( !htmlCopy.isEmpty() )
187+
{
188+
m->setHtml( htmlCopy );
189+
}
190+
}
162191

163192
// With qgis running under Linux, but with a Windows based X
164193
// server (Xwin32), ::Selection was necessary to get the data into
165194
// the Windows clipboard (which seems contrary to the Qt
166195
// docs). With a Linux X server, ::Clipboard was required.
167196
// The simple solution was to put the text into both clipboards.
168-
169197
#ifdef Q_OS_LINUX
170-
cb->setText( textCopy, QClipboard::Selection );
198+
cb->setMimeData( m, QClipboard::Selection );
171199
#endif
172-
cb->setText( textCopy, QClipboard::Clipboard );
200+
cb->setMimeData( m, QClipboard::Clipboard );
173201

174202
QgsDebugMsgLevel( QString( "replaced system clipboard with: %1." ).arg( textCopy ), 4 );
175203
}

tests/src/app/testqgisappclipboard.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ void TestQgisAppClipboard::copyToText()
149149
result = mQgisApp->clipboard()->generateClipboardText();
150150
QCOMPARE( result, QString( "wkt_geom\tint_field\tstring_field\nPoint (5 6)\t9\tval\nPoint (7 8)\t19\tval2" ) );
151151

152+
// HTML test
153+
mQgisApp->clipboard()->replaceWithCopyOf( feats );
154+
result = mQgisApp->clipboard()->data( "text/html" );
155+
QCOMPARE( result, QString( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/></head><body><table border=\"1\"><tr><td>wkt_geom</td><td>int_field</td><td>string_field</td></tr><tr><td>Point (5 6)</td><td>9</td><td>val</td></tr><tr><td>Point (7 8)</td><td>19</td><td>val2</td></tr></table></body></html>" ) );
156+
152157
// GeoJSON
153158
settings.setValue( QStringLiteral( "/qgis/copyFeatureFormat" ), QgsClipboard::GeoJSON );
154159
result = mQgisApp->clipboard()->generateClipboardText();

0 commit comments

Comments
 (0)