128 changes: 127 additions & 1 deletion src/app/qgsprojectproperties.cpp
Original file line number Diff line number Diff line change
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 @@ -39,12 +40,13 @@

//qt includes
#include <QColorDialog>
#include <QInputDialog>
#include <QFileDialog>
#include <QHeaderView> // Qt 4.4
#include <QMessageBox>

//stdc++ includes


QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *parent, Qt::WFlags fl )
: QDialog( parent, fl )
, mMapCanvas( mapCanvas )
Expand Down Expand Up @@ -113,6 +115,22 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
myColor = QColor( myRedInt, myGreenInt, myBlueInt );
pbnCanvasColor->setColor( myColor );

//get project scales
QStringList myScales = QgsProject::instance()->readListEntry( "Scales", "/ScalesList" );
if ( !myScales.isEmpty() )
{
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 );
}
}

grpProjectScales->setChecked( QgsProject::instance()->readBoolEntry( "Scales", "/useProjectScales" ) );

QgsMapLayer* currentLayer = 0;

QStringList noIdentifyLayerIdList = QgsProject::instance()->readListEntry( "Identify", "/disabledLayers" );
Expand Down Expand Up @@ -423,6 +441,33 @@ void QgsProjectProperties::apply()
QgsProject::instance()->writeEntry( "Gui", "/CanvasColorGreenPart", myColor.green() );
QgsProject::instance()->writeEntry( "Gui", "/CanvasColorBluePart", myColor.blue() );

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

if ( !myScales.isEmpty() )
{
QgsProject::instance()->writeEntry( "Scales", "/ScalesList", myScales );
QgsProject::instance()->writeEntry( "Scales", "/useProjectScales", grpProjectScales->isChecked() );
}
else
{
QgsProject::instance()->removeEntry( "Scales", "/" );
}

//use global or project scales depending on checkbox state
if ( grpProjectScales->isChecked() )
{
emit scalesChanged( myScales );
}
else
{
emit scalesChanged();
}

QStringList noIdentifyLayerList;
for ( int i = 0; i < twIdentifyLayers->rowCount(); i++ )
{
Expand Down Expand Up @@ -695,6 +740,87 @@ void QgsProjectProperties::on_pbnWMSSetUsedSRS_clicked()
mWMSList->addItems( crsList.values() );
}

void QgsProjectProperties::on_pbnAddScale_clicked()
{
int myScale = QInputDialog::getInt(
this,
tr( "Enter scale" ),
tr( "Scale denominator" ),
-1,
1
);

if ( myScale != -1 )
{
QListWidgetItem* newItem = new QListWidgetItem( lstScales );
newItem->setText( QString( "1:%1" ).arg( myScale ) );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
lstScales->addItem( newItem );
lstScales->setCurrentItem( newItem );
}
}

void QgsProjectProperties::on_pbnRemoveScale_clicked()
{
int currentRow = lstScales->currentRow();
QListWidgetItem* itemToRemove = lstScales->takeItem( currentRow );
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
25 changes: 25 additions & 0 deletions src/app/qgsprojectproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
*/
void on_pbnCanvasColor_clicked();

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

/*! Let the user remove a scale from the list of project scales
* used in scale combobox instead of global ones
* @note added in QGIS 2.0
*/
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 Expand Up @@ -117,6 +139,9 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
//! Signal used to inform listeners that the mouse display precision may have changed
void displayPrecisionChanged();

//! Signal used to inform listeners that project scale list may have chnaged
void scalesChanged( const QStringList &scales = QStringList() );

//! let listening canvases know to refresh
void refresh();

Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
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
5 changes: 5 additions & 0 deletions src/core/qgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ const QString GEOWKT =
* @note deprecated in 1.8 due to violation of coding conventions (globals
* should be in all caps).
*/

const QString PROJECT_SCALES =
"1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
"1:10000,1:5000,1:2500,1:1000,1:500";

#ifndef _MSC_VER
Q_DECL_DEPRECATED
#endif
Expand Down
86 changes: 86 additions & 0 deletions src/core/qgsscaleutils.cpp
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
47 changes: 34 additions & 13 deletions src/gui/qgsscalecombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,17 @@
* *
***************************************************************************/

#include "qgis.h"
#include "qgsscalecombobox.h"

#include <QAbstractItemView>
#include <QSettings>

QgsScaleComboBox::QgsScaleComboBox( QWidget* parent ) : QComboBox( parent )
{
// make combobox editable and populate with predefined scales
setEditable( true );
addItem( "1:1000000" );
addItem( "1:500000" );
addItem( "1:250000" );
addItem( "1:100000" );
addItem( "1:50000" );
addItem( "1:25000" );
addItem( "1:10000" );
addItem( "1:5000" );
addItem( "1:2500" );
addItem( "1:1000" );
addItem( "1:500" );
updateScales();

setEditable( true );
setInsertPolicy( QComboBox::NoInsert );
setCompleter( 0 );
}
Expand All @@ -43,6 +34,36 @@ QgsScaleComboBox::~QgsScaleComboBox()
{
}

void QgsScaleComboBox::updateScales( const QStringList &scales )
{
QStringList myScalesList;
QString oldScale = currentText();

if ( scales.isEmpty() )
{
QSettings settings;
QString myScales = settings.value( "Map/scales", PROJECT_SCALES ).toString();
if ( !myScales.isEmpty() )
{
myScalesList = myScales.split( "," );
}
}
else
{
QStringList::const_iterator scaleIt = scales.constBegin();
for ( ; scaleIt != scales.constEnd(); ++scaleIt )
{
myScalesList.append( *scaleIt );
}
}

blockSignals( true );
clear();
addItems( myScalesList );
setEditText( oldScale );
blockSignals( false );
}

void QgsScaleComboBox::showPopup()
{
QComboBox::showPopup();
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgsscalecombobox.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox
QgsScaleComboBox( QWidget* parent = 0 );
virtual ~QgsScaleComboBox();

public slots:
void updateScales( const QStringList &scales = QStringList() );

protected:
void showPopup();
};
Expand Down
110 changes: 97 additions & 13 deletions src/ui/qgsoptionsbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<x>0</x>
<y>0</y>
<width>760</width>
<height>1005</height>
<height>887</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
Expand Down Expand Up @@ -858,7 +858,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>760</width>
<width>630</width>
<height>625</height>
</rect>
</property>
Expand Down Expand Up @@ -1104,9 +1104,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>760</width>
<height>506</height>
<y>-313</y>
<width>762</width>
<height>750</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_4">
Expand Down Expand Up @@ -1360,6 +1360,90 @@
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="groupBox_15">
<property name="title">
<string>Predefined scales</string>
</property>
<layout class="QGridLayout" name="gridLayout_26">
<item row="0" column="0">
<widget class="QListWidget" name="mListGlobalScales"/>
</item>
<item row="0" column="1">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QToolButton" name="pbnAddScale">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionNewAttribute.png</normaloff>:/images/themes/default/mActionNewAttribute.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="pbnRemoveScale">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionDeleteAttribute.png</normaloff>:/images/themes/default/mActionDeleteAttribute.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="pbnDefaultScaleValues">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionCopySelected.png</normaloff>:/images/themes/default/mActionCopySelected.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="pbnImportScales">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionFolder.png</normaloff>:/images/themes/default/mActionFolder.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="pbnExportScales">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionFileSave.png</normaloff>:/images/themes/default/mActionFileSave.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
Expand Down Expand Up @@ -1391,8 +1475,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>778</width>
<height>436</height>
<width>270</width>
<height>93</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_10">
Expand Down Expand Up @@ -1472,7 +1556,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>760</width>
<width>571</width>
<height>627</height>
</rect>
</property>
Expand Down Expand Up @@ -1848,8 +1932,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>778</width>
<height>436</height>
<width>425</width>
<height>417</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_15">
Expand Down Expand Up @@ -2022,8 +2106,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>760</width>
<height>551</height>
<width>519</width>
<height>584</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_17">
Expand Down Expand Up @@ -2119,7 +2203,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>760</width>
<width>355</width>
<height>554</height>
</rect>
</property>
Expand Down
69 changes: 66 additions & 3 deletions src/ui/qgsprojectpropertiesbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>604</width>
<height>456</height>
<height>588</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -295,6 +295,69 @@
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="grpProjectScales">
<property name="title">
<string>Project scales</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_7">
<item row="0" column="0">
<widget class="QListWidget" name="lstScales"/>
</item>
<item row="0" column="1">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QToolButton" name="pbnAddScale">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionNewAttribute.png</normaloff>:/images/themes/default/mActionNewAttribute.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="pbnRemoveScale">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionDeleteAttribute.png</normaloff>:/images/themes/default/mActionDeleteAttribute.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="pbnImportScales">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionFolder.png</normaloff>:/images/themes/default/mActionFolder.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="pbnExportScales">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionFileSave.png</normaloff>:/images/themes/default/mActionFileSave.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab2">
Expand Down Expand Up @@ -694,8 +757,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>705</width>
<height>781</height>
<width>637</width>
<height>808</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
Expand Down