Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add check to make sure minScale<=maxScale.
Those two fields are present in the vector layer properties. It
was possible to set a minScale bigger than the maxScale.
Now, the maxScale is forced to minScale if the user tries to
set it to a lower value.
  • Loading branch information
Patrick Valsecchi committed Mar 18, 2016
1 parent 01c2cfb commit 4f18701
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 63 deletions.
8 changes: 6 additions & 2 deletions python/gui/qgsscalecombobox.sip
Expand Up @@ -17,9 +17,11 @@ class QgsScaleComboBox : QComboBox
//! Function to set the selected scale from text
bool setScaleString( const QString& scaleTxt );
//! Function to read the selected scale as double
double scale();
double scale() const;
//! Function to set the selected scale from double
void setScale( double scale );
//! Function to read the min scale
double minScale() const;

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

signals:
//! Signal is emitted when *user* has finished editing/selecting a new scale.
void scaleChanged();
void scaleChanged( double scale );

public slots:
void updateScales( const QStringList &scales = QStringList() );
//! Function to set the min scale
void setMinScale( double scale );

protected:
void showPopup();
Expand Down
9 changes: 7 additions & 2 deletions python/gui/qgsscalewidget.sip
Expand Up @@ -26,9 +26,11 @@ class QgsScaleWidget : QWidget
//! Function to set the selected scale from text
bool setScaleString( const QString& scaleTxt );
//! Function to read the selected scale as double
double scale();
double scale() const;
//! Function to set the selected scale from double
void setScale( double scale );
//! Function to read the min scale
double minScale() const;

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

//! Function to set the min scale
void setMinScale( double scale );

signals:
//! Signal is emitted when *user* has finished editing/selecting a new scale.
void scaleChanged();
void scaleChanged( double scale );


};
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -2057,7 +2057,7 @@ void QgisApp::createStatusBar()
mScaleEdit->setToolTip( tr( "Current map scale (formatted as x:y)" ) );

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

if ( QgsMapCanvas::rotationEnabled() )
{
Expand Down
44 changes: 25 additions & 19 deletions src/gui/qgsscalecombobox.cpp
Expand Up @@ -24,7 +24,7 @@
#include <QSettings>
#include <QLineEdit>

QgsScaleComboBox::QgsScaleComboBox( QWidget* parent ) : QComboBox( parent ), mScale( 1.0 )
QgsScaleComboBox::QgsScaleComboBox( QWidget* parent ) : QComboBox( parent ), mScale( 1.0 ), mMinScale( 0.0 )
{
updateScales();

Expand Down Expand Up @@ -126,6 +126,11 @@ bool QgsScaleComboBox::setScaleString( const QString& scaleTxt )
{
bool ok;
double newScale = toDouble( scaleTxt, &ok );
double oldScale = mScale;
if ( newScale < mMinScale )
{
newScale = mMinScale;
}
if ( ! ok )
{
return false;
Expand All @@ -135,12 +140,16 @@ bool QgsScaleComboBox::setScaleString( const QString& scaleTxt )
mScale = newScale;
setEditText( toString( mScale ) );
clearFocus();
if ( mScale != oldScale )
{
emit scaleChanged( mScale );
}
return true;
}
}

//! Function to read the selected scale as double
double QgsScaleComboBox::scale()
double QgsScaleComboBox::scale() const
{
return mScale;
}
Expand All @@ -154,34 +163,24 @@ void QgsScaleComboBox::setScale( double scale )
//! Slot called when QComboBox has changed
void QgsScaleComboBox::fixupScale()
{
double newScale;
double oldScale = mScale;
bool ok, userSetScale;
QStringList txtList = currentText().split( ':' );
userSetScale = txtList.size() != 2;
bool userSetScale = txtList.size() != 2;

// QgsDebugMsg( QString( "entered with oldScale: %1" ).arg( oldScale ) );
newScale = toDouble( currentText(), &ok );
bool ok;
double newScale = toDouble( currentText(), &ok );

// Valid string representation
if ( ok && ( newScale != oldScale ) )
if ( ok )
{
// if a user types scale = 2345, we transform to 1:2345
if ( userSetScale && newScale >= 1.0 )
{
mScale = 1 / newScale;
newScale = 1 / newScale;
}
else
{
mScale = newScale;
}
setScale( mScale );
emit scaleChanged();
setScale( newScale );
}
else
{
// Invalid string representation or same scale
// Reset to the old
setScale( mScale );
}
}
Expand Down Expand Up @@ -241,4 +240,11 @@ double QgsScaleComboBox::toDouble( const QString& scaleString, bool * returnOk )
return scale;
}


void QgsScaleComboBox::setMinScale( double scale )
{
mMinScale = scale;
if ( mScale < scale )
{
setScale( scale );
}
}
9 changes: 7 additions & 2 deletions src/gui/qgsscalecombobox.h
Expand Up @@ -36,9 +36,11 @@ class GUI_EXPORT QgsScaleComboBox : public QComboBox
//! Function to set the selected scale from text
bool setScaleString( const QString& scaleTxt );
//! Function to read the selected scale as double
double scale();
double scale() const;
//! Function to set the selected scale from double
void setScale( double scale );
//! Function to read the min scale
double minScale() const { return mMinScale; }

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

signals:
//! Signal is emitted when *user* has finished editing/selecting a new scale.
void scaleChanged();
void scaleChanged( double scale );

public slots:
void updateScales( const QStringList &scales = QStringList() );
//! Function to set the min scale
void setMinScale( double scale );

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

private:
double mScale;
double mMinScale;
};

#endif // QGSSCALECOMBOBOX_H
1 change: 1 addition & 0 deletions src/gui/qgsscalerangewidget.cpp
Expand Up @@ -43,6 +43,7 @@ QgsScaleRangeWidget::QgsScaleRangeWidget( QWidget *parent )

mMinimumScaleWidget = new QgsScaleWidget( this );
mMaximumScaleWidget = new QgsScaleWidget( this );
connect( mMinimumScaleWidget, SIGNAL( scaleChanged( double ) ), mMaximumScaleWidget, SLOT( setMinScale( double ) ) );
mMinimumScaleWidget->setShowCurrentScaleButton( true );
mMaximumScaleWidget->setShowCurrentScaleButton( true );
reloadProjectScales();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsscalewidget.cpp
Expand Up @@ -37,7 +37,7 @@ QgsScaleWidget::QgsScaleWidget( QWidget *parent )
layout->addWidget( mCurrentScaleButton );
mCurrentScaleButton->hide();

connect( mScaleComboBox, SIGNAL( scaleChanged() ), this, SIGNAL( scaleChanged() ) );
connect( mScaleComboBox, SIGNAL( scaleChanged( double ) ), this, SIGNAL( scaleChanged( double ) ) );
connect( mCurrentScaleButton, SIGNAL( clicked() ), this, SLOT( setScaleFromCanvas() ) );
}

Expand Down
11 changes: 9 additions & 2 deletions src/gui/qgsscalewidget.h
Expand Up @@ -32,6 +32,8 @@ class GUI_EXPORT QgsScaleWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY( bool showCurrentScaleButton READ showCurrentScaleButton WRITE setShowCurrentScaleButton )
Q_PROPERTY( bool scale READ scale WRITE setScale NOTIFY scaleChanged )
Q_PROPERTY( bool minScale READ minScale WRITE setMinScale )

public:
explicit QgsScaleWidget( QWidget *parent = nullptr );
Expand All @@ -51,9 +53,11 @@ class GUI_EXPORT QgsScaleWidget : public QWidget
//! Function to set the selected scale from text
bool setScaleString( const QString& scaleTxt ) { return mScaleComboBox->setScaleString( scaleTxt ); }
//! Function to read the selected scale as double
double scale() { return mScaleComboBox->scale();}
double scale() const { return mScaleComboBox->scale();}
//! Function to set the selected scale from double
void setScale( double scale ) { return mScaleComboBox->setScale( scale ); }
//! Function to read the min scale
double minScale() const { return mScaleComboBox->minScale(); }

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

//! Function to set the min scale
void setMinScale( double scale ) { mScaleComboBox->setMinScale( scale ); }

signals:
//! Signal is emitted when *user* has finished editing/selecting a new scale.
void scaleChanged();
void scaleChanged( double scale );

private:
QgsScaleComboBox* mScaleComboBox;
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgsunitselectionwidget.cpp
Expand Up @@ -28,8 +28,9 @@ QgsMapUnitScaleDialog::QgsMapUnitScaleDialog( QWidget* parent )
mSpinBoxMaxSize->setShowClearButton( false );
connect( mCheckBoxMinScale, SIGNAL( toggled( bool ) ), this, SLOT( configureMinComboBox() ) );
connect( mCheckBoxMaxScale, SIGNAL( toggled( bool ) ), this, SLOT( configureMaxComboBox() ) );
connect( mComboBoxMinScale, SIGNAL( scaleChanged() ), this, SLOT( configureMaxComboBox() ) );
connect( mComboBoxMaxScale, SIGNAL( scaleChanged() ), this, SLOT( configureMinComboBox() ) );
connect( mComboBoxMinScale, SIGNAL( scaleChanged( double ) ), this, SLOT( configureMaxComboBox() ) );
connect( mComboBoxMinScale, SIGNAL( scaleChanged( double ) ), mComboBoxMaxScale, SLOT( setMinScale( double ) ) );
connect( mComboBoxMaxScale, SIGNAL( scaleChanged( double ) ), this, SLOT( configureMinComboBox() ) );

connect( mCheckBoxMinSize, SIGNAL( toggled( bool ) ), mSpinBoxMinSize, SLOT( setEnabled( bool ) ) );
connect( mCheckBoxMaxSize, SIGNAL( toggled( bool ) ), mSpinBoxMaxSize, SLOT( setEnabled( bool ) ) );
Expand Down

0 comments on commit 4f18701

Please sign in to comment.