Quantum GIS API Documentation  master-ce49b66
src/core/qgsscaleutils.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsscaleutils.cpp
00003     ---------------------
00004     begin                : July 2012
00005     copyright            : (C) 2012 by Alexander Bruy
00006     email                : alexander dot bruy at gmail dot com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include <QFile>
00017 #include <QDomDocument>
00018 #include <QTextStream>
00019 
00020 #include "qgsscaleutils.h"
00021 
00022 bool QgsScaleUtils::saveScaleList( const QString &fileName, const QStringList &scales, QString &errorMessage )
00023 {
00024   QDomDocument doc;
00025   QDomElement root = doc.createElement( "qgsScales" );
00026   root.setAttribute( "version", "1.0" );
00027   doc.appendChild( root );
00028 
00029   for ( int i = 0; i < scales.count(); ++i )
00030   {
00031     QDomElement el = doc.createElement( "scale" );
00032     el.setAttribute( "value", scales.at( i ) );
00033     root.appendChild( el );
00034   }
00035 
00036   QFile file( fileName );
00037   if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
00038   {
00039     errorMessage = QString( "Cannot write file %1:\n%2." ).arg( fileName ).arg( file.errorString() );
00040     return false;
00041   }
00042 
00043   QTextStream out( &file );
00044   doc.save( out, 4 );
00045   return true;
00046 }
00047 
00048 bool QgsScaleUtils::loadScaleList( const QString &fileName, QStringList &scales, QString &errorMessage )
00049 {
00050   QFile file( fileName );
00051   if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
00052   {
00053     errorMessage = QString( "Cannot read file %1:\n%2." ).arg( fileName ).arg( file.errorString() );
00054     return false;
00055   }
00056 
00057   QDomDocument doc;
00058   QString errorStr;
00059   int errorLine;
00060   int errorColumn;
00061 
00062   if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
00063   {
00064     errorMessage = QString( "Parse error at line %1, column %2:\n%3" )
00065                    .arg( errorLine )
00066                    .arg( errorColumn )
00067                    .arg( errorStr );
00068     return false;
00069   }
00070 
00071   QDomElement root = doc.documentElement();
00072   if ( root.tagName() != "qgsScales" )
00073   {
00074     errorMessage = "The file is not an scales exchange file.";
00075     return false;
00076   }
00077 
00078   QDomElement child = root.firstChildElement();
00079   while ( !child.isNull() )
00080   {
00081     scales.append( child.attribute( "value" ) );
00082     child = child.nextSiblingElement();
00083   }
00084 
00085   return true;
00086 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines