Skip to content
Permalink
Browse files

Merge pull request #2558 from SebDieBln/OrderScales

Keep scales ordered in Settings > Options > Predefined scales (fixes #8320)
  • Loading branch information
nyalldawson committed Dec 13, 2015
2 parents 710b9d2 + edea51b commit 7f674a4e965968451dd95eb2c7b91e0d932d3714
Showing with 130 additions and 38 deletions.
  1. +58 −22 src/app/qgsoptions.cpp
  2. +6 −0 src/app/qgsoptions.h
  3. +57 −16 src/app/qgsprojectproperties.cpp
  4. +9 −0 src/app/qgsprojectproperties.h
@@ -687,15 +687,12 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
if ( !myPaths.isEmpty() )
{
QStringList myScalesList = myPaths.split( ',' );
QStringList::const_iterator scaleIt = myScalesList.constBegin();
for ( ; scaleIt != myScalesList.constEnd(); ++scaleIt )
Q_FOREACH ( const QString& scale, myScalesList )
{
QListWidgetItem* newItem = new QListWidgetItem( mListGlobalScales );
newItem->setText( *scaleIt );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mListGlobalScales->addItem( newItem );
addScaleToScaleList( scale );
}
}
connect( mListGlobalScales, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( scaleItemChanged( QListWidgetItem* ) ) );

//
// Color palette
@@ -1923,10 +1920,7 @@ void QgsOptions::on_pbnAddScale_clicked()

if ( myScale != -1 )
{
QListWidgetItem* newItem = new QListWidgetItem( mListGlobalScales );
newItem->setText( QString( "1:%1" ).arg( myScale ) );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mListGlobalScales->addItem( newItem );
QListWidgetItem* newItem = addScaleToScaleList( QString( "1:%1" ).arg( myScale ) );
mListGlobalScales->setCurrentItem( newItem );
}
}
@@ -1943,13 +1937,9 @@ void QgsOptions::on_pbnDefaultScaleValues_clicked()
mListGlobalScales->clear();

QStringList myScalesList = PROJECT_SCALES.split( ',' );
QStringList::const_iterator scaleIt = myScalesList.constBegin();
for ( ; scaleIt != myScalesList.constEnd(); ++scaleIt )
Q_FOREACH ( const QString& scale, myScalesList )
{
QListWidgetItem* newItem = new QListWidgetItem( mListGlobalScales );
newItem->setText( *scaleIt );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mListGlobalScales->addItem( newItem );
addScaleToScaleList( scale );
}
}

@@ -1969,13 +1959,9 @@ void QgsOptions::on_pbnImportScales_clicked()
QgsDebugMsg( msg );
}

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

@@ -2164,3 +2150,53 @@ void QgsOptions::on_mButtonExportColors_clicked()
return;
}
}

QListWidgetItem* QgsOptions::addScaleToScaleList( const QString &newScale )
{
QListWidgetItem* newItem = new QListWidgetItem( newScale );
addScaleToScaleList( newItem );
return newItem;
}

void QgsOptions::addScaleToScaleList( QListWidgetItem* newItem )
{
// If the new scale already exists, delete it.
QListWidgetItem* duplicateItem = mListGlobalScales->findItems( newItem->text(), Qt::MatchExactly ).value( 0 );
delete duplicateItem;

int newDenominator = newItem->text().split( ":" ).value( 1 ).toInt();
int i;
for ( i = 0; i < mListGlobalScales->count(); i++ )
{
int denominator = mListGlobalScales->item( i )->text().split( ":" ).value( 1 ).toInt();
if ( newDenominator > denominator )
break;
}

newItem->setData( Qt::UserRole, newItem->text() );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mListGlobalScales->insertItem( i, newItem );
}

void QgsOptions::scaleItemChanged( QListWidgetItem* changedScaleItem )
{
// Check if the new value is valid, restore the old value if not.
QRegExp regExp( "1:0*[1-9]\\d*" );
if ( regExp.exactMatch( changedScaleItem->text() ) )
{
//Remove leading zeroes from the denominator
regExp.setPattern( "1:0*" );
changedScaleItem->setText( changedScaleItem->text().replace( regExp, "1:" ) );
}
else
{
QMessageBox::warning( this, tr( "Invalid scale" ), tr( "The text you entered is not a valid scale." ) );
changedScaleItem->setText( changedScaleItem->data( Qt::UserRole ).toString() );
}

// Take the changed item out of the list and re-add it. This keeps things ordered and creates correct meta-data for the changed item.
int row = mListGlobalScales->row( changedScaleItem );
mListGlobalScales->takeItem( row );
addScaleToScaleList( changedScaleItem );
mListGlobalScales->setCurrentItem( changedScaleItem );
}
@@ -188,6 +188,9 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
/** Auto slot executed when the active page in the option section widget is changed */
void on_mOptionsStackedWidget_currentChanged( int theIndx );

/** A scale in the list of predefined scales changed */
void scaleItemChanged( QListWidgetItem* changedScaleItem );

/* Load the list of drivers available in GDAL */
void loadGdalDriverList();

@@ -214,6 +217,9 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption

void saveDefaultDatumTransformations();

QListWidgetItem* addScaleToScaleList( const QString &newScale );
void addScaleToScaleList( QListWidgetItem* newItem );

protected:
QgisAppStyleSheet* mStyleSheetBuilder;
QMap<QString, QVariant> mStyleSheetNewOpts;
@@ -183,15 +183,12 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
QStringList myScales = QgsProject::instance()->readListEntry( "Scales", "/ScalesList" );
if ( !myScales.isEmpty() )
{
QStringList::const_iterator scaleIt = myScales.constBegin();
for ( ; scaleIt != myScales.constEnd(); ++scaleIt )
Q_FOREACH ( const QString& scale, myScales )
{
QListWidgetItem* newItem = new QListWidgetItem( lstScales );
newItem->setText( *scaleIt );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
lstScales->addItem( newItem );
addScaleToScaleList( scale );
}
}
connect( lstScales, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( scaleItemChanged( QListWidgetItem* ) ) );

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

@@ -1362,10 +1359,7 @@ void QgsProjectProperties::on_pbnAddScale_clicked()

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 );
QListWidgetItem* newItem = addScaleToScaleList( QString( "1:%1" ).arg( myScale ) );
lstScales->setCurrentItem( newItem );
}
}
@@ -1393,13 +1387,9 @@ void QgsProjectProperties::on_pbnImportScales_clicked()
QgsDebugMsg( msg );
}

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

@@ -1826,3 +1816,54 @@ void QgsProjectProperties::on_mButtonExportColors_clicked()
return;
}
}

QListWidgetItem* QgsProjectProperties::addScaleToScaleList( const QString &newScale )
{
// TODO QGIS3: Rework the scale list widget to be a reusable piece of code, see PR #2558
QListWidgetItem* newItem = new QListWidgetItem( newScale );
addScaleToScaleList( newItem );
return newItem;
}

void QgsProjectProperties::addScaleToScaleList( QListWidgetItem* newItem )
{
// If the new scale already exists, delete it.
QListWidgetItem* duplicateItem = lstScales->findItems( newItem->text(), Qt::MatchExactly ).value( 0 );
delete duplicateItem;

int newDenominator = newItem->text().split( ":" ).value( 1 ).toInt();
int i;
for ( i = 0; i < lstScales->count(); i++ )
{
int denominator = lstScales->item( i )->text().split( ":" ).value( 1 ).toInt();
if ( newDenominator > denominator )
break;
}

newItem->setData( Qt::UserRole, newItem->text() );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
lstScales->insertItem( i, newItem );
}

void QgsProjectProperties::scaleItemChanged( QListWidgetItem* changedScaleItem )
{
// Check if the new value is valid, restore the old value if not.
QRegExp regExp( "1:0*[1-9]\\d*" );
if ( regExp.exactMatch( changedScaleItem->text() ) )
{
//Remove leading zeroes from the denominator
regExp.setPattern( "1:0*" );
changedScaleItem->setText( changedScaleItem->text().replace( regExp, "1:" ) );
}
else
{
QMessageBox::warning( this, tr( "Invalid scale" ), tr( "The text you entered is not a valid scale." ) );
changedScaleItem->setText( changedScaleItem->data( Qt::UserRole ).toString() );
}

// Take the changed item out of the list and re-add it. This keeps things ordered and creates correct meta-data for the changed item.
int row = lstScales->row( changedScaleItem );
lstScales->takeItem( row );
addScaleToScaleList( changedScaleItem );
lstScales->setCurrentItem( changedScaleItem );
}
@@ -90,6 +90,9 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
/** Let the user load scales from file */
void on_pbnExportScales_clicked();

/** A scale in the list of project scales changed */
void scaleItemChanged( QListWidgetItem* changedScaleItem );

/*!
* Slots for WMS project settings
*/
@@ -208,6 +211,12 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
//! Populates list with ellipsoids from Sqlite3 db
void populateEllipsoidList();

//! Create a new scale item and add it to the list of scales
QListWidgetItem* addScaleToScaleList( const QString &newScale );

//! Add a scale item to the list of scales
void addScaleToScaleList( QListWidgetItem* newItem );

static const char * GEO_NONE_DESC;

};

0 comments on commit 7f674a4

Please sign in to comment.
You can’t perform that action at this time.