Skip to content

Commit f5dce33

Browse files
committedJul 18, 2012
scales import/export
1 parent fc3e49b commit f5dce33

File tree

7 files changed

+266
-3
lines changed

7 files changed

+266
-3
lines changed
 

‎src/app/qgsoptions.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsgenericprojectionselector.h"
2323
#include "qgscoordinatereferencesystem.h"
2424
#include "qgstolerance.h"
25+
#include "qgsscaleutils.h"
2526
#include "qgsnetworkaccessmanager.h"
2627
#include "qgsproject.h"
2728

@@ -1478,3 +1479,57 @@ void QgsOptions::on_pbnDefaultScaleValues_clicked()
14781479
mListGlobalScales->addItem( newItem );
14791480
}
14801481
}
1482+
1483+
void QgsOptions::on_pbnImportScales_clicked()
1484+
{
1485+
QString fileName = QFileDialog::getOpenFileName( this, tr( "Load scales" ), ".",
1486+
tr( "XML files (*.xml *.XML)" ) );
1487+
if ( fileName.isEmpty() )
1488+
{
1489+
return;
1490+
}
1491+
1492+
QString msg;
1493+
QStringList myScales;
1494+
if ( !QgsScaleUtils::loadScaleList( fileName, myScales, msg ) )
1495+
{
1496+
QgsDebugMsg( msg );
1497+
}
1498+
1499+
QStringList::const_iterator scaleIt = myScales.constBegin();
1500+
for ( ; scaleIt != myScales.constEnd(); ++scaleIt )
1501+
{
1502+
QListWidgetItem* newItem = new QListWidgetItem( mListGlobalScales );
1503+
newItem->setText( *scaleIt );
1504+
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
1505+
mListGlobalScales->addItem( newItem );
1506+
}
1507+
}
1508+
1509+
void QgsOptions::on_pbnExportScales_clicked()
1510+
{
1511+
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save scales" ), ".",
1512+
tr( "XML files (*.xml *.XML)" ) );
1513+
if ( fileName.isEmpty() )
1514+
{
1515+
return;
1516+
}
1517+
1518+
// ensure the user never ommited the extension from the file name
1519+
if ( !fileName.toLower().endsWith( ".xml" ) )
1520+
{
1521+
fileName += ".xml";
1522+
}
1523+
1524+
QStringList myScales;
1525+
for ( int i = 0; i < mListGlobalScales->count(); ++i )
1526+
{
1527+
myScales.append( mListGlobalScales->item( i )->text() );
1528+
}
1529+
1530+
QString msg;
1531+
if ( !QgsScaleUtils::saveScaleList( fileName, myScales, msg ) )
1532+
{
1533+
QgsDebugMsg( msg );
1534+
}
1535+
}

‎src/app/qgsoptions.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,34 @@ class QgsOptions : public QDialog, private Ui::QgsOptionsBase
129129
void on_mBrowseCacheDirectory_clicked();
130130
void on_mClearCache_clicked();
131131

132-
/* Let the user add a scale to the list of scales
132+
/** Let the user add a scale to the list of scales
133133
* used in scale combobox
134134
* @note added in QGIS 2.0
135135
*/
136136
void on_pbnAddScale_clicked();
137137

138-
/* Let the user remove a scale from the list of scales
138+
/** Let the user remove a scale from the list of scales
139139
* used in scale combobox
140140
* @note added in QGIS 2.0
141141
*/
142142
void on_pbnRemoveScale_clicked();
143143

144-
/* Let the user restore default scales
144+
/** Let the user restore default scales
145145
* used in scale combobox
146146
* @note added in QGIS 2.0
147147
*/
148148
void on_pbnDefaultScaleValues_clicked();
149149

150+
/** Let the user load scales from file
151+
* @note added in QGIS 2.0
152+
*/
153+
void on_pbnImportScales_clicked();
154+
155+
/** Let the user load scales from file
156+
* @note added in QGIS 2.0
157+
*/
158+
void on_pbnExportScales_clicked();
159+
150160
/** Auto slot executed when the active page in the main widget stack is changed
151161
* @note added in 2.0
152162
*/

‎src/app/qgsprojectproperties.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "qgsrenderer.h"
3131
#include "qgssnappingdialog.h"
3232
#include "qgsrasterlayer.h"
33+
#include "qgsscaleutils.h"
3334
#include "qgsgenericprojectionselector.h"
3435
#include "qgsstylev2.h"
3536
#include "qgssymbolv2.h"
@@ -40,6 +41,7 @@
4041
//qt includes
4142
#include <QColorDialog>
4243
#include <QInputDialog>
44+
#include <QFileDialog>
4345
#include <QHeaderView> // Qt 4.4
4446
#include <QMessageBox>
4547

