QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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
22bool QgsScaleUtils::saveScaleList( const QString &fileName, const QStringList &scales, QString &errorMessage )
23{
24 QDomDocument doc;
25 QDomElement root = doc.createElement( QStringLiteral( "qgsScales" ) );
26 root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
27 doc.appendChild( root );
28
29 for ( int i = 0; i < scales.count(); ++i )
30 {
31 QDomElement el = doc.createElement( QStringLiteral( "scale" ) );
32 el.setAttribute( QStringLiteral( "value" ), scales.at( i ) );
33 root.appendChild( el );
34 }
35
36 QFile file( fileName );
37 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
38 {
39 errorMessage = QStringLiteral( "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
48bool 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 = QStringLiteral( "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 = QStringLiteral( "Parse error at line %1, column %2:\n%3" )
65 .arg( errorLine )
66 .arg( errorColumn )
67 .arg( errorStr );
68 return false;
69 }
70
71 const QDomElement root = doc.documentElement();
72 if ( root.tagName() != QLatin1String( "qgsScales" ) )
73 {
74 errorMessage = QStringLiteral( "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( QStringLiteral( "value" ) ) );
82 child = child.nextSiblingElement();
83 }
84
85 return true;
86}
static bool loadScaleList(const QString &fileName, QStringList &scales, QString &errorMessage)
Load scales from the given file.
static bool saveScaleList(const QString &fileName, const QStringList &scales, QString &errorMessage)
Save scales to the given file.