00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "qgsprojectfiletransform.h"
00021 #include "qgsprojectversion.h"
00022 #include "qgslogger.h"
00023 #include "qgsvectordataprovider.h"
00024 #include "qgsvectorlayer.h"
00025 #include <QTextStream>
00026 #include <QDomDocument>
00027 #include <QPrinter>
00028 #include <cstdlib>
00029 #include "qgsprojectproperty.h"
00030
00031 typedef QgsProjectVersion PFV;
00032
00033
00034 QgsProjectFileTransform::transform QgsProjectFileTransform::transformers[] =
00035 {
00036 {PFV( 0, 8, 0 ), PFV( 0, 8, 1 ), &QgsProjectFileTransform::transformNull},
00037 {PFV( 0, 8, 1 ), PFV( 0, 9, 0 ), &QgsProjectFileTransform::transform081to090},
00038 {PFV( 0, 9, 0 ), PFV( 0, 9, 1 ), &QgsProjectFileTransform::transformNull},
00039 {PFV( 0, 9, 1 ), PFV( 0, 10, 0 ), &QgsProjectFileTransform::transform091to0100},
00040 {PFV( 0, 9, 2 ), PFV( 0, 10, 0 ), &QgsProjectFileTransform::transformNull},
00041 {PFV( 0, 10, 0 ), PFV( 0, 11, 0 ), &QgsProjectFileTransform::transform0100to0110},
00042 {PFV( 0, 11, 0 ), PFV( 1, 0, 0 ), &QgsProjectFileTransform::transform0110to1000},
00043 {PFV( 1, 0, 0 ), PFV( 1, 1, 0 ), &QgsProjectFileTransform::transformNull},
00044 {PFV( 1, 0, 2 ), PFV( 1, 1, 0 ), &QgsProjectFileTransform::transformNull},
00045 {PFV( 1, 1, 0 ), PFV( 1, 2, 0 ), &QgsProjectFileTransform::transform1100to1200},
00046 {PFV( 1, 2, 0 ), PFV( 1, 3, 0 ), &QgsProjectFileTransform::transformNull},
00047 {PFV( 1, 3, 0 ), PFV( 1, 4, 0 ), &QgsProjectFileTransform::transformNull},
00048 {PFV( 1, 4, 0 ), PFV( 1, 5, 0 ), &QgsProjectFileTransform::transform1400to1500},
00049 };
00050
00051 bool QgsProjectFileTransform::updateRevision( QgsProjectVersion newVersion )
00052 {
00053 bool returnValue = false;
00054
00055 if ( ! mDom.isNull() )
00056 {
00057 for ( std::size_t i = 0; i < sizeof( transformers ) / sizeof( transform ); i++ )
00058 {
00059 if ( transformers[i].from == mCurrentVersion )
00060 {
00061
00062 ( this->*( transformers[i].transformFunc ) )();
00063 mCurrentVersion = transformers[i].to;
00064 returnValue = true;
00065 }
00066 }
00067 }
00068 return returnValue;
00069 }
00070
00071 void QgsProjectFileTransform::dump()
00072 {
00073 QgsDebugMsg( QString( "Current project file version is %1.%2.%3" )
00074 .arg( mCurrentVersion.majorVersion() )
00075 .arg( mCurrentVersion.minorVersion() )
00076 .arg( mCurrentVersion.subVersion() ) );
00077 #ifdef QGISDEBUG
00078
00079 std::cout << mDom.toString( 2 ).toLatin1().constData();
00080 #endif
00081 }
00082
00083
00084
00085
00086
00087 void QgsProjectFileTransform::transform081to090()
00088 {
00089 QgsDebugMsg( "Entering..." );
00090 if ( ! mDom.isNull() )
00091 {
00092
00093
00094 QDomElement mapCanvas;
00095
00096
00097 QDomNode qgis = mDom.firstChildElement( "qgis" );
00098 if ( ! qgis.isNull() )
00099 {
00100 QgsDebugMsg( "Populating new mapcanvas" );
00101
00102
00103 mapCanvas = mDom.createElement( "mapcanvas" );
00104
00105 qgis.appendChild( mapCanvas );
00106
00107 mapCanvas.appendChild( qgis.namedItem( "units" ) );
00108
00109 mapCanvas.appendChild( qgis.namedItem( "extent" ) );
00110
00111
00112
00113 QDomElement properties = qgis.firstChildElement( "properties" );
00114 QDomElement spatial = properties.firstChildElement( "SpatialRefSys" );
00115 QDomElement hasCrsTransformEnabled = spatial.firstChildElement( "ProjectionsEnabled" );
00116
00117
00118 QDomElement projection = mDom.createElement( "projections" );
00119 QgsDebugMsg( QString( "Projection flag: " ) + hasCrsTransformEnabled.text() );
00120
00121 projection.appendChild( mDom.createTextNode( hasCrsTransformEnabled.text() ) );
00122
00123 mapCanvas.appendChild( projection );
00124
00125 }
00126
00127
00128
00129
00130 QDomNodeList mapLayers = mDom.elementsByTagName( "maplayer" );
00131 bool doneDestination = false;
00132 for ( int i = 0; i < mapLayers.count(); i++ )
00133 {
00134 QDomNode mapLayer = mapLayers.item( i );
00135
00136 QDomNode coordinateTransform = mapLayer.namedItem( "coordinatetransform" );
00137
00138 QDomNode sourceCrs = coordinateTransform.namedItem( "sourcesrs" );
00139
00140 sourceCrs.toElement().setTagName( "srs" );
00141
00142 mapLayer.appendChild( sourceCrs );
00143
00144
00145 if ( ! doneDestination )
00146 {
00147
00148 QDomNode destinationCRS = coordinateTransform.namedItem( "destinationsrs" );
00149
00150
00151 mapCanvas.appendChild( destinationCRS );
00152
00153 doneDestination = true;
00154 }
00155 mapLayer.removeChild( coordinateTransform );
00156
00157
00158
00159 }
00160
00161
00162 QDomNodeList legendLayerFiles = mDom.elementsByTagName( "legendlayerfile" );
00163 QgsDebugMsg( QString( "Legend layer file entries: " ) + QString::number( legendLayerFiles.count() ) );
00164 for ( int i = 0; i < mapLayers.count(); i++ )
00165 {
00166
00167 QDomElement mapLayer = mapLayers.item( i ).toElement();
00168
00169 QString id = mapLayer.firstChildElement( "id" ).text();
00170 QgsDebugMsg( QString( "Handling layer " + id ) );
00171
00172 for ( int j = 0; j < legendLayerFiles.count(); j++ )
00173 {
00174 QDomElement legendLayerFile = legendLayerFiles.item( j ).toElement();
00175 if ( id == legendLayerFile.attribute( "layerid" ) )
00176 {
00177
00178 QgsDebugMsg( "Found matching id" );
00179
00180
00181 legendLayerFile.setAttribute( "visible", mapLayer.attribute( "visible" ) );
00182
00183
00184 legendLayerFile.setAttribute( "isInOverview", mapLayer.attribute( "showInOverviewFlag" ) );
00185 }
00186 }
00187 }
00188 }
00189 return;
00190
00191 }
00192
00193 void QgsProjectFileTransform::transform091to0100()
00194 {
00195 QgsDebugMsg( "entering" );
00196 if ( ! mDom.isNull() )
00197 {
00198
00199 QDomNodeList rasterPropertyList = mDom.elementsByTagName( "rasterproperties" );
00200 QgsDebugMsg( QString( "Raster properties file entries: " ) + QString::number( rasterPropertyList.count() ) );
00201 for ( int i = 0; i < rasterPropertyList.count(); i++ )
00202 {
00203
00204 QDomNode rasterProperty = rasterPropertyList.item( i );
00205
00206
00207 rasterProperty.namedItem( "stdDevsToPlotDouble" ).toElement().setTagName( "mStandardDeviations" );
00208
00209 rasterProperty.namedItem( "invertHistogramFlag" ).toElement().setTagName( "mInvertPixelsFlag" );
00210 rasterProperty.namedItem( "showDebugOverLayFlag" ).toElement().setTagName( "mDebugOverLayFlag" );
00211
00212 rasterProperty.namedItem( "redBandNameQString" ).toElement().setTagName( "mRedBandName" );
00213 rasterProperty.namedItem( "blueBandNameQString" ).toElement().setTagName( "mBlueBandName" );
00214 rasterProperty.namedItem( "greenBandNameQString" ).toElement().setTagName( "mGreenBandName" );
00215 rasterProperty.namedItem( "grayBandNameQString" ).toElement().setTagName( "mGrayBandName" );
00216 }
00217
00218
00219 QDomNodeList symbolPropertyList = mDom.elementsByTagName( "symbol" );
00220 for ( int i = 0; i < symbolPropertyList.count(); i++ )
00221 {
00222
00223 QDomNode symbolProperty = symbolPropertyList.item( i );
00224
00225 QDomElement pointSymbol = symbolProperty.firstChildElement( "pointsymbol" );
00226 if ( pointSymbol.text().startsWith( "hard:" ) )
00227 {
00228
00229 int lineWidth = symbolProperty.firstChildElement( "outlinewidth" ).text().toInt();
00230 int pointSize = symbolProperty.firstChildElement( "pointsize" ).text().toInt();
00231
00232 if ( pointSize != 0 )
00233 {
00234
00235
00236
00237 pointSize = pointSize + 2 + 2 * lineWidth;
00238 QgsDebugMsg( QString( "Setting point size to %1" ).arg( pointSize ) );
00239 QDomElement newPointSizeProperty = mDom.createElement( "pointsize" );
00240 QDomText newPointSizeTxt = mDom.createTextNode( QString::number( pointSize ) );
00241 newPointSizeProperty.appendChild( newPointSizeTxt );
00242 symbolProperty.replaceChild( newPointSizeProperty, pointSymbol );
00243 }
00244 }
00245 }
00246
00247 }
00248 return;
00249
00250 }
00251
00252 void QgsProjectFileTransform::transform0100to0110()
00253 {
00254 if ( ! mDom.isNull() )
00255 {
00256
00257 QPrinter myPrinter( QPrinter::ScreenResolution );
00258 int screenDpi = myPrinter.resolution();
00259 double widthScaleFactor = 25.4 / screenDpi;
00260
00261 QDomNodeList outlineWidthList = mDom.elementsByTagName( "outlinewidth" );
00262 for ( int i = 0; i < outlineWidthList.size(); ++i )
00263 {
00264
00265 QDomElement currentOutlineElem = outlineWidthList.at( i ).toElement();
00266 double outlineWidth = currentOutlineElem.text().toDouble();
00267 outlineWidth *= widthScaleFactor;
00268
00269
00270 QDomNode outlineTextNode = currentOutlineElem.firstChild();
00271 QDomText newOutlineText = mDom.createTextNode( QString::number( outlineWidth ) );
00272 currentOutlineElem.replaceChild( newOutlineText, outlineTextNode );
00273
00274 }
00275
00276
00277 QDomNodeList pointSizeList = mDom.elementsByTagName( "pointsize" );
00278 for ( int i = 0; i < pointSizeList.size(); ++i )
00279 {
00280
00281 QDomElement currentPointSizeElem = pointSizeList.at( i ).toElement();
00282 double pointSize = currentPointSizeElem.text().toDouble();
00283 pointSize *= widthScaleFactor;
00284
00285
00286 QDomNode pointSizeTextNode = currentPointSizeElem.firstChild();
00287 QDomText newPointSizeText = mDom.createTextNode( QString::number(( int )pointSize ) );
00288 currentPointSizeElem.replaceChild( newPointSizeText, pointSizeTextNode );
00289 }
00290 }
00291 }
00292
00293 void QgsProjectFileTransform::transform0110to1000()
00294 {
00295 if ( ! mDom.isNull() )
00296 {
00297 QDomNodeList layerList = mDom.elementsByTagName( "maplayer" );
00298 for ( int i = 0; i < layerList.size(); ++i )
00299 {
00300 QDomElement layerElem = layerList.at( i ).toElement();
00301 QString typeString = layerElem.attribute( "type" );
00302 if ( typeString != "vector" )
00303 {
00304 continue;
00305 }
00306
00307
00308 QDomNode dataSourceNode = layerElem.namedItem( "datasource" );
00309 if ( dataSourceNode.isNull() )
00310 {
00311 return;
00312 }
00313 QString dataSource = dataSourceNode.toElement().text();
00314
00315
00316 QDomNode providerNode = layerElem.namedItem( "provider" );
00317 if ( providerNode.isNull() )
00318 {
00319 return;
00320 }
00321 QString providerKey = providerNode.toElement().text();
00322
00323
00324 QgsVectorLayer* theLayer = new QgsVectorLayer( dataSource, "", providerKey, false );
00325 if ( !theLayer->isValid() )
00326 {
00327 delete theLayer;
00328 return;
00329 }
00330
00331 QgsVectorDataProvider* theProvider = theLayer->dataProvider();
00332 if ( !theProvider )
00333 {
00334 return;
00335 }
00336 QgsFieldMap theFieldMap = theProvider->fields();
00337
00338
00339 QDomNodeList classificationFieldList = layerElem.elementsByTagName( "classificationfield" );
00340 for ( int j = 0; j < classificationFieldList.size(); ++j )
00341 {
00342 QDomElement classificationFieldElem = classificationFieldList.at( j ).toElement();
00343 int fieldNumber = classificationFieldElem.text().toInt();
00344 QgsFieldMap::const_iterator field_it = theFieldMap.find( fieldNumber );
00345 if ( field_it != theFieldMap.constEnd() )
00346 {
00347 QDomText fieldName = mDom.createTextNode( field_it.value().name() );
00348 QDomNode nameNode = classificationFieldElem.firstChild();
00349 classificationFieldElem.replaceChild( fieldName, nameNode );
00350 }
00351 }
00352
00353 }
00354 }
00355 }
00356
00357 void QgsProjectFileTransform::transform1100to1200()
00358 {
00359 QgsDebugMsg( "Entering..." );
00360 if ( mDom.isNull() )
00361 return;
00362
00363 QDomNode qgis = mDom.firstChildElement( "qgis" );
00364 if ( qgis.isNull() )
00365 return;
00366
00367 QDomElement properties = qgis.firstChildElement( "properties" );
00368 if ( properties.isNull() )
00369 return;
00370
00371 QDomElement digitizing = properties.firstChildElement( "Digitizing" );
00372 if ( digitizing.isNull() )
00373 return;
00374
00375 QDomElement tolList = digitizing.firstChildElement( "LayerSnappingToleranceList" );
00376 if ( tolList.isNull() )
00377 return;
00378
00379 QDomElement tolUnitList = digitizing.firstChildElement( "LayerSnappingToleranceUnitList" );
00380 if ( !tolUnitList.isNull() )
00381 return;
00382
00383 QStringList units;
00384 for ( int i = 0; i < tolList.childNodes().count(); i++ )
00385 units << "0";
00386
00387 QgsPropertyValue value( units );
00388 value.writeXML( "LayerSnappingToleranceUnitList", digitizing, mDom );
00389 }
00390
00391 void QgsProjectFileTransform::transform1400to1500()
00392 {
00393
00394 if ( mDom.isNull() )
00395 {
00396 return;
00397 }
00398
00399 QDomNodeList layerItemList = mDom.elementsByTagName( "LayerItem" );
00400 QDomElement currentLayerItemElem;
00401 QString currentLayerId;
00402
00403 for ( int i = 0; i < layerItemList.size(); ++i )
00404 {
00405 currentLayerItemElem = layerItemList.at( i ).toElement();
00406 if ( currentLayerItemElem.isNull() )
00407 {
00408 continue;
00409 }
00410 currentLayerId = currentLayerItemElem.attribute( "layerId" );
00411
00412 QDomNodeList vectorClassificationList = currentLayerItemElem.elementsByTagName( "VectorClassificationItem" );
00413 QDomElement currentClassificationElem;
00414 for ( int j = 0; j < vectorClassificationList.size(); ++j )
00415 {
00416 currentClassificationElem = vectorClassificationList.at( j ).toElement();
00417 if ( !currentClassificationElem.isNull() )
00418 {
00419 currentClassificationElem.setAttribute( "layerId", currentLayerId );
00420 }
00421 }
00422
00423
00424 QDomNodeList textItemList = currentLayerItemElem.elementsByTagName( "TextItem" );
00425 QDomElement currentTextItem;
00426
00427 for ( int j = 0; j < textItemList.size(); ++j )
00428 {
00429 currentTextItem = textItemList.at( j ).toElement();
00430 if ( currentTextItem.isNull() )
00431 {
00432 continue;
00433 }
00434
00435 QDomElement classificationElement;
00436 if ( vectorClassificationList.size() > 0 )
00437 {
00438 classificationElement = mDom.createElement( "VectorClassificationItem" );
00439 }
00440 else
00441 {
00442 classificationElement = mDom.createElement( "RasterClassificationItem" );
00443 }
00444
00445 classificationElement.setAttribute( "layerId", currentLayerId );
00446 classificationElement.setAttribute( "text", currentTextItem.attribute( "text" ) );
00447 currentLayerItemElem.replaceChild( classificationElement, currentTextItem );
00448 }
00449 }
00450 }