Skip to content
Permalink
Browse files

Handle changes to existing predefined scales

* reject invalid changes
* reposition the changed scale to maintain correct order
* avoid creating duplicates
* remove leading zeroes from denominator
  • Loading branch information
SebDieBln committed Dec 8, 2015
1 parent 33ea2a0 commit 4921d9e95706474ddc199bb73d9c3d2f82b73fc3
Showing with 41 additions and 7 deletions.
  1. +37 −7 src/app/qgsoptions.cpp
  2. +4 −0 src/app/qgsoptions.h
@@ -692,6 +692,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
addScaleToScaleList( scale );
}
}
connect( mListGlobalScales, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( scaleItemChanged( QListWidgetItem* ) ) );

//
// Color palette
@@ -2152,21 +2153,50 @@ void QgsOptions::on_mButtonExportColors_clicked()

QListWidgetItem* QgsOptions::addScaleToScaleList( const QString &newScale )
{
QListWidgetItem* newItem;
newItem = mListGlobalScales->findItems( newScale, Qt::MatchExactly ).value( 0 );
if ( newItem )
return newItem;
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 = newScale.split( ":" ).value( 1 ).toInt();
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 = new QListWidgetItem( newScale );

newItem->setData( Qt::UserRole, newItem->text() );
newItem->setFlags( Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mListGlobalScales->insertItem( i, newItem );
return 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();

@@ -215,6 +218,7 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
void saveDefaultDatumTransformations();

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

protected:
QgisAppStyleSheet* mStyleSheetBuilder;

0 comments on commit 4921d9e

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