QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsscaleutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsscaleutils.cpp
3  ---------------------
4  begin : July 2012
5  copyright : (C) 2012 by Alexander Bruy
6  email : alexander dot bruy at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <QFile>
17 #include <QDomDocument>
18 #include <QTextStream>
19 
20 #include "qgsscaleutils.h"
21 
22 bool QgsScaleUtils::saveScaleList( const QString &fileName, const QStringList &scales, QString &errorMessage )
23 {
24  QDomDocument doc;
25  QDomElement root = doc.createElement( "qgsScales" );
26  root.setAttribute( "version", "1.0" );
27  doc.appendChild( root );
28 
29  for ( int i = 0; i < scales.count(); ++i )
30  {
31  QDomElement el = doc.createElement( "scale" );
32  el.setAttribute( "value", scales.at( i ) );
33  root.appendChild( el );
34  }
35 
36  QFile file( fileName );
37  if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
38  {
39  errorMessage = QString( "Cannot write file %1:\n%2." ).arg( fileName, file.errorString() );
40  return false;
41  }
42 
43  QTextStream out( &file );
44  doc.save( out, 4 );
45  return true;
46 }
47 
48 bool QgsScaleUtils::loadScaleList( const QString &fileName, QStringList &scales, QString &errorMessage )
49 {
50  QFile file( fileName );
51  if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
52  {
53  errorMessage = QString( "Cannot read file %1:\n%2." ).arg( fileName, file.errorString() );
54  return false;
55  }
56 
57  QDomDocument doc;
58  QString errorStr;
59  int errorLine;
60  int errorColumn;
61 
62  if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
63  {
64  errorMessage = QString( "Parse error at line %1, column %2:\n%3" )
65  .arg( errorLine )
66  .arg( errorColumn )
67  .arg( errorStr );
68  return false;
69  }
70 
71  QDomElement root = doc.documentElement();
72  if ( root.tagName() != "qgsScales" )
73  {
74  errorMessage = "The file is not an scales exchange file.";
75  return false;
76  }
77 
78  QDomElement child = root.firstChildElement();
79  while ( !child.isNull() )
80  {
81  scales.append( child.attribute( "value" ) );
82  child = child.nextSiblingElement();
83  }
84 
85  return true;
86 }
QDomNode appendChild(const QDomNode &newChild)
QString attribute(const QString &name, const QString &defValue) const
QString errorString() const
const T & at(int i) const
QDomElement nextSiblingElement(const QString &tagName) const
QDomElement documentElement() const
static bool loadScaleList(const QString &fileName, QStringList &scales, QString &errorMessage)
Load scales from the given file.
int count(const T &value) const
void append(const T &value)
void setAttribute(const QString &name, const QString &value)
static bool saveScaleList(const QString &fileName, const QStringList &scales, QString &errorMessage)
Save scales to the given file.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
bool isNull() const
void save(QTextStream &str, int indent) const
QDomElement firstChildElement(const QString &tagName) const
QString tagName() const
QDomElement createElement(const QString &tagName)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)