Skip to content

Commit

Permalink
move magnification in map settings
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed May 18, 2016
1 parent 95038b1 commit d23a110
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 22 deletions.
9 changes: 8 additions & 1 deletion python/core/qgsmapsettings.sip
Expand Up @@ -17,7 +17,7 @@ class QgsMapSettings
//! The actual visible extent used for rendering could be slightly different
//! since the given extent may be expanded in order to fit the aspect ratio
//! of output size. Use visibleExtent() to get the resulting extent.
void setExtent( const QgsRectangle& rect );
void setExtent( const QgsRectangle& rect, bool magnified = true );

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

//! Set the magnification factor.
//! @note added in 2.16
void setMagnificationFactor( double factor );
//! Return the magnification factor.
//! @note added in 2.16
double magnificationFactor() const;

//! Get list of layer IDs for map rendering
//! The layers are stored in the reverse order of how they are rendered (layer with index 0 will be on top)
QStringList layers() const;
Expand Down
1 change: 1 addition & 0 deletions python/gui/qgsmapcanvas.sip
Expand Up @@ -57,6 +57,7 @@ class QgsMapCanvas : QGraphicsView
void setMagnificationFactor( double level );

//! Returns the magnification factor
//! @note added in 2.16
double magnificationFactor() const;

void setLayerSet( QList<QgsMapCanvasLayer>& layers );
Expand Down
32 changes: 30 additions & 2 deletions src/core/qgsmapsettings.cpp
Expand Up @@ -35,6 +35,7 @@ QgsMapSettings::QgsMapSettings()
, mSize( QSize( 0, 0 ) )
, mExtent()
, mRotation( 0.0 )
, mMagnificationFactor( 1.0 )
, mProjectionsEnabled( false )
, mDestCRS( GEOCRS_ID, QgsCoordinateReferenceSystem::InternalCrsId ) // WGS 84
, mDatumTransformStore( mDestCRS )
Expand All @@ -53,15 +54,42 @@ QgsMapSettings::QgsMapSettings()
setMapUnits( QGis::Degrees );
}

void QgsMapSettings::setMagnificationFactor( double factor )
{
double ratio = mMagnificationFactor / factor;
mMagnificationFactor = factor;

double rot = rotation();
setRotation( 0.0 );

QgsRectangle ext = visibleExtent();
ext.scale( ratio );

mRotation = rot;
mExtent = ext;
mDpi = outputDpi() / ratio;

updateDerived();
}

double QgsMapSettings::magnificationFactor() const
{
return mMagnificationFactor;
}

QgsRectangle QgsMapSettings::extent() const
{
return mExtent;
}

void QgsMapSettings::setExtent( const QgsRectangle& extent )
void QgsMapSettings::setExtent( const QgsRectangle& extent, bool magnified )
{
mExtent = extent;
QgsRectangle magnifiedExtent = extent;

if ( !magnified )
magnifiedExtent.scale( 1 / mMagnificationFactor );

mExtent = magnifiedExtent;

updateDerived();
}
Expand Down
10 changes: 9 additions & 1 deletion src/core/qgsmapsettings.h
Expand Up @@ -64,7 +64,7 @@ class CORE_EXPORT QgsMapSettings
//! The actual visible extent used for rendering could be slightly different
//! since the given extent may be expanded in order to fit the aspect ratio
//! of output size. Use visibleExtent() to get the resulting extent.
void setExtent( const QgsRectangle& rect );
void setExtent( const QgsRectangle& rect, bool magnified = true );

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

//! Set the magnification factor.
//! @note added in 2.16
void setMagnificationFactor( double factor );
//! Return the magnification factor.
//! @note added in 2.16
double magnificationFactor() const;

//! Get list of layer IDs for map rendering
//! The layers are stored in the reverse order of how they are rendered (layer with index 0 will be on top)
QStringList layers() const;
Expand Down Expand Up @@ -261,6 +268,7 @@ class CORE_EXPORT QgsMapSettings
QgsRectangle mExtent;

double mRotation;
double mMagnificationFactor;

QStringList mLayers;
QMap<QString, QString> mLayerStyleOverrides;
Expand Down
24 changes: 7 additions & 17 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -313,21 +313,15 @@ QgsMapCanvas::~QgsMapCanvas()

void QgsMapCanvas::setMagnificationFactor( double level )
{
QgsMapSettings settings = mSettings;
settings.setRotation( 0.0 );

double ratio = mMagnificationFactor / level;
mMagnificationFactor = level;

QgsRectangle ext = settings.visibleExtent();
ext.scale( ratio );

mSettings.setOutputDpi( mSettings.outputDpi() / ratio );
setExtent( ext, true );

mSettings.setMagnificationFactor( level );
refresh();
}

double QgsMapCanvas::magnificationFactor() const
{
return mSettings.magnificationFactor();
}

void QgsMapCanvas::enableAntiAliasing( bool theFlag )
{
mSettings.setFlag( QgsMapSettings::Antialiasing, theFlag );
Expand Down Expand Up @@ -905,11 +899,7 @@ void QgsMapCanvas::setExtent( QgsRectangle const & r, bool magnified )
}
else
{
QgsRectangle magnifiedExtent = r;
if ( ! magnified )
magnifiedExtent.scale( 1 / mMagnificationFactor );

mSettings.setExtent( magnifiedExtent );
mSettings.setExtent( r, magnified );
}
emit extentsChanged();
updateScale();
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsmapcanvas.h
Expand Up @@ -126,7 +126,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
void setMagnificationFactor( double level );

//! Returns the magnification factor
double magnificationFactor() const { return mMagnificationFactor; };
//! @note added in 2.16
double magnificationFactor() const;

void setLayerSet( QList<QgsMapCanvasLayer>& layers );

Expand Down

0 comments on commit d23a110

Please sign in to comment.