Skip to content

Commit bd3fd74

Browse files
committedMar 20, 2016
Merge pull request #2863 from pvalsecc/minMaxScale
Add check to make sure minScale<=maxScale.
2 parents a3c76c6 + 4f18701 commit bd3fd74

10 files changed

+107
-63
lines changed
 

‎python/gui/qgsscalecombobox.sip

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ class QgsScaleComboBox : QComboBox
1717
//! Function to set the selected scale from text
1818
bool setScaleString( const QString& scaleTxt );
1919
//! Function to read the selected scale as double
20-
double scale();
20+
double scale() const;
2121
//! Function to set the selected scale from double
2222
void setScale( double scale );
23+
//! Function to read the min scale
24+
double minScale() const;
2325

2426
//! Helper function to convert a double to scale string
2527
// Performs rounding, so an exact representation is not to
@@ -30,10 +32,12 @@ class QgsScaleComboBox : QComboBox
3032

3133
signals:
3234
//! Signal is emitted when *user* has finished editing/selecting a new scale.
33-
void scaleChanged();
35+
void scaleChanged( double scale );
3436

3537
public slots:
3638
void updateScales( const QStringList &scales = QStringList() );
39+
//! Function to set the min scale
40+
void setMinScale( double scale );
3741

3842
protected:
3943
void showPopup();

‎python/gui/qgsscalewidget.sip

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ class QgsScaleWidget : QWidget
2626
//! Function to set the selected scale from text
2727
bool setScaleString( const QString& scaleTxt );
2828
//! Function to read the selected scale as double
29-
double scale();
29+
double scale() const;
3030
//! Function to set the selected scale from double
3131
void setScale( double scale );
32+
//! Function to read the min scale
33+
double minScale() const;
3234

3335
//! Helper function to convert a double to scale string
3436
// Performs rounding, so an exact representation is not to
@@ -43,9 +45,12 @@ class QgsScaleWidget : QWidget
4345
//! assign the current scale from the map canvas
4446
void setScaleFromCanvas();
4547

48+
//! Function to set the min scale
49+
void setMinScale( double scale );
50+
4651
signals:
4752
//! Signal is emitted when *user* has finished editing/selecting a new scale.
48-
void scaleChanged();
53+
void scaleChanged( double scale );
4954

5055

5156
};

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,7 @@ void QgisApp::createStatusBar()
20572057
mScaleEdit->setToolTip( tr( "Current map scale (formatted as x:y)" ) );
20582058

20592059
statusBar()->addPermanentWidget( mScaleEdit, 0 );
2060-
connect( mScaleEdit, SIGNAL( scaleChanged() ), this, SLOT( userScale() ) );
2060+
connect( mScaleEdit, SIGNAL( scaleChanged( double ) ), this, SLOT( userScale() ) );
20612061

