@@ -847,44 +847,47 @@ QString QgsMapLayer::loadNamedStyle( const QString theURI, bool &theResultFlag )
847847 return " " ;
848848}
849849
850+ void QgsMapLayer::exportNamedStyle (QDomDocument &doc, QString &errorMsg)
851+ {
852+ QDomImplementation DomImplementation;
853+ QDomDocumentType documentType = DomImplementation.createDocumentType ( " qgis" , " http://mrcc.com/qgis.dtd" , " SYSTEM" );
854+ QDomDocument myDocument ( documentType );
855+
856+ QDomElement myRootNode = myDocument.createElement ( " qgis" );
857+ myRootNode.setAttribute ( " version" , QString ( " %1" ).arg ( QGis::QGIS_VERSION ) );
858+ myDocument.appendChild ( myRootNode );
859+
860+ myRootNode.setAttribute ( " hasScaleBasedVisibilityFlag" , hasScaleBasedVisibility () ? 1 : 0 );
861+ myRootNode.setAttribute ( " minimumScale" , QString::number ( minimumScale () ) );
862+ myRootNode.setAttribute ( " maximumScale" , QString::number ( maximumScale () ) );
863+
864+ #if 0
865+ // <transparencyLevelInt>
866+ QDomElement transparencyLevelIntElement = myDocument.createElement( "transparencyLevelInt" );
867+ QDomText transparencyLevelIntText = myDocument.createTextNode( QString::number( getTransparency() ) );
868+ transparencyLevelIntElement.appendChild( transparencyLevelIntText );
869+ myRootNode.appendChild( transparencyLevelIntElement );
870+ #endif
871+
872+ if ( !writeSymbology ( myRootNode, myDocument, errorMsg ) )
873+ {
874+ errorMsg = QObject::tr ( " Could not save symbology because:\n %1" ).arg ( errorMsg );
875+ return ;
876+ }
877+ doc = myDocument;
878+ }
879+
850880QString QgsMapLayer::saveDefaultStyle ( bool & theResultFlag )
851881{
852882 return saveNamedStyle ( styleURI (), theResultFlag );
853883}
854884
855885QString QgsMapLayer::saveNamedStyle ( const QString theURI, bool & theResultFlag )
856886{
857- QString myErrorMessage;
858-
859- QDomImplementation DomImplementation;
860- QDomDocumentType documentType =
861- DomImplementation.createDocumentType (
862- " qgis" , " http://mrcc.com/qgis.dtd" , " SYSTEM" );
863- QDomDocument myDocument ( documentType );
864- QDomElement myRootNode = myDocument.createElement ( " qgis" );
865- myRootNode.setAttribute ( " version" , QString ( " %1" ).arg ( QGis::QGIS_VERSION ) );
866- myDocument.appendChild ( myRootNode );
867-
868- // use scale dependent visibility flag
869- myRootNode.setAttribute ( " hasScaleBasedVisibilityFlag" , hasScaleBasedVisibility () ? 1 : 0 );
870- myRootNode.setAttribute ( " minimumScale" , QString::number ( minimumScale () ) );
871- myRootNode.setAttribute ( " maximumScale" , QString::number ( maximumScale () ) );
872-
873- #if 0
874- // <transparencyLevelInt>
875- QDomElement transparencyLevelIntElement = myDocument.createElement( "transparencyLevelInt" );
876- QDomText transparencyLevelIntText = myDocument.createTextNode( QString::number( getTransparency() ) );
877- transparencyLevelIntElement.appendChild( transparencyLevelIntText );
878- myRootNode.appendChild( transparencyLevelIntElement );
879- #endif
880-
881- // now append layer node to map layer node
882887
883- QString errorMsg;
884- if ( !writeSymbology ( myRootNode, myDocument, errorMsg ) )
885- {
886- return tr ( " Could not save symbology because:\n %1" ).arg ( errorMsg );
887- }
888+ QString myErrorMessage;
889+ QDomDocument myDocument;
890+ exportNamedStyle ( myDocument, myErrorMessage );
888891
889892 // check if the uri is a file or ends with .qml,
890893 // which indicates that it should become one
@@ -1017,40 +1020,54 @@ QString QgsMapLayer::saveNamedStyle( const QString theURI, bool & theResultFlag
10171020 return myErrorMessage;
10181021}
10191022
1020- QString QgsMapLayer::saveSldStyle ( const QString theURI, bool & theResultFlag )
1021- {
1022- QDomDocument myDocument = QDomDocument ();
1023+ void QgsMapLayer::exportSldStyle ( QDomDocument &doc, QString &errorMsg ){
1024+ QDomDocument myDocument = QDomDocument ();
10231025
1024- QDomNode header = myDocument.createProcessingInstruction ( " xml" , " version=\" 1.0\" encoding=\" UTF-8\" " );
1025- myDocument.appendChild ( header );
1026+ QDomNode header = myDocument.createProcessingInstruction ( " xml" , " version=\" 1.0\" encoding=\" UTF-8\" " );
1027+ myDocument.appendChild ( header );
10261028
1027- // Create the root element
1028- QDomElement root = myDocument.createElementNS ( " http://www.opengis.net/sld" , " StyledLayerDescriptor" );
1029- root.setAttribute ( " version" , " 1.1.0" );
1030- root.setAttribute ( " xsi:schemaLocation" , " http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" );
1031- root.setAttribute ( " xmlns:ogc" , " http://www.opengis.net/ogc" );
1032- root.setAttribute ( " xmlns:se" , " http://www.opengis.net/se" );
1033- root.setAttribute ( " xmlns:xlink" , " http://www.w3.org/1999/xlink" );
1034- root.setAttribute ( " xmlns:xsi" , " http://www.w3.org/2001/XMLSchema-instance" );
1035- myDocument.appendChild ( root );
1029+ // Create the root element
1030+ QDomElement root = myDocument.createElementNS ( " http://www.opengis.net/sld" , " StyledLayerDescriptor" );
1031+ root.setAttribute ( " version" , " 1.1.0" );
1032+ root.setAttribute ( " xsi:schemaLocation" , " http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" );
1033+ root.setAttribute ( " xmlns:ogc" , " http://www.opengis.net/ogc" );
1034+ root.setAttribute ( " xmlns:se" , " http://www.opengis.net/se" );
1035+ root.setAttribute ( " xmlns:xlink" , " http://www.w3.org/1999/xlink" );
1036+ root.setAttribute ( " xmlns:xsi" , " http://www.w3.org/2001/XMLSchema-instance" );
1037+ myDocument.appendChild ( root );
10361038
1037- // Create the NamedLayer element
1038- QDomElement namedLayerNode = myDocument.createElement ( " NamedLayer" );
1039- root.appendChild ( namedLayerNode );
1039+ // Create the NamedLayer element
1040+ QDomElement namedLayerNode = myDocument.createElement ( " NamedLayer" );
1041+ root.appendChild ( namedLayerNode );
10401042
1041- QString errorMsg ;
1042- QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
1043- if ( !vlayer )
1044- {
1045- theResultFlag = false ;
1046- return tr ( " Could not save symbology because: \n %1 " ). arg ( " Non-vector layers not supported yet " ) ;
1047- }
1043+ QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this ) ;
1044+ if ( !vlayer )
1045+ {
1046+ errorMsg = tr ( " Could not save symbology because: \n %1 " )
1047+ . arg ( " Non-vector layers not supported yet " ) ;
1048+ return ;
1049+ }
10481050
1049- if ( !vlayer->writeSld ( namedLayerNode, myDocument, errorMsg ) )
1051+ if ( !vlayer->writeSld ( namedLayerNode, myDocument, errorMsg ) )
1052+ {
1053+ errorMsg = tr ( " Could not save symbology because:\n %1" ).arg ( errorMsg );
1054+ return ;
1055+ }
1056+
1057+ doc = myDocument;
1058+ }
1059+
1060+ QString QgsMapLayer::saveSldStyle ( const QString theURI, bool & theResultFlag )
1061+ {
1062+ QString errorMsg;
1063+ QDomDocument myDocument;
1064+ exportSldStyle ( myDocument, errorMsg );
1065+ if ( !errorMsg.isNull () )
10501066 {
10511067 theResultFlag = false ;
1052- return tr ( " Could not save symbology because: \n %1 " ). arg ( errorMsg ) ;
1068+ return errorMsg;
10531069 }
1070+ QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( this );
10541071
10551072 // check if the uri is a file or ends with .sld,
10561073 // which indicates that it should become one
0 commit comments