Skip to content
Permalink
Browse files

Do not allow invalid map settings (fixes #12757)

  • Loading branch information
Sandro Santilli authored and wonder-sk committed May 19, 2015
1 parent 9c73a22 commit 1bf9844652ad2b92ab2da2a37ebf941257416403
Showing with 99 additions and 16 deletions.
  1. +98 −15 src/core/qgsmaptopixel.cpp
  2. +1 −1 src/core/qgsmaptopixel.h
@@ -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
@@ -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
@@ -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;
@@ -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
@@ -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,
@@ -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;
@@ -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
@@ -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 );
}
}
@@ -226,7 +226,7 @@ class CORE_EXPORT QgsMapToPixel
double yMin; //!< @deprecated in 2.8
QTransform mMatrix;

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


0 comments on commit 1bf9844

Please sign in to comment.
You can’t perform that action at this time.