20622062
if ( QgsMapCanvas::rotationEnabled() )
20632063
{

‎src/gui/qgsscalecombobox.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <QSettings>
2525
#include <QLineEdit>
2626

27-
QgsScaleComboBox::QgsScaleComboBox( QWidget* parent ) : QComboBox( parent ), mScale( 1.0 )
27+
QgsScaleComboBox::QgsScaleComboBox( QWidget* parent ) : QComboBox( parent ), mScale( 1.0 ), mMinScale( 0.0 )
2828
{
2929
updateScales();
3030

@@ -126,6 +126,11 @@ bool QgsScaleComboBox::setScaleString( const QString& scaleTxt )
126126
{
127127
bool ok;
128128
double newScale = toDouble( scaleTxt, &ok );
129+
double oldScale = mScale;
130+
if ( newScale < mMinScale )
131+
{
132+
newScale = mMinScale;
133+
}
129134
if ( ! ok )
130135
{
131136
return false;
@@ -135,12 +140,16 @@ bool QgsScaleComboBox::setScaleString( const QString& scaleTxt )
135140
mScale = newScale;
136141
setEditText( toString( mScale ) );
137142
clearFocus();
143+
if ( mScale != oldScale )
144+
{
145+
emit scaleChanged( mScale );
146+
}
138147
return true;
139148
}
140149
}
141150

142151
//! Function to read the selected scale as double
143-
double QgsScaleComboBox::scale()
152+
double QgsScaleComboBox::scale() const
144153
{
145154
return mScale;
146155
}
@@ -154,34 +163,24 @@ void QgsScaleComboBox::setScale( double scale )
154163
//! Slot called when QComboBox has changed
155164
void QgsScaleComboBox::fixupScale()
156165
{
157-
double newScale;
158-
double oldScale = mScale;
159-
bool ok, userSetScale;
160166
QStringList txtList = currentText().split( ':' );
161-
userSetScale = txtList.size() != 2;
167+
bool userSetScale = txtList.size() != 2;
162168

163-
// QgsDebugMsg( QString( "entered with oldScale: %1" ).arg( oldScale ) );
164-
newScale = toDouble( currentText(), &ok );
169+
bool ok;
170+
double newScale = toDouble( currentText(), &ok );
165171

166172
// Valid string representation
167-
if ( ok && ( newScale != oldScale ) )
173+
if ( ok )
168174
{
169175
// if a user types scale = 2345, we transform to 1:2345
170176
if ( userSetScale && newScale >= 1.0 )
171177
{
172-
mScale = 1 / newScale;
178+
newScale = 1 / newScale;
173179
}
174-
else
175-
{
176-
mScale = newScale;
177-
}
178-
setScale( mScale );
179-
emit scaleChanged();
180+
setScale( newScale );
180181
}
181182
else
182183
{
183-
// Invalid string representation or same scale
184-
// Reset to the old
185184
setScale( mScale );
186185
}
187186
}
@@ -241,4 +240,11 @@ double QgsScaleComboBox::toDouble( const QString& scaleString, bool * returnOk )
241240
return scale;
242241
}
243242

244-
243+
void QgsScaleComboBox::setMinScale( double scale )
244+
{
245+
mMinScale = scale;
246+
if ( mScale < scale )
247+
{
248+
setScale( scale );
249+
}
250+
}

‎src/gui/qgsscalecombobox.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox
3636
//! Function to set the selected scale from text
3737
bool setScaleString( const QString& scaleTxt );
3838
//! Function to read the selected scale as double
39-
double scale();
39+
double scale() const;
4040
//! Function to set the selected scale from double
4141
void setScale( double scale );
42+
//! Function to read the min scale
43+
double minScale() const { return mMinScale; }
4244

4345
//! Helper function to convert a double to scale string
4446
// Performs rounding, so an exact representation is not to
@@ -49,10 +51,12 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox
4951

5052
signals:
5153
//! Signal is emitted when *user* has finished editing/selecting a new scale.
52-
void scaleChanged();
54+
void scaleChanged( double scale );
5355

5456
public slots:
5557
void updateScales( const QStringList &scales = QStringList() );
58+
//! Function to set the min scale
59+
void setMinScale( double scale );
5660

5761
protected:
5862
void showPopup() override;
@@ -62,6 +66,7 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox
6266

6367
private:
6468
double mScale;
69+
double mMinScale;
6570
};
6671

6772
#endif // QGSSCALECOMBOBOX_H

‎src/gui/qgsscalerangewidget.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ QgsScaleRangeWidget::QgsScaleRangeWidget( QWidget *parent )
4343

4444
mMinimumScaleWidget = new QgsScaleWidget( this );
4545
mMaximumScaleWidget = new QgsScaleWidget( this );
46+
connect( mMinimumScaleWidget, SIGNAL( scaleChanged( double ) ), mMaximumScaleWidget, SLOT( setMinScale( double ) ) );
4647
mMinimumScaleWidget->setShowCurrentScaleButton( true );
4748
mMaximumScaleWidget->setShowCurrentScaleButton( true );
4849
reloadProjectScales();

‎src/gui/qgsscalewidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ QgsScaleWidget::QgsScaleWidget( QWidget *parent )
3737
layout->addWidget( mCurrentScaleButton );
3838
mCurrentScaleButton->hide();
3939

40-
connect( mScaleComboBox, SIGNAL( scaleChanged() ), this, SIGNAL( scaleChanged() ) );
40+
connect( mScaleComboBox, SIGNAL( scaleChanged( double ) ), this, SIGNAL( scaleChanged( double ) ) );
4141
connect( mCurrentScaleButton, SIGNAL( clicked() ), this, SLOT( setScaleFromCanvas() ) );
4242
}
4343

‎src/gui/qgsscalewidget.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class GUI_EXPORT QgsScaleWidget : public QWidget
3232
{
3333
Q_OBJECT
3434
Q_PROPERTY( bool showCurrentScaleButton READ showCurrentScaleButton WRITE setShowCurrentScaleButton )
35+
Q_PROPERTY( bool scale READ scale WRITE setScale NOTIFY scaleChanged )
36+
Q_PROPERTY( bool minScale READ minScale WRITE setMinScale )
3537

3638
public:
3739
explicit QgsScaleWidget( QWidget *parent = nullptr );
@@ -51,9 +53,11 @@ class GUI_EXPORT QgsScaleWidget : public QWidget
5153
//! Function to set the selected scale from text
5254
bool setScaleString( const QString& scaleTxt ) { return mScaleComboBox->setScaleString( scaleTxt ); }
5355
//! Function to read the selected scale as double
54-
double scale() { return mScaleComboBox->scale();}
56+
double scale() const { return mScaleComboBox->scale();}
5557
//! Function to set the selected scale from double
5658
void setScale( double scale ) { return mScaleComboBox->setScale( scale ); }
59+
//! Function to read the min scale
60+
double minScale() const { return mScaleComboBox->minScale(); }
5761

5862
//! Helper function to convert a double to scale string
5963
// Performs rounding, so an exact representation is not to
@@ -68,9 +72,12 @@ class GUI_EXPORT QgsScaleWidget : public QWidget
6872
//! assign the current scale from the map canvas
6973
void setScaleFromCanvas();
7074

75+
//! Function to set the min scale
76+
void setMinScale( double scale ) { mScaleComboBox->setMinScale( scale ); }
77+
7178
signals:
7279
//! Signal is emitted when *user* has finished editing/selecting a new scale.
73-
void scaleChanged();
80+
void scaleChanged( double scale );
7481

7582
private:
7683
QgsScaleComboBox* mScaleComboBox;

‎src/gui/qgsunitselectionwidget.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ QgsMapUnitScaleDialog::QgsMapUnitScaleDialog( QWidget* parent )
2828
mSpinBoxMaxSize->setShowClearButton( false );
2929
connect( mCheckBoxMinScale, SIGNAL( toggled( bool ) ), this, SLOT( configureMinComboBox() ) );
3030
connect( mCheckBoxMaxScale, SIGNAL( toggled( bool ) ), this, SLOT( configureMaxComboBox() ) );
31-
connect( mComboBoxMinScale, SIGNAL( scaleChanged() ), this, SLOT( configureMaxComboBox() ) );
32-
connect( mComboBoxMaxScale, SIGNAL( scaleChanged() ), this, SLOT( configureMinComboBox() ) );
31+
connect( mComboBoxMinScale, SIGNAL( scaleChanged( double ) ), this, SLOT( configureMaxComboBox() ) );
32+
connect( mComboBoxMinScale, SIGNAL( scaleChanged( double ) ), mComboBoxMaxScale, SLOT( setMinScale( double ) ) );
33+
connect( mComboBoxMaxScale, SIGNAL( scaleChanged( double ) ), this, SLOT( configureMinComboBox() ) );
3334

3435
connect( mCheckBoxMinSize, SIGNAL( toggled( bool ) ), mSpinBoxMinSize, SLOT( setEnabled( bool ) ) );
3536
connect( mCheckBoxMaxSize, SIGNAL( toggled( bool ) ), mSpinBoxMaxSize, SLOT( setEnabled( bool ) ) );

‎tests/src/gui/testqgsscalecombobox.cpp

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TestQgsScaleComboBox : public QObject
2929
Q_OBJECT
3030
public:
3131
TestQgsScaleComboBox()
32-
: s( 0 )
32+
: s( nullptr )
3333
{}
3434

3535
private slots:
@@ -39,7 +39,10 @@ class TestQgsScaleComboBox : public QObject
3939
void cleanup();// will be called after every testfunction.
4040
void basic();
4141
void slot_test();
42+
void min_test();
4243
private:
44+
void enterScale( const QString& scale );
45+
void enterScale( double scale );
4346
QgsScaleComboBox *s;
4447
};
4548

@@ -48,73 +51,56 @@ void TestQgsScaleComboBox::initTestCase()
4851
QgsApplication::init();
4952
QgsApplication::initQgis();
5053

51-
// Create a combobox, and init with predefined scales.
52-
s = new QgsScaleComboBox();
53-
QgsDebugMsg( QString( "Initial scale is %1" ).arg( s->scaleString() ) );
5454
}
5555

5656
void TestQgsScaleComboBox::cleanupTestCase()
5757
{
58-
delete s;
5958
QgsApplication::exitQgis();
6059
}
6160

6261
void TestQgsScaleComboBox::init()
6362
{
63+
// Create a combobox, and init with predefined scales.
64+
s = new QgsScaleComboBox();
65+
QgsDebugMsg( QString( "Initial scale is %1" ).arg( s->scaleString() ) );
6466
}
6567

