Skip to content

Instantly share code, notes, and snippets.

@kimaidou
Created May 7, 2012 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimaidou/2627175 to your computer and use it in GitHub Desktop.
Save kimaidou/2627175 to your computer and use it in GitHub Desktop.
Project Scales List
diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp
index bf22859..2df8ade 100644
--- a/src/app/qgisapp.cpp
+++ b/src/app/qgisapp.cpp
@@ -2797,6 +2797,11 @@ void QgisApp::fileNew( bool thePromptToSaveFlag )
myRenderer->setProjectionsEnabled( settings.value( "/Projections/otfTransformEnabled", 0 ).toBool() );
updateCRSStatusBar();
+
+ // update the scale list combobox
+ QStringList projectScaleList;
+ projectScaleList = prj->readListEntry( "ProjectScaleList", "/" );
+ mScaleEdit->updateScales( projectScaleList );
// set the initial map tool
mMapCanvas->setMapTool( mMapTools.mPan );
@@ -2914,6 +2919,13 @@ void QgisApp::fileOpen()
emit projectRead(); // let plug-ins know that we've read in a new
// project so that they can check any project
// specific plug-in state
+
+ // update the scale list combobox
+ QStringList projectScaleList;
+ double currentScale = mMapCanvas->scale();
+ projectScaleList = QgsProject::instance()->readListEntry( "ProjectScaleList", "/" );
+ mScaleEdit->updateScales( projectScaleList );
+ showScale( currentScale );
// add this to the list of recently used project files
saveRecentProjectPath( fullPath, settings );
@@ -2983,6 +2995,13 @@ bool QgisApp::addProject( QString projectFile )
// add this to the list of recently used project files
saveRecentProjectPath( projectFile, settings );
+
+ // update the scale list combobox
+ QStringList projectScaleList;
+ double currentScale = mMapCanvas->scale();
+ projectScaleList = QgsProject::instance()->readListEntry( "ProjectScaleList", "/" );
+ mScaleEdit->updateScales( projectScaleList );
+ showScale( currentScale );
QApplication::restoreOverrideCursor();
@@ -6245,7 +6264,12 @@ void QgisApp::projectProperties()
// Line below was commented out by wonder three years ago (r4949).
// It is needed to refresh scale bar after changing display units.
connect( pp, SIGNAL( refresh() ), mMapCanvas, SLOT( refresh() ) );
-
+
+ // pass projectScalesChanged signal
+ // to QgsScaleComboBox
+ connect( pp, SIGNAL( projectScalesChanged( QStringList ) ),
+ mScaleEdit, SLOT( updateScales( QStringList ) ) );
+
QgsMapRenderer* myRender = mMapCanvas->mapRenderer();
bool wasProjected = myRender->hasCrsTransformEnabled();
long oldCRSID = myRender->destinationCrs().srsid();
diff --git a/src/app/qgsprojectproperties.cpp b/src/app/qgsprojectproperties.cpp
index b1d4658..4ce1fc6 100644
--- a/src/app/qgsprojectproperties.cpp
+++ b/src/app/qgsprojectproperties.cpp
@@ -276,6 +276,16 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
twWFSLayers->setRowCount( j );
twWFSLayers->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
+ // Project Scales List
+ QStringList projectScaleList;
+ projectScaleList = QgsProject::instance()->readListEntry( "ProjectScaleList", "/" );
+ grpProjectScaleList->setChecked( projectScaleList.size() > 0 );
+ if ( grpProjectScaleList->isChecked() )
+ {
+ mProjectScaleList->addItems( projectScaleList );
+ }
+ grpProjectScaleList->setChecked( mProjectScaleList->count() > 0 );
+
restoreState();
}
@@ -505,7 +515,40 @@ void QgsProjectProperties::apply()
}
}
QgsProject::instance()->writeEntry( "WFSLayers", "/", wfsLayerList );
+
+
+ // Project Scales List write
+ QString initProjectScaleList;
+ initProjectScaleList = QgsProject::instance()->readListEntry( "ProjectScaleList", "/" ).join(",");
+ QStringList projectScaleList;
+ if ( grpProjectScaleList->isChecked() && mProjectScaleList->count() == 0 )
+ {
+ QMessageBox::information( this,
+ tr( "Project Scales List" ),
+ tr( "No scale for this project. Disabling scale restriction." ) );
+ grpProjectScaleList->setChecked( false );
+ }
+ QgsProject::instance()->removeEntry( "ProjectScaleList", "/" );
+ if ( grpProjectScaleList->isChecked() )
+ {
+ for ( int i = 0; i < mProjectScaleList->count(); i++ )
+ {
+ projectScaleList << mProjectScaleList->item( i )->text();
+ }
+ QgsProject::instance()->writeEntry( "ProjectScaleList", "/", projectScaleList );
+ }
+ else
+ {
+ projectScaleList.clear();
+ QgsProject::instance()->removeEntry( "ProjectScaleList", "/" );
+ }
+ if ( initProjectScaleList != projectScaleList.join(",") )
+ {
+ emit projectScalesChanged( projectScaleList );
+ mMapCanvas->updateScale();
+ }
+
//todo XXX set canvas color
emit refresh();
}
@@ -677,3 +720,68 @@ void QgsProjectProperties::on_pbnWMSSetUsedSRS_clicked()
mWMSList->clear();
mWMSList->addItems( crsList.values() );
}
+
+// Project Scale List add/remove
+void QgsProjectProperties::on_pbnScaleListRemoveScale_clicked()
+{
+ foreach( QListWidgetItem *item, mProjectScaleList->selectedItems() )
+ {
+ delete item;
+ }
+}
+
+bool stringSortAsInt( const QString &s1, const QString &s2 )
+{
+ return( s1.toInt() > s2.toInt() );
+}
+
+void QgsProjectProperties::on_pbnScaleListAddScale_clicked()
+{
+ QString newScale = mProjectScaleInput->text();
+ bool scOk = true;
+ if ( newScale.isEmpty() )
+ {
+ QMessageBox::information( this,
+ tr( "Project Scale Addition" ),
+ tr( "Scale cannot be empty" ) );
+ scOk = false;
+ }
+ else
+ {
+ int newScaleInt = newScale.toInt(&scOk);
+ if (!scOk)
+ {
+ QMessageBox::information( this,
+ tr( "Project Scale Addition" ),
+ tr( "Scale must be an integer" ) );
+ mProjectScaleInput->setText( QString("") );
+ }
+ }
+ if( scOk )
+ {
+ QStringList projectScaleList;
+ for ( int i = 0; i < mProjectScaleList->count(); i++ )
+ {
+ projectScaleList << mProjectScaleList->item( i )->text();
+ if( newScale == mProjectScaleList->item( i )->text() )
+ {
+ scOk = false;
+ }
+ }
+ if (scOk)
+ {
+ projectScaleList << newScale;
+ qSort(projectScaleList.begin(), projectScaleList.end(), stringSortAsInt);
+ mProjectScaleList->clear();
+ mProjectScaleList->addItems( projectScaleList );
+ mProjectScaleInput->setText( QString("") );
+ }
+ else
+ {
+ QMessageBox::information( this,
+ tr( "Project Scale Addition" ),
+ tr( "Scale is already in the list" ) );
+ }
+ }
+
+}
diff --git a/src/app/qgsprojectproperties.h b/src/app/qgsprojectproperties.h
index 899c64a..9ce9f43 100644
--- a/src/app/qgsprojectproperties.h
+++ b/src/app/qgsprojectproperties.h
@@ -90,6 +90,12 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
void on_pbnWMSAddSRS_clicked();
void on_pbnWMSRemoveSRS_clicked();
void on_pbnWMSSetUsedSRS_clicked();
+
+ /*!
+ * Slots for project scales settings
+ */
+ void on_pbnScaleListRemoveScale_clicked();
+ void on_pbnScaleListAddScale_clicked();
/*!
* Slot to show the context help for this dialog
@@ -107,6 +113,9 @@ class QgsProjectProperties : public QDialog, private Ui::QgsProjectPropertiesBas
//! Signal used to inform listeners that the mouse display precision may have changed
void displayPrecisionChanged();
+ /** Signal emitted when the user defined scales change */
+ void projectScalesChanged( QStringList );
+
//! let listening canvases know to refresh
void refresh();
diff --git a/src/gui/qgsscalecombobox.cpp b/src/gui/qgsscalecombobox.cpp
index 5d9e5eb..3669a73 100644
--- a/src/gui/qgsscalecombobox.cpp
+++ b/src/gui/qgsscalecombobox.cpp
@@ -18,23 +18,15 @@
#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" );
-
+ QStringList defaultScaleList;
+ updateScales( defaultScaleList );
setInsertPolicy( QComboBox::NoInsert );
}
@@ -68,3 +60,24 @@ void QgsScaleComboBox::showPopup()
view()->setCurrentIndex( model()->index( idx, 0 ) );
blockSignals( false );
}
+
+
+void QgsScaleComboBox::updateScales( QStringList list)
+{
+ // If list is empty, load default list
+ if ( list.isEmpty() )
+ {
+ list << "1:1000000" << "1:500000" << "1:250000" << "1:100000" << "1:50000" ;
+ list << "1:25000" << "1:10000" << "1:5000" << "1:2500" << "1:1000" << "1:500" ;
+ }
+ else
+ {
+ // Need to add "1:" before each scale taken from project configuration
+ list.replaceInStrings(QRegExp("^(.*)$"), "1:\\1");
+ }
+ // populate the list
+ blockSignals( true );
+ clear();
+ addItems( list );
+ blockSignals( false );
+}
diff --git a/src/gui/qgsscalecombobox.h b/src/gui/qgsscalecombobox.h
index b1623a6..7e05a0e 100644
--- a/src/gui/qgsscalecombobox.h
+++ b/src/gui/qgsscalecombobox.h
@@ -30,9 +30,13 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox
public:
QgsScaleComboBox( QWidget* parent = 0 );
virtual ~QgsScaleComboBox();
+
+ public slots:
+ void updateScales( QStringList );
protected:
void showPopup();
+
};
#endif // QGSSCALECOMBOBOX_H
diff --git a/src/ui/qgsprojectpropertiesbase.ui b/src/ui/qgsprojectpropertiesbase.ui
index d8bd82c..e360e7c 100644
--- a/src/ui/qgsprojectpropertiesbase.ui
+++ b/src/ui/qgsprojectpropertiesbase.ui
@@ -190,7 +190,7 @@
</item>
</layout>
</widget>
- </item>
+ </item>
<item row="1" column="0">
<widget class="QGroupBox" name="btnGrpMapUnits">
<property name="title">
@@ -295,6 +295,41 @@
</layout>
</widget>
</item>
+ <item row="3" column="0">
+ <widget class="QGroupBox" name="grpProjectScaleList">
+ <property name="title">
+ <string>Project Scales</string>
+ </property>
+ <property name="checkable">
+ <bool>true</bool>
+ </property>
+ <property name="checked">
+ <bool>false</bool>
+ </property>
+ <layout class="QGridLayout" name="gridLayout_9">
+ <item row="0" column="0" colspan="3">
+ <widget class="QListWidget" name="mProjectScaleList"/>
+ </item>
+ <item row="1" column="0">
+ <widget class="QLineEdit" name="mProjectScaleInput"/>
+ </item>
+ <item row="1" column="1">
+ <widget class="QPushButton" name="pbnScaleListAddScale">
+ <property name="text">
+ <string>Add</string>
+ </property>
+ </widget>
+ </item>
+ <item row="1" column="2">
+ <widget class="QPushButton" name="pbnScaleListRemoveScale">
+ <property name="text">
+ <string>Remove</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ </item>
</layout>
</widget>
<widget class="QWidget" name="tab2">
@@ -329,7 +364,7 @@
<widget class="QgsProjectionSelector" name="projectionSelector" native="true"/>
</item>
</layout>
- </widget>
+ </widget>
<widget class="QWidget" name="tab3">
<attribute name="icon">
<iconset resource="../../images/images.qrc">
@@ -743,6 +778,9 @@
<tabstop>pbnWMSAddSRS</tabstop>
<tabstop>pbnWMSSetUsedSRS</tabstop>
<tabstop>pbnWMSRemoveSRS</tabstop>
+ <tabstop>mProjectScaleInput</tabstop>
+ <tabstop>pbnScaleListRemoveScale</tabstop>
+ <tabstop>pbnScaleListAddScale</tabstop>
</tabstops>
<resources>
<include location="../../images/images.qrc"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment