Skip to content

Commit

Permalink
Do not allow invalid map settings (fixes #12757)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Santilli authored and wonder-sk committed Jun 19, 2015
1 parent 9c73a22 commit 1bf9844
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 16 deletions.
113 changes: 98 additions & 15 deletions src/core/qgsmaptopixel.cpp
Expand Up @@ -99,9 +99,16 @@ int QgsMapToPixel::mapWidth() const
return mWidth;
}

void QgsMapToPixel::updateMatrix()
bool QgsMapToPixel::updateMatrix()
{
mMatrix = transform();
QTransform newMatrix = transform();

// http://hub.qgis.org/issues/12757
if ( !newMatrix.isInvertible() )
return false;

mMatrix = newMatrix;
return true;
}

QgsPoint QgsMapToPixel::toMapPoint( qreal x, qreal y ) const
Expand Down Expand Up @@ -133,8 +140,12 @@ QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const

void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
{
double oldUnits = mMapUnitsPerPixel;
mMapUnitsPerPixel = mapUnitsPerPixel;
updateMatrix();
if ( !updateMatrix() )
{
mMapUnitsPerPixel = oldUnits;
}
}

double QgsMapToPixel::mapUnitsPerPixel() const
Expand All @@ -144,6 +155,11 @@ double QgsMapToPixel::mapUnitsPerPixel() const

void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
{
double oldRotation = mRotation;
double oldXCenter = xCenter;
double oldYCenter = yCenter;
double oldWidth = mWidth;

mRotation = degrees;
xCenter = cx;
yCenter = cy;
Expand All @@ -152,7 +168,14 @@ void QgsMapToPixel::setMapRotation( double degrees, double cx, double cy )
// set width not that we can compute it
mWidth = (( xCenter - xMin ) * 2 ) / mMapUnitsPerPixel;
}
updateMatrix();

if ( !updateMatrix() )
{
mRotation = oldRotation;
xCenter = oldXCenter;
yCenter = oldYCenter;
mWidth = oldWidth;
}
}

double QgsMapToPixel::mapRotation() const
Expand All @@ -163,30 +186,63 @@ double QgsMapToPixel::mapRotation() const
// @deprecated in 2.8
void QgsMapToPixel::setYMinimum( double ymin )
{
double oldRotation = mRotation;
double oldYCenter = yCenter;

yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
mRotation = 0.0;
updateMatrix();
if ( !updateMatrix() )
{
mRotation = oldRotation;
yCenter = oldYCenter;
}
}

// @deprecated in 2.8
void QgsMapToPixel::setXMinimum( double xmin )
{
double oldRotation = mRotation;
double oldXCenter = xCenter;

xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
mRotation = 0.0;
updateMatrix();

if ( !updateMatrix() )
{
mRotation = oldRotation;
xCenter = oldXCenter;
}
}

// @deprecated in 2.8
void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax )
{
double oldMUPP = mMapUnitsPerPixel;
double oldXMin = xMin;
double oldYMin = yMin;
double oldHeight = mHeight;
double oldXCenter = xCenter;
double oldYCenter = yCenter;
double oldRotation = mRotation;

mMapUnitsPerPixel = mapUnitsPerPixel;
xMin = xmin;
yMin = ymin;
mHeight = ymax;
xCenter = xmin + mWidth * mMapUnitsPerPixel / 2.0;
yCenter = ymin + mHeight * mMapUnitsPerPixel / 2.0;
mRotation = 0.0;
updateMatrix();

if ( !updateMatrix() )
{
mMapUnitsPerPixel = oldMUPP;
xMin = oldXMin;
yMin = oldYMin;
mHeight = oldHeight;
xCenter = oldXCenter;
yCenter = oldYCenter;
mRotation = oldRotation;
}
}

void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
Expand All @@ -196,6 +252,15 @@ void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
int height,
double rotation )
{
double oldMUPP = mMapUnitsPerPixel;
double oldXCenter = xCenter;
double oldYCenter = yCenter;
double oldWidth = mWidth;
double oldHeight = mHeight;
double oldRotation = mRotation;
double oldXMin = xMin;
double oldYMin = yMin;

mMapUnitsPerPixel = mapUnitsPerPixel;
xCenter = xc;
yCenter = yc;
Expand All @@ -204,7 +269,18 @@ void QgsMapToPixel::setParameters( double mapUnitsPerPixel,
mRotation = rotation;
xMin = xc - ( mWidth * mMapUnitsPerPixel / 2.0 );
yMin = yc - ( mHeight * mMapUnitsPerPixel / 2.0 );
updateMatrix();

if ( !updateMatrix() )
{
mMapUnitsPerPixel = oldMUPP;
xCenter = oldXCenter;
yCenter = oldYCenter;
mWidth = oldWidth;
mHeight = oldHeight;
mRotation = oldRotation;
xMin = oldXMin;
yMin = oldYMin;
}
}

QString QgsMapToPixel::showParameters() const
Expand Down Expand Up @@ -250,18 +326,25 @@ void QgsMapToPixel::transformInPlace( qreal &x, qreal &y ) const

QTransform QgsMapToPixel::transform() const
{
// NOTE: operations are done in the reverse order in which
// they are configured, so translation to geographical
// center happens first, then scaling, then rotation
// and finally translation to output viewport center

double rotation = mapRotation();
if ( qgsDoubleNear( rotation, 0.0 ) )
{
//no rotation, return a simplified matrix
return QTransform::fromScale( 1.0 / mMapUnitsPerPixel, -1.0 / mMapUnitsPerPixel )
.translate( -xMin, - ( yMin + mHeight * mMapUnitsPerPixel ) );
}

double cy = mapHeight() / 2.0;
double cx = mapWidth() / 2.0;
return QTransform::fromTranslate( cx, cy )
.rotate( rotation )
.scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
.translate( -xCenter, -yCenter );
else
{
double cy = mapHeight() / 2.0;
double cx = mapWidth() / 2.0;
return QTransform::fromTranslate( cx, cy )
.rotate( rotation )
.scale( 1 / mMapUnitsPerPixel, -1 / mMapUnitsPerPixel )
.translate( -xCenter, -yCenter );
}
}
2 changes: 1 addition & 1 deletion src/core/qgsmaptopixel.h
Expand Up @@ -226,7 +226,7 @@ class CORE_EXPORT QgsMapToPixel
double yMin; //!< @deprecated in 2.8
QTransform mMatrix;

void updateMatrix();
bool updateMatrix();
};


Expand Down

0 comments on commit 1bf9844

Please sign in to comment.