Skip to content

Commit cf99817

Browse files
committedMay 28, 2016
allow to lock scale to use magnification to zoom in/out
this also removes the option for different behavior of mouse wheel
1 parent 69a1718 commit cf99817

14 files changed

+491
-264
lines changed
 

‎python/core/qgsmapsettings.sip

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ class QgsMapSettings
3939
//! Set DPI used for conversion between real world units (e.g. mm) and pixels
4040
void setOutputDpi( int dpi );
4141

42-
//! Set the magnification factor.
43-
//! @note added in 2.16
44-
void setMagnificationFactor( double factor );
45-
//! Return the magnification factor.
42+
43+
/**
44+
* @brief setMagnificationFactor set the magnification factor
45+
* @param factor the factor of magnification
46+
* @note added in 2.16
47+
*/
48+
void setMagnificationFactor( double factor );
49+
50+
//! Return the magnification factor.
4651
//! @note added in 2.16
4752
double magnificationFactor() const;
4853

‎python/gui/qgsmapcanvas.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,12 @@ class QgsMapCanvas : QGraphicsView
279279
QgsMapLayer* currentLayer();
280280

281281
//! set wheel action and zoom factor (should be greater than 1)
282+
//! @deprecated
282283
void setWheelAction( WheelAction action, double factor = 2 );
283284

285+
//! set the wheel zoom factor
286+
void setWheelFactor( double factor );
287+
284288
//! Zoom in with fixed factor
285289
void zoomIn();
286290

‎src/app/qgisapp.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,13 +2136,15 @@ void QgisApp::createStatusBar()
21362136

21372137
mScaleWidget = new QgsStatusBarScaleWidget( mMapCanvas, statusBar() );
21382138
mScaleWidget->setFont( myFont );
2139+
connect( mScaleWidget, SIGNAL( scaleLockChanged( bool ) ), mMapCanvas, SLOT( setScaleLocked( bool ) ) );
21392140
statusBar()->addPermanentWidget( mScaleWidget, 0 );
21402141

21412142
// zoom widget
2142-
QSettings mySettings;
2143-
mMagnifierWidget = new QgsStatusBarMagnifierWidget( mMapCanvas, statusBar() );
2143+
mMagnifierWidget = new QgsStatusBarMagnifierWidget( statusBar() );
21442144
mMagnifierWidget->setFont( myFont );
2145-
mMagnifierWidget->setMagnificationLevel( mySettings.value( "/qgis/magnifier_level", 100 ).toInt() );
2145+
connect( mMapCanvas, SIGNAL( magnificationChanged( double ) ), mMagnifierWidget, SLOT( updateMagnification( double ) ) );
2146+
connect( mMagnifierWidget, SIGNAL( magnificationChanged( double ) ), mMapCanvas, SLOT( setMagnificationFactor( double ) ) );
2147+
mMagnifierWidget->updateMagnification( QSettings().value( "/qgis/magnifier_factor_default", 1.0 ).toDouble() );
21462148
statusBar()->addPermanentWidget( mMagnifierWidget, 0 );
21472149

21482150
if ( QgsMapCanvas::rotationEnabled() )
@@ -2689,9 +2691,8 @@ void QgisApp::createOverview()
26892691
// Anti Aliasing enabled by default as of QGIS 1.7
26902692
mMapCanvas->enableAntiAliasing( mySettings.value( "/qgis/enable_anti_aliasing", true ).toBool() );
26912693

2692-
int action = mySettings.value( "/qgis/wheel_action", 2 ).toInt();
26932694
double zoomFactor = mySettings.value( "/qgis/zoom_factor", 2 ).toDouble();
2694-
mMapCanvas->setWheelAction( static_cast< QgsMapCanvas::WheelAction >( action ), zoomFactor );
2695+
mMapCanvas->setWheelFactor( zoomFactor );
26952696

26962697
mMapCanvas->setCachingEnabled( mySettings.value( "/qgis/enable_render_caching", true ).toBool() );
26972698

@@ -8563,9 +8564,8 @@ void QgisApp::showOptionsDialog( QWidget *parent, const QString& currentPage )
85638564

85648565
mMapCanvas->enableAntiAliasing( mySettings.value( "/qgis/enable_anti_aliasing" ).toBool() );
85658566

8566-
int action = mySettings.value( "/qgis/wheel_action", 2 ).toInt();
85678567
double zoomFactor = mySettings.value( "/qgis/zoom_factor", 2 ).toDouble();
8568-
mMapCanvas->setWheelAction( static_cast< QgsMapCanvas::WheelAction >( action ), zoomFactor );
8568+
mMapCanvas->setWheelFactor( zoomFactor );
85698569

85708570
mMapCanvas->setCachingEnabled( mySettings.value( "/qgis/enable_render_caching", true ).toBool() );
85718571

@@ -8580,8 +8580,6 @@ void QgisApp::showOptionsDialog( QWidget *parent, const QString& currentPage )
85808580
layer->setLayerName( layer->originalName() );
85818581
}
85828582

8583-
mMagnifierWidget->setMagnificationLevel( mySettings.value( "/qgis/magnifier_level" ).toInt() );
8584-
85858583
//update any open compositions so they reflect new composer settings
85868584
//we have to push the changes to the compositions here, because compositions
85878585
//have no access to qgisapp and accordingly can't listen in to changes
@@ -8612,6 +8610,10 @@ void QgisApp::showOptionsDialog( QWidget *parent, const QString& currentPage )
86128610

86138611
mMapCanvas->setSegmentationTolerance( mySettings.value( "/qgis/segmentationTolerance", "0.01745" ).toDouble() );
86148612
mMapCanvas->setSegmentationToleranceType( QgsAbstractGeometryV2::SegmentationToleranceType( mySettings.value( "/qgis/segmentationToleranceType", "0" ).toInt() ) );
8613+
8614+
double factor = mySettings.value( "/qgis/magnifier_factor_default", 1.0 ).toDouble();
8615+
mMagnifierWidget->setDefaultFactor( factor );
8616+
mMagnifierWidget->updateMagnification( factor );
86158617
}
86168618

86178619
delete optionsDialog;

‎src/app/qgsoptions.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,14 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl )
607607
mSimplifyMaximumScaleComboBox->setScale( 1.0 / mSettings->value( "/qgis/simplifyMaxScale", 1 ).toFloat() );
608608

609609
// Magnifier
610-
doubleSpinBoxMagnifierDefault->setRange( 100, 1000 );
610+
double magnifierMin = 100 * mSettings->value( "/qgis/magnifier_factor_min", 0.1 ).toDouble();
611+
double magnifierMax = 100 * mSettings->value( "/qgis/magnifier_factor_max", 10 ).toDouble();
612+
double magnifierVal = 100 * mSettings->value( "/qgis/magnifier_factor_default", 1.0 ).toDouble();
613+
doubleSpinBoxMagnifierDefault->setRange( magnifierMin, magnifierMax );
611614
doubleSpinBoxMagnifierDefault->setSingleStep( 50 );
612615
doubleSpinBoxMagnifierDefault->setDecimals( 0 );
613616
doubleSpinBoxMagnifierDefault->setSuffix( "%" );
614-
doubleSpinBoxMagnifierDefault->setValue( mSettings->value( "/qgis/magnifier_level", 100 ).toInt() );
617+
doubleSpinBoxMagnifierDefault->setValue( magnifierVal );
615618

616619
// Default local simplification algorithm
617620
mSimplifyAlgorithmComboBox->addItem( tr( "Distance" ), ( int )QgsVectorSimplifyMethod::Distance );
@@ -731,7 +734,6 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl )
731734
}
732735
leTemplateFolder->setText( templateDirName );
733736

734-
cmbWheelAction->setCurrentIndex( mSettings->value( "/qgis/wheel_action", 2 ).toInt() );
735737
spinZoomFactor->setValue( mSettings->value( "/qgis/zoom_factor", 2 ).toDouble() );
736738

737739
// predefined scales for scale combobox
@@ -1224,7 +1226,7 @@ void QgsOptions::saveOptions()
12241226
mSettings->setValue( "/qgis/simplifyMaxScale", 1.0 / mSimplifyMaximumScaleComboBox->scale() );
12251227

12261228
// magnification
1227-
mSettings->setValue( "/qgis/magnifier_level", doubleSpinBoxMagnifierDefault->value() );
1229+
mSettings->setValue( "/qgis/magnifier_factor_default", doubleSpinBoxMagnifierDefault->value() / 100 );
12281230

12291231
//curve segmentation
12301232
int segmentationType = mToleranceTypeComboBox->itemData( mToleranceTypeComboBox->currentIndex() ).toInt();
@@ -1341,7 +1343,6 @@ void QgsOptions::saveOptions()
13411343
mSettings->setValue( "/qgis/default_measure_color_green", myColor.green() );
13421344
mSettings->setValue( "/qgis/default_measure_color_blue", myColor.blue() );
13431345

1344-
mSettings->setValue( "/qgis/wheel_action", cmbWheelAction->currentIndex() );
13451346
mSettings->setValue( "/qgis/zoom_factor", spinZoomFactor->value() );
13461347

13471348
//digitizing

‎src/app/qgsstatusbarmagnifierwidget.cpp

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
#include <QFont>
1818
#include <QHBoxLayout>
1919
#include <QLabel>
20+
#include <QSettings>
2021

