Skip to content

Commit

Permalink
Moved export/import vector geometry type to QgsMaplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
SebDieBln committed Nov 21, 2015
1 parent ff3a622 commit efeacaa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
32 changes: 0 additions & 32 deletions src/app/qgisapp.cpp
Expand Up @@ -6812,24 +6812,6 @@ void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
rootNode.setAttribute( "minimumScale", QString::number( selectionLayer->minimumScale() ) );
rootNode.setAttribute( "maximumScale", QString::number( selectionLayer->maximumScale() ) );

/*
* 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 Down Expand Up @@ -6871,20 +6853,6 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )

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

//Test for matching geometry type on vector layers when pasting
if ( selectionLayer->type() == QgsMapLayer::VectorLayer )
{
QgsVectorLayer *selectionVectorLayer = static_cast<QgsVectorLayer*>( selectionLayer );
int pasteLayerGeometryType = doc.elementsByTagName( "layerGeometryType" ).item( 0 ).toElement().text().toInt();
if ( selectionVectorLayer->geometryType() != pasteLayerGeometryType )
{
messageBar()->pushMessage( tr( "Cannot paste style to layer with a different geometry type" ),
tr( "Your copied style does not match the layer you are pasting to" ),
QgsMessageBar::INFO, messageTimeout() );
return;
}
}

if ( !selectionLayer->readSymbology( rootNode, errorMsg ) )
{
messageBar()->pushMessage( errorMsg,
Expand Down
31 changes: 31 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -1173,6 +1173,18 @@ bool QgsMapLayer::importNamedStyle( QDomDocument& myDocument, QString& myErrorMe
return false;
}

//Test for matching geometry type on vector layers when applying, if geometry type is given in the style
if ( type() == QgsMapLayer::VectorLayer && !myRoot.firstChildElement( "layerGeometryType" ).isNull() )
{
QgsVectorLayer *vl = static_cast<QgsVectorLayer*>( this );
int importLayerGeometryType = myRoot.firstChildElement( "layerGeometryType" ).text().toInt();
if ( vl->geometryType() != importLayerGeometryType )
{
myErrorMessage = tr( "Cannot apply style to layer with a different geometry type" );
return false;
}
}

// use scale dependent visibility flag
setScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 );
setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() );
Expand Down Expand Up @@ -1220,6 +1232,25 @@ void QgsMapLayer::exportNamedStyle( QDomDocument &doc, QString &errorMsg )
errorMsg = QObject::tr( "Could not save symbology because:\n%1" ).arg( errorMsg );
return;
}

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

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

layerGeometryType.appendChild( type );
myRootNode.appendChild( layerGeometryType );
}

doc = myDocument;
}

Expand Down

0 comments on commit efeacaa

Please sign in to comment.