@@ -824,6 +824,8 @@ void QgisApp::createActions()
824824 connect ( mActionCutFeatures , SIGNAL ( triggered () ), this , SLOT ( editCut () ) );
825825 connect ( mActionCopyFeatures , SIGNAL ( triggered () ), this , SLOT ( editCopy () ) );
826826 connect ( mActionPasteFeatures , SIGNAL ( triggered () ), this , SLOT ( editPaste () ) );
827+ connect ( mActionCopyStyle , SIGNAL ( triggered () ), this , SLOT ( copyStyle () ) );
828+ connect ( mActionPasteStyle , SIGNAL ( triggered () ), this , SLOT ( pasteStyle () ) );
827829 connect ( mActionAddFeature , SIGNAL ( triggered () ), this , SLOT ( addFeature () ) );
828830 connect ( mActionMoveFeature , SIGNAL ( triggered () ), this , SLOT ( moveFeature () ) );
829831 connect ( mActionReshapeFeatures , SIGNAL ( triggered () ), this , SLOT ( reshapeFeatures () ) );
@@ -4297,7 +4299,6 @@ void QgisApp::editCut( QgsMapLayer * layerContainingSelection )
42974299 }
42984300}
42994301
4300-
43014302void QgisApp::editCopy ( QgsMapLayer * layerContainingSelection )
43024303{
43034304 if ( mMapCanvas && mMapCanvas ->isDrawing () )
@@ -4376,6 +4377,72 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
43764377 }
43774378}
43784379
4380+ void QgisApp::copyStyle ( QgsMapLayer * sourceLayer )
4381+ {
4382+ QgsMapLayer *selectionLayer = sourceLayer ? sourceLayer : activeLayer ();
4383+ if ( selectionLayer )
4384+ {
4385+ QDomImplementation DomImplementation;
4386+ QDomDocumentType documentType =
4387+ DomImplementation.createDocumentType (
4388+ " qgis" , " http://mrcc.com/qgis.dtd" , " SYSTEM" );
4389+ QDomDocument doc ( documentType );
4390+ QDomElement rootNode = doc.createElement ( " qgis" );
4391+ rootNode.setAttribute ( " version" , QString ( " %1" ).arg ( QGis::QGIS_VERSION ) );
4392+ doc.appendChild ( rootNode );
4393+ QString errorMsg;
4394+ if ( !selectionLayer->writeSymbology ( rootNode, doc, errorMsg ) )
4395+ {
4396+ QMessageBox::warning ( this ,
4397+ tr ( " Error" ),
4398+ tr ( " Cannot copy style: %1" )
4399+ .arg ( errorMsg ),
4400+ QMessageBox::Ok );
4401+ return ;
4402+ }
4403+ // Copies data in text form as well, so the XML can be pasted into a text editor
4404+ clipboard ()->setData ( QGSCLIPBOARD_STYLE_MIME, doc.toByteArray (), doc.toString () );
4405+ // Enables the paste menu element
4406+ mActionPasteStyle ->setEnabled ( true );
4407+ }
4408+ }
4409+
4410+ void QgisApp::pasteStyle ( QgsMapLayer * destinationLayer )
4411+ {
4412+ QgsMapLayer *selectionLayer = destinationLayer ? destinationLayer : activeLayer ();
4413+ if ( selectionLayer )
4414+ {
4415+ if ( clipboard ()->hasFormat ( QGSCLIPBOARD_STYLE_MIME ) )
4416+ {
4417+ QDomDocument doc ( " qgis" );
4418+ QString errorMsg;
4419+ int errorLine, errorColumn;
4420+ if ( !doc.setContent ( clipboard ()->data ( QGSCLIPBOARD_STYLE_MIME ), false , &errorMsg, &errorLine, &errorColumn ) )
4421+ {
4422+ QMessageBox::information ( this ,
4423+ tr ( " Error" ),
4424+ tr ( " Cannot parse style: %1:%2:%3" )
4425+ .arg ( errorMsg )
4426+ .arg ( errorLine )
4427+ .arg ( errorColumn ),
4428+ QMessageBox::Ok );
4429+ return ;
4430+ }
4431+ QDomElement rootNode = doc.firstChildElement ( " qgis" );
4432+ if ( !selectionLayer->readSymbology ( rootNode, errorMsg ) )
4433+ {
4434+ QMessageBox::information ( this ,
4435+ tr ( " Error" ),
4436+ tr ( " Cannot read style: %1" )
4437+ .arg ( errorMsg ),
4438+ QMessageBox::Ok );
4439+ return ;
4440+ }
4441+
4442+ mMapLegend ->refreshLayerSymbology ( selectionLayer->id (), false );
4443+ }
4444+ }
4445+ }
43794446
43804447void QgisApp::pasteTransformations ()
43814448{
@@ -6299,6 +6366,8 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
62996366 mActionCutFeatures ->setEnabled ( false );
63006367 mActionCopyFeatures ->setEnabled ( false );
63016368 mActionPasteFeatures ->setEnabled ( false );
6369+ mActionCopyStyle ->setEnabled ( false );
6370+ mActionPasteStyle ->setEnabled ( false );
63026371
63036372 mActionUndo ->setEnabled ( false );
63046373 mActionRedo ->setEnabled ( false );
@@ -6329,6 +6398,9 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
63296398 mActionAddToOverview ->setEnabled ( true );
63306399 mActionZoomToLayer ->setEnabled ( true );
63316400
6401+ mActionCopyStyle ->setEnabled ( true );
6402+ mActionPasteStyle ->setEnabled ( clipboard ()->hasFormat ( QGSCLIPBOARD_STYLE_MIME ) );
6403+
63326404 /* **********Vector layers****************/
63336405 if ( layer->type () == QgsMapLayer::VectorLayer )
63346406 {
0 commit comments