26 #include <QTextStream>    27 #include <QDomDocument>    71   Q_UNUSED( newVersion )
    72   bool returnValue = 
false;
   105   if ( !mDom.isNull() )
   109       if ( transformer.to >= mCurrentVersion && ( transformer.from == mCurrentVersion || transformer.from.
isNull() ) )
   112         ( *( transformer.transformFunc ) )( this );
   113         mCurrentVersion = transformer.to;
   123   QgsDebugMsg( QStringLiteral( 
"Current project file version is %1.%2.%3" )
   129   std::cout << mDom.toString( 2 ).toLatin1().constData(); 
   140   if ( ! pft->
dom().isNull() )
   144     QDomElement mapCanvas; 
   147     QDomNode qgis = pft->
dom().firstChildElement( QStringLiteral( 
"qgis" ) );
   148     if ( ! qgis.isNull() )
   150       QgsDebugMsg( QStringLiteral( 
"Populating new mapcanvas" ) );
   153       mapCanvas = pft->
dom().createElement( QStringLiteral( 
"mapcanvas" ) );
   155       qgis.appendChild( mapCanvas );
   157       mapCanvas.appendChild( qgis.namedItem( QStringLiteral( 
"units" ) ) );
   159       mapCanvas.appendChild( qgis.namedItem( QStringLiteral( 
"extent" ) ) );
   163       QDomElement properties = qgis.firstChildElement( QStringLiteral( 
"properties" ) );
   164       QDomElement spatial = properties.firstChildElement( QStringLiteral( 
"SpatialRefSys" ) );
   165       QDomElement hasCrsTransformEnabled = spatial.firstChildElement( QStringLiteral( 
"ProjectionsEnabled" ) );
   168       QDomElement projection = pft->
dom().createElement( QStringLiteral( 
"projections" ) );
   169       QgsDebugMsg( QStringLiteral( 
"Projection flag: " ) + hasCrsTransformEnabled.text() );
   171       projection.appendChild( pft->
dom().createTextNode( hasCrsTransformEnabled.text() ) );
   173       mapCanvas.appendChild( projection );
   180     QDomNodeList mapLayers = pft->
dom().elementsByTagName( QStringLiteral( 
"maplayer" ) );
   181     bool doneDestination = 
false;
   182     for ( 
int i = 0; i < mapLayers.count(); i++ )
   184       QDomNode mapLayer = mapLayers.item( i );
   186       QDomNode coordinateTransform = mapLayer.namedItem( QStringLiteral( 
"coordinatetransform" ) );
   188       QDomNode sourceCrs = coordinateTransform.namedItem( QStringLiteral( 
"sourcesrs" ) );
   190       sourceCrs.toElement().setTagName( QStringLiteral( 
"srs" ) );
   192       mapLayer.appendChild( sourceCrs );
   195       if ( ! doneDestination )
   198         QDomNode destinationCRS = coordinateTransform.namedItem( QStringLiteral( 
"destinationsrs" ) );
   201         mapCanvas.appendChild( destinationCRS );
   203         doneDestination = 
true;
   205       mapLayer.removeChild( coordinateTransform );
   212     QDomNodeList legendLayerFiles = pft->
dom().elementsByTagName( QStringLiteral( 
"legendlayerfile" ) );
   213     QgsDebugMsg( QStringLiteral( 
"Legend layer file entries: " ) + QString::number( legendLayerFiles.count() ) );
   214     for ( 
int i = 0; i < mapLayers.count(); i++ )
   217       QDomElement mapLayer = mapLayers.item( i ).toElement();
   219       QString 
id = mapLayer.firstChildElement( QStringLiteral( 
"id" ) ).text();
   220       QgsDebugMsg( QStringLiteral( 
"Handling layer %1" ).arg( 
id ) );
   222       for ( 
int j = 0; j < legendLayerFiles.count(); j++ )
   224         QDomElement legendLayerFile = legendLayerFiles.item( j ).toElement();
   225         if ( 
id == legendLayerFile.attribute( QStringLiteral( 
"layerid" ) ) )
   228           QgsDebugMsg( QStringLiteral( 
"Found matching id" ) );
   231           legendLayerFile.setAttribute( QStringLiteral( 
"visible" ), mapLayer.attribute( QStringLiteral( 
"visible" ) ) );
   234           legendLayerFile.setAttribute( QStringLiteral( 
"isInOverview" ), mapLayer.attribute( QStringLiteral( 
"showInOverviewFlag" ) ) );
   243   if ( ! pft->
dom().isNull() )
   246     QDomNodeList rasterPropertyList = pft->
dom().elementsByTagName( QStringLiteral( 
"rasterproperties" ) );
   247     QgsDebugMsg( QStringLiteral( 
"Raster properties file entries: " ) + QString::number( rasterPropertyList.count() ) );
   248     for ( 
int i = 0; i < rasterPropertyList.count(); i++ )
   251       QDomNode rasterProperty = rasterPropertyList.item( i );
   254       rasterProperty.namedItem( QStringLiteral( 
"stdDevsToPlotDouble" ) ).toElement().setTagName( QStringLiteral( 
"mStandardDeviations" ) );
   256       rasterProperty.namedItem( QStringLiteral( 
"invertHistogramFlag" ) ).toElement().setTagName( QStringLiteral( 
"mInvertPixelsFlag" ) );
   257       rasterProperty.namedItem( QStringLiteral( 
"showDebugOverLayFlag" ) ).toElement().setTagName( QStringLiteral( 
"mDebugOverLayFlag" ) );
   259       rasterProperty.namedItem( QStringLiteral( 
"redBandNameQString" ) ).toElement().setTagName( QStringLiteral( 
"mRedBandName" ) );
   260       rasterProperty.namedItem( QStringLiteral( 
"blueBandNameQString" ) ).toElement().setTagName( QStringLiteral( 
"mBlueBandName" ) );
   261       rasterProperty.namedItem( QStringLiteral( 
"greenBandNameQString" ) ).toElement().setTagName( QStringLiteral( 
"mGreenBandName" ) );
   262       rasterProperty.namedItem( QStringLiteral( 
"grayBandNameQString" ) ).toElement().setTagName( QStringLiteral( 
"mGrayBandName" ) );
   266     QDomNodeList symbolPropertyList = pft->
dom().elementsByTagName( QStringLiteral( 
"symbol" ) );
   267     for ( 
int i = 0; i < symbolPropertyList.count(); i++ )
   270       QDomNode symbolProperty = symbolPropertyList.item( i );
   272       QDomElement pointSymbol = symbolProperty.firstChildElement( QStringLiteral( 
"pointsymbol" ) );
   273       if ( pointSymbol.text().startsWith( QLatin1String( 
"hard:" ) ) )
   276         int lineWidth = symbolProperty.firstChildElement( QStringLiteral( 
"outlinewidth" ) ).text().toInt();
   277         int pointSize = symbolProperty.firstChildElement( QStringLiteral( 
"pointsize" ) ).text().toInt();
   279         if ( pointSize != 0 )
   284           pointSize = pointSize + 2 + 2 * lineWidth;
   285           QgsDebugMsg( QStringLiteral( 
"Setting point size to %1" ).arg( pointSize ) );
   286           QDomElement newPointSizeProperty = pft->
dom().createElement( QStringLiteral( 
"pointsize" ) );
   287           QDomText newPointSizeTxt = pft->
dom().createTextNode( QString::number( pointSize ) );
   288           newPointSizeProperty.appendChild( newPointSizeTxt );
   289           symbolProperty.replaceChild( newPointSizeProperty, pointSymbol );
   299   if ( ! pft->
dom().isNull() )
   301 #ifndef QT_NO_PRINTER   303     QPrinter myPrinter( QPrinter::ScreenResolution );
   304     int screenDpi = myPrinter.resolution();
   305     double widthScaleFactor = 25.4 / screenDpi;
   307     QDomNodeList outlineWidthList = pft->
dom().elementsByTagName( QStringLiteral( 
"outlinewidth" ) );
   308     for ( 
int i = 0; i < outlineWidthList.size(); ++i )
   311       QDomElement currentOutlineElem = outlineWidthList.at( i ).toElement();
   312       double outlineWidth = currentOutlineElem.text().toDouble();
   313       outlineWidth *= widthScaleFactor;
   316       QDomNode outlineTextNode = currentOutlineElem.firstChild();
   317       QDomText newOutlineText = pft->
dom().createTextNode( QString::number( outlineWidth ) );
   318       currentOutlineElem.replaceChild( newOutlineText, outlineTextNode );
   323     QDomNodeList pointSizeList = pft->
dom().elementsByTagName( QStringLiteral( 
"pointsize" ) );
   324     for ( 
int i = 0; i < pointSizeList.size(); ++i )
   327       QDomElement currentPointSizeElem = pointSizeList.at( i ).toElement();
   328       double pointSize = currentPointSizeElem.text().toDouble();
   329       pointSize *= widthScaleFactor;
   332       QDomNode pointSizeTextNode = currentPointSizeElem.firstChild();
   333       QDomText newPointSizeText = pft->
dom().createTextNode( QString::number( static_cast< int >( pointSize ) ) );
   334       currentPointSizeElem.replaceChild( newPointSizeText, pointSizeTextNode );
   342   if ( ! pft->
dom().isNull() )
   344     QDomNodeList layerList = pft->
dom().elementsByTagName( QStringLiteral( 
"maplayer" ) );
   345     for ( 
int i = 0; i < layerList.size(); ++i )
   347       QDomElement layerElem = layerList.at( i ).toElement();
   348       QString typeString = layerElem.attribute( QStringLiteral( 
"type" ) );
   349       if ( typeString != QLatin1String( 
"vector" ) )
   355       QDomNode dataSourceNode = layerElem.namedItem( QStringLiteral( 
"datasource" ) );
   356       if ( dataSourceNode.isNull() )
   360       QString dataSource = dataSourceNode.toElement().text();
   363       QDomNode providerNode = layerElem.namedItem( QStringLiteral( 
"provider" ) );
   364       if ( providerNode.isNull() )
   368       QString providerKey = providerNode.toElement().text();
   372       options.loadDefaultStyle = 
false;
   388       QDomNodeList classificationFieldList = layerElem.elementsByTagName( QStringLiteral( 
"classificationfield" ) );
   389       for ( 
int j = 0; j < classificationFieldList.size(); ++j )
   391         QDomElement classificationFieldElem = classificationFieldList.
at( j ).toElement();
   392         int fieldNumber = classificationFieldElem.text().toInt();
   393         if ( fieldNumber >= 0 && fieldNumber < fields.
count() )
   395           QDomText fieldName = pft->
dom().createTextNode( fields.
at( fieldNumber ).
name() );
   396           QDomNode nameNode = classificationFieldElem.firstChild();
   397           classificationFieldElem.replaceChild( fieldName, nameNode );
   408   if ( pft->
dom().isNull() )
   411   QDomNode qgis = pft->
dom().firstChildElement( QStringLiteral( 
"qgis" ) );
   415   QDomElement properties = qgis.firstChildElement( QStringLiteral( 
"properties" ) );
   416   if ( properties.isNull() )
   419   QDomElement digitizing = properties.firstChildElement( QStringLiteral( 
"Digitizing" ) );
   420   if ( digitizing.isNull() )
   423   QDomElement tolList = digitizing.firstChildElement( QStringLiteral( 
"LayerSnappingToleranceList" ) );
   424   if ( tolList.isNull() )
   427   QDomElement tolUnitList = digitizing.firstChildElement( QStringLiteral( 
"LayerSnappingToleranceUnitList" ) );
   428   if ( !tolUnitList.isNull() )
   432   for ( 
int i = 0; i < tolList.childNodes().count(); i++ )
   433     units << QStringLiteral( 
"0" );
   436   value.
writeXml( QStringLiteral( 
"LayerSnappingToleranceUnitList" ), digitizing, pft->
dom() );
   442   if ( pft->
dom().isNull() )
   447   QDomNodeList layerItemList = pft->
dom().elementsByTagName( QStringLiteral( 
"LayerItem" ) );
   448   QDomElement currentLayerItemElem;
   449   QString currentLayerId;
   451   for ( 
int i = 0; i < layerItemList.size(); ++i )
   453     currentLayerItemElem = layerItemList.at( i ).toElement();
   454     if ( currentLayerItemElem.isNull() )
   458     currentLayerId = currentLayerItemElem.attribute( QStringLiteral( 
"layerId" ) );
   460     QDomNodeList vectorClassificationList = currentLayerItemElem.elementsByTagName( QStringLiteral( 
"VectorClassificationItem" ) );
   461     QDomElement currentClassificationElem;
   462     for ( 
int j = 0; j < vectorClassificationList.size(); ++j )
   464       currentClassificationElem = vectorClassificationList.at( j ).toElement();
   465       if ( !currentClassificationElem.isNull() )
   467         currentClassificationElem.setAttribute( QStringLiteral( 
"layerId" ), currentLayerId );
   472     QDomNodeList textItemList = currentLayerItemElem.elementsByTagName( QStringLiteral( 
"TextItem" ) );
   473     QDomElement currentTextItem;
   475     for ( 
int j = 0; j < textItemList.size(); ++j )
   477       currentTextItem = textItemList.at( j ).toElement();
   478       if ( currentTextItem.isNull() )
   483       QDomElement classificationElement;
   484       if ( !vectorClassificationList.isEmpty() ) 
   486         classificationElement = pft->
dom().createElement( QStringLiteral( 
"VectorClassificationItem" ) );
   490         classificationElement = pft->
dom().createElement( QStringLiteral( 
"RasterClassificationItem" ) );
   493       classificationElement.setAttribute( QStringLiteral( 
"layerId" ), currentLayerId );
   494       classificationElement.setAttribute( QStringLiteral( 
"text" ), currentTextItem.attribute( QStringLiteral( 
"text" ) ) );
   495       currentLayerItemElem.replaceChild( classificationElement, currentTextItem );
   502   if ( pft->
dom().isNull() )
   510   QDomNodeList layerItemList = pft->
dom().elementsByTagName( QStringLiteral( 
"rasterproperties" ) );
   511   for ( 
int i = 0; i < layerItemList.size(); ++i )
   513     QDomElement rasterPropertiesElem = layerItemList.at( i ).toElement();
   514     QDomNode layerNode = rasterPropertiesElem.parentNode();
   515     QDomElement dataSourceElem = layerNode.firstChildElement( QStringLiteral( 
"datasource" ) );
   516     QDomElement layerNameElem = layerNode.firstChildElement( QStringLiteral( 
"layername" ) );
   520     rasterLayer.
readLayerXml( layerNode.toElement(), context );
   526   QDomNodeList composerMapList = pft->
dom().elementsByTagName( QStringLiteral( 
"ComposerMap" ) );
   527   for ( 
int i = 0; i < composerMapList.size(); ++i )
   529     QDomNodeList gridList = composerMapList.at( i ).toElement().elementsByTagName( QStringLiteral( 
"Grid" ) );
   530     for ( 
int j = 0; j < gridList.size(); ++j )
   532       QDomNodeList annotationList = gridList.at( j ).toElement().elementsByTagName( QStringLiteral( 
"Annotation" ) );
   533       for ( 
int k = 0; k < annotationList.size(); ++k )
   535         QDomElement annotationElem = annotationList.at( k ).toElement();
   538         if ( annotationElem.hasAttribute( QStringLiteral( 
"position" ) ) )
   540           int pos = annotationElem.attribute( QStringLiteral( 
"position" ) ).toInt();
   541           annotationElem.setAttribute( QStringLiteral( 
"leftPosition" ), pos );
   542           annotationElem.setAttribute( QStringLiteral( 
"rightPosition" ), pos );
   543           annotationElem.setAttribute( QStringLiteral( 
"topPosition" ), pos );
   544           annotationElem.setAttribute( QStringLiteral( 
"bottomPosition" ), pos );
   545           annotationElem.removeAttribute( QStringLiteral( 
"position" ) );
   549         if ( annotationElem.hasAttribute( QStringLiteral( 
"direction" ) ) )
   551           int dir = annotationElem.attribute( QStringLiteral( 
"direction" ) ).toInt();
   554             annotationElem.setAttribute( QStringLiteral( 
"leftDirection" ), 0 );
   555             annotationElem.setAttribute( QStringLiteral( 
"rightDirection" ), 0 );
   556             annotationElem.setAttribute( QStringLiteral( 
"topDirection" ), 1 );
   557             annotationElem.setAttribute( QStringLiteral( 
"bottomDirection" ), 1 );
   561             annotationElem.setAttribute( QStringLiteral( 
"leftDirection" ), 1 );
   562             annotationElem.setAttribute( QStringLiteral( 
"rightDirection" ), 1 );
   563             annotationElem.setAttribute( QStringLiteral( 
"topDirection" ), 0 );
   564             annotationElem.setAttribute( QStringLiteral( 
"bottomDirection" ), 0 );
   568             annotationElem.setAttribute( QStringLiteral( 
"leftDirection" ), dir );
   569             annotationElem.setAttribute( QStringLiteral( 
"rightDirection" ), dir );
   570             annotationElem.setAttribute( QStringLiteral( 
"topDirection" ), dir );
   571             annotationElem.setAttribute( QStringLiteral( 
"bottomDirection" ), dir );
   573           annotationElem.removeAttribute( QStringLiteral( 
"direction" ) );
   580   QDomNodeList composerList = pft->
dom().elementsByTagName( QStringLiteral( 
"Composer" ) );
   581   for ( 
int i = 0; i < composerList.size(); ++i )
   583     QDomElement composerElem = composerList.at( i ).toElement();
   586     QDomElement compositionElem = composerElem.firstChildElement( QStringLiteral( 
"Composition" ) );
   587     if ( compositionElem.isNull() )
   592     QDomNodeList composerChildren = composerElem.childNodes();
   594     if ( composerChildren.size() < 1 )
   599     for ( 
int j = composerChildren.size() - 1; j >= 0; --j )
   601       QDomElement childElem = composerChildren.at( j ).toElement();
   602       if ( childElem.tagName() == QLatin1String( 
"Composition" ) )
   607       composerElem.removeChild( childElem );
   608       compositionElem.appendChild( childElem );
   616   QDomNodeList rendererList = pft->
dom().elementsByTagName( QStringLiteral( 
"renderer-v2" ) );
   617   for ( 
int i = 0; i < rendererList.size(); ++i )
   619     QDomNodeList layerList = rendererList.at( i ).toElement().elementsByTagName( QStringLiteral( 
"layer" ) );
   620     for ( 
int j = 0; j < layerList.size(); ++j )
   622       QDomElement layerElem = layerList.at( j ).toElement();
   623       if ( layerElem.attribute( QStringLiteral( 
"class" ) ) == QLatin1String( 
"SimpleFill" ) )
   625         QDomNodeList propList = layerElem.elementsByTagName( QStringLiteral( 
"prop" ) );
   626         for ( 
int k = 0; k < propList.size(); ++k )
   628           QDomElement propElem = propList.at( k ).toElement();
   629           if ( propElem.attribute( QStringLiteral( 
"k" ) ) == QLatin1String( 
"color" ) || propElem.attribute( QStringLiteral( 
"k" ) ) == QLatin1String( 
"color_border" ) )
   631             propElem.setAttribute( QStringLiteral( 
"v" ), propElem.attribute( QStringLiteral( 
"v" ) ).section( 
',', 0, 2 ) + 
",255" );
   644   QDomNodeList composerPictureList = pft->
dom().elementsByTagName( QStringLiteral( 
"ComposerPicture" ) );
   645   for ( 
int i = 0; i < composerPictureList.size(); ++i )
   647     QDomElement picture = composerPictureList.at( i ).toElement();
   648     picture.setAttribute( QStringLiteral( 
"anchorPoint" ), QString::number( 4 ) );
   655   QDomElement propsElem = pft->
dom().firstChildElement( QStringLiteral( 
"qgis" ) ).toElement().firstChildElement( QStringLiteral( 
"properties" ) );
   656   if ( !propsElem.isNull() )
   658     QDomNodeList srsNodes = propsElem.elementsByTagName( QStringLiteral( 
"SpatialRefSys" ) );
   660     QDomElement projElem;
   661     if ( srsNodes.count() > 0 )
   663       srsElem = srsNodes.at( 0 ).toElement();
   664       QDomNodeList projNodes = srsElem.elementsByTagName( QStringLiteral( 
"ProjectionsEnabled" ) );
   665       if ( projNodes.count() == 0 )
   667         projElem = pft->
dom().createElement( QStringLiteral( 
"ProjectionsEnabled" ) );
   668         projElem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"int" ) );
   669         QDomText projText = pft->
