Skip to content

Commit

Permalink
invalidate map settings when ...
Browse files Browse the repository at this point in the history
parameters of QgsMapToPixel leads to an invalid transform
  • Loading branch information
vcloarec authored and nyalldawson committed May 31, 2021
1 parent 8ff0efd commit 09b5ba4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
5 changes: 5 additions & 0 deletions python/core/auto_generated/qgsmaptopixel.sip.in
Expand Up @@ -181,9 +181,14 @@ Set parameters for use in transforming coordinates
:param heightPixels: Output height, in pixels
:param rotation: clockwise rotation in degrees

.. note::

if parameters leads to a non valid transform, parameters are not changed

.. versionadded:: 2.8
%End


QString showParameters() const;
%Docstring
String representation of the parameters used in the transform
Expand Down
17 changes: 10 additions & 7 deletions src/core/qgsmapsettings.cpp
Expand Up @@ -194,12 +194,16 @@ void QgsMapSettings::updateDerived()
mScaleCalculator.setDpi( mDpi * mDevicePixelRatio );
mScale = mScaleCalculator.calculate( mVisibleExtent, mSize.width() );

mMapToPixel.setParameters( mapUnitsPerPixel(),
visibleExtent().center().x(),
visibleExtent().center().y(),
outputSize().width(),
outputSize().height(),
mRotation );
bool ok = true;
mMapToPixel.setParameters(
mapUnitsPerPixel(),
visibleExtent().center().x(),
visibleExtent().center().y(),
outputSize().width(),
outputSize().height(),
mRotation, &ok );

mValid = ok;

#if 1 // set visible extent taking rotation in consideration
if ( mRotation )
Expand Down Expand Up @@ -228,7 +232,6 @@ void QgsMapSettings::updateDerived()
QgsDebugMsgLevel( QStringLiteral( "Visible Extent: %1" ).arg( mVisibleExtent.asWktCoordinates() ), 5 );
QgsDebugMsgLevel( QStringLiteral( "Magnification factor: %1" ).arg( mMagnificationFactor ), 5 );

mValid = true;
}


Expand Down
20 changes: 19 additions & 1 deletion src/core/qgsmaptopixel.cpp
Expand Up @@ -118,7 +118,8 @@ void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
double yc,
int width,
int height,
double rotation )
double rotation,
bool *ok )
{
double oldMUPP = mMapUnitsPerPixel;
double oldXCenter = mXCenter;
Expand Down Expand Up @@ -148,9 +149,25 @@ void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
mRotation = oldRotation;
mXMin = oldXMin;
mYMin = oldYMin;
*ok = false;
}
else
{
*ok = true;
}
}

void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
double xc,
double yc,
int width,
int height,
double rotation )
{
bool ok;
setParameters( mapUnitsPerPixel, xc, yc, width, height, rotation, &ok );
}

QString QgsMapToPixel::showParameters() const
{
QString rep;
Expand Down Expand Up @@ -185,3 +202,4 @@ QTransform QgsMapToPixel::transform() const
.translate( -mXCenter, -mYCenter );
}
}

16 changes: 16 additions & 0 deletions src/core/qgsmaptopixel.h
Expand Up @@ -237,10 +237,26 @@ class CORE_EXPORT QgsMapToPixel
* \param widthPixels Output width, in pixels
* \param heightPixels Output height, in pixels
* \param rotation clockwise rotation in degrees
*
* \note if parameters leads to a non valid transform, parameters are not changed
* \since QGIS 2.8
*/
void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation );

/**
* Set parameters for use in transforming coordinates
* \param mapUnitsPerPixel Map units per pixel
* \param centerX X coordinate of map center, in geographical units
* \param centerY Y coordinate of map center, in geographical units
* \param widthPixels Output width, in pixels
* \param heightPixels Output height, in pixels
* \param rotation clockwise rotation in degrees
* \param ok will be set to true if parameters leads to a valid transform, otherwise parameters are not changed and ok will be set to false
*
* \since QGIS 3.20
*/
void setParameters( double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation, bool *ok ) SIP_SKIP;

//! String representation of the parameters used in the transform
QString showParameters() const;

Expand Down

0 comments on commit 09b5ba4

Please sign in to comment.