use a QMimeData clipboard object to paste features as HTML table (#6243)

This commit is contained in:
Mathieu Pellerin 2018-02-03 10:54:07 +07:00 committed by GitHub
parent 0a93674329
commit a66a4dbd83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 5 deletions

View File

@ -154,22 +154,50 @@ void QgsClipboard::setSystemClipboard()
// that just the next call to systemClipboardChanged() should be ignored
mIgnoreNextSystemClipboardChange = true;
QString textCopy = generateClipboardText();
QClipboard *cb = QApplication::clipboard();
// Copy text into the clipboard
QString textCopy = generateClipboardText();
QMimeData *m = new QMimeData();
m->setText( textCopy );
if ( mFeatureClipboard.count() < 1000 )
{
QgsSettings settings;
CopyFormat format = AttributesWithWKT;
if ( settings.contains( QStringLiteral( "/qgis/copyFeatureFormat" ) ) )
{
format = static_cast< CopyFormat >( settings.value( QStringLiteral( "qgis/copyFeatureFormat" ), true ).toInt() );
}
QString htmlCopy;
switch ( format )
{
case AttributesOnly:
case AttributesWithWKT:
htmlCopy = textCopy;
htmlCopy.replace( '\n', QStringLiteral( "</td></tr><tr><td>" ) );
htmlCopy.replace( '\t', QStringLiteral( "</td><td>" ) );
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>" );
break;
case GeoJSON:
break;
}
if ( !htmlCopy.isEmpty() )
{
m->setHtml( htmlCopy );
}
}
// 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 );
cb->setMimeData( m, QClipboard::Selection );
#endif
cb->setText( textCopy, QClipboard::Clipboard );
cb->setMimeData( m, QClipboard::Clipboard );
QgsDebugMsgLevel( QString( "replaced system clipboard with: %1." ).arg( textCopy ), 4 );
}

View File

@ -149,6 +149,11 @@ void TestQgisAppClipboard::copyToText()
result = mQgisApp->clipboard()->generateClipboardText();
QCOMPARE( result, QString( "wkt_geom\tint_field\tstring_field\nPoint (5 6)\t9\tval\nPoint (7 8)\t19\tval2" ) );
// HTML test
mQgisApp->clipboard()->replaceWithCopyOf( feats );
result = mQgisApp->clipboard()->data( "text/html" );
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>" ) );
// GeoJSON
settings.setValue( QStringLiteral( "/qgis/copyFeatureFormat" ), QgsClipboard::GeoJSON );
result = mQgisApp->clipboard()->generateClipboardText();