dom().createTextNode( QStringLiteral( 
"0" ) );
   670         projElem.appendChild( projText );
   671         srsElem.appendChild( projElem );
   676       srsElem = pft->
dom().createElement( QStringLiteral( 
"SpatialRefSys" ) );
   677       projElem = pft->
dom().createElement( QStringLiteral( 
"ProjectionsEnabled" ) );
   678       projElem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"int" ) );
   679       QDomText projText = pft->
dom().createTextNode( QStringLiteral( 
"0" ) );
   680       projElem.appendChild( projText );
   681       srsElem.appendChild( projElem );
   682       propsElem.appendChild( srsElem );
   688     QDomNodeList canvasNodes = pft->
dom().elementsByTagName( QStringLiteral( 
"mapcanvas" ) );
   689     if ( canvasNodes.count() > 0 )
   691       QDomElement canvasElem = canvasNodes.at( 0 ).toElement();
   692       QDomNodeList canvasSrsNodes = canvasElem.elementsByTagName( QStringLiteral( 
"spatialrefsys" ) );
   693       if ( canvasSrsNodes.count() > 0 )
   695         QDomElement canvasSrsElem = canvasSrsNodes.at( 0 ).toElement();
   700         QDomNodeList proj4Nodes = canvasSrsElem.elementsByTagName( QStringLiteral( 
"proj4" ) );
   701         if ( proj4Nodes.count() > 0 )
   703           QDomElement proj4Node = proj4Nodes.at( 0 ).toElement();
   704           proj = proj4Node.text();
   706         QDomNodeList authidNodes = canvasSrsElem.elementsByTagName( QStringLiteral( 
"authid" ) );
   707         if ( authidNodes.count() > 0 )
   709           QDomElement authidNode = authidNodes.at( 0 ).toElement();
   710           authid = authidNode.text();
   712         QDomNodeList srsidNodes = canvasSrsElem.elementsByTagName( QStringLiteral( 
"srsid" ) );
   713         if ( srsidNodes.count() > 0 )
   715           QDomElement srsidNode = srsidNodes.at( 0 ).toElement();
   716           srsid = srsidNode.text();
   720         QDomNodeList oldProjectProj4Nodes = srsElem.elementsByTagName( QStringLiteral( 
"ProjectCRSProj4String" ) );
   721         for ( 
int i = oldProjectProj4Nodes.count(); i >= 0; --i )
   723           srsElem.removeChild( oldProjectProj4Nodes.at( i ) );
   725         QDomNodeList oldProjectCrsNodes = srsElem.elementsByTagName( QStringLiteral( 
"ProjectCrs" ) );
   726         for ( 
int i = oldProjectCrsNodes.count(); i >= 0; --i )
   728           srsElem.removeChild( oldProjectCrsNodes.at( i ) );
   730         QDomNodeList oldProjectCrsIdNodes = srsElem.elementsByTagName( QStringLiteral( 
"ProjectCRSID" ) );
   731         for ( 
int i = oldProjectCrsIdNodes.count(); i >= 0; --i )
   733           srsElem.removeChild( oldProjectCrsIdNodes.at( i ) );
   735         QDomNodeList projectionsEnabledNodes = srsElem.elementsByTagName( QStringLiteral( 
"ProjectionsEnabled" ) );
   736         for ( 
int i = projectionsEnabledNodes.count(); i >= 0; --i )
   738           srsElem.removeChild( projectionsEnabledNodes.at( i ) );
   741         QDomElement proj4Elem = pft->
