Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
scales import/export
  • Loading branch information
alexbruy committed Jul 18, 2012
1 parent fc3e49b commit f5dce33
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 3 deletions.
55 changes: 55 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsgenericprojectionselector.h"
#include "qgscoordinatereferencesystem.h"
#include "qgstolerance.h"
#include "qgsscaleutils.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsproject.h"

Expand Down Expand Up @@ -1478,3 +1479,57 @@ void QgsOptions::on_pbnDefaultScaleValues_clicked()
mListGlobalScales->addItem( newItem );
}
}

void QgsOptions::on_pbnImportScales_clicked()
{
QString fileName = QFileDialog::getOpenFileName( this, tr( "Load scales" ), ".",
tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
{
return;
}

QString msg;
QStringList myScales;
if ( !QgsScaleUtils::loadScaleList( fileName, myScales, msg ) )
{
QgsDebugMsg( msg );
}

QStringList::const_iterator scaleIt = myScales.constBegin();
for ( ; scaleIt != myScales.constEnd(); ++scaleIt )
{
QListWidgetItem* newItem = new QListWidgetItem( mListGlobalScales );
newItem->setText( *scaleIt );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mListGlobalScales->addItem( newItem );
}
}

void QgsOptions::on_pbnExportScales_clicked()
{
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save scales" ), ".",
tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
{
return;
}

// ensure the user never ommited the extension from the file name
if ( !fileName.toLower().endsWith( ".xml" ) )
{
fileName += ".xml";
}

QStringList myScales;
for ( int i = 0; i < mListGlobalScales->count(); ++i )
{
myScales.append( mListGlobalScales->item( i )->text() );
}

QString msg;
if ( !QgsScaleUtils::saveScaleList( fileName, myScales, msg ) )
{
QgsDebugMsg( msg );
}
}
16 changes: 13 additions & 3 deletions src/app/qgsoptions.h
Expand Up @@ -129,24 +129,34 @@ class QgsOptions : public QDialog, private Ui::QgsOptionsBase
void on_mBrowseCacheDirectory_clicked();
void on_mClearCache_clicked();

/* Let the user add a scale to the list of scales
/** Let the user add a scale to the list of scales
* used in scale combobox
* @note added in QGIS 2.0
*/
void on_pbnAddScale_clicked();

/* Let the user remove a scale from the list of scales
/** Let the user remove a scale from the list of scales
* used in scale combobox
* @note added in QGIS 2.0
*/
void on_pbnRemoveScale_clicked();

/* Let the user restore default scales
/** Let the user restore default scales
* used in scale combobox
* @note added in QGIS 2.0
*/
void on_pbnDefaultScaleValues_clicked();

/** Let the user load scales from file
* @note added in QGIS 2.0
*/
void on_pbnImportScales_clicked();

/** Let the user load scales from file
* @note added in QGIS 2.0
*/
void on_pbnExportScales_clicked();

/** Auto slot executed when the active page in the main widget stack is changed
* @note added in 2.0
*/
Expand Down
56 changes: 56 additions & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -30,6 +30,7 @@
#include "qgsrenderer.h"
#include "qgssnappingdialog.h"
#include "qgsrasterlayer.h"
#include "qgsscaleutils.h"
#include "qgsgenericprojectionselector.h"
#include "qgsstylev2.h"
#include "qgssymbolv2.h"
Expand All @@ -40,6 +41,7 @@
//qt includes
#include <QColorDialog>
#include <QInputDialog>
#include <QFileDialog>
#include <QHeaderView> // Qt 4.4
#include <QMessageBox>

Expand Down Expand Up @@ -765,6 +767,60 @@ void QgsProjectProperties::on_pbnRemoveScale_clicked()
delete itemToRemove;
}

