QGIS API Documentation
2.10.1-Pisa
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
src
core
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 ).
arg
( 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 ).
arg
( 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
QDomNode appendChild(const QDomNode &newChild)
QDomElement::attribute
QString attribute(const QString &name, const QString &defValue) const
QIODevice::errorString
QString errorString() const
QList::at
const T & at(int i) const
QDomNode::nextSiblingElement
QDomElement nextSiblingElement(const QString &tagName) const
QDomDocument::documentElement
QDomElement documentElement() const
QFile
QTextStream
QgsScaleUtils::loadScaleList
static bool loadScaleList(const QString &fileName, QStringList &scales, QString &errorMessage)
load scales from the given file
Definition:
qgsscaleutils.cpp:48
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
QDomElement::setAttribute
void setAttribute(const QString &name, const QString &value)
QString
QgsScaleUtils::saveScaleList
static bool saveScaleList(const QString &fileName, const QStringList &scales, QString &errorMessage)
save scales to the given file
Definition:
qgsscaleutils.cpp:22
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
QDomDocument
QDomNode::isNull
bool isNull() const
qgsscaleutils.h
QDomNode::save
void save(QTextStream &str, int indent) const
QDomNode::firstChildElement
QDomElement firstChildElement(const QString &tagName) const
QDomElement::tagName
QString tagName() const
QDomDocument::createElement
QDomElement createElement(const QString &tagName)
QDomElement
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QDomDocument::setContent
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
Generated on Sat Aug 22 2015 02:53:40 for QGIS API Documentation by
1.8.6