2122
#include <qgsapplication.h>
2223
#include "qgsstatusbarmagnifierwidget.h"
23-
#include "qgsmapcanvas.h"
2424
#include "qgsdoublespinbox.h"
2525

26-
QgsStatusBarMagnifierWidget::QgsStatusBarMagnifierWidget(QgsMapCanvas *canvas, QWidget* parent) :
27-
QWidget( parent ),
28-
mCanvas( canvas ),
29-
mMagnifier( 100 ),
30-
mMagnifierMin( 100 ),
31-
mMagnifierMax( 1000 )
26+
QgsStatusBarMagnifierWidget::QgsStatusBarMagnifierWidget( QWidget* parent ) :
27+
QWidget( parent )
3228
{
29+
QSettings settings;
30+
int minimumFactor = ( int ) 100 * settings.value( "/qgis/magnifier_factor_min", 0.1 ).toDouble();
31+
int maximumFactor = ( int ) 100 * settings.value( "/qgis/magnifier_factor_max", 10 ).toDouble();
32+
int defaultFactor = ( int ) 100 * settings.value( "/qgis/magnifier_factor_default", 1.0 ).toDouble();
33+
3334
// label
3435
mLabel = new QLabel( this );
3536
mLabel->setMinimumWidth( 10 );
@@ -41,17 +42,17 @@ QgsStatusBarMagnifierWidget::QgsStatusBarMagnifierWidget(QgsMapCanvas *canvas, Q
4142

4243
mSpinBox = new QgsDoubleSpinBox( this );
4344
mSpinBox->setSuffix( "%" );
44-
mSpinBox->setClearValue( mMagnifierMin );
4545
mSpinBox->setKeyboardTracking( false );
4646
mSpinBox->setMaximumWidth( 120 );
4747
mSpinBox->setDecimals( 0 );
48-
mSpinBox->setRange( mMagnifierMin, mMagnifierMax );
48+
mSpinBox->setRange( minimumFactor, maximumFactor );
4949
mSpinBox->setWrapping( false );
5050
mSpinBox->setSingleStep( 50 );
5151
mSpinBox->setToolTip( tr( "Magnifier level" ) );
52+
mSpinBox->setClearValueMode( QgsDoubleSpinBox::CustomValue );
53+
mSpinBox->setClearValue( defaultFactor );
5254

53-
connect( mSpinBox, SIGNAL( valueChanged( double ) ), this,
54-
SLOT( updateMagnifier() ) );
55+
connect( mSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( setMagnification( double ) ) );
5556

5657
// layout
5758
mLayout = new QHBoxLayout( this );
@@ -62,17 +63,15 @@ QgsStatusBarMagnifierWidget::QgsStatusBarMagnifierWidget(QgsMapCanvas *canvas, Q
6263
mLayout->setSpacing( 0 );
6364

6465
setLayout( mLayout );
65-
66-
updateMagnifier();
6766
}
6867

6968
QgsStatusBarMagnifierWidget::~QgsStatusBarMagnifierWidget()
7069
{
7170
}
7271

73-
double QgsStatusBarMagnifierWidget::magnificationLevel()
72+
void QgsStatusBarMagnifierWidget::setDefaultFactor( double factor )
7473
{
75-
return mMagnifier;
74+
mSpinBox->setClearValue(( int )100*factor );
7675
}
7776

7877
void QgsStatusBarMagnifierWidget::setFont( const QFont& myFont )
@@ -81,24 +80,12 @@ void QgsStatusBarMagnifierWidget::setFont( const QFont& myFont )
8180
mSpinBox->setFont( myFont );
8281
}
8382

84-
bool QgsStatusBarMagnifierWidget::setMagnificationLevel( int level )
83+
void QgsStatusBarMagnifierWidget::updateMagnification( double factor )
8584
{
86-
bool rc = false;
87-
88-
if ( level >= mMagnifierMin && level <= mMagnifierMax )
89-
{
90-
mSpinBox->setValue( level );
91-
rc = true;
92-
}
93-
94-
return rc;
85+
mSpinBox->setValue( factor * 100 );
9586
}
9687

97-
void QgsStatusBarMagnifierWidget::updateMagnifier()
88+
void QgsStatusBarMagnifierWidget::setMagnification( double value )
9889
{
99-
// get current data
100-
mMagnifier = mSpinBox->value();
101-
102-
// update map canvas
103-
mCanvas->setMagnificationFactor( mMagnifier / double( mMagnifierMin ) );
90+
emit magnificationChanged( value / 100 );
10491
}

‎src/app/qgsstatusbarmagnifierwidget.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
class QLabel;
2121
class QFont;
2222
class QHBoxLayout;
23-
class QgsMapCanvas;
2423
class QgsDoubleSpinBox;
2524

2625
#include <QWidget>
@@ -40,39 +39,36 @@ class APP_EXPORT QgsStatusBarMagnifierWidget : public QWidget
4039
* @param parent is the parent widget
4140
* @param canvas the map canvas
4241
*/
43-
QgsStatusBarMagnifierWidget( QgsMapCanvas *canvas, QWidget* parent = 0 );
42+
QgsStatusBarMagnifierWidget( QWidget* parent = 0 );
4443

4544
/** Destructor */
4645
virtual ~QgsStatusBarMagnifierWidget();
4746

47+
void setDefaultFactor( double factor );
48+
4849
/** Set the font of the text
4950
* @param font the font to use
5051
*/
5152
void setFont( const QFont& font );
5253

53-
/** Returns the current magnification level
54-
* @return magnification level
55-
*/
56-
double magnificationLevel();
5754

58-
/** Set the magnification level
59-
* @param level the magnification level
60-
* @return true if the level is valid, false otherwise
61-
*/
62-
bool setMagnificationLevel( int level );
55+
public slots:
56+
//! will be triggered from map canvas changes (from mouse wheel, zoom)
57+
void updateMagnification( double factor );
58+
6359

6460
private slots:
61+
//! will be triggered form user input in spin box
62+
void setMagnification( double value );
63+
64+
signals:
65+
void magnificationChanged( double factor );
6566

66-
void updateMagnifier();
6767

6868
private:
69-
QgsMapCanvas *mCanvas;
7069
QHBoxLayout *mLayout;
7170
QLabel *mLabel;
7271
QgsDoubleSpinBox *mSpinBox;
73-
int mMagnifier;
74-
int mMagnifierMin;
75-
int mMagnifierMax;
7672
};
7773

