Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make the style copy/paste functionality closer to the geometry copy/p…

…aste implementation
  • Loading branch information...
commit e1c934af13a84fa057694a76f89eaebe86748e93 1 parent 6b54dff
Hugo Mercier mhugo authored
3  src/app/legend/qgslegend.cpp
View
@@ -33,6 +33,7 @@
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"
#include "qgsgenericprojectionselector.h"
+#include "qgsclipboard.h"
#include <QFont>
#include <QDomDocument>
@@ -702,7 +703,7 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
{
QgisApp* app = QgisApp::instance();
theMenu.addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );
- if ( QApplication::clipboard()->mimeData()->hasFormat( "application/qgis.style" ) )
+ if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
theMenu.addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
}
18 src/app/qgisapp.cpp
View
@@ -4397,11 +4397,10 @@ void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
QMessageBox::Ok );
return;
}
- QClipboard* clipboard = QApplication::clipboard();
- QMimeData *mimeData = new QMimeData();
- mimeData->setData( "application/qgis.style", doc.toByteArray() );
- // transfers the ownership to the clipboard object
- clipboard->setMimeData( mimeData );
+ // Copies data in text form as well, so the XML can be pasted into a text editor
+ clipboard()->setData( QGSCLIPBOARD_STYLE_MIME, doc.toByteArray(), doc.toString() );
+ // Enables the paste menu element
+ mActionPasteStyle->setEnabled( true );
}
}
@@ -4410,15 +4409,12 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
QgsMapLayer *selectionLayer = destinationLayer ? destinationLayer : activeLayer();
if ( selectionLayer )
{
- QClipboard* clipboard = QApplication::clipboard();
-
- const QMimeData* mimeData = clipboard->mimeData();
- if ( mimeData->hasFormat( "application/qgis.style" ) )
+ if ( clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
QDomDocument doc( "qgis" );
QString errorMsg;
int errorLine, errorColumn;
- if ( !doc.setContent ( mimeData->data( "application/qgis.style" ), false, &errorMsg, &errorLine, &errorColumn ) )
+ if ( !doc.setContent ( clipboard()->data( QGSCLIPBOARD_STYLE_MIME ), false, &errorMsg, &errorLine, &errorColumn ) )
{
QMessageBox::information( this,
tr( "Error" ),
@@ -6400,7 +6396,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionZoomToLayer->setEnabled( true );
mActionCopyStyle->setEnabled( true );
- mActionPasteStyle->setEnabled( QApplication::clipboard()->mimeData()->hasFormat( "application/qgis.style" ) );
+ mActionPasteStyle->setEnabled( clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) );
/***********Vector layers****************/
if ( layer->type() == QgsMapLayer::VectorLayer )
40 src/app/qgsclipboard.cpp
View
@@ -22,6 +22,7 @@
#include <QStringList>
#include <QClipboard>
#include <QSettings>
+#include <QMimeData>
#include "qgsclipboard.h"
#include "qgsfeature.h"
@@ -104,9 +105,9 @@ void QgsClipboard::replaceWithCopyOf( const QgsFieldMap& fields, QgsFeatureList&
// docs). With a Linux X server, ::Clipboard was required.
// The simple solution was to put the text into both clipboards.
- // The ::Selection setText() below one may need placing inside so
- // #ifdef so that it doesn't get compiled under Windows.
+#ifndef Q_OS_WIN
cb->setText( textCopy, QClipboard::Selection );
+#endif
cb->setText( textCopy, QClipboard::Clipboard );
QgsDebugMsg( QString( "replaced system clipboard with: %1." ).arg( textCopy ) );
@@ -163,3 +164,38 @@ QgsCoordinateReferenceSystem QgsClipboard::crs()
{
return mCRS;
}
+
+void QgsClipboard::setData( const QString& mimeType, const QByteArray& data, const QString* text )
+{
+ QMimeData *mdata = new QMimeData();
+ mdata->setData( mimeType, data );
+ if ( text )
+ {
+ mdata->setText( *text );
+ }
+ // Transfers ownership to the clipboard object
+#ifndef Q_OS_WIN
+ QApplication::clipboard()->setMimeData( mdata, QClipboard::Selection );
+#endif
+ QApplication::clipboard()->setMimeData( mdata, QClipboard::Clipboard );
+}
+
+void QgsClipboard::setData( const QString& mimeType, const QByteArray& data, const QString& text )
+{
+ setData( mimeType, data, &text );
+}
+
+void QgsClipboard::setData( const QString& mimeType, const QByteArray& data )
+{
+ setData( mimeType, data, 0 );
+}
+
+bool QgsClipboard::hasFormat( const QString& mimeType )
+{
+ return QApplication::clipboard()->mimeData()->hasFormat( mimeType );
+}
+
+QByteArray QgsClipboard::data( const QString& mimeType )
+{
+ return QApplication::clipboard()->mimeData()->data( mimeType );
+}
31 src/app/qgsclipboard.h
View
@@ -41,6 +41,11 @@
TODO: Make it work
*/
+/*
+ * Constants used to describe copy-paste MIME types
+ */
+#define QGSCLIPBOARD_STYLE_MIME "application/qgis.style"
+
class QgsClipboard
{
public:
@@ -98,6 +103,32 @@ class QgsClipboard
*/
QgsCoordinateReferenceSystem crs();
+ /*
+ * Stores a MimeData together with a text into the system clipboard
+ */
+ void setData( const QString& mimeType, const QByteArray& data, const QString* text = 0 );
+ /*
+ * Stores a MimeData together with a text into the system clipboard
+ */
+ void setData( const QString& mimeType, const QByteArray& data, const QString& text );
+ /*
+ * Stores a MimeData into the system clipboard
+ */
+ void setData( const QString& mimeType, const QByteArray& data );
+ /*
+ * Stores a text into the system clipboard
+ */
+ void setText( const QString& text );
+ /*
+ * Proxy to QMimeData::hasFormat
+ * Tests whether the system clipboard contains data of a given MIME type
+ */
+ bool hasFormat( const QString& mimeType );
+ /*
+ * Retrieve data from the system clipboard.
+ * No copy is involved, since the return QByteArray is implicitly shared
+ */
+ QByteArray data( const QString& mimeType );
private:
/** QGIS-internal vector feature clipboard.
Please sign in to comment.
Something went wrong with that request. Please try again.