void QgsProjectProperties::on_pbnImportScales_clicked()
{
QString fileName = QFileDialog::getOpenFileName( this, tr( "Load scales" ), ".",
tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
{
return;
}

QString msg;
QStringList myScales;
if ( !QgsScaleUtils::loadScaleList( fileName, myScales, msg ) )
{
QgsDebugMsg( msg );
}

QStringList::const_iterator scaleIt = myScales.constBegin();
for ( ; scaleIt != myScales.constEnd(); ++scaleIt )
{
QListWidgetItem* newItem = new QListWidgetItem( lstScales );
newItem->setText( *scaleIt );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
lstScales->addItem( newItem );
}
}

void QgsProjectProperties::on_pbnExportScales_clicked()
{
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save scales" ), ".",
tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
{
return;
}

// ensure the user never ommited the extension from the file name
if ( !fileName.toLower().endsWith( ".xml" ) )
{
fileName += ".xml";
}

QStringList myScales;
for ( int i = 0; i < lstScales->count(); ++i )
{
myScales.append( lstScales->item( i )->text() );
}

QString msg;
if ( !QgsScaleUtils::saveScaleList( fileName, myScales, msg ) )
{
QgsDebugMsg( msg );
}
}

void QgsProjectProperties::populateStyles()
{
// Styles - taken from qgsstylev2managerdialog
Expand Down
10 changes: 10 additions & 0 deletions src/app/qgsprojectproperties.h
Expand Up @@ -95,6 +95,16 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
*/
void on_pbnRemoveScale_clicked();

/** Let the user load scales from file
* @note added in QGIS 2.0
*/
void on_pbnImportScales_clicked();

/** Let the user load scales from file
* @note added in QGIS 2.0
*/
void on_pbnExportScales_clicked();

/*!
* Slots for WMS project settings
*/
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -186,6 +186,7 @@ SET(QGIS_CORE_SRCS
qgsspatialindex.cpp

qgspaintenginehack.cpp
qgsscaleutils.cpp
)

IF(WIN32)
Expand Down Expand Up @@ -426,6 +427,7 @@ SET(QGIS_CORE_HDRS
qgsspatialindex.h

qgspaintenginehack.h
qgsscaleutils.h
)

IF (QT_MOBILITY_LOCATION_FOUND)
Expand Down
86 changes: 86 additions & 0 deletions src/core/qgsscaleutils.cpp
@@ -0,0 +1,86 @@
/***************************************************************************
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( "qgsScales" );
root.setAttribute( "version", "1.0" );
doc.appendChild( root );

for ( int i = 0; i < scales.count(); ++i )
{
QDomElement el = doc.createElement( "scale" );
el.setAttribute( "value", scales.at( i ) );
root.appendChild( el );
}

QFile file( fileName );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
errorMessage = QString( "Cannot write file %1:\n%2." ).arg( fileName ).arg( 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 = QString( "Cannot read file %1:\n%2." ).arg( fileName ).arg( file.errorString() );
return false;
}

QDomDocument doc;
QString errorStr;
int errorLine;
int errorColumn;

if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
{
errorMessage = QString( "Parse error at line %1, column %2:\n%3" )
.arg( errorLine )
.arg( errorColumn )
.arg( errorStr );
return false;
}

QDomElement root = doc.documentElement();
if ( root.tagName() != "qgsScales" )
{
errorMessage = "The file is not an scales exchange file.";
return false;
}

QDomElement child = root.firstChildElement();
while ( !child.isNull() )
{
scales.append( child.attribute( "value" ) );
child = child.nextSiblingElement();
}

return true;
}
44 changes: 44 additions & 0 deletions src/core/qgsscaleutils.h
@@ -0,0 +1,44 @@
/***************************************************************************
qgscaleutils.h
----------------------
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 <QString>
#include <QStringList>

#ifndef QGSSCALEUTILS_H
#define QGSSCALEUTILS_H

class CORE_EXPORT QgsScaleUtils
{
public:
/** save scales to the given file
* @param fileName the name of the output file
* @param scales the list of scales to save
* @param errorMessage it will contain the error message if something
* went wrong
* @return true on success and false if failed
*/
static bool saveScaleList( const QString &fileName, const QStringList &scales, QString &errorMessage );

/** load scales from the given file
* @param fileName the name of the file to process
* @param scales it will contain loaded scales
* @param errorMessage it will contain the error message if something
* went wrong
* @return true on success and false if failed
*/
static bool loadScaleList( const QString &fileName, QStringList &scales, QString &errorMessage );
};

#endif

0 comments on commit f5dce33

Please sign in to comment.