7874
#endif

‎src/app/qgsstatusbarscalewidget.cpp

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,83 @@
1717
#include <QHBoxLayout>
1818
#include <QLabel>
1919
#include <QLineEdit>
20+
#include <QToolButton>
2021
#include <QValidator>
2122

2223
#include "qgsstatusbarscalewidget.h"
2324

2425
#include "qgsmapcanvas.h"
2526
#include "qgsscalecombobox.h"
2627

27-
QgsStatusBarScaleWidget::QgsStatusBarScaleWidget(QgsMapCanvas *canvas, QWidget *parent)
28-
: QWidget(parent)
28+
QgsStatusBarScaleWidget::QgsStatusBarScaleWidget( QgsMapCanvas *canvas, QWidget *parent )
29+
: QWidget( parent )
2930
, mMapCanvas( canvas )
3031
{
31-
// add a label to show current scale
32-
mLabel = new QLabel( QString(), this );
33-
mLabel->setObjectName( "mScaleLable" );
34-
mLabel->setMinimumWidth( 10 );
35-
//mScaleLabel->setMaximumHeight( 20 );
36-
mLabel->setMargin( 3 );
37-
mLabel->setAlignment( Qt::AlignCenter );
38-
mLabel->setFrameStyle( QFrame::NoFrame );
39-
mLabel->setText( tr( "Scale" ) );
40-
mLabel->setToolTip( tr( "Current map scale" ) );
41-
42-
mScale = new QgsScaleComboBox( this );
43-
mScale->setObjectName( "mScaleEdit" );
44-
// seems setFont() change font only for popup not for line edit,
45-
// so we need to set font for it separately
46-
mScale->setMinimumWidth( 10 );
47-
mScale->setContentsMargins( 0, 0, 0, 0 );
48-
mScale->setWhatsThis( tr( "Displays the current map scale" ) );
49-
mScale->setToolTip( tr( "Current map scale (formatted as x:y)" ) );
50-
51-
// layout
52-
mLayout = new QHBoxLayout( this );
53-
mLayout->addWidget( mLabel );
54-
mLayout->addWidget( mScale );
55-
mLayout->setContentsMargins( 0, 0, 0, 0 );
56-
mLayout->setAlignment( Qt::AlignRight );
57-
mLayout->setSpacing( 0 );
58-
59-
setLayout( mLayout );
60-
61-
connect( mScale, SIGNAL( scaleChanged( double ) ), this, SLOT( userScale() ) );
32+
// add a label to show current scale
33+
mLabel = new QLabel( QString(), this );
34+
mLabel->setObjectName( "mScaleLable" );
35+
mLabel->setMinimumWidth( 10 );
36+
//mScaleLabel->setMaximumHeight( 20 );
37+
mLabel->setMargin( 3 );
38+
mLabel->setAlignment( Qt::AlignCenter );
39+
mLabel->setFrameStyle( QFrame::NoFrame );
40+
mLabel->setText( tr( "Scale" ) );
41+
mLabel->setToolTip( tr( "Current map scale" ) );
42+
43+
mScale = new QgsScaleComboBox( this );
44+
mScale->setObjectName( "mScaleEdit" );
45+
// seems setFont() change font only for popup not for line edit,
46+
// so we need to set font for it separately
47+
mScale->setMinimumWidth( 10 );
48+
mScale->setContentsMargins( 0, 0, 0, 0 );
49+
mScale->setWhatsThis( tr( "Displays the current map scale" ) );
50+
mScale->setToolTip( tr( "Current map scale (formatted as x:y)" ) );
51+
52+
mLockButton = new QToolButton( this );
53+
mLockButton->setIcon( QIcon( QgsApplication::getThemeIcon( "/locked.svg" ) ) );
54+
mLockButton->setToolTip( tr( "Lock the scale to use magnifier to zoom in or out." ) );
55+
mLockButton->setCheckable( true );
56+
mLockButton->setChecked( false );
57+
58+
// layout
59+
mLayout = new QHBoxLayout( this );
60+
mLayout->addWidget( mLabel );
61+
mLayout->addWidget( mScale );
62+
mLayout->addWidget( mLockButton );
63+
mLayout->setContentsMargins( 0, 0, 0, 0 );
64+
mLayout->setAlignment( Qt::AlignRight );
65+
mLayout->setSpacing( 0 );
66+
67+
setLayout( mLayout );
68+
69+
connect( mScale, SIGNAL( scaleChanged( double ) ), this, SLOT( userScale() ) );
70+
71+
connect( mLockButton, SIGNAL( toggled( bool ) ), this, SIGNAL( scaleLockChanged( bool ) ) );
6272
}
6373

6474
QgsStatusBarScaleWidget::~QgsStatusBarScaleWidget()
6575
{
6676
}
6777

68-
void QgsStatusBarScaleWidget::setScale(double scale)
78+
void QgsStatusBarScaleWidget::setScale( double scale )
79+
{
80+
mScale->setScale( scale );
81+
}
82+
83+
bool QgsStatusBarScaleWidget::isLocked()
6984
{
70-
mScale->setScale(scale);
85+
return mLockButton->isChecked();
7186
}
7287

73-
void QgsStatusBarScaleWidget::setFont(const QFont &font)
88+
void QgsStatusBarScaleWidget::setFont( const QFont &font )
7489
{
75-
mLabel->setFont( font );
76-
mScale->lineEdit()->setFont( font );
90+
mLabel->setFont( font );
91+
mScale->lineEdit()->setFont( font );
7792
}
7893

79-
void QgsStatusBarScaleWidget::updateScales(const QStringList &scales)
94+
void QgsStatusBarScaleWidget::updateScales( const QStringList &scales )
8095
{
81-
mScale->updateScales(scales);
96+
mScale->updateScales( scales );
8297
}
8398

8499
void QgsStatusBarScaleWidget::userScale()

‎src/app/qgsstatusbarscalewidget.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
class QFont;
2121
class QHBoxLayout;
2222
class QLabel;
23+
class QToolButton;
2324
class QValidator;
2425

