Skip to content

Commit d23a110

Browse files
committed
move magnification in map settings
1 parent 95038b1 commit d23a110

File tree

6 files changed

+57
-22
lines changed

6 files changed

+57
-22
lines changed

python/core/qgsmapsettings.sip

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class QgsMapSettings
1717
//! The actual visible extent used for rendering could be slightly different
1818
//! since the given extent may be expanded in order to fit the aspect ratio
1919
//! of output size. Use visibleExtent() to get the resulting extent.
20-
void setExtent( const QgsRectangle& rect );
20+
void setExtent( const QgsRectangle& rect, bool magnified = true );
2121

2222
//! Return the size of the resulting map image
2323
QSize outputSize() const;
@@ -39,6 +39,13 @@ 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.
46+
//! @note added in 2.16
47+
double magnificationFactor() const;
48+
4249
//! Get list of layer IDs for map rendering
4350
//! The layers are stored in the reverse order of how they are rendered (layer with index 0 will be on top)
4451
QStringList layers() const;

python/gui/qgsmapcanvas.sip

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class QgsMapCanvas : QGraphicsView
5757
void setMagnificationFactor( double level );
5858

5959
//! Returns the magnification factor
60+
//! @note added in 2.16
6061
double magnificationFactor() const;
6162

6263
void setLayerSet( QList<QgsMapCanvasLayer>& layers );

src/core/qgsmapsettings.cpp

+30-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ QgsMapSettings::QgsMapSettings()
3535
, mSize( QSize( 0, 0 ) )
3636
, mExtent()
3737
, mRotation( 0.0 )
38+
, mMagnificationFactor( 1.0 )
3839
, mProjectionsEnabled( false )
3940
, mDestCRS( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId ) // WGS 84
4041
, mDatumTransformStore( mDestCRS )
@@ -53,15 +54,42 @@ QgsMapSettings::QgsMapSettings()
5354
setMapUnits( QGis::Degrees );
5455
}
5556

57+
void QgsMapSettings::setMagnificationFactor( double factor )
58+
{
59+
double ratio = mMagnificationFactor / factor;
60+
mMagnificationFactor = factor;
61+
62+
double rot = rotation();
63+
setRotation( 0.0 );
64+
65+
QgsRectangle ext = visibleExtent();
66+
ext.scale( ratio );
67+
68+
mRotation = rot;
69+
mExtent = ext;
70+
mDpi = outputDpi() / ratio;
71+
72+
updateDerived();
73+
}
74+
75+
double QgsMapSettings::magnificationFactor() const
76+
{
77+
return mMagnificationFactor;
78+
}
5679

5780
QgsRectangle QgsMapSettings::extent() const
5881
{
5982
return mExtent;
6083
}
6184

62-
void QgsMapSettings::setExtent( const QgsRectangle& extent )
85+
void QgsMapSettings::setExtent( const QgsRectangle& extent, bool magnified )
6386
{
64-
mExtent = extent;
87+
QgsRectangle magnifiedExtent = extent;
88+
89+
if ( !magnified )
90+
magnifiedExtent.scale( 1 / mMagnificationFactor );
91+
92+
mExtent = magnifiedExtent;
6593

6694
updateDerived();
6795
}

src/core/qgsmapsettings.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CORE_EXPORT QgsMapSettings
6464
//! The actual visible extent used for rendering could be slightly different
6565
//! since the given extent may be expanded in order to fit the aspect ratio
6666
//! of output size. Use visibleExtent() to get the resulting extent.
67-
void setExtent( const QgsRectangle& rect );
67+
void setExtent( const QgsRectangle& rect, bool magnified = true );
6868

6969
//! Return the size of the resulting map image
7070
QSize outputSize() const;
@@ -86,6 +86,13 @@ class CORE_EXPORT QgsMapSettings
8686
//! Set DPI used for conversion between real world units (e.g. mm) and pixels
8787
void setOutputDpi( int dpi );
8888

89+
//! Set the magnification factor.
90+
//! @note added in 2.16
91+
void setMagnificationFactor( double factor );
92+
//! Return the magnification factor.
93+
//! @note added in 2.16
94+
double magnificationFactor() const;
95+
8996
//! Get list of layer IDs for map rendering
9097
//! The layers are stored in the reverse order of how they are rendered (layer with index 0 will be on top)
9198
QStringList layers() const;
@@ -261,6 +268,7 @@ class CORE_EXPORT QgsMapSettings
261268
QgsRectangle mExtent;
262269

263270
double mRotation;
271+
double mMagnificationFactor;
264272

265273
QStringList mLayers;
266274
QMap<QString, QString> mLayerStyleOverrides;

src/gui/qgsmapcanvas.cpp

+7-17
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,15 @@ QgsMapCanvas::~QgsMapCanvas()
313313

314314
void QgsMapCanvas::setMagnificationFactor( double level )
315315
{
316-
QgsMapSettings settings = mSettings;
317-
settings.setRotation( 0.0 );
318-
319-
double ratio = mMagnificationFactor / level;
320-
mMagnificationFactor = level;
321-
322-
QgsRectangle ext = settings.visibleExtent();
323-
ext.scale( ratio );
324-
325-
mSettings.setOutputDpi( mSettings.outputDpi() / ratio );
326-
setExtent( ext, true );
327-
316+
mSettings.setMagnificationFactor( level );
328317
refresh();
329318
}
330319

320+
double QgsMapCanvas::magnificationFactor() const
321+
{
322+
return mSettings.magnificationFactor();
323+
}
324+
331325
void QgsMapCanvas::enableAntiAliasing( bool theFlag )
332326
{
333327
mSettings.setFlag( QgsMapSettings::Antialiasing, theFlag );
@@ -905,11 +899,7 @@ void QgsMapCanvas::setExtent( QgsRectangle const & r, bool magnified )
905899
}
906900
else
907901
{
908-
QgsRectangle magnifiedExtent = r;
909-
if ( ! magnified )
910-
magnifiedExtent.scale( 1 / mMagnificationFactor );
911-
912-
mSettings.setExtent( magnifiedExtent );
902+
mSettings.setExtent( r, magnified );
913903
}
914904
emit extentsChanged();
915905
updateScale();

src/gui/qgsmapcanvas.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
126126
void setMagnificationFactor( double level );
127127

128128
//! Returns the magnification factor
129-
double magnificationFactor() const { return mMagnificationFactor; };
129+
//! @note added in 2.16
130+
double magnificationFactor() const;
130131

131132
void setLayerSet( QList<QgsMapCanvasLayer>& layers );
132133

0 commit comments

Comments
 (0)