6668
void TestQgsScaleComboBox::basic()
6769
{
68-
QLineEdit *l = s->lineEdit();
69-
7070
// Testing conversion from "1:nnn".
71-
l->setText( "" );
72-
QTest::keyClicks( l, "1:2345" );
73-
QTest::keyClick( l, Qt::Key_Return );
71+
enterScale( "1:2345" );
7472
QCOMPARE( s->scaleString(), QString( "1:%1" ).arg( QLocale::system().toString( 2345 ) ) );
7573
QCOMPARE( s->scale(), 1.0 / 2345.0 );
7674

7775
// Testing conversion from number to "1:x"
78-
l->setText( "" );
79-
QTest::keyClicks( l, QLocale::system().toString( 0.02 ) );
80-
QTest::keyClick( l, Qt::Key_Return );
76+
enterScale( 0.02 );
8177
QCOMPARE( s->scaleString(), QString( "1:%1" ).arg( QLocale::system().toString( 50 ) ) );
8278
QCOMPARE( s->scale(), 0.02 );
8379

8480
// Testing conversion from number to "1:x"
85-
l->setText( "" );
86-
QTest::keyClicks( l, QLocale::system().toString( 42 ) );
87-
QTest::keyClick( l, Qt::Key_Return );
81+
enterScale( 42 );
8882
QCOMPARE( s->scaleString(), QString( "1:%1" ).arg( QLocale::system().toString( 42 ) ) );
8983
QCOMPARE( s->scale(), 1.0 / 42.0 );
9084

9185
// Testing conversion from number to "1:x,000"
92-
l->setText( "" );
9386
QString str = QString( "1%01000%01000" ).arg( QLocale::system().groupSeparator() );
94-
QTest::keyClicks( l, str );
95-
QTest::keyClick( l, Qt::Key_Return );
87+
enterScale( str );
9688
QCOMPARE( s->scaleString(), QString( "1:%1" ).arg( str ) );
9789
QCOMPARE( s->scale(), 1.0 / 1000000.0 );
9890

9991
// Testing conversion from number to "1:x,000" with wonky separators
10092
//(eg four digits between thousands, which should be fixed automatically)
101-
l->setText( "" );
10293
str = QString( "1%010000%01000" ).arg( QLocale::system().groupSeparator() );
10394
QString fixedStr = QString( "10%01000%01000" ).arg( QLocale::system().groupSeparator() );
104-
QTest::keyClicks( l, str );
105-
QTest::keyClick( l, Qt::Key_Return );
95+
enterScale( str );
10696
QCOMPARE( s->scaleString(), QString( "1:%1" ).arg( fixedStr ) );
10797
QCOMPARE( s->scale(), 1.0 / 10000000.0 );
10898

10999
// Testing rounding and conversion from illegal
110100

111-
l->setText( "" );
112-
QTest::keyClicks( l, QLocale::system().toString( 0.24 ) );
113-
QTest::keyClick( l, Qt::Key_Return );
101+
enterScale( 0.24 );
114102

115-
l->setText( "" );
116-
QTest::keyClicks( l, "1:x:2" );
117-
QTest::keyClick( l, Qt::Key_Return );
103+
enterScale( "1:x:2" );
118104
QCOMPARE( s->scaleString(), QString( "1:%1" ).arg( QLocale::system().toString( 4 ) ) );
119105
QCOMPARE( s->scale(), 0.25 );
120106

@@ -138,19 +124,48 @@ void TestQgsScaleComboBox::basic()
138124
void TestQgsScaleComboBox::slot_test()
139125
{
140126
QLineEdit *l = s->lineEdit();
141-
l->setText( "" );
142127

143-
QSignalSpy spyScaleChanged( s, SIGNAL( scaleChanged() ) );
128+
QSignalSpy spyScaleChanged( s, SIGNAL( scaleChanged( double ) ) );
144129
QSignalSpy spyFixup( l, SIGNAL( editingFinished() ) );
145130

146-
QTest::keyClicks( l, QLocale::system().toString( 0.02 ) );
147-
QTest::keyClick( l, Qt::Key_Return );
131+
enterScale( 0.02 );
148132
QCOMPARE( spyFixup.count(), 2 ); // Qt emits twice!?
149133
QCOMPARE( spyScaleChanged.count(), 1 );
150134
}
151135

136+
void TestQgsScaleComboBox::min_test()
137+
{
138+
s->setMinScale( 0.01 );
139+
140+
enterScale( 0.02 );
141+
QCOMPARE( s->scale(), 0.02 );
142+
143+
enterScale( 0.002 );
144+
QCOMPARE( s->scale(), 0.01 );
145+
146+
s->setMinScale( 0.015 );
147+
QCOMPARE( s->scale(), 0.015 );
148+
149+
s->setScale( 0.5 );
150+
QCOMPARE( s->scale(), 0.5 );
151+
}
152+
153+
void TestQgsScaleComboBox::enterScale( const QString& scale )
154+
{
155+
QLineEdit *l = s->lineEdit();
156+
l->setText( "" );
157+
QTest::keyClicks( l, scale );
158+
QTest::keyClick( l, Qt::Key_Return );
159+
}
160+
161+
void TestQgsScaleComboBox::enterScale( double scale )
162+
{
163+
enterScale( QLocale::system().toString( scale ) );
164+
}
165+
152166
void TestQgsScaleComboBox::cleanup()
153167
{
168+
delete s;
154169
}
155170

156171
QTEST_MAIN( TestQgsScaleComboBox )

0 commit comments

Comments
 (0)
Please sign in to comment.