2526
class QgsMapCanvas;
@@ -35,8 +36,8 @@ class QgsScaleComboBox;
3536
class APP_EXPORT QgsStatusBarScaleWidget : public QWidget
3637
{
3738
Q_OBJECT
38-
public:
39-
explicit QgsStatusBarScaleWidget(QgsMapCanvas* canvas, QWidget *parent = 0);
39+
public:
40+
explicit QgsStatusBarScaleWidget( QgsMapCanvas* canvas, QWidget *parent = 0 );
4041

4142
/** Destructor */
4243
virtual ~QgsStatusBarScaleWidget();
@@ -47,19 +48,30 @@ class APP_EXPORT QgsStatusBarScaleWidget : public QWidget
4748
*/
4849
void setScale( double scale );
4950

51+
/**
52+
* @brief isLocked check if the scale should be locked to use magnifier instead of scale to zoom in/out
53+
* @return True if the scale shall be locked
54+
*/
55+
bool isLocked();
56+
5057
/** Set the font of the text
5158
* @param font the font to use
5259
*/
5360
void setFont( const QFont& font );
5461

55-
public slots:
56-
void updateScales( const QStringList &scales = QStringList() );
62+
public slots:
63+
void updateScales( const QStringList &scales = QStringList() );
5764

58-
private slots:
65+
private slots:
5966
void userScale();
60-
private:
67+
68+
signals:
69+
void scaleLockChanged( bool );
70+
71+
private:
6172
QgsMapCanvas* mMapCanvas;
6273
QHBoxLayout *mLayout;
74+
QToolButton* mLockButton;
6375

6476
//! Widget that will live on the statusbar to display "scale 1:"
6577
QLabel* mLabel;

‎src/core/qgsmapsettings.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ QgsMapSettings::QgsMapSettings()
5959
void QgsMapSettings::setMagnificationFactor( double factor )
6060
{
6161
double ratio = mMagnificationFactor / factor;
62+
6263
mMagnificationFactor = factor;
6364

6465
double rot = rotation();
@@ -69,7 +70,9 @@ void QgsMapSettings::setMagnificationFactor( double factor )
6970

7071
mRotation = rot;
7172
mExtent = ext;
72-
mDpi = outputDpi() / ratio;
73+
mDpi = mDpi / ratio;
74+
75+
QgsDebugMsg( QString( "Magnification factor: %1 dpi: %2 ratio: %3" ).arg( factor ).arg( mDpi ).arg( ratio ) );
7376

7477
updateDerived();
7578
}
@@ -240,7 +243,7 @@ void QgsMapSettings::setOutputSize( QSize size )
240243

241244
int QgsMapSettings::outputDpi() const
242245
{
243-
return mDpi;
246+
return ( int )mDpi;
244247
}
245248

246249
void QgsMapSettings::setOutputDpi( int dpi )

‎src/core/qgsmapsettings.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,17 @@ class CORE_EXPORT QgsMapSettings
8383

8484
//! Return DPI used for conversion between real world units (e.g. mm) and pixels
8585
//! Default value is 96
86+
//! TODO QGIS 3 return double ?
8687
int outputDpi() const;
8788
//! Set DPI used for conversion between real world units (e.g. mm) and pixels
8889
void setOutputDpi( int dpi );
8990

90-
//! Set the magnification factor.
91-
//! @note added in 2.16
91+
/**
92+
* @brief setMagnificationFactor set the magnification factor
93+
* @param factor the factor of magnification
94+
* @param factorChanged defines if it actually changed in the map settings
95+
* @note added in 2.16
96+
*/
9297
void setMagnificationFactor( double factor );
9398
//! Return the magnification factor.
9499
//! @note added in 2.16
@@ -273,7 +278,7 @@ class CORE_EXPORT QgsMapSettings
273278

274279
protected:
275280

276-
int mDpi;
281+
double mDpi;
277282

278283
QSize mSize;
279284

‎src/gui/qgsmapcanvas.cpp

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ void QgsMapCanvasRendererSync::onLayersC2R()
189189
QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
190190
: QGraphicsView( parent )
191191
, mCanvasProperties( new CanvasProperties )
192-
, mMagnificationFactor( 1.0 )
193192
, mJob( nullptr )
194193
, mJobCancelled( false )
195194
, mLabelingResults( nullptr )
@@ -198,6 +197,7 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
198197
, mCache( nullptr )
199198
, mPreviewEffect( nullptr )
200199
, mSnappingUtils( nullptr )
200+
, mScaleLocked( false )
201201
, mExpressionContextScope( tr( "Map Canvas" ) )
202202
{
203203
setObjectName( name );
@@ -214,8 +214,6 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
214214
mFrozen = false;
215215
mRefreshScheduled = false;
216216

217-
setWheelAction( WheelZoom );
218-
219217
// by default, the canvas is rendered
220218
mRenderFlag = true;
221219

@@ -318,10 +316,22 @@ QgsMapCanvas::~QgsMapCanvas()
318316

319317
} // dtor
320318

321-
void QgsMapCanvas::setMagnificationFactor( double level )
319+
void QgsMapCanvas::setMagnificationFactor( double factor )
322320
{
323-
mSettings.setMagnificationFactor( level );
324-
refresh();
321+
// do not go higher or lower than min max magnification ratio
322+
QSettings settings;
323+
double magnifierMin = settings.value( "/qgis/magnifier_factor_min", 0.1 ).toDouble();
324+
double magnifierMax = settings.value( "/qgis/magnifier_factor_max", 10 ).toDouble();
325+
factor = factor > magnifierMax ? magnifierMax : factor;
326+
factor = factor < magnifierMin ? magnifierMin : factor;
327+
328+
// the magnifier widget is in integer percent
329+
if ( qAbs( factor - mSettings.magnificationFactor() ) >= 0.01 )
330+
{
331+
mSettings.setMagnificationFactor( factor );
332+
refresh();
333+
emit magnificationChanged( factor );
334+
}
325335
}
326336

327337
double QgsMapCanvas::magnificationFactor() const
@@ -1029,7 +1039,6 @@ void QgsMapCanvas::clear()
10291039
} // clear
10301040

10311041

1032-
10331042
void QgsMapCanvas::zoomToFullExtent()
10341043
{
10351044
QgsRectangle extent = fullExtent();
@@ -1485,54 +1494,37 @@ void QgsMapCanvas::wheelEvent( QWheelEvent *e )
14851494
return;
14861495
}
14871496