dom().createElement( QStringLiteral( 
"ProjectCRSProj4String" ) );
   742         proj4Elem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"QString" ) );
   743         QDomText proj4Text = pft->
dom().createTextNode( proj );
   744         proj4Elem.appendChild( proj4Text );
   745         QDomElement projectCrsElem = pft->
dom().createElement( QStringLiteral( 
"ProjectCrs" ) );
   746         projectCrsElem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"QString" ) );
   747         QDomText projectCrsText = pft->
dom().createTextNode( authid );
   748         projectCrsElem.appendChild( projectCrsText );
   749         QDomElement projectCrsIdElem = pft->
dom().createElement( QStringLiteral( 
"ProjectCRSID" ) );
   750         projectCrsIdElem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"int" ) );
   751         QDomText srsidText = pft->
dom().createTextNode( srsid );
   752         projectCrsIdElem.appendChild( srsidText );
   753         QDomElement projectionsEnabledElem = pft->
dom().createElement( QStringLiteral( 
"ProjectionsEnabled" ) );
   754         projectionsEnabledElem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"int" ) );
   755         QDomText projectionsEnabledText = pft->
dom().createTextNode( QStringLiteral( 
"1" ) );
   756         projectionsEnabledElem.appendChild( projectionsEnabledText );
   757         srsElem.appendChild( proj4Elem );
   758         srsElem.appendChild( projectCrsElem );
   759         srsElem.appendChild( projectCrsIdElem );
   760         srsElem.appendChild( projectionsEnabledElem );
   762         QDomNodeList srsNodes = propsElem.elementsByTagName( QStringLiteral( 
"SpatialRefSys" ) );
   763         for ( 
int i = srsNodes.count(); i >= 0; --i )
   765           propsElem.removeChild( srsNodes.at( i ) );
   767         propsElem.appendChild( srsElem );
   773   QDomNodeList mapLayers = pft->
