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