@@ -765,6 +767,60 @@ void QgsProjectProperties::on_pbnRemoveScale_clicked()
765767
delete itemToRemove;
766768
}
767769

770+
void QgsProjectProperties::on_pbnImportScales_clicked()
771+
{
772+
QString fileName = QFileDialog::getOpenFileName( this, tr( "Load scales" ), ".",
773+
tr( "XML files (*.xml *.XML)" ) );
774+
if ( fileName.isEmpty() )
775+
{
776+
return;
777+
}
778+
779+
QString msg;
780+
QStringList myScales;
781+
if ( !QgsScaleUtils::loadScaleList( fileName, myScales, msg ) )
782+
{
783+
QgsDebugMsg( msg );
784+
}
785+
786+
QStringList::const_iterator scaleIt = myScales.constBegin();
787+
for ( ; scaleIt != myScales.constEnd(); ++scaleIt )
788+
{
789+
QListWidgetItem* newItem = new QListWidgetItem( lstScales );
790+
newItem->setText( *scaleIt );
791+
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
792+
lstScales->addItem( newItem );
793+
}
794+
}
795+
796+
void QgsProjectProperties::on_pbnExportScales_clicked()
797+
{
798+
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save scales" ), ".",
799+
tr( "XML files (*.xml *.XML)" ) );
800+
if ( fileName.isEmpty() )
801+
{
802+
return;
803+
}
804+
805+
// ensure the user never ommited the extension from the file name
806+
if ( !fileName.toLower().endsWith( ".xml" ) )
807+
{
808+
fileName += ".xml";
809+
}
810+
811+
QStringList myScales;
812+
for ( int i = 0; i < lstScales->count(); ++i )
813+
{
814+
myScales.append( lstScales->item( i )->text() );
815+
}
816+
817+
QString msg;
818+
if ( !QgsScaleUtils::saveScaleList( fileName, myScales, msg ) )
819+
{
820+
QgsDebugMsg( msg );
821+
}
822+
}
823+
768824
void QgsProjectProperties::populateStyles()
769825
{
770826
// Styles - taken from qgsstylev2managerdialog

‎src/app/qgsprojectproperties.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
9595
*/
9696
void on_pbnRemoveScale_clicked();
9797

98+
/** Let the user load scales from file
99+
* @note added in QGIS 2.0
100+
*/
101+
void on_pbnImportScales_clicked();
102+
103+
/** Let the user load scales from file
104+
* @note added in QGIS 2.0
105+
*/
106+
void on_pbnExportScales_clicked();
107+
98108
/*!
99109
* Slots for WMS project settings
100110
*/

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ SET(QGIS_CORE_SRCS
186186
qgsspatialindex.cpp
187187

188188
qgspaintenginehack.cpp
189+
qgsscaleutils.cpp
189190
)
190191

191192
IF(WIN32)
@@ -426,6 +427,7 @@ SET(QGIS_CORE_HDRS
426427
qgsspatialindex.h
427428

428429
qgspaintenginehack.h
430+
qgsscaleutils.h
429431
)
430432

431433
IF (QT_MOBILITY_LOCATION_FOUND)

‎src/core/qgsscaleutils.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

‎src/core/qgsscaleutils.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/***************************************************************************
2+
qgscaleutils.h
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 <QString>
17+
#include <QStringList>
18+
19+
#ifndef QGSSCALEUTILS_H
20+
#define QGSSCALEUTILS_H
21+
22+
class CORE_EXPORT QgsScaleUtils
23+
{
24+
public:
25+
/** save scales to the given file
26+
* @param fileName the name of the output file
27+
* @param scales the list of scales to save
28+
* @param errorMessage it will contain the error message if something
29+
* went wrong
30+
* @return true on success and false if failed
31+
*/
32+
static bool saveScaleList( const QString &fileName, const QStringList &scales, QString &errorMessage );
33+
34+
/** load scales from the given file
35+
* @param fileName the name of the file to process
36+
* @param scales it will contain loaded scales
37+
* @param errorMessage it will contain the error message if something
38+
* went wrong
39+
* @return true on success and false if failed
40+
*/
41+
static bool loadScaleList( const QString &fileName, QStringList &scales, QString &errorMessage );
42+
};
43+
44+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.