QGIS API Documentation  2.99.0-Master (a18066b)
qgsprojectfiletransform.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprojectfiletransform.cpp - description
3  -------------------
4  begin : Sun 15 dec 2007
5  copyright : (C) 2007 by Magnus Homann
6  email : magnus at homann.se
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
20 #include "qgsprojectversion.h"
21 #include "qgslogger.h"
22 #include "qgsrasterlayer.h"
23 #include "qgsvectordataprovider.h"
24 #include "qgsvectorlayer.h"
25 #include <QTextStream>
26 #include <QDomDocument>
27 #include <QPrinter> //to find out screen resolution
28 #include <cstdlib>
29 #include "qgspathresolver.h"
30 #include "qgsproject.h"
31 #include "qgsprojectproperty.h"
32 #include "qgsrasterbandstats.h"
33 #include "qgsrasterdataprovider.h"
34 #include "qgsxmlutils.h"
35 
37 
38 
39 QgsProjectFileTransform::TransformItem QgsProjectFileTransform::sTransformers[] =
40 {
41  {PFV( 0, 8, 0 ), PFV( 0, 8, 1 ), &QgsProjectFileTransform::transformNull},
42  {PFV( 0, 8, 1 ), PFV( 0, 9, 0 ), &QgsProjectFileTransform::transform081to090},
43  {PFV( 0, 9, 0 ), PFV( 0, 9, 1 ), &QgsProjectFileTransform::transformNull},
44  {PFV( 0, 9, 1 ), PFV( 0, 10, 0 ), &QgsProjectFileTransform::transform091to0100},
45  // Following line is a hack that takes us straight from 0.9.2 to 0.11.0
46  // due to an unknown bug in migrating 0.9.2 files which we didn't pursue (TS & GS)
47  {PFV( 0, 9, 2 ), PFV( 0, 11, 0 ), &QgsProjectFileTransform::transformNull},
48  {PFV( 0, 10, 0 ), PFV( 0, 11, 0 ), &QgsProjectFileTransform::transform0100to0110},
49  {PFV( 0, 11, 0 ), PFV( 1, 0, 0 ), &QgsProjectFileTransform::transform0110to1000},
50  {PFV( 1, 0, 0 ), PFV( 1, 1, 0 ), &QgsProjectFileTransform::transformNull},
51  {PFV( 1, 0, 2 ), PFV( 1, 1, 0 ), &QgsProjectFileTransform::transformNull},
52  {PFV( 1, 1, 0 ), PFV( 1, 2, 0 ), &QgsProjectFileTransform::transform1100to1200},
53  {PFV( 1, 2, 0 ), PFV( 1, 3, 0 ), &QgsProjectFileTransform::transformNull},
54  {PFV( 1, 3, 0 ), PFV( 1, 4, 0 ), &QgsProjectFileTransform::transformNull},
55  {PFV( 1, 4, 0 ), PFV( 1, 5, 0 ), &QgsProjectFileTransform::transform1400to1500},
56  {PFV( 1, 5, 0 ), PFV( 1, 6, 0 ), &QgsProjectFileTransform::transformNull},
57  {PFV( 1, 6, 0 ), PFV( 1, 7, 0 ), &QgsProjectFileTransform::transformNull},
58  {PFV( 1, 7, 0 ), PFV( 1, 8, 0 ), &QgsProjectFileTransform::transformNull},
59  {PFV( 1, 8, 0 ), PFV( 1, 9, 0 ), &QgsProjectFileTransform::transform1800to1900},
60  {PFV( 1, 9, 0 ), PFV( 2, 0, 0 ), &QgsProjectFileTransform::transformNull},
61  {PFV( 2, 0, 0 ), PFV( 2, 1, 0 ), &QgsProjectFileTransform::transformNull},
62  {PFV( 2, 1, 0 ), PFV( 2, 2, 0 ), &QgsProjectFileTransform::transformNull},
63  {PFV( 2, 2, 0 ), PFV( 2, 3, 0 ), &QgsProjectFileTransform::transform2200to2300},
64  // A transformer with a NULL from version means that it should be run when upgrading
65  // from any version and will take care that it's not going to cause trouble if it's
66  // run several times on the same file.
67  {PFV(), PFV( 2, 99, 0 ), &QgsProjectFileTransform::transform2990},
68 };
69 
71 {
72  Q_UNUSED( newVersion );
73  bool returnValue = false;
74 
75  if ( !mDom.isNull() )
76  {
77  for ( std::size_t i = 0; i < sizeof( sTransformers ) / sizeof( TransformItem ); i++ )
78  {
79  const TransformItem& transformer = sTransformers[i];
80  if ( transformer.from == mCurrentVersion || transformer.from.isNull() )
81  {
82  // Run the transformer, and update the revision in every case
83  ( this->*( transformer.transformFunc ) )();
84  mCurrentVersion = transformer.to;
85  returnValue = true;
86  }
87  }
88  }
89  return returnValue;
90 }
91 
93 {
94  QgsDebugMsg( QString( "Current project file version is %1.%2.%3" )
95  .arg( mCurrentVersion.majorVersion() )
96  .arg( mCurrentVersion.minorVersion() )
97  .arg( mCurrentVersion.subVersion() ) );
98 #ifdef QGISDEBUG
99  // Using QgsDebugMsg() didn't print the entire mDom...
100  std::cout << mDom.toString( 2 ).toLatin1().constData(); // OK
101 #endif
102 }
103 
104 /*
105  * Transformers below!
106  */
107 
108 void QgsProjectFileTransform::transform081to090()
109 {
110  QgsDebugMsg( "Entering..." );
111  if ( ! mDom.isNull() )
112  {
113  // Start with inserting a mapcanvas element and populate it
114 
115  QDomElement mapCanvas; // A null element.
116 
117  // there should only be one <qgis>
118  QDomNode qgis = mDom.firstChildElement( QStringLiteral( "qgis" ) );
119  if ( ! qgis.isNull() )
120  {
121  QgsDebugMsg( "Populating new mapcanvas" );
122 
123  // Create a mapcanvas
124  mapCanvas = mDom.createElement( QStringLiteral( "mapcanvas" ) );
125  // Append mapcanvas to parent 'qgis'.
126  qgis.appendChild( mapCanvas );
127  // Re-parent units
128  mapCanvas.appendChild( qgis.namedItem( QStringLiteral( "units" ) ) );
129  // Re-parent extent
130  mapCanvas.appendChild( qgis.namedItem( QStringLiteral( "extent" ) ) );
131 
132  // See if we can find if projection is on.
133 
134  QDomElement properties = qgis.firstChildElement( QStringLiteral( "properties" ) );
135  QDomElement spatial = properties.firstChildElement( QStringLiteral( "SpatialRefSys" ) );
136  QDomElement hasCrsTransformEnabled = spatial.firstChildElement( QStringLiteral( "ProjectionsEnabled" ) );
137  // Type is 'int', and '1' if on.
138  // Create an element
139  QDomElement projection = mDom.createElement( QStringLiteral( "projections" ) );
140  QgsDebugMsg( QString( "Projection flag: " ) + hasCrsTransformEnabled.text() );
141  // Set flag from ProjectionsEnabled
142  projection.appendChild( mDom.createTextNode( hasCrsTransformEnabled.text() ) );
143  // Set new element as child of <mapcanvas>
144  mapCanvas.appendChild( projection );
145 
146  }
147 
148 
149  // Transforming coordinate-transforms
150  // Create a list of all map layers
151  QDomNodeList mapLayers = mDom.elementsByTagName( QStringLiteral( "maplayer" ) );
152  bool doneDestination = false;
153  for ( int i = 0; i < mapLayers.count(); i++ )
154  {
155  QDomNode mapLayer = mapLayers.item( i );
156  // Find the coordinatetransform
157  QDomNode coordinateTransform = mapLayer.namedItem( QStringLiteral( "coordinatetransform" ) );
158  // Find the sourcesrs
159  QDomNode sourceCrs = coordinateTransform.namedItem( QStringLiteral( "sourcesrs" ) );
160  // Rename to srs
161  sourceCrs.toElement().setTagName( QStringLiteral( "srs" ) );
162  // Re-parent to maplayer
163  mapLayer.appendChild( sourceCrs );
164  // Re-move coordinatetransform
165  // Take the destination CRS of the first layer and use for mapcanvas projection
166  if ( ! doneDestination )
167  {
168  // Use destination CRS from the last layer
169  QDomNode destinationCRS = coordinateTransform.namedItem( QStringLiteral( "destinationsrs" ) );
170  // Re-parent the destination CRS to the mapcanvas
171  // If mapcanvas wasn't set, nothing will happen.
172  mapCanvas.appendChild( destinationCRS );
173  // Only do this once
174  doneDestination = true;
175  }
176  mapLayer.removeChild( coordinateTransform );
177  //QDomNode id = mapLayer.namedItem("id");
178  //QgsDebugMsg(QString("Found maplayer ") + id.toElement().text());
179 
180  }
181 
182  // Set the flag 'visible' to match the status of 'checked'
183  QDomNodeList legendLayerFiles = mDom.elementsByTagName( QStringLiteral( "legendlayerfile" ) );
184  QgsDebugMsg( QString( "Legend layer file entries: " ) + QString::number( legendLayerFiles.count() ) );
185  for ( int i = 0; i < mapLayers.count(); i++ )
186  {
187  // Get one maplayer element from list
188  QDomElement mapLayer = mapLayers.item( i ).toElement();
189  // Find it's id.
190  QString id = mapLayer.firstChildElement( QStringLiteral( "id" ) ).text();
191  QgsDebugMsg( QString( "Handling layer " + id ) );
192  // Now, look it up in legend
193  for ( int j = 0; j < legendLayerFiles.count(); j++ )
194  {
195  QDomElement legendLayerFile = legendLayerFiles.item( j ).toElement();
196  if ( id == legendLayerFile.attribute( QStringLiteral( "layerid" ) ) )
197  {
198  // Found a the legend layer that matches the maplayer
199  QgsDebugMsg( "Found matching id" );
200 
201  // Set visible flag from maplayer to legendlayer
202  legendLayerFile.setAttribute( QStringLiteral( "visible" ), mapLayer.attribute( QStringLiteral( "visible" ) ) );
203 
204  // Set overview flag from maplayer to legendlayer
205  legendLayerFile.setAttribute( QStringLiteral( "isInOverview" ), mapLayer.attribute( QStringLiteral( "showInOverviewFlag" ) ) );
206  }
207  }
208  }
209  }
210 }
211 
212 void QgsProjectFileTransform::transform091to0100()
213 {
214  if ( ! mDom.isNull() )
215  {
216  // Insert transforms here!
217  QDomNodeList rasterPropertyList = mDom.elementsByTagName( QStringLiteral( "rasterproperties" ) );
218  QgsDebugMsg( QString( "Raster properties file entries: " ) + QString::number( rasterPropertyList.count() ) );
219  for ( int i = 0; i < rasterPropertyList.count(); i++ )
220  {
221  // Get one rasterproperty element from list, and rename the sub-properties.
222  QDomNode rasterProperty = rasterPropertyList.item( i );
223  // rasterProperty.namedItem("").toElement().setTagName("");
224 
225  rasterProperty.namedItem( QStringLiteral( "stdDevsToPlotDouble" ) ).toElement().setTagName( QStringLiteral( "mStandardDeviations" ) );
226 
227  rasterProperty.namedItem( QStringLiteral( "invertHistogramFlag" ) ).toElement().setTagName( QStringLiteral( "mInvertPixelsFlag" ) );
228  rasterProperty.namedItem( QStringLiteral( "showDebugOverLayFlag" ) ).toElement().setTagName( QStringLiteral( "mDebugOverLayFlag" ) );
229 
230  rasterProperty.namedItem( QStringLiteral( "redBandNameQString" ) ).toElement().setTagName( QStringLiteral( "mRedBandName" ) );
231  rasterProperty.namedItem( QStringLiteral( "blueBandNameQString" ) ).toElement().setTagName( QStringLiteral( "mBlueBandName" ) );
232  rasterProperty.namedItem( QStringLiteral( "greenBandNameQString" ) ).toElement().setTagName( QStringLiteral( "mGreenBandName" ) );
233  rasterProperty.namedItem( QStringLiteral( "grayBandNameQString" ) ).toElement().setTagName( QStringLiteral( "mGrayBandName" ) );
234  }
235 
236  // Changing symbol size for hard: symbols
237  QDomNodeList symbolPropertyList = mDom.elementsByTagName( QStringLiteral( "symbol" ) );
238  for ( int i = 0; i < symbolPropertyList.count(); i++ )
239  {
240  // Get the <poinmtsymbol> to check for 'hard:' for each <symbol>
241  QDomNode symbolProperty = symbolPropertyList.item( i );
242 
243  QDomElement pointSymbol = symbolProperty.firstChildElement( QStringLiteral( "pointsymbol" ) );
244  if ( pointSymbol.text().startsWith( QLatin1String( "hard:" ) ) )
245  {
246  // Get pointsize and line width
247  int lineWidth = symbolProperty.firstChildElement( QStringLiteral( "outlinewidth" ) ).text().toInt();
248  int pointSize = symbolProperty.firstChildElement( QStringLiteral( "pointsize" ) ).text().toInt();
249  // Just a precaution, checking for 0
250  if ( pointSize != 0 )
251  {
252  // int r = (s-2*lw)/2-1 --> 2r = (s-2*lw)-2 --> 2r+2 = s-2*lw
253  // --> 2r+2+2*lw = s
254  // where '2r' is the old size.
255  pointSize = pointSize + 2 + 2 * lineWidth;
256  QgsDebugMsg( QString( "Setting point size to %1" ).arg( pointSize ) );
257  QDomElement newPointSizeProperty = mDom.createElement( QStringLiteral( "pointsize" ) );
258  QDomText newPointSizeTxt = mDom.createTextNode( QString::number( pointSize ) );
259  newPointSizeProperty.appendChild( newPointSizeTxt );
260  symbolProperty.replaceChild( newPointSizeProperty, pointSymbol );
261  }
262  }
263  }
264 
265  }
266 }
267 
268 void QgsProjectFileTransform::transform0100to0110()
269 {
270  if ( ! mDom.isNull() )
271  {
272 #ifndef QT_NO_PRINTER
273  //Change 'outlinewidth' in QgsSymbol
274  QPrinter myPrinter( QPrinter::ScreenResolution );
275  int screenDpi = myPrinter.resolution();
276  double widthScaleFactor = 25.4 / screenDpi;
277 
278  QDomNodeList outlineWidthList = mDom.elementsByTagName( QStringLiteral( "outlinewidth" ) );
279  for ( int i = 0; i < outlineWidthList.size(); ++i )
280  {
281  //calculate new width
282  QDomElement currentOutlineElem = outlineWidthList.at( i ).toElement();
283  double outlineWidth = currentOutlineElem.text().toDouble();
284  outlineWidth *= widthScaleFactor;
285 
286  //replace old text node
287  QDomNode outlineTextNode = currentOutlineElem.firstChild();
288  QDomText newOutlineText = mDom.createTextNode( QString::number( outlineWidth ) );
289  currentOutlineElem.replaceChild( newOutlineText, outlineTextNode );
290 
291  }
292 
293  //Change 'pointsize' in QgsSymbol
294  QDomNodeList pointSizeList = mDom.elementsByTagName( QStringLiteral( "pointsize" ) );
295  for ( int i = 0; i < pointSizeList.size(); ++i )
296  {
297  //calculate new size
298  QDomElement currentPointSizeElem = pointSizeList.at( i ).toElement();
299  double pointSize = currentPointSizeElem.text().toDouble();
300  pointSize *= widthScaleFactor;
301 
302  //replace old text node
303  QDomNode pointSizeTextNode = currentPointSizeElem.firstChild();
304  QDomText newPointSizeText = mDom.createTextNode( QString::number( static_cast< int >( pointSize ) ) );
305  currentPointSizeElem.replaceChild( newPointSizeText, pointSizeTextNode );
306  }
307 #endif
308  }
309 }
310 
311 void QgsProjectFileTransform::transform0110to1000()
312 {
313  if ( ! mDom.isNull() )
314  {
315  QDomNodeList layerList = mDom.elementsByTagName( QStringLiteral( "maplayer" ) );
316  for ( int i = 0; i < layerList.size(); ++i )
317  {
318  QDomElement layerElem = layerList.at( i ).toElement();
319  QString typeString = layerElem.attribute( QStringLiteral( "type" ) );
320  if ( typeString != QLatin1String( "vector" ) )
321  {
322  continue;
323  }
324 
325  //datasource
326  QDomNode dataSourceNode = layerElem.namedItem( QStringLiteral( "datasource" ) );
327  if ( dataSourceNode.isNull() )
328  {
329  return;
330  }
331  QString dataSource = dataSourceNode.toElement().text();
332 
333  //provider key
334  QDomNode providerNode = layerElem.namedItem( QStringLiteral( "provider" ) );
335  if ( providerNode.isNull() )
336  {
337  return;
338  }
339  QString providerKey = providerNode.toElement().text();
340 
341  //create the layer to get the provider for int->fieldName conversion
342  QgsVectorLayer* layer = new QgsVectorLayer( dataSource, QLatin1String( "" ), providerKey, false );
343  if ( !layer->isValid() )
344  {
345  delete layer;
346  return;
347  }
348 
349  QgsVectorDataProvider* provider = layer->dataProvider();
350  if ( !provider )
351  {
352  return;
353  }
354  QgsFields fields = provider->fields();
355 
356  //read classificationfield
357  QDomNodeList classificationFieldList = layerElem.elementsByTagName( QStringLiteral( "classificationfield" ) );
358  for ( int j = 0; j < classificationFieldList.size(); ++j )
359  {
360  QDomElement classificationFieldElem = classificationFieldList.at( j ).toElement();
361  int fieldNumber = classificationFieldElem.text().toInt();
362  if ( fieldNumber >= 0 && fieldNumber < fields.count() )
363  {
364  QDomText fieldName = mDom.createTextNode( fields.at( fieldNumber ).name() );
365  QDomNode nameNode = classificationFieldElem.firstChild();
366  classificationFieldElem.replaceChild( fieldName, nameNode );
367  }
368  }
369 
370  }
371  }
372 }
373 
374 void QgsProjectFileTransform::transform1100to1200()
375 {
376  QgsDebugMsg( "Entering..." );
377  if ( mDom.isNull() )
378  return;
379 
380  QDomNode qgis = mDom.firstChildElement( QStringLiteral( "qgis" ) );
381  if ( qgis.isNull() )
382  return;
383 
384  QDomElement properties = qgis.firstChildElement( QStringLiteral( "properties" ) );
385  if ( properties.isNull() )
386  return;
387 
388  QDomElement digitizing = properties.firstChildElement( QStringLiteral( "Digitizing" ) );
389  if ( digitizing.isNull() )
390  return;
391 
392  QDomElement tolList = digitizing.firstChildElement( QStringLiteral( "LayerSnappingToleranceList" ) );
393  if ( tolList.isNull() )
394  return;
395 
396  QDomElement tolUnitList = digitizing.firstChildElement( QStringLiteral( "LayerSnappingToleranceUnitList" ) );
397  if ( !tolUnitList.isNull() )
398  return;
399 
400  QStringList units;
401  for ( int i = 0; i < tolList.childNodes().count(); i++ )
402  units << QStringLiteral( "0" );
403 
404  QgsProjectPropertyValue value( units );
405  value.writeXml( QStringLiteral( "LayerSnappingToleranceUnitList" ), digitizing, mDom );
406 }
407 
408 void QgsProjectFileTransform::transform1400to1500()
409 {
410  //Adapt the XML description of the composer legend model to version 1.5
411  if ( mDom.isNull() )
412  {
413  return;
414  }
415  //Add layer id to <VectorClassificationItem>
416  QDomNodeList layerItemList = mDom.elementsByTagName( QStringLiteral( "LayerItem" ) );
417  QDomElement currentLayerItemElem;
418  QString currentLayerId;
419 
420  for ( int i = 0; i < layerItemList.size(); ++i )
421  {
422  currentLayerItemElem = layerItemList.at( i ).toElement();
423  if ( currentLayerItemElem.isNull() )
424  {
425  continue;
426  }
427  currentLayerId = currentLayerItemElem.attribute( QStringLiteral( "layerId" ) );
428 
429  QDomNodeList vectorClassificationList = currentLayerItemElem.elementsByTagName( QStringLiteral( "VectorClassificationItem" ) );
430  QDomElement currentClassificationElem;
431  for ( int j = 0; j < vectorClassificationList.size(); ++j )
432  {
433  currentClassificationElem = vectorClassificationList.at( j ).toElement();
434  if ( !currentClassificationElem.isNull() )
435  {
436  currentClassificationElem.setAttribute( QStringLiteral( "layerId" ), currentLayerId );
437  }
438  }
439 
440  //replace the text items with VectorClassification or RasterClassification items
441  QDomNodeList textItemList = currentLayerItemElem.elementsByTagName( QStringLiteral( "TextItem" ) );
442  QDomElement currentTextItem;
443 
444  for ( int j = 0; j < textItemList.size(); ++j )
445  {
446  currentTextItem = textItemList.at( j ).toElement();
447  if ( currentTextItem.isNull() )
448  {
449  continue;
450  }
451 
452  QDomElement classificationElement;
453  if ( !vectorClassificationList.isEmpty() ) //we guess it is a vector layer
454  {
455  classificationElement = mDom.createElement( QStringLiteral( "VectorClassificationItem" ) );
456  }
457  else
458  {
459  classificationElement = mDom.createElement( QStringLiteral( "RasterClassificationItem" ) );
460  }
461 
462  classificationElement.setAttribute( QStringLiteral( "layerId" ), currentLayerId );
463  classificationElement.setAttribute( QStringLiteral( "text" ), currentTextItem.attribute( QStringLiteral( "text" ) ) );
464  currentLayerItemElem.replaceChild( classificationElement, currentTextItem );
465  }
466  }
467 }
468 
469 void QgsProjectFileTransform::transform1800to1900()
470 {
471  if ( mDom.isNull() )
472  {
473  return;
474  }
475 
476  QDomNodeList layerItemList = mDom.elementsByTagName( QStringLiteral( "rasterproperties" ) );
477  for ( int i = 0; i < layerItemList.size(); ++i )
478  {
479  QDomElement rasterPropertiesElem = layerItemList.at( i ).toElement();
480  QDomNode layerNode = rasterPropertiesElem.parentNode();
481  QDomElement dataSourceElem = layerNode.firstChildElement( QStringLiteral( "datasource" ) );
482  QDomElement layerNameElem = layerNode.firstChildElement( QStringLiteral( "layername" ) );
483  QgsRasterLayer rasterLayer;
484  // TODO: We have to use more data from project file to read the layer it correctly,
485  // OTOH, we should not read it until it was converted
486  rasterLayer.readLayerXml( layerNode.toElement(), QgsProject::instance()->pathResolver() );
487  convertRasterProperties( mDom, layerNode, rasterPropertiesElem, &rasterLayer );
488  }
489 
490  //composer: replace mGridAnnotationPosition with mLeftGridAnnotationPosition & co.
491  // and mGridAnnotationDirection with mLeftGridAnnotationDirection & co.
492  QDomNodeList composerMapList = mDom.elementsByTagName( QStringLiteral( "ComposerMap" ) );
493  for ( int i = 0; i < composerMapList.size(); ++i )
494  {
495  QDomNodeList gridList = composerMapList.at( i ).toElement().elementsByTagName( QStringLiteral( "Grid" ) );
496  for ( int j = 0; j < gridList.size(); ++j )
497  {
498  QDomNodeList annotationList = gridList.at( j ).toElement().elementsByTagName( QStringLiteral( "Annotation" ) );
499  for ( int k = 0; k < annotationList.size(); ++k )
500  {
501  QDomElement annotationElem = annotationList.at( k ).toElement();
502 
503  //position
504  if ( annotationElem.hasAttribute( QStringLiteral( "position" ) ) )
505  {
506  int pos = annotationElem.attribute( QStringLiteral( "position" ) ).toInt();
507  annotationElem.setAttribute( QStringLiteral( "leftPosition" ), pos );
508  annotationElem.setAttribute( QStringLiteral( "rightPosition" ), pos );
509  annotationElem.setAttribute( QStringLiteral( "topPosition" ), pos );
510  annotationElem.setAttribute( QStringLiteral( "bottomPosition" ), pos );
511  annotationElem.removeAttribute( QStringLiteral( "position" ) );
512  }
513 
514  //direction
515  if ( annotationElem.hasAttribute( QStringLiteral( "direction" ) ) )
516  {
517  int dir = annotationElem.attribute( QStringLiteral( "direction" ) ).toInt();
518  if ( dir == 2 )
519  {
520  annotationElem.setAttribute( QStringLiteral( "leftDirection" ), 0 );
521  annotationElem.setAttribute( QStringLiteral( "rightDirection" ), 0 );
522  annotationElem.setAttribute( QStringLiteral( "topDirection" ), 1 );
523  annotationElem.setAttribute( QStringLiteral( "bottomDirection" ), 1 );
524  }
525  else if ( dir == 3 )
526  {
527  annotationElem.setAttribute( QStringLiteral( "leftDirection" ), 1 );
528  annotationElem.setAttribute( QStringLiteral( "rightDirection" ), 1 );
529  annotationElem.setAttribute( QStringLiteral( "topDirection" ), 0 );
530  annotationElem.setAttribute( QStringLiteral( "bottomDirection" ), 0 );
531  }
532  else
533  {
534  annotationElem.setAttribute( QStringLiteral( "leftDirection" ), dir );
535  annotationElem.setAttribute( QStringLiteral( "rightDirection" ), dir );
536  annotationElem.setAttribute( QStringLiteral( "topDirection" ), dir );
537  annotationElem.setAttribute( QStringLiteral( "bottomDirection" ), dir );
538  }
539  annotationElem.removeAttribute( QStringLiteral( "direction" ) );
540  }
541  }
542  }
543  }
544 
545  //Composer: move all items under Composition element
546  QDomNodeList composerList = mDom.elementsByTagName( QStringLiteral( "Composer" ) );
547  for ( int i = 0; i < composerList.size(); ++i )
548  {
549  QDomElement composerElem = composerList.at( i ).toElement();
550 
551  //find <QgsComposition element
552  QDomElement compositionElem = composerElem.firstChildElement( QStringLiteral( "Composition" ) );
553  if ( compositionElem.isNull() )
554  {
555  continue;
556  }
557 
558  QDomNodeList composerChildren = composerElem.childNodes();
559 
560  if ( composerChildren.size() < 1 )
561  {
562  continue;
563  }
564 
565  for ( int j = composerChildren.size() - 1; j >= 0; --j )
566  {
567  QDomElement childElem = composerChildren.at( j ).toElement();
568  if ( childElem.tagName() == QLatin1String( "Composition" ) )
569  {
570  continue;
571  }
572 
573  composerElem.removeChild( childElem );
574  compositionElem.appendChild( childElem );
575 
576  }
577  }
578 
579  // SimpleFill symbol layer v2: avoid double transparency
580  // replacing alpha value of symbol layer's color with 255 (the
581  // transparency value is already stored as symbol transparency).
582  QDomNodeList rendererList = mDom.elementsByTagName( QStringLiteral( "renderer-v2" ) );
583  for ( int i = 0; i < rendererList.size(); ++i )
584  {
585  QDomNodeList layerList = rendererList.at( i ).toElement().elementsByTagName( QStringLiteral( "layer" ) );
586  for ( int j = 0; j < layerList.size(); ++j )
587  {
588  QDomElement layerElem = layerList.at( j ).toElement();
589  if ( layerElem.attribute( QStringLiteral( "class" ) ) == QLatin1String( "SimpleFill" ) )
590  {
591  QDomNodeList propList = layerElem.elementsByTagName( QStringLiteral( "prop" ) );
592  for ( int k = 0; k < propList.size(); ++k )
593  {
594  QDomElement propElem = propList.at( k ).toElement();
595  if ( propElem.attribute( QStringLiteral( "k" ) ) == QLatin1String( "color" ) || propElem.attribute( QStringLiteral( "k" ) ) == QLatin1String( "color_border" ) )
596  {
597  propElem.setAttribute( QStringLiteral( "v" ), propElem.attribute( QStringLiteral( "v" ) ).section( ',', 0, 2 ) + ",255" );
598  }
599  }
600  }
601  }
602  }
603 
604  QgsDebugMsg( mDom.toString() );
605 }
606 
607 void QgsProjectFileTransform::transform2200to2300()
608 {
609  //composer: set placement for all picture items to middle, to mimic <=2.2 behavior
610  QDomNodeList composerPictureList = mDom.elementsByTagName( QStringLiteral( "ComposerPicture" ) );
611  for ( int i = 0; i < composerPictureList.size(); ++i )
612  {
613  QDomElement picture = composerPictureList.at( i ).toElement();
614  picture.setAttribute( QStringLiteral( "anchorPoint" ), QString::number( 4 ) );
615  }
616 }
617 
618 void QgsProjectFileTransform::transform2990()
619 {
620  QDomNodeList mapLayers = mDom.elementsByTagName( QStringLiteral( "maplayer" ) );
621 
622  for ( int mapLayerIndex = 0; mapLayerIndex < mapLayers.count(); ++mapLayerIndex )
623  {
624  QDomElement layerElem = mapLayers.at( mapLayerIndex ).toElement();
625 
626  // The newly added fieldConfiguration element
627  QDomElement fieldConfigurationElement = mDom.createElement( QStringLiteral( "fieldConfiguration" ) );
628  layerElem.appendChild( fieldConfigurationElement );
629 
630  QDomNodeList editTypeNodes = layerElem.namedItem( QStringLiteral( "edittypes" ) ).childNodes();
631  QDomElement constraintExpressionsElem = mDom.createElement( QStringLiteral( "constraintExpressions" ) );
632  layerElem.appendChild( constraintExpressionsElem );
633 
634  for ( int i = 0; i < editTypeNodes.size(); ++i )
635  {
636  QDomNode editTypeNode = editTypeNodes.at( i );
637  QDomElement editTypeElement = editTypeNode.toElement();
638 
639  QDomElement fieldElement = mDom.createElement( QStringLiteral( "field" ) );
640  fieldConfigurationElement.appendChild( fieldElement );
641 
642  QString name = editTypeElement.attribute( QStringLiteral( "name" ) );
643  fieldElement.setAttribute( QStringLiteral( "name" ), name );
644  QDomElement constraintExpressionElem = mDom.createElement( QStringLiteral( "constraint" ) );
645  constraintExpressionElem.setAttribute( "field", name );
646  constraintExpressionsElem.appendChild( constraintExpressionElem );
647 
648  QDomElement editWidgetElement = mDom.createElement( QStringLiteral( "editWidget" ) );
649  fieldElement.appendChild( editWidgetElement );
650 
651  QString ewv2Type = editTypeElement.attribute( QStringLiteral( "widgetv2type" ) );
652  editWidgetElement.setAttribute( "type", ewv2Type );
653 
654  QDomElement ewv2CfgElem = editTypeElement.namedItem( QStringLiteral( "widgetv2config" ) ).toElement();
655 
656  if ( !ewv2CfgElem.isNull() )
657  {
658  QDomElement editWidgetConfigElement = mDom.createElement( QStringLiteral( "config" ) );
659  editWidgetElement.appendChild( editWidgetConfigElement );
660 
661  QVariantMap editWidgetConfiguration;
662 
663  QDomNamedNodeMap configAttrs = ewv2CfgElem.attributes();
664  for ( int configIndex = 0; configIndex < configAttrs.count(); ++configIndex )
665  {
666  QDomAttr configAttr = configAttrs.item( configIndex ).toAttr();
667  if ( configAttr.name() == QStringLiteral( "fieldEditable" ) )
668  {
669  editWidgetConfigElement.setAttribute( QStringLiteral( "fieldEditable" ), configAttr.value() );
670  }
671  else if ( configAttr.name() == QStringLiteral( "labelOnTop" ) )
672  {
673  editWidgetConfigElement.setAttribute( QStringLiteral( "labelOnTop" ), configAttr.value() );
674  }
675  else if ( configAttr.name() == QStringLiteral( "notNull" ) )
676  {
677  editWidgetConfigElement.setAttribute( QStringLiteral( "notNull" ), configAttr.value() );
678  }
679  else if ( configAttr.name() == QStringLiteral( "constraint" ) )
680  {
681  constraintExpressionElem.setAttribute( "exp", configAttr.value() );
682  }
683  else if ( configAttr.name() == QStringLiteral( "constraintDescription" ) )
684  {
685  constraintExpressionElem.setAttribute( "desc", configAttr.value() );
686  }
687  else
688  {
689  editWidgetConfiguration.insert( configAttr.name(), configAttr.value() );
690  }
691  }
692 
693  if ( ewv2Type == QStringLiteral( "ValueMap" ) )
694  {
695  QDomNodeList configElements = ewv2CfgElem.childNodes();
696  QVariantMap map;
697  for ( int configIndex = 0; configIndex < configElements.count(); ++configIndex )
698  {
699  QDomElement configElem = configElements.at( configIndex ).toElement();
700  map.insert( configElem.attribute( QStringLiteral( "key" ) ), configElem.attribute( QStringLiteral( "value" ) ) );
701  }
702  editWidgetConfiguration.insert( QStringLiteral( "map" ), map );
703  }
704  else if ( ewv2Type == QStringLiteral( "Photo" ) )
705  {
706  editWidgetElement.setAttribute( "type", QStringLiteral( "ExternalResource" ) );
707 
708  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewer" ), 1 );
709  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewerHeight" ), editWidgetConfiguration.value( QStringLiteral( "Height" ) ) );
710  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewerWidth" ), editWidgetConfiguration.value( QStringLiteral( "Width" ) ) );
711  editWidgetConfiguration.insert( QStringLiteral( "RelativeStorage" ), 1 );
712  }
713  else if ( ewv2Type == QStringLiteral( "FileName" ) )
714  {
715  editWidgetElement.setAttribute( "type", QStringLiteral( "ExternalResource" ) );
716 
717  editWidgetConfiguration.insert( QStringLiteral( "RelativeStorage" ), 1 );
718  }
719  else if ( ewv2Type == QStringLiteral( "WebView" ) )
720  {
721  editWidgetElement.setAttribute( "type", QStringLiteral( "ExternalResource" ) );
722 
723  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewerHeight" ), editWidgetConfiguration.value( QStringLiteral( "Height" ) ) );
724  editWidgetConfiguration.insert( QStringLiteral( "DocumentViewerWidth" ), editWidgetConfiguration.value( QStringLiteral( "Width" ) ) );
725  editWidgetConfiguration.insert( QStringLiteral( "RelativeStorage" ), 1 );
726  }
727 
728  editWidgetConfigElement.appendChild( QgsXmlUtils::writeVariant( editWidgetConfiguration, mDom ) );
729  }
730  }
731  }
732 }
733 
734 void QgsProjectFileTransform::convertRasterProperties( QDomDocument& doc, QDomNode& parentNode,
735  QDomElement& rasterPropertiesElem, QgsRasterLayer* rlayer )
736 {
737  //no data
738  //TODO: We would need to set no data on all bands, but we don't know number of bands here
739  QDomNode noDataNode = rasterPropertiesElem.namedItem( QStringLiteral( "mNoDataValue" ) );
740  QDomElement noDataElement = noDataNode.toElement();
741  if ( !noDataElement.text().isEmpty() )
742  {
743  QgsDebugMsg( "mNoDataValue = " + noDataElement.text() );
744  QDomElement noDataElem = doc.createElement( QStringLiteral( "noData" ) );
745 
746  QDomElement noDataRangeList = doc.createElement( QStringLiteral( "noDataRangeList" ) );
747  noDataRangeList.setAttribute( QStringLiteral( "bandNo" ), 1 );
748 
749  QDomElement noDataRange = doc.createElement( QStringLiteral( "noDataRange" ) );
750  noDataRange.setAttribute( QStringLiteral( "min" ), noDataElement.text() );
751  noDataRange.setAttribute( QStringLiteral( "max" ), noDataElement.text() );
752  noDataRangeList.appendChild( noDataRange );
753 
754  noDataElem.appendChild( noDataRangeList );
755 
756  parentNode.appendChild( noDataElem );
757  }
758 
759  QDomElement rasterRendererElem = doc.createElement( QStringLiteral( "rasterrenderer" ) );
760  //convert general properties
761 
762  //invert color
763  rasterRendererElem.setAttribute( QStringLiteral( "invertColor" ), QStringLiteral( "0" ) );
764  QDomElement invertColorElem = rasterPropertiesElem.firstChildElement( QStringLiteral( "mInvertColor" ) );
765  if ( !invertColorElem.isNull() )
766  {
767  if ( invertColorElem.text() == QLatin1String( "true" ) )
768  {
769  rasterRendererElem.setAttribute( QStringLiteral( "invertColor" ), QStringLiteral( "1" ) );
770  }
771  }
772 
773  //opacity
774  rasterRendererElem.setAttribute( QStringLiteral( "opacity" ), QStringLiteral( "1" ) );
775  QDomElement transparencyElem = parentNode.firstChildElement( QStringLiteral( "transparencyLevelInt" ) );
776  if ( !transparencyElem.isNull() )
777  {
778  double transparency = transparencyElem.text().toInt();
779  rasterRendererElem.setAttribute( QStringLiteral( "opacity" ), QString::number( transparency / 255.0 ) );
780  }
781 
782  //alphaBand was not saved until now (bug)
783  rasterRendererElem.setAttribute( QStringLiteral( "alphaBand" ), -1 );
784 
785  //gray band is used for several renderers
786  int grayBand = rasterBandNumber( rasterPropertiesElem, QStringLiteral( "mGrayBandName" ), rlayer );
787 
788  //convert renderer specific properties
789  QString drawingStyle = rasterPropertiesElem.firstChildElement( QStringLiteral( "mDrawingStyle" ) ).text();
790 
791  // While PalettedColor should normally contain only integer values, usually
792  // color palette 0-255, it may happen (Tim, issue #7023) that it contains
793  // colormap classification with double values and text labels
794  // (which should normally only appear in SingleBandPseudoColor drawingStyle)
795  // => we have to check first the values and change drawingStyle if necessary
796  if ( drawingStyle == QLatin1String( "PalettedColor" ) )
797  {
798  QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( QStringLiteral( "customColorRamp" ) );
799  QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( QStringLiteral( "colorRampEntry" ) );
800 
801  for ( int i = 0; i < colorRampEntryList.size(); ++i )
802  {
803  QDomElement colorRampEntryElem = colorRampEntryList.at( i ).toElement();
804  QString strValue = colorRampEntryElem.attribute( QStringLiteral( "value" ) );
805  double value = strValue.toDouble();
806  if ( value < 0 || value > 10000 || !qgsDoubleNear( value, static_cast< int >( value ) ) )
807  {
808  QgsDebugMsg( QString( "forcing SingleBandPseudoColor value = %1" ).arg( value ) );
809  drawingStyle = QStringLiteral( "SingleBandPseudoColor" );
810  break;
811  }
812  }
813  }
814 
815  if ( drawingStyle == QLatin1String( "SingleBandGray" ) )
816  {
817  rasterRendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "singlebandgray" ) );
818  rasterRendererElem.setAttribute( QStringLiteral( "grayBand" ), grayBand );
819  transformContrastEnhancement( doc, rasterPropertiesElem, rasterRendererElem );
820  }
821  else if ( drawingStyle == QLatin1String( "SingleBandPseudoColor" ) )
822  {
823  rasterRendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "singlebandpseudocolor" ) );
824  rasterRendererElem.setAttribute( QStringLiteral( "band" ), grayBand );
825  QDomElement newRasterShaderElem = doc.createElement( QStringLiteral( "rastershader" ) );
826  QDomElement newColorRampShaderElem = doc.createElement( QStringLiteral( "colorrampshader" ) );
827  newRasterShaderElem.appendChild( newColorRampShaderElem );
828  rasterRendererElem.appendChild( newRasterShaderElem );
829 
830  //switch depending on mColorShadingAlgorithm
831  QString colorShadingAlgorithm = rasterPropertiesElem.firstChildElement( QStringLiteral( "mColorShadingAlgorithm" ) ).text();
832  if ( colorShadingAlgorithm == QLatin1String( "PseudoColorShader" ) || colorShadingAlgorithm == QLatin1String( "FreakOutShader" ) )
833  {
834  newColorRampShaderElem.setAttribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) );
835 
836  //get minmax from rasterlayer
837  QgsRasterBandStats rasterBandStats = rlayer->dataProvider()->bandStatistics( grayBand );
838  double minValue = rasterBandStats.minimumValue;
839  double maxValue = rasterBandStats.maximumValue;
840  double breakSize = ( maxValue - minValue ) / 3;
841 
842  QStringList colorList;
843  if ( colorShadingAlgorithm == QLatin1String( "FreakOutShader" ) )
844  {
845  colorList << QStringLiteral( "#ff00ff" ) << QStringLiteral( "#00ffff" ) << QStringLiteral( "#ff0000" ) << QStringLiteral( "#00ff00" );
846  }
847  else //pseudocolor
848  {
849  colorList << QStringLiteral( "#0000ff" ) << QStringLiteral( "#00ffff" ) << QStringLiteral( "#ffff00" ) << QStringLiteral( "#ff0000" );
850  }
851  QStringList::const_iterator colorIt = colorList.constBegin();
852  double boundValue = minValue;
853  for ( ; colorIt != colorList.constEnd(); ++colorIt )
854  {
855  QDomElement newItemElem = doc.createElement( QStringLiteral( "item" ) );
856  newItemElem.setAttribute( QStringLiteral( "value" ), QString::number( boundValue ) );
857  newItemElem.setAttribute( QStringLiteral( "label" ), QString::number( boundValue ) );
858  newItemElem.setAttribute( QStringLiteral( "color" ), *colorIt );
859  newColorRampShaderElem.appendChild( newItemElem );
860  boundValue += breakSize;
861  }
862  }
863  else if ( colorShadingAlgorithm == QLatin1String( "ColorRampShader" ) )
864  {
865  QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( QStringLiteral( "customColorRamp" ) );
866  QString type = customColorRampElem.firstChildElement( QStringLiteral( "colorRampType" ) ).text();
867  newColorRampShaderElem.setAttribute( QStringLiteral( "colorRampType" ), type );
868  QDomNodeList colorNodeList = customColorRampElem.elementsByTagName( QStringLiteral( "colorRampEntry" ) );
869 
870  QString value, label;
871  QColor newColor;
872  int red, green, blue;
873  QDomElement currentItemElem;
874  for ( int i = 0; i < colorNodeList.size(); ++i )
875  {
876  currentItemElem = colorNodeList.at( i ).toElement();
877  value = currentItemElem.attribute( QStringLiteral( "value" ) );
878  label = currentItemElem.attribute( QStringLiteral( "label" ) );
879  red = currentItemElem.attribute( QStringLiteral( "red" ) ).toInt();
880  green = currentItemElem.attribute( QStringLiteral( "green" ) ).toInt();
881  blue = currentItemElem.attribute( QStringLiteral( "blue" ) ).toInt();
882  newColor = QColor( red, green, blue );
883  QDomElement newItemElem = doc.createElement( QStringLiteral( "item" ) );
884  newItemElem.setAttribute( QStringLiteral( "value" ), value );
885  newItemElem.setAttribute( QStringLiteral( "label" ), label );
886  newItemElem.setAttribute( QStringLiteral( "color" ), newColor.name() );
887  newColorRampShaderElem.appendChild( newItemElem );
888  }
889  }
890  }
891  else if ( drawingStyle == QLatin1String( "PalettedColor" ) )
892  {
893  rasterRendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "paletted" ) );
894  rasterRendererElem.setAttribute( QStringLiteral( "band" ), grayBand );
895  QDomElement customColorRampElem = rasterPropertiesElem.firstChildElement( QStringLiteral( "customColorRamp" ) );
896  QDomNodeList colorRampEntryList = customColorRampElem.elementsByTagName( QStringLiteral( "colorRampEntry" ) );
897  QDomElement newColorPaletteElem = doc.createElement( QStringLiteral( "colorPalette" ) );
898 
899  int red = 0;
900  int green = 0;
901  int blue = 0;
902  int value = 0;
903  QDomElement colorRampEntryElem;
904  for ( int i = 0; i < colorRampEntryList.size(); ++i )
905  {
906  colorRampEntryElem = colorRampEntryList.at( i ).toElement();
907  QDomElement newPaletteElem = doc.createElement( QStringLiteral( "paletteEntry" ) );
908  value = static_cast< int >( colorRampEntryElem.attribute( QStringLiteral( "value" ) ).toDouble() );
909  newPaletteElem.setAttribute( QStringLiteral( "value" ), value );
910  red = colorRampEntryElem.attribute( QStringLiteral( "red" ) ).toInt();
911  green = colorRampEntryElem.attribute( QStringLiteral( "green" ) ).toInt();
912  blue = colorRampEntryElem.attribute( QStringLiteral( "blue" ) ).toInt();
913  newPaletteElem.setAttribute( QStringLiteral( "color" ), QColor( red, green, blue ).name() );
914  QString label = colorRampEntryElem.attribute( QStringLiteral( "label" ) );
915  if ( !label.isEmpty() )
916  {
917  newPaletteElem.setAttribute( QStringLiteral( "label" ), label );
918  }
919  newColorPaletteElem.appendChild( newPaletteElem );
920  }
921  rasterRendererElem.appendChild( newColorPaletteElem );
922  }
923  else if ( drawingStyle == QLatin1String( "MultiBandColor" ) )
924  {
925  rasterRendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "multibandcolor" ) );
926 
927  //red band, green band, blue band
928  int redBand = rasterBandNumber( rasterPropertiesElem, QStringLiteral( "mRedBandName" ), rlayer );
929  int greenBand = rasterBandNumber( rasterPropertiesElem, QStringLiteral( "mGreenBandName" ), rlayer );
930  int blueBand = rasterBandNumber( rasterPropertiesElem, QStringLiteral( "mBlueBandName" ), rlayer );
931  rasterRendererElem.setAttribute( QStringLiteral( "redBand" ), redBand );
932  rasterRendererElem.setAttribute( QStringLiteral( "greenBand" ), greenBand );
933  rasterRendererElem.setAttribute( QStringLiteral( "blueBand" ), blueBand );
934 
935  transformContrastEnhancement( doc, rasterPropertiesElem, rasterRendererElem );
936  }
937  else
938  {
939  return;
940  }
941 
942  //replace rasterproperties element with rasterrenderer element
943  if ( !parentNode.isNull() )
944  {
945  parentNode.replaceChild( rasterRendererElem, rasterPropertiesElem );
946  }
947 }
948 
949 int QgsProjectFileTransform::rasterBandNumber( const QDomElement &rasterPropertiesElem, const QString &bandName,
950  QgsRasterLayer *rlayer )
951 {
952  if ( !rlayer )
953  {
954  return -1;
955  }
956 
957  int band = -1;
958  QDomElement rasterBandElem = rasterPropertiesElem.firstChildElement( bandName );
959  if ( !rasterBandElem.isNull() )
960  {
961  QRegExp re( "(\\d+)" );
962 
963  if ( re.indexIn( rasterBandElem.text() ) >= 0 )
964  {
965  return re.cap( 1 ).toInt();
966  }
967  }
968  return band;
969 }
970 
971 void QgsProjectFileTransform::transformContrastEnhancement( QDomDocument& doc, const QDomElement& rasterproperties, QDomElement& rendererElem )
972 {
973  if ( rasterproperties.isNull() || rendererElem.isNull() )
974  {
975  return;
976  }
977 
978  double minimumValue = 0;
979  double maximumValue = 0;
980  QDomElement contrastMinMaxElem = rasterproperties.firstChildElement( QStringLiteral( "contrastEnhancementMinMaxValues" ) );
981  if ( contrastMinMaxElem.isNull() )
982  {
983  return;
984  }
985 
986  QDomElement contrastEnhancementAlgorithmElem = rasterproperties.firstChildElement( QStringLiteral( "mContrastEnhancementAlgorithm" ) );
987  if ( contrastEnhancementAlgorithmElem.isNull() )
988  {
989  return;
990  }
991 
992  //convert enhancement name to enumeration
993  int algorithmEnum = 0;
994  QString algorithmString = contrastEnhancementAlgorithmElem.text();
995  if ( algorithmString == QLatin1String( "StretchToMinimumMaximum" ) )
996  {
997  algorithmEnum = 1;
998  }
999  else if ( algorithmString == QLatin1String( "StretchAndClipToMinimumMaximum" ) )
1000  {
1001  algorithmEnum = 2;
1002  }
1003  else if ( algorithmString == QLatin1String( "ClipToMinimumMaximum" ) )
1004  {
1005  algorithmEnum = 3;
1006  }
1007  else if ( algorithmString == QLatin1String( "UserDefinedEnhancement" ) )
1008  {
1009  algorithmEnum = 4;
1010  }
1011 
1012  QDomNodeList minMaxEntryList = contrastMinMaxElem.elementsByTagName( QStringLiteral( "minMaxEntry" ) );
1013  QStringList enhancementNameList;
1014  if ( minMaxEntryList.size() == 1 )
1015  {
1016  enhancementNameList << QStringLiteral( "contrastEnhancement" );
1017  }
1018  if ( minMaxEntryList.size() == 3 )
1019  {
1020  enhancementNameList << QStringLiteral( "redContrastEnhancement" ) << QStringLiteral( "greenContrastEnhancement" ) << QStringLiteral( "blueContrastEnhancement" );
1021  }
1022  if ( minMaxEntryList.size() > enhancementNameList.size() )
1023  {
1024  return;
1025  }
1026 
1027  QDomElement minMaxEntryElem;
1028  for ( int i = 0; i < minMaxEntryList.size(); ++i )
1029  {
1030  minMaxEntryElem = minMaxEntryList.at( i ).toElement();
1031  QDomElement minElem = minMaxEntryElem.firstChildElement( QStringLiteral( "min" ) );
1032  if ( minElem.isNull() )
1033  {
1034  return;
1035  }
1036  minimumValue = minElem.text().toDouble();
1037 
1038  QDomElement maxElem = minMaxEntryElem.firstChildElement( QStringLiteral( "max" ) );
1039  if ( maxElem.isNull() )
1040  {
1041  return;
1042  }
1043  maximumValue = maxElem.text().toDouble();
1044 
1045  QDomElement newContrastEnhancementElem = doc.createElement( enhancementNameList.at( i ) );
1046  QDomElement newMinValElem = doc.createElement( QStringLiteral( "minValue" ) );
1047  QDomText minText = doc.createTextNode( QString::number( minimumValue ) );
1048  newMinValElem.appendChild( minText );
1049  newContrastEnhancementElem.appendChild( newMinValElem );
1050  QDomElement newMaxValElem = doc.createElement( QStringLiteral( "maxValue" ) );
1051  QDomText maxText = doc.createTextNode( QString::number( maximumValue ) );
1052  newMaxValElem.appendChild( maxText );
1053  newContrastEnhancementElem.appendChild( newMaxValElem );
1054 
1055  QDomElement newAlgorithmElem = doc.createElement( QStringLiteral( "algorithm" ) );
1056  QDomText newAlgorithmText = doc.createTextNode( QString::number( algorithmEnum ) );
1057  newAlgorithmElem.appendChild( newAlgorithmText );
1058  newContrastEnhancementElem.appendChild( newAlgorithmElem );
1059 
1060  rendererElem.appendChild( newContrastEnhancementElem );
1061  }
1062 }
1063 
1064 void QgsProjectFileTransform::transformRasterTransparency( QDomDocument& doc, const QDomElement& orig, QDomElement& rendererElem )
1065 {
1066  //soon...
1067  Q_UNUSED( doc );
1068  Q_UNUSED( orig );
1069  Q_UNUSED( rendererElem );
1070 }
1071 
static void convertRasterProperties(QDomDocument &doc, QDomNode &parentNode, QDomElement &rasterPropertiesElem, QgsRasterLayer *rlayer)
QgsProjectVersion PFV
void dump()
Prints the contents via QgsDebugMsg()
QString name
Definition: qgsfield.h:53
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
bool updateRevision(const QgsProjectVersion &version)
double maximumValue
The maximum cell value in the raster band.
Container of fields for a vector layer.
Definition: qgsfields.h:39
virtual QgsRasterBandStats bandStatistics(int bandNo, int stats=QgsRasterBandStats::All, const QgsRectangle &extent=QgsRectangle(), int sampleSize=0)
Get band statistics.
bool isValid() const
Return the status of the layer.
bool isNull() const
Returns true if this is a NULL project version.
int count() const
Return number of items.
Definition: qgsfields.cpp:115
Project property value node, contains a QgsProjectPropertyKey&#39;s value.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:198
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:135
The RasterBandStats struct is a container for statistics about a single raster band.
QgsPathResolver pathResolver() const
Return path resolver object with considering whether the project uses absolute or relative paths and ...
bool readLayerXml(const QDomElement &layerElement, const QgsPathResolver &pathResolver)
Sets state from Dom document.
A class to describe the version of a project.
virtual QgsFields fields() const =0
Returns the fields associated with this data provider.
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.
Definition: qgsproject.cpp:355
double minimumValue
The minimum cell value in the raster band.
QgsRasterDataProvider * dataProvider()
Returns the data provider.
QgsVectorDataProvider * dataProvider()
Returns the data provider.
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.