1488-
switch ( mWheelAction )
1489-
{
1490-
case WheelZoom:
1491-
// zoom without changing extent
1492-
if ( e->delta() > 0 )
1493-
zoomIn();
1494-
else
1495-
zoomOut();
1496-
break;
1497-
1498-
case WheelZoomAndRecenter:
1499-
// zoom and don't change extent
1500-
zoomWithCenter( e->x(), e->y(), e->delta() > 0 );
1501-
break;
1502-
1503-
case WheelZoomToMouseCursor:
1504-
{
1505-
// zoom map to mouse cursor
1506-
double scaleFactor = e->delta() > 0 ? 1 / mWheelZoomFactor : mWheelZoomFactor;
1497+
double signedWheelFactor = e->delta() > 0 ? 1 / mWheelZoomFactor : mWheelZoomFactor;
15071498

1508-
QgsPoint oldCenter = center();
1509-
QgsPoint mousePos( getCoordinateTransform()->toMapPoint( e->x(), e->y() ) );
1510-
QgsPoint newCenter( mousePos.x() + (( oldCenter.x() - mousePos.x() ) * scaleFactor ),
1511-
mousePos.y() + (( oldCenter.y() - mousePos.y() ) * scaleFactor ) );
1499+
// zoom map to mouse cursor by scaling
1500+
QgsPoint oldCenter = center();
1501+
QgsPoint mousePos( getCoordinateTransform()->toMapPoint( e->x(), e->y() ) );
1502+
QgsPoint newCenter( mousePos.x() + (( oldCenter.x() - mousePos.x() ) * signedWheelFactor ),
1503+
mousePos.y() + (( oldCenter.y() - mousePos.y() ) * signedWheelFactor ) );
15121504

1513-
zoomByFactor( scaleFactor, &newCenter );
1514-
break;
1515-
}
1516-
1517-
case WheelNothing:
1518-
// well, nothing!
1519-
break;
1520-
}
1505+
zoomByFactor( signedWheelFactor, &newCenter );
15211506
}
15221507

15231508
void QgsMapCanvas::setWheelAction( WheelAction action, double factor )
15241509
{
1525-
mWheelAction = action;
1510+
Q_UNUSED( action );
1511+
setWheelFactor( factor );
1512+
}
1513+
1514+
void QgsMapCanvas::setWheelFactor( double factor )
1515+
{
15261516
mWheelZoomFactor = factor;
15271517
}
15281518

15291519
void QgsMapCanvas::zoomIn()
15301520
{
1521+
// magnification is alreday handled in zoomByFactor
15311522
zoomByFactor( 1 / mWheelZoomFactor );
15321523
}
15331524

15341525
void QgsMapCanvas::zoomOut()
15351526
{
1527+
// magnification is alreday handled in zoomByFactor
15361528
zoomByFactor( mWheelZoomFactor );
15371529
}
15381530

@@ -1545,12 +1537,24 @@ void QgsMapCanvas::zoomWithCenter( int x, int y, bool zoomIn )
15451537
{
15461538
double scaleFactor = ( zoomIn ? 1 / mWheelZoomFactor : mWheelZoomFactor );
15471539

1548-
// transform the mouse pos to map coordinates
1549-
QgsPoint center = getCoordinateTransform()->toMapPoint( x, y );
1550-
QgsRectangle r = mapSettings().visibleExtent();
1551-
r.scale( scaleFactor, &center );
1552-
setExtent( r, true );
1553-
refresh();
1540+
if ( mScaleLocked )
1541+
{
1542+
setMagnificationFactor( mapSettings().magnificationFactor() / scaleFactor );
1543+
}
1544+
else
1545+
{
1546+
// transform the mouse pos to map coordinates
1547+
QgsPoint center = getCoordinateTransform()->toMapPoint( x, y );
1548+
QgsRectangle r = mapSettings().visibleExtent();
1549+
r.scale( scaleFactor, &center );
1550+
setExtent( r, true );
1551+
refresh();
1552+
}
1553+
}
1554+
1555+
void QgsMapCanvas::setScaleLocked( bool isLocked )
1556+
{
1557+
mScaleLocked = isLocked;
15541558
}
15551559

