1
0
mirror of https://github.com/qgis/QGIS.git synced 2025-04-02 00:04:53 -04:00
QGIS/src/core/qgsscaleutils.cpp
Nyall Dawson 4166a3ea62 Fix most clazy qstring-unneeded-heap-allocations warnings
By flipping string literals to QStringLiteral/QLatin1String

see

https://woboq.com/blog/qstringliteral.html
2016-10-24 15:26:24 +10:00

87 lines
2.9 KiB
C++

/***************************************************************************
qgsscaleutils.cpp
---------------------
begin : July 2012
copyright : (C) 2012 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QFile>
#include <QDomDocument>
#include <QTextStream>
#include "qgsscaleutils.h"
bool QgsScaleUtils::saveScaleList( const QString &fileName, const QStringList &scales, QString &errorMessage )
{
QDomDocument doc;
QDomElement root = doc.createElement( QStringLiteral( "qgsScales" ) );
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
doc.appendChild( root );
for ( int i = 0; i < scales.count(); ++i )
{
QDomElement el = doc.createElement( QStringLiteral( "scale" ) );
el.setAttribute( QStringLiteral( "value" ), scales.at( i ) );
root.appendChild( el );
}
QFile file( fileName );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
errorMessage = QStringLiteral( "Cannot write file %1:\n%2." ).arg( fileName, file.errorString() );
return false;
}
QTextStream out( &file );
doc.save( out, 4 );
return true;
}
bool QgsScaleUtils::loadScaleList( const QString &fileName, QStringList &scales, QString &errorMessage )
{
QFile file( fileName );
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
errorMessage = QStringLiteral( "Cannot read file %1:\n%2." ).arg( fileName, file.errorString() );
return false;
}
QDomDocument doc;
QString errorStr;
int errorLine;
int errorColumn;
if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
{
errorMessage = QStringLiteral( "Parse error at line %1, column %2:\n%3" )
.arg( errorLine )
.arg( errorColumn )
.arg( errorStr );
return false;
}
QDomElement root = doc.documentElement();
if ( root.tagName() != QLatin1String( "qgsScales" ) )
{
errorMessage = QStringLiteral( "The file is not an scales exchange file." );
return false;
}
QDomElement child = root.firstChildElement();
while ( !child.isNull() )
{
scales.append( child.attribute( QStringLiteral( "value" ) ) );
child = child.nextSiblingElement();
}
return true;
}