Skip to content

Commit

Permalink
Fix #12655 copy/paste style between different vector layer geometry t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
Hvitnov committed May 21, 2015
1 parent df039ee commit 1075925
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -6489,6 +6489,7 @@ QgsVectorLayer *QgisApp::pasteToNewMemoryVector()
void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
{
QgsMapLayer *selectionLayer = sourceLayer ? sourceLayer : activeLayer();

if ( selectionLayer )
{
QDomImplementation DomImplementation;
Expand All @@ -6499,6 +6500,26 @@ void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
QDomElement rootNode = doc.createElement( "qgis" );
rootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) );
doc.appendChild( rootNode );

/*
* Check to see if the layer is vector - in which case we should also copy its geometryType
* to avoid eventually pasting to a layer with a different geometry
*/
if ( selectionLayer->type() == 0 )
{
//Getting the selectionLayer geometry
QgsVectorLayer *SelectionGeometry = static_cast<QgsVectorLayer*>(selectionLayer);
QString geoType = QString::number(SelectionGeometry->geometryType());

//Adding geometryinformation
QDomElement layerGeometryType = doc.createElement("layerGeometryType");
QDomText type = doc.createTextNode(geoType);

layerGeometryType.appendChild(type);
rootNode.appendChild(layerGeometryType);

}

QString errorMsg;
if ( !selectionLayer->writeSymbology( rootNode, doc, errorMsg ) )
{
Expand All @@ -6515,12 +6536,19 @@ void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
mActionPasteStyle->setEnabled( true );
}
}
/**
\param destinatioLayer The layer that the clipboard will be pasted to
(defaults to the active layer on the legend)
*/


void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
{
{

QgsMapLayer *selectionLayer = destinationLayer ? destinationLayer : activeLayer();
if ( selectionLayer )
{

if ( clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
QDomDocument doc( "qgis" );
Expand All @@ -6537,7 +6565,28 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
QMessageBox::Ok );
return;
}

QDomElement rootNode = doc.firstChildElement( "qgis" );

//Test for matching geometry type on vector layers when pasting
if (selectionLayer->type() == QgsMapLayer::LayerType::VectorLayer)
{
QgsVectorLayer *selectionVectorLayer = static_cast<QgsVectorLayer*>(selectionLayer);
int pasteLayerGeometryType = doc.elementsByTagName("layerGeometryType").item(0).toElement().text().toInt();

if ( selectionVectorLayer->geometryType() != pasteLayerGeometryType )
{
QMessageBox::information( this,
tr( "Error" ),
tr( "Cannot paste style to layers that have a different geometry type" )
.arg( errorMsg ),
QMessageBox::Ok );
return;
}
}



if ( !selectionLayer->readSymbology( rootNode, errorMsg ) )
{
QMessageBox::information( this,
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -354,6 +354,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
{ Q_UNUSED( node ); errorMessage = QString( "Layer type %1 not supported" ).arg( type() ); return false; }



/** Read the symbology for the current layer from the Dom node supplied.
* @param node node that will contain the symbology definition for this layer.
* @param errorMessage reference to string that will be updated with any error messages
Expand Down

0 comments on commit 1075925

Please sign in to comment.