15561560
void QgsMapCanvas::mouseMoveEvent( QMouseEvent * e )
@@ -2027,10 +2031,18 @@ void QgsMapCanvas::getDatumTransformInfo( const QgsMapLayer* ml, const QString&
20272031

20282032
void QgsMapCanvas::zoomByFactor( double scaleFactor, const QgsPoint* center )
20292033
{
2030-
QgsRectangle r = mapSettings().extent();
2031-
r.scale( scaleFactor, center );
2032-
setExtent( r, true );
2033-
refresh();
2034+
if ( mScaleLocked )
2035+
{
2036+
// zoom map to mouse cursor by magnifying
2037+
setMagnificationFactor( mapSettings().magnificationFactor() / scaleFactor );
2038+
}
2039+
else
2040+
{
2041+
QgsRectangle r = mapSettings().extent();
2042+
r.scale( scaleFactor, center );
2043+
setExtent( r, true );
2044+
refresh();
2045+
}
20342046
}
20352047

20362048
void QgsMapCanvas::selectionChangedSlot()

‎src/gui/qgsmapcanvas.h

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
111111

112112
public:
113113

114+
//TODO QGIS 3 remove this
114115
enum WheelAction { WheelZoom, WheelZoomAndRecenter, WheelZoomToMouseCursor, WheelNothing };
115116

116117
//! Constructor
@@ -119,12 +120,6 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
119120
//! Destructor
120121
~QgsMapCanvas();
121122

122-
//! Sets the factor of magnification to apply to the map canvas. Indeed, we
123-
//! increase/decrease the DPI of the map settings according to this factor
124-
//! in order to render marker point, labels, ... bigger.
125-
//! @note added in 2.16
126-
void setMagnificationFactor( double level );
127-
128123
//! Returns the magnification factor
129124
//! @note added in 2.16
130125
double magnificationFactor() const;
@@ -348,8 +343,12 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
348343
QgsMapLayer* currentLayer();
349344

350345
//! set wheel action and zoom factor (should be greater than 1)
346+
//! @deprecated No more options for wheel action
351347
void setWheelAction( WheelAction action, double factor = 2 );
352348

349+
//! set wheel zoom factor (should be greater than 1)
350+
void setWheelFactor( double factor );
351+
353352
//! Zoom in with fixed factor
354353
void zoomIn();
355354

@@ -366,6 +365,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
366365
//! Zooms in/out with a given center
367366
void zoomWithCenter( int x, int y, bool zoomIn );
368367

368+
bool scaleLocked() { return mScaleLocked;}
369+
369370
//! used to determine if anti-aliasing is enabled or not
370371
void enableAntiAliasing( bool theFlag );
371372

@@ -521,6 +522,15 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
521522
//! @note added in 2.8
522523
static void enableRotation( bool enabled );
523524

525+
//! Sets the factor of magnification to apply to the map canvas. Indeed, we
526+
//! increase/decrease the DPI of the map settings according to this factor
527+
//! in order to render marker point, labels, ... bigger.
528+
//! @note added in 2.16
529+
void setMagnificationFactor( double factor );
530+
531+
//! lock the scale, so zooming can be performed using magnication
532+
//! @note added in 2.16
533+
void setScaleLocked( bool isLocked );
524534

525535
private slots:
526536
//! called when current maptool is destroyed
@@ -552,6 +562,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
552562
//! @note added in 2.8
553563
void rotationChanged( double );
554564

565+
//! Emitted when the scale of the map changes
566+
//! @note added in 2.16
567+
void magnificationChanged( double );
568+
555569
/** Emitted when the canvas has rendered.
556570
* Passes a pointer to the painter on which the map was drawn. This is
557571
* useful for plugins that wish to draw on the map after it has been
@@ -733,15 +747,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
733747
//! Scale factor multiple for default zoom in/out
734748
double mWheelZoomFactor;
735749

736-
//! Mouse wheel action
737-
WheelAction mWheelAction;
738-
739750
//! Timer that periodically fires while map rendering is in progress to update the visible map
740751
QTimer mMapUpdateTimer;
741752

742-
//! magnification factor
743-
double mMagnificationFactor;
744-
745753
//! Job that takes care of map rendering in background
746754
QgsMapRendererQImageJob* mJob;
747755

@@ -768,6 +776,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
768776

769777
QgsSnappingUtils* mSnappingUtils;
770778

779+
//! lock the scale, so zooming can be performed using magnication
780+
bool mScaleLocked;
781+
771782
QgsExpressionContextScope mExpressionContextScope;
772783

773784
//! Force a resize of the map canvas item

‎src/plugins/georeferencer/qgsgeorefplugingui.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,8 @@ void QgsGeorefPluginGui::createMapCanvas()
993993
this, SLOT( releasePoint( const QPoint & ) ) );
994994

995995
QSettings s;
996-
int action = s.value( "/qgis/wheel_action", 2 ).toInt();
997996
double zoomFactor = s.value( "/qgis/zoom_factor", 2 ).toDouble();
998-
mCanvas->setWheelAction(( QgsMapCanvas::WheelAction ) action, zoomFactor );
997+
mCanvas->setWheelFactor( zoomFactor );
999998

1000999
mExtentsChangedRecursionGuard = false;
10011000

‎src/ui/qgsoptionsbase.ui

Lines changed: 260 additions & 85 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.