Skip to content

Commit

Permalink
use a QMimeData clipboard object to paste features as HTML table (#6243)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Feb 3, 2018
1 parent 0a93674 commit a66a4db
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/app/qgsclipboard.cpp
Expand Up @@ -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 );
}
Expand Down
5 changes: 5 additions & 0 deletions tests/src/app/testqgisappclipboard.cpp
Expand Up @@ -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();
Expand Down

0 comments on commit a66a4db

Please sign in to comment.