Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement basic copy/paste of styles between layers

  • Loading branch information...
commit 6b54dff90c45b08c676df2192e3f82006dd77b73 1 parent 23d5362
@mhugo mhugo authored
View
11 src/app/legend/qgslegend.cpp
@@ -42,6 +42,7 @@
#include <QMouseEvent>
#include <QPixmap>
#include <QTreeWidgetItem>
+#include <QClipboard>
const int AUTOSCROLL_MARGIN = 16;
@@ -697,6 +698,16 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
// ends here
}
+ if ( selectedLayers().length() == 1 )
+ {
+ QgisApp* app = QgisApp::instance();
+ theMenu.addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );
+ if ( QApplication::clipboard()->mimeData()->hasFormat( "application/qgis.style" ) )
+ {
+ theMenu.addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
+ }
+ }
+
theMenu.addAction( QgisApp::getThemeIcon( "/folder_new.png" ), tr( "&Add New Group" ), this, SLOT( addGroupToCurrentItem() ) );
theMenu.addAction( QgisApp::getThemeIcon( "/mActionExpandTree.png" ), tr( "&Expand All" ), this, SLOT( expandAll() ) );
theMenu.addAction( QgisApp::getThemeIcon( "/mActionCollapseTree.png" ), tr( "&Collapse All" ), this, SLOT( collapseAll() ) );
View
78 src/app/qgisapp.cpp
@@ -824,6 +824,8 @@ void QgisApp::createActions()
connect( mActionCutFeatures, SIGNAL( triggered() ), this, SLOT( editCut() ) );
connect( mActionCopyFeatures, SIGNAL( triggered() ), this, SLOT( editCopy() ) );
connect( mActionPasteFeatures, SIGNAL( triggered() ), this, SLOT( editPaste() ) );
+ connect( mActionCopyStyle, SIGNAL( triggered() ), this, SLOT( copyStyle() ) );
+ connect( mActionPasteStyle, SIGNAL( triggered() ), this, SLOT( pasteStyle() ) );
connect( mActionAddFeature, SIGNAL( triggered() ), this, SLOT( addFeature() ) );
connect( mActionMoveFeature, SIGNAL( triggered() ), this, SLOT( moveFeature() ) );
connect( mActionReshapeFeatures, SIGNAL( triggered() ), this, SLOT( reshapeFeatures() ) );
@@ -4294,7 +4296,6 @@ void QgisApp::editCut( QgsMapLayer * layerContainingSelection )
}
}
-
void QgisApp::editCopy( QgsMapLayer * layerContainingSelection )
{
if ( mMapCanvas && mMapCanvas->isDrawing() )
@@ -4373,6 +4374,76 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
}
}
+void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
+{
+ QgsMapLayer *selectionLayer = sourceLayer ? sourceLayer : activeLayer();
+ if ( selectionLayer )
+ {
+ QDomImplementation DomImplementation;
+ QDomDocumentType documentType =
+ DomImplementation.createDocumentType(
+ "qgis", "http://mrcc.com/qgis.dtd", "SYSTEM" );
+ QDomDocument doc( documentType );
+ QDomElement rootNode = doc.createElement( "qgis" );
+ rootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) );
+ doc.appendChild( rootNode );
+ QString errorMsg;
+ if ( !selectionLayer->writeSymbology( rootNode, doc, errorMsg ) )
+ {
+ QMessageBox::warning( this,
+ tr( "Error" ),
+ tr( "Cannot copy style: %1" )
+ .arg( errorMsg ),
+ 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 );
+ }
+}
+
+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" ) )
+ {
+ QDomDocument doc( "qgis" );
+ QString errorMsg;
+ int errorLine, errorColumn;
+ if ( !doc.setContent ( mimeData->data( "application/qgis.style" ), false, &errorMsg, &errorLine, &errorColumn ) )
+ {
+ QMessageBox::information( this,
+ tr( "Error" ),
+ tr( "Cannot parse style: %1:%2:%3" )
+ .arg( errorMsg )
+ .arg( errorLine )
+ .arg( errorColumn ),
+ QMessageBox::Ok );
+ return;
+ }
+ QDomElement rootNode = doc.firstChildElement( "qgis" );
+ if ( !selectionLayer->readSymbology( rootNode, errorMsg ) )
+ {
+ QMessageBox::information( this,
+ tr( "Error" ),
+ tr( "Cannot read style: %1" )
+ .arg( errorMsg ),
+ QMessageBox::Ok );
+ return;
+ }
+
+ mMapLegend->refreshLayerSymbology( selectionLayer->id(), false );
+ }
+ }
+}
void QgisApp::pasteTransformations()
{
@@ -6296,6 +6367,8 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionCutFeatures->setEnabled( false );
mActionCopyFeatures->setEnabled( false );
mActionPasteFeatures->setEnabled( false );
+ mActionCopyStyle->setEnabled( false );
+ mActionPasteStyle->setEnabled( false );
mActionUndo->setEnabled( false );
mActionRedo->setEnabled( false );
@@ -6326,6 +6399,9 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionAddToOverview->setEnabled( true );
mActionZoomToLayer->setEnabled( true );
+ mActionCopyStyle->setEnabled( true );
+ mActionPasteStyle->setEnabled( QApplication::clipboard()->mimeData()->hasFormat( "application/qgis.style" ) );
+
/***********Vector layers****************/
if ( layer->type() == QgsMapLayer::VectorLayer )
{
View
11 src/app/qgisapp.h
@@ -429,6 +429,17 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
*/
void editPaste( QgsMapLayer * destinationLayer = 0 );
+ /**
+ \param sourceLayer The layer where the style will be taken from
+ (defaults to the active layer on the legend)
+ */
+ void copyStyle( QgsMapLayer * sourceLayer = 0 );
+ //! copies style on the clipboard to the active layer
+ /**
+ \param destinatioLayer The layer that the clipboard will be pasted to
+ (defaults to the active layer on the legend)
+ */
+ void pasteStyle( QgsMapLayer * destinationLayer = 0 );
void loadOGRSublayers( QString layertype, QString uri, QStringList list );
void loadGDALSublayers( QString uri, QStringList list );
View
23 src/ui/qgisapp.ui
@@ -17,7 +17,7 @@
<x>0</x>
<y>0</y>
<width>1052</width>
- <height>21</height>
+ <height>25</height>
</rect>
</property>
<widget class="QMenu" name="mEditMenu">
@@ -152,6 +152,9 @@
<addaction name="mActionAddLayerSeparator"/>
<addaction name="mActionAddWfsLayer"/>
<addaction name="separator"/>
+ <addaction name="mActionCopyStyle"/>
+ <addaction name="mActionPasteStyle"/>
+ <addaction name="separator"/>
<addaction name="mActionOpenTable"/>
<addaction name="mActionSaveEdits"/>
<addaction name="mActionToggleEditing"/>
@@ -1648,6 +1651,24 @@
<string>Offset Curve</string>
</property>
</action>
+ <action name="mActionCopyStyle">
+ <property name="icon">
+ <iconset resource="../../images/images.qrc">
+ <normaloff>:/images/themes/default/mActionEditCopy.png</normaloff>:/images/themes/default/mActionEditCopy.png</iconset>
+ </property>
+ <property name="text">
+ <string>Copy style</string>
+ </property>
+ </action>
+ <action name="mActionPasteStyle">
+ <property name="icon">
+ <iconset resource="../../images/images.qrc">
+ <normaloff>:/images/themes/default/mActionEditPaste.png</normaloff>:/images/themes/default/mActionEditPaste.png</iconset>
+ </property>
+ <property name="text">
+ <string>Paste style</string>
+ </property>
+ </action>
</widget>
<resources>
<include location="../../images/images.qrc"/>

0 comments on commit 6b54dff

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