dom().elementsByTagName( QStringLiteral( 
"maplayer" ) );
   775   for ( 
int mapLayerIndex = 0; mapLayerIndex < mapLayers.count(); ++mapLayerIndex )
   777     QDomElement layerElem = mapLayers.at( mapLayerIndex ).toElement();
   780     QDomElement fieldConfigurationElement = pft->
dom().createElement( QStringLiteral( 
"fieldConfiguration" ) );
   781     layerElem.appendChild( fieldConfigurationElement );
   783     QDomNodeList editTypeNodes = layerElem.namedItem( QStringLiteral( 
"edittypes" ) ).childNodes();
   784     QDomElement constraintExpressionsElem = pft->
dom().createElement( QStringLiteral( 
"constraintExpressions" ) );
   785     layerElem.appendChild( constraintExpressionsElem );
   787     for ( 
int i = 0; i < editTypeNodes.size(); ++i )
   789       QDomNode editTypeNode = editTypeNodes.at( i );
   790       QDomElement editTypeElement = editTypeNode.toElement();
   792       QDomElement fieldElement = pft->
dom().createElement( QStringLiteral( 
"field" ) );
   793       fieldConfigurationElement.appendChild( fieldElement );
   795       QString name = editTypeElement.attribute( QStringLiteral( 
"name" ) );
   796       fieldElement.setAttribute( QStringLiteral( 
"name" ), name );
   797       QDomElement constraintExpressionElem = pft->
dom().createElement( QStringLiteral( 
"constraint" ) );
   798       constraintExpressionElem.setAttribute( QStringLiteral( 
"field" ), name );
   799       constraintExpressionsElem.appendChild( constraintExpressionElem );
   801       QDomElement editWidgetElement = pft->
dom().createElement( QStringLiteral( 
"editWidget" ) );
   802       fieldElement.appendChild( editWidgetElement );
   804       QString ewv2Type = editTypeElement.attribute( QStringLiteral( 
"widgetv2type" ) );
   805       editWidgetElement.setAttribute( QStringLiteral( 
"type" ), ewv2Type );
   807       QDomElement ewv2CfgElem = editTypeElement.namedItem( QStringLiteral( 
"widgetv2config" ) ).toElement();
   809       if ( !ewv2CfgElem.isNull() )
   811         QDomElement editWidgetConfigElement = pft->
dom().createElement( QStringLiteral( 
"config" ) );
   812         editWidgetElement.appendChild( editWidgetConfigElement );
   814         QVariantMap editWidgetConfiguration;
   816         QDomNamedNodeMap configAttrs = ewv2CfgElem.attributes();
   817         for ( 
int configIndex = 0; configIndex < configAttrs.count(); ++configIndex )
   819           QDomAttr configAttr = configAttrs.item( configIndex ).toAttr();
   820           if ( configAttr.name() == QStringLiteral( 
"fieldEditable" ) )
   822             editWidgetConfigElement.setAttribute( QStringLiteral( 
"fieldEditable" ), configAttr.value() );
   824           else if ( configAttr.name() == QStringLiteral( 
"labelOnTop" ) )
   826             editWidgetConfigElement.setAttribute( QStringLiteral( 
"labelOnTop" ), configAttr.value() );
   828           else if ( configAttr.name() == QStringLiteral( 
"notNull" ) )
   830             editWidgetConfigElement.setAttribute( QStringLiteral( 
"notNull" ), configAttr.value() );
   832           else if ( configAttr.name() == QStringLiteral( 
"constraint" ) )
   834             constraintExpressionElem.setAttribute( QStringLiteral( 
"exp" ), configAttr.value() );
   836           else if ( configAttr.name() == QStringLiteral( 
"constraintDescription" ) )
   838             constraintExpressionElem.setAttribute( QStringLiteral( 
"desc" ), configAttr.value() );
   842             editWidgetConfiguration.insert( configAttr.name(), configAttr.value() );
   846         if ( ewv2Type == QStringLiteral( 
"ValueMap" ) )
   848           QDomNodeList configElements = ewv2CfgElem.childNodes();
   850           for ( 
int configIndex = 0; configIndex < configElements.count(); ++configIndex )
   852             QDomElement configElem = configElements.at( configIndex ).toElement();
   853             map.insert( configElem.attribute( QStringLiteral( 
"key" ) ), configElem.attribute( QStringLiteral( 
"value" ) ) );
   855           editWidgetConfiguration.insert( QStringLiteral( 
"map" ), map );
   857         else if ( ewv2Type == QStringLiteral( 
"Photo" ) )
   859           editWidgetElement.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"ExternalResource" ) );
   861           editWidgetConfiguration.insert( QStringLiteral( 
"DocumentViewer" ), 1 );
   862           editWidgetConfiguration.insert( QStringLiteral( 
"DocumentViewerHeight" ), editWidgetConfiguration.value( QStringLiteral( 
"Height" ) ) );
   863           editWidgetConfiguration.insert( QStringLiteral( 
"DocumentViewerWidth" ), editWidgetConfiguration.value( QStringLiteral( 
"Width" ) ) );
   864           editWidgetConfiguration.insert( QStringLiteral( 
"RelativeStorage" ), 1 );
   866         else if ( ewv2Type == QStringLiteral( 
"FileName" ) )
   868           editWidgetElement.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"ExternalResource" ) );
   870           editWidgetConfiguration.insert( QStringLiteral( 
"RelativeStorage" ), 1 );
   872         else if ( ewv2Type == QStringLiteral( 
"WebView" ) )
   874           editWidgetElement.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"ExternalResource" ) );
   876           editWidgetConfiguration.insert( QStringLiteral( 
"DocumentViewerHeight" ), editWidgetConfiguration.value( QStringLiteral( 
"Height" ) ) );
   877           editWidgetConfiguration.insert( QStringLiteral( 
"DocumentViewerWidth" ), editWidgetConfiguration.value( QStringLiteral( 
"Width" ) ) );
   878           editWidgetConfiguration.insert( QStringLiteral( 
"RelativeStorage" ), 1 );
   892   QDomNode noDataNode = rasterPropertiesElem.namedItem( QStringLiteral( 
"mNoDataValue" ) );
   893   QDomElement noDataElement = noDataNode.toElement();
   894   if ( !noDataElement.text().isEmpty() )
   896     QgsDebugMsg( 
"mNoDataValue = " + noDataElement.text() );
   897     QDomElement noDataElem = doc.createElement( QStringLiteral( 
"noData" ) );
   899     QDomElement noDataRangeList = doc.createElement( QStringLiteral( 
"noDataRangeList" ) );
   900     noDataRangeList.setAttribute( QStringLiteral( 
"bandNo" ), 1 );
   902     QDomElement noDataRange = doc.createElement( QStringLiteral( 
"noDataRange" ) );
   903     noDataRange.setAttribute( QStringLiteral( 
"min" ), noDataElement.text() );
   904     noDataRange.setAttribute( QStringLiteral( 
"max" ), noDataElement.text() );
   905     noDataRangeList.appendChild( noDataRange );
   907     noDataElem.appendChild( noDataRangeList );
   909     parentNode.appendChild( noDataElem );
   912   QDomElement rasterRendererElem = doc.createElement( QStringLiteral( 
"rasterrenderer" ) );
   916   rasterRendererElem.setAttribute( QStringLiteral( 
"invertColor" ), QStringLiteral( 
"0" ) );
   917   QDomElement  invertColorElem = rasterPropertiesElem.firstChildElement( QStringLiteral( 
"mInvertColor" ) );
   918   if ( !invertColorElem.isNull() )
   920     if ( invertColorElem.text() == QLatin1String( 
"true" ) )
   922       rasterRendererElem.setAttribute( QStringLiteral( 
"invertColor" ), QStringLiteral( 
"1" ) );
   927   rasterRendererElem.setAttribute( QStringLiteral( 
"opacity" ), QStringLiteral( 
"1" ) );
   928   QDomElement transparencyElem = parentNode.firstChildElement( QStringLiteral( 
"transparencyLevelInt" ) );
   929   if ( !transparencyElem.isNull() )
   931     double transparency = transparencyElem.text().toInt();
   932     rasterRendererElem.setAttribute( QStringLiteral( 
"opacity" ), QString::number( transparency / 255.0 ) );
   936   rasterRendererElem.setAttribute( QStringLiteral( 
"alphaBand" ), -1 );
   939   int grayBand = 
rasterBandNumber( rasterPropertiesElem, QStringLiteral( 
"mGrayBandName" ), rlayer );
   942   QString drawingStyle = rasterPropertiesElem.firstChildElement( QStringLiteral( 
"mDrawingStyle" ) ).text();
   949   if ( drawingStyle == QLatin1String( 
"PalettedColor" ) )
   951     QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( QStringLiteral( 
"customColorRamp" ) );
   952     QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( QStringLiteral( 
"colorRampEntry" ) );
   954     for ( 
int i = 0; i < colorRampEntryList.size(); ++i )
   956       QDomElement colorRampEntryElem = colorRampEntryList.at( i ).toElement();
   957       QString strValue = colorRampEntryElem.attribute( QStringLiteral( 
"value" ) );
   958       double value = strValue.toDouble();
   959       if ( value < 0 || value > 10000 || !
qgsDoubleNear( value, static_cast< int >( value ) ) )
   961         QgsDebugMsg( QStringLiteral( 
"forcing SingleBandPseudoColor value = %1" ).arg( value ) );
   962         drawingStyle = QStringLiteral( 
"SingleBandPseudoColor" );
   968   if ( drawingStyle == QLatin1String( 
"SingleBandGray" ) )
   970     rasterRendererElem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"singlebandgray" ) );
   971     rasterRendererElem.setAttribute( QStringLiteral( 
"grayBand" ), grayBand );
   974   else if ( drawingStyle == QLatin1String( 
"SingleBandPseudoColor" ) )
   976     rasterRendererElem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"singlebandpseudocolor" ) );
   977     rasterRendererElem.setAttribute( QStringLiteral( 
"band" ), grayBand );
   978     QDomElement newRasterShaderElem = doc.createElement( QStringLiteral( 
"rastershader" ) );
   979     QDomElement newColorRampShaderElem = doc.createElement( QStringLiteral( 
"colorrampshader" ) );
   980     newRasterShaderElem.appendChild( newColorRampShaderElem );
   981     rasterRendererElem.appendChild( newRasterShaderElem );
   984     QString colorShadingAlgorithm = rasterPropertiesElem.firstChildElement( QStringLiteral( 
"mColorShadingAlgorithm" ) ).text();
   985     if ( colorShadingAlgorithm == QLatin1String( 
"PseudoColorShader" ) || colorShadingAlgorithm == QLatin1String( 
"FreakOutShader" ) )
   987       newColorRampShaderElem.setAttribute( QStringLiteral( 
"colorRampType" ), QStringLiteral( 
"INTERPOLATED" ) );
   993       double breakSize = ( maxValue - minValue ) / 3;
   995       QStringList colorList;
   996       if ( colorShadingAlgorithm == QLatin1String( 
"FreakOutShader" ) )
   998         colorList << QStringLiteral( 
"#ff00ff" ) << QStringLiteral( 
"#00ffff" ) << QStringLiteral( 
"#ff0000" ) << QStringLiteral( 
"#00ff00" );
  1002         colorList << QStringLiteral( 
"#0000ff" ) << QStringLiteral( 
"#00ffff" ) << QStringLiteral( 
"#ffff00" ) << QStringLiteral( 
"#ff0000" );
  1004       QStringList::const_iterator colorIt = colorList.constBegin();
  1005       double boundValue = minValue;
  1006       for ( ; colorIt != colorList.constEnd(); ++colorIt )
  1008         QDomElement newItemElem = doc.createElement( QStringLiteral( 
"item" ) );
  1009         newItemElem.setAttribute( QStringLiteral( 
"value" ), QString::number( boundValue ) );
  1010         newItemElem.setAttribute( QStringLiteral( 
"label" ), QString::number( boundValue ) );
  1011         newItemElem.setAttribute( QStringLiteral( 
"color" ), *colorIt );
  1012         newColorRampShaderElem.appendChild( newItemElem );
  1013         boundValue += breakSize;
  1016     else if ( colorShadingAlgorithm == QLatin1String( 
"ColorRampShader" ) )
  1018       QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( QStringLiteral( 
"customColorRamp" ) );
  1019       QString type = customColorRampElem.firstChildElement( QStringLiteral( 
"colorRampType" ) ).text();
  1020       newColorRampShaderElem.setAttribute( QStringLiteral( 
"colorRampType" ), type );
  1021       QDomNodeList colorNodeList = customColorRampElem.elementsByTagName( QStringLiteral( 
"colorRampEntry" ) );
  1023       QString value, label;
  1025       int red, green, blue;
  1026       QDomElement currentItemElem;
  1027       for ( 
int i = 0; i < colorNodeList.size(); ++i )
  1029         currentItemElem = colorNodeList.at( i ).toElement();
  1030         value = currentItemElem.attribute( QStringLiteral( 
"value" ) );
  1031         label = currentItemElem.attribute( QStringLiteral( 
"label" ) );
  1032         red = currentItemElem.attribute( QStringLiteral( 
"red" ) ).toInt();
  1033         green = currentItemElem.attribute( QStringLiteral( 
"green" ) ).toInt();
  1034         blue = currentItemElem.attribute( QStringLiteral( 
"blue" ) ).toInt();
  1035         newColor = QColor( red, green, blue );
  1036         QDomElement newItemElem = doc.createElement( QStringLiteral( 
"item" ) );
  1037         newItemElem.setAttribute( QStringLiteral( 
"value" ), value );
  1038         newItemElem.setAttribute( QStringLiteral( 
"label" ), label );
  1039         newItemElem.setAttribute( QStringLiteral( 
"color" ), newColor.name() );
  1040         newColorRampShaderElem.appendChild( newItemElem );
  1044   else if ( drawingStyle == QLatin1String( 
"PalettedColor" ) )
  1046     rasterRendererElem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"paletted" ) );
  1047     rasterRendererElem.setAttribute( QStringLiteral( 
"band" ), grayBand );
  1048     QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( QStringLiteral( 
"customColorRamp" ) );
  1049     QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( QStringLiteral( 
"colorRampEntry" ) );
  1050     QDomElement newColorPaletteElem = doc.createElement( QStringLiteral( 
"colorPalette" ) );
  1056     QDomElement colorRampEntryElem;
  1057     for ( 
int i = 0; i < colorRampEntryList.size(); ++i )
  1059       colorRampEntryElem = colorRampEntryList.at( i ).toElement();
  1060       QDomElement newPaletteElem = doc.createElement( QStringLiteral( 
"paletteEntry" ) );
  1061       value = 
static_cast< int >( colorRampEntryElem.attribute( QStringLiteral( 
"value" ) ).toDouble() );
  1062       newPaletteElem.setAttribute( QStringLiteral( 
"value" ), value );
  1063       red = colorRampEntryElem.attribute( QStringLiteral( 
"red" ) ).toInt();
  1064       green = colorRampEntryElem.attribute( QStringLiteral( 
"green" ) ).toInt();
  1065       blue = colorRampEntryElem.attribute( QStringLiteral( 
"blue" ) ).toInt();
  1066       newPaletteElem.setAttribute( QStringLiteral( 
"color" ), QColor( red, green, blue ).name() );
  1067       QString label = colorRampEntryElem.attribute( QStringLiteral( 
"label" ) );
  1068       if ( !label.isEmpty() )
  1070         newPaletteElem.setAttribute( QStringLiteral( 
"label" ), label );
  1072       newColorPaletteElem.appendChild( newPaletteElem );
  1074     rasterRendererElem.appendChild( newColorPaletteElem );
  1076   else if ( drawingStyle == QLatin1String( 
"MultiBandColor" ) )
  1078     rasterRendererElem.setAttribute( QStringLiteral( 
"type" ), QStringLiteral( 
"multibandcolor" ) );
  1081     int redBand = 
rasterBandNumber( rasterPropertiesElem, QStringLiteral( 
"mRedBandName" ), rlayer );
  1082     int greenBand = 
rasterBandNumber( rasterPropertiesElem, QStringLiteral( 
"mGreenBandName" ), rlayer );
  1083     int blueBand = 
rasterBandNumber( rasterPropertiesElem, QStringLiteral( 
"mBlueBandName" ), rlayer );
  1084     rasterRendererElem.setAttribute( QStringLiteral( 
"redBand" ), redBand );
  1085     rasterRendererElem.setAttribute( QStringLiteral( 
"greenBand" ), greenBand );
  1086     rasterRendererElem.setAttribute( QStringLiteral( 
"blueBand" ), blueBand );
  1096   if ( !parentNode.isNull() )
  1098     parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem );
  1109   return mCurrentVersion;
  1120   QDomElement rasterBandElem = rasterPropertiesElem.firstChildElement( bandName );
  1121   if ( !rasterBandElem.isNull() )
  1123     QRegExp re( 
"(\\d+)" );
  1125     if ( re.indexIn( rasterBandElem.text() ) >= 0 )
  1127       return re.cap( 1 ).toInt();
  1135   if ( rasterproperties.isNull() || rendererElem.isNull() )
  1140   double minimumValue = 0;
  1141   double maximumValue = 0;
  1142   QDomElement contrastMinMaxElem = rasterproperties.firstChildElement( QStringLiteral( 
"contrastEnhancementMinMaxValues" ) );
  1143   if ( contrastMinMaxElem.isNull() )
  1148   QDomElement contrastEnhancementAlgorithmElem = rasterproperties.firstChildElement( QStringLiteral( 
"mContrastEnhancementAlgorithm" ) );
  1149   if ( contrastEnhancementAlgorithmElem.isNull() )
  1155   int algorithmEnum = 0;
  1156   QString algorithmString = contrastEnhancementAlgorithmElem.text();
  1157   if ( algorithmString == QLatin1String( 
"StretchToMinimumMaximum" ) )
  1161   else if ( algorithmString == QLatin1String( 
"StretchAndClipToMinimumMaximum" ) )
  1165   else if ( algorithmString == QLatin1String( 
"ClipToMinimumMaximum" ) )
  1169   else if ( algorithmString == QLatin1String( 
"UserDefinedEnhancement" ) )
  1174   QDomNodeList minMaxEntryList = contrastMinMaxElem.elementsByTagName( QStringLiteral( 
"minMaxEntry" ) );
  1175   QStringList enhancementNameList;
  1176   if ( minMaxEntryList.size() == 1 )
  1178     enhancementNameList << QStringLiteral( 
"contrastEnhancement" );
  1180   if ( minMaxEntryList.size() == 3 )
  1182     enhancementNameList << QStringLiteral( 
"redContrastEnhancement" ) << QStringLiteral( 
"greenContrastEnhancement" ) << QStringLiteral( 
"blueContrastEnhancement" );
  1184   if ( minMaxEntryList.size() > enhancementNameList.size() )
  1189   QDomElement minMaxEntryElem;
  1190   for ( 
int i = 0; i < minMaxEntryList.size(); ++i )
  1192     minMaxEntryElem = minMaxEntryList.at( i ).toElement();
  1193     QDomElement minElem = minMaxEntryElem.firstChildElement( QStringLiteral( 
"min" ) );
  1194     if ( minElem.isNull() )
  1198     minimumValue = minElem.text().toDouble();
  1200     QDomElement maxElem = minMaxEntryElem.firstChildElement( QStringLiteral( 
"max" ) );
  1201     if ( maxElem.isNull() )
  1205     maximumValue = maxElem.text().toDouble();
  1207     QDomElement newContrastEnhancementElem = doc.createElement( enhancementNameList.at( i ) );
  1208     QDomElement newMinValElem = doc.createElement( QStringLiteral( 
"minValue" ) );
  1209     QDomText minText = doc.createTextNode( QString::number( minimumValue ) );
  1210     newMinValElem.appendChild( minText );
  1211     newContrastEnhancementElem.appendChild( newMinValElem );
  1212     QDomElement newMaxValElem = doc.createElement( QStringLiteral( 
"maxValue" ) );
  1213     QDomText maxText = doc.createTextNode( QString::number( maximumValue ) );
  1214     newMaxValElem.appendChild( maxText );
  1215     newContrastEnhancementElem.appendChild( newMaxValElem );
  1217     QDomElement newAlgorithmElem = doc.createElement( QStringLiteral( 
"algorithm" ) );
  1218     QDomText newAlgorithmText = doc.createTextNode( QString::number( algorithmEnum ) );
  1219     newAlgorithmElem.appendChild( newAlgorithmText );
  1220     newContrastEnhancementElem.appendChild( newAlgorithmElem );
  1222     rendererElem.appendChild( newContrastEnhancementElem );
 
The class is used as a container of context for various read/write operations on other objects...
 
void setPathResolver(const QgsPathResolver &resolver)
Sets up path resolver for conversion between relative and absolute paths. 
 
bool readLayerXml(const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags=nullptr)
Sets state from DOM document. 
 
Setting options for loading vector layers. 
 
Represents a raster layer. 
 
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference) 
 
double maximumValue
The maximum cell value in the raster band. 
 
Container of fields for a vector layer. 
 
bool isValid() const
Returns the status of the layer. 
 
bool isNull() const
Returns true if this is a NULL project version. 
 
int count() const
Returns number of items. 
 
Project property value node, contains a QgsProjectPropertyKey's value. 
 
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1) 
 
QgsRasterDataProvider * dataProvider() override
Returns the source data provider. 
 
The RasterBandStats struct is a container for statistics about a single raster band. 
 
QgsFields fields() const override=0
Returns the fields associated with this data provider. 
 
A class to describe the version of a project. 
 
Contains information about the context in which a coordinate transform is executed. 
 
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0, QgsRasterBlockFeedback *feedback=nullptr)
Returns the band statistics. 
 
bool writeXml(const QString &nodeName, QDomElement &element, QDomDocument &document) override
Writes the property hierarchy to a specified DOM element. 
 
static QgsProject * instance()
Returns the QgsProject singleton instance. 
 
double minimumValue
The minimum cell value in the raster band. 
 
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr. 
 
This is the base class for vector data providers. 
 
Represents a vector layer which manages a vector based data sets. 
 
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.