Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…aste implementation
  • Loading branch information...
commit e1c934af13a84fa057694a76f89eaebe86748e93 1 parent 6b54dff
Hugo Mercier authored April 16, 2012
3  src/app/legend/qgslegend.cpp
@@ -33,6 +33,7 @@
33 33
 #include "qgsrasterlayer.h"
34 34
 #include "qgsvectorlayer.h"
35 35
 #include "qgsgenericprojectionselector.h"
  36
+#include "qgsclipboard.h"
36 37
 
37 38
 #include <QFont>
38 39
 #include <QDomDocument>
@@ -702,7 +703,7 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
702 703
   {
703 704
     QgisApp* app = QgisApp::instance();
704 705
     theMenu.addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );
705  
-    if ( QApplication::clipboard()->mimeData()->hasFormat( "application/qgis.style" ) )
  706
+    if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
706 707
     {
707 708
       theMenu.addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
708 709
     }
18  src/app/qgisapp.cpp
@@ -4397,11 +4397,10 @@ void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
4397 4397
 			    QMessageBox::Ok );
4398 4398
       return;
4399 4399
     }
4400  
-    QClipboard* clipboard = QApplication::clipboard();
4401  
-    QMimeData *mimeData = new QMimeData();
4402  
-    mimeData->setData( "application/qgis.style", doc.toByteArray() );
4403  
-    // transfers the ownership to the clipboard object
4404  
-    clipboard->setMimeData( mimeData );
  4400
+    // Copies data in text form as well, so the XML can be pasted into a text editor
  4401
+    clipboard()->setData( QGSCLIPBOARD_STYLE_MIME, doc.toByteArray(), doc.toString() );
  4402
+    // Enables the paste menu element
  4403
+    mActionPasteStyle->setEnabled( true );
4405 4404
   }
4406 4405
 }
4407 4406
 
@@ -4410,15 +4409,12 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
4410 4409
   QgsMapLayer *selectionLayer = destinationLayer ? destinationLayer : activeLayer();
4411 4410
   if ( selectionLayer )
4412 4411
   {
4413  
-    QClipboard* clipboard = QApplication::clipboard();
4414  
-
4415  
-    const QMimeData* mimeData = clipboard->mimeData();
4416  
-    if ( mimeData->hasFormat( "application/qgis.style" ) )
  4412
+    if ( clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
4417 4413
     {
4418 4414
       QDomDocument doc( "qgis" );
4419 4415
       QString errorMsg;
4420 4416
       int errorLine, errorColumn;
4421  
-      if ( !doc.setContent ( mimeData->data( "application/qgis.style" ), false, &errorMsg, &errorLine, &errorColumn ) )
  4417
+      if ( !doc.setContent ( clipboard()->data( QGSCLIPBOARD_STYLE_MIME ), false, &errorMsg, &errorLine, &errorColumn ) )
4422 4418
       {
4423 4419
 	QMessageBox::information( this,
4424 4420
 				  tr( "Error" ),
@@ -6400,7 +6396,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
6400 6396
   mActionZoomToLayer->setEnabled( true );
6401 6397
 
6402 6398
   mActionCopyStyle->setEnabled( true );
6403  
-  mActionPasteStyle->setEnabled( QApplication::clipboard()->mimeData()->hasFormat( "application/qgis.style" ) );
  6399
+  mActionPasteStyle->setEnabled( clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) );
6404 6400
 
6405 6401
   /***********Vector layers****************/
6406 6402
   if ( layer->type() == QgsMapLayer::VectorLayer )
40  src/app/qgsclipboard.cpp
@@ -22,6 +22,7 @@
22 22
 #include <QStringList>
23 23
 #include <QClipboard>
24 24
 #include <QSettings>
  25
+#include <QMimeData>
25 26
 
26 27
 #include "qgsclipboard.h"
27 28
 #include "qgsfeature.h"
@@ -104,9 +105,9 @@ void QgsClipboard::replaceWithCopyOf( const QgsFieldMap& fields, QgsFeatureList&
104 105
   // docs). With a Linux X server, ::Clipboard was required.
105 106
   // The simple solution was to put the text into both clipboards.
106 107
 
107  
-  // The ::Selection setText() below one may need placing inside so
108  
-  // #ifdef so that it doesn't get compiled under Windows.
  108
+#ifndef Q_OS_WIN
109 109
   cb->setText( textCopy, QClipboard::Selection );
  110
+#endif
110 111
   cb->setText( textCopy, QClipboard::Clipboard );
111 112
 
112 113
   QgsDebugMsg( QString( "replaced system clipboard with: %1." ).arg( textCopy ) );
@@ -163,3 +164,38 @@ QgsCoordinateReferenceSystem QgsClipboard::crs()
163 164
 {
164 165
   return mCRS;
165 166
 }
  167
+
  168
+void QgsClipboard::setData( const QString& mimeType, const QByteArray& data, const QString* text )
  169
+{
  170
+    QMimeData *mdata = new QMimeData();
  171
+    mdata->setData( mimeType, data );
  172
+    if ( text )
  173
+    {
  174
+	mdata->setText( *text );
  175
+    }
  176
+    // Transfers ownership to the clipboard object
  177
+#ifndef Q_OS_WIN
  178
+    QApplication::clipboard()->setMimeData( mdata, QClipboard::Selection );
  179
+#endif
  180
+    QApplication::clipboard()->setMimeData( mdata, QClipboard::Clipboard );
  181
+}
  182
+
  183
+void QgsClipboard::setData( const QString& mimeType, const QByteArray& data, const QString& text )
  184
+{
  185
+    setData( mimeType, data, &text );
  186
+}
  187
+
  188
+void QgsClipboard::setData( const QString& mimeType, const QByteArray& data )
  189
+{
  190
+    setData( mimeType, data, 0 );
  191
+}
  192
+
  193
+bool QgsClipboard::hasFormat( const QString& mimeType )
  194
+{
  195
+    return QApplication::clipboard()->mimeData()->hasFormat( mimeType );
  196
+}
  197
+
  198
+QByteArray QgsClipboard::data( const QString& mimeType )
  199
+{
  200
+    return QApplication::clipboard()->mimeData()->data( mimeType );
  201
+}
31  src/app/qgsclipboard.h
@@ -41,6 +41,11 @@
41 41
   TODO: Make it work
42 42
 */
43 43
 
  44
+/*
  45
+ * Constants used to describe copy-paste MIME types
  46
+ */
  47
+#define QGSCLIPBOARD_STYLE_MIME "application/qgis.style"
  48
+
44 49
 class QgsClipboard
45 50
 {
46 51
   public:
@@ -98,6 +103,32 @@ class QgsClipboard
98 103
      */
99 104
     QgsCoordinateReferenceSystem crs();
100 105
 
  106
+    /*
  107
+     * Stores a MimeData together with a text into the system clipboard
  108
+     */
  109
+    void setData( const QString& mimeType, const QByteArray& data, const QString* text = 0 );
  110
+    /*
  111
+     * Stores a MimeData together with a text into the system clipboard
  112
+     */
  113
+    void setData( const QString& mimeType, const QByteArray& data, const QString& text );
  114
+    /*
  115
+     * Stores a MimeData into the system clipboard
  116
+     */
  117
+    void setData( const QString& mimeType, const QByteArray& data );
  118
+    /*
  119
+     * Stores a text into the system clipboard
  120
+     */
  121
+    void setText( const QString& text );
  122
+    /*
  123
+     * Proxy to QMimeData::hasFormat
  124
+     * Tests whether the system clipboard contains data of a given MIME type
  125
+     */
  126
+    bool hasFormat( const QString& mimeType );
  127
+    /*
  128
+     * Retrieve data from the system clipboard.
  129
+     * No copy is involved, since the return QByteArray is implicitly shared
  130
+     */
  131
+    QByteArray data( const QString& mimeType );
101 132
   private:
102 133
 
103 134
     /** QGIS-internal vector feature clipboard.

0 notes on commit e1c934a

Please sign in to comment.
Something went wrong with that request. Please try again.