Skip to content

Commit

Permalink
QgsMapToPixel API cleanup
Browse files Browse the repository at this point in the history
* overload toMapCoordinates to also take double (use different PyName)
* deprecate toMapPoint which is strictly equivalent to toMapCoordinates
  • Loading branch information
3nids committed Sep 29, 2018
1 parent 110c8a0 commit 157ba0f
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 33 deletions.
12 changes: 10 additions & 2 deletions python/core/auto_generated/qgsmaptopixel.sip.in
Expand Up @@ -95,8 +95,11 @@ transform.


QgsPointXY toMapCoordinates( int x, int y ) const;
%Docstring
Transform device coordinates to map (world) coordinates
%End

QgsPointXY toMapCoordinatesF( double x, double y ) const;
QgsPointXY toMapCoordinates( double x, double y ) const /PyName=toMapCoordinatesF/;
%Docstring
Transform device coordinates to map (world) coordinates
%End
Expand All @@ -110,7 +113,12 @@ Transform device coordinates to map (world) coordinates
:return: QgsPointXY in map coorndiates
%End

QgsPointXY toMapPoint( double x, double y ) const;
QgsPointXY toMapPoint( double x, double y ) const;
%Docstring
Transform device coordinates to map (world) coordinates

.. deprecated:: since QGIS 3.4 use toMapCoordinates instead
%End

void setMapUnitsPerPixel( double mapUnitsPerPixel );
%Docstring
Expand Down
8 changes: 4 additions & 4 deletions src/core/qgsmapsettings.cpp
Expand Up @@ -350,10 +350,10 @@ QPolygonF QgsMapSettings::visiblePolygon() const
const QSize &sz = outputSize();
const QgsMapToPixel &m2p = mapToPixel();

poly << m2p.toMapCoordinatesF( 0, 0 ).toQPointF();
poly << m2p.toMapCoordinatesF( sz.width(), 0 ).toQPointF();
poly << m2p.toMapCoordinatesF( sz.width(), sz.height() ).toQPointF();
poly << m2p.toMapCoordinatesF( 0, sz.height() ).toQPointF();
poly << m2p.toMapCoordinates( 0.0, 0.0 ).toQPointF();
poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), 0.0 ).toQPointF();
poly << m2p.toMapCoordinates( static_cast<double>( sz.width() ), static_cast<double>( sz.height() ) ).toQPointF();
poly << m2p.toMapCoordinates( 0.0, static_cast<double>( sz.height() ) ).toQPointF();

return poly;
}
Expand Down
11 changes: 5 additions & 6 deletions src/core/qgsmaptopixel.cpp
Expand Up @@ -88,32 +88,31 @@ bool QgsMapToPixel::updateMatrix()
return true;
}

QgsPointXY QgsMapToPixel::toMapPoint( double x, double y ) const
QgsPointXY QgsMapToPixel::toMapCoordinates( double x, double y ) const
{
bool invertible;
QTransform matrix = mMatrix.inverted( &invertible );
assert( invertible );
qreal mx, my;
qreal x_qreal = x, y_qreal = y;
matrix.map( x_qreal, y_qreal, &mx, &my );
//QgsDebugMsg(QString("XXX toMapPoint x:%1 y:%2 -> x:%3 y:%4").arg(x).arg(y).arg(mx).arg(my));
return QgsPointXY( mx, my );
}

QgsPointXY QgsMapToPixel::toMapCoordinates( QPoint p ) const
{
QgsPointXY mapPt = toMapPoint( p.x(), p.y() );
QgsPointXY mapPt = toMapCoordinates( static_cast<double>( p.x() ), static_cast<double>( p.y() ) );
return QgsPointXY( mapPt );
}

QgsPointXY QgsMapToPixel::toMapCoordinates( int x, int y ) const
{
return toMapPoint( x, y );
return toMapCoordinates( static_cast<double>( x ), static_cast<double>( y ) );
}

QgsPointXY QgsMapToPixel::toMapCoordinatesF( double x, double y ) const
QgsPointXY QgsMapToPixel::toMapPoint( double x, double y ) const
{
return toMapPoint( x, y );
return toMapCoordinates( x, y );
}

void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel )
Expand Down
9 changes: 7 additions & 2 deletions src/core/qgsmaptopixel.h
Expand Up @@ -117,10 +117,11 @@ class CORE_EXPORT QgsMapToPixel
}
#endif

//! Transform device coordinates to map (world) coordinates
QgsPointXY toMapCoordinates( int x, int y ) const;

//! Transform device coordinates to map (world) coordinates
QgsPointXY toMapCoordinatesF( double x, double y ) const;
QgsPointXY toMapCoordinates( double x, double y ) const SIP_PYNAME( toMapCoordinatesF );

/**
* Transform device coordinates to map (world) coordinates
Expand All @@ -129,7 +130,11 @@ class CORE_EXPORT QgsMapToPixel
*/
QgsPointXY toMapCoordinates( QPoint p ) const;

QgsPointXY toMapPoint( double x, double y ) const;
/**
* Transform device coordinates to map (world) coordinates
* \deprecated since QGIS 3.4 use toMapCoordinates instead
*/
Q_DECL_DEPRECATED QgsPointXY toMapPoint( double x, double y ) const;

/**
* Set map units per pixel
Expand Down
8 changes: 4 additions & 4 deletions src/core/raster/qgsrasterlayerrenderer.cpp
Expand Up @@ -188,10 +188,10 @@ QgsRasterLayerRenderer::QgsRasterLayerRenderer( QgsRasterLayer *layer, QgsRender
mRasterViewPort->mBottomRightPoint.setY( std::ceil( mRasterViewPort->mBottomRightPoint.y() ) );
// recalc myRasterExtent to aligned values
myRasterExtent.set(
mapToPixel.toMapCoordinatesF( mRasterViewPort->mTopLeftPoint.x(),
mRasterViewPort->mBottomRightPoint.y() ),
mapToPixel.toMapCoordinatesF( mRasterViewPort->mBottomRightPoint.x(),
mRasterViewPort->mTopLeftPoint.y() )
mapToPixel.toMapCoordinates( mRasterViewPort->mTopLeftPoint.x(),
mRasterViewPort->mBottomRightPoint.y() ),
mapToPixel.toMapCoordinates( mRasterViewPort->mBottomRightPoint.x(),
mRasterViewPort->mTopLeftPoint.y() )
);

//raster viewport top left / bottom right are already rounded to int
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgshighlight.cpp
Expand Up @@ -407,7 +407,7 @@ void QgsHighlight::updateRect()
// This is an hack to pass QgsMapCanvasItem::setRect what it
// expects (encoding of position and size of the item)
const QgsMapToPixel &m2p = mMapCanvas->mapSettings().mapToPixel();
QgsPointXY topLeft = m2p.toMapPoint( 0, 0 );
QgsPointXY topLeft = m2p.toMapCoordinates( 0, 0 );
double res = m2p.mapUnitsPerPixel();
QSizeF imageSize = mMapCanvas->mapSettings().outputSize();
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + imageSize.width()*res, topLeft.y() - imageSize.height()*res );
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -681,7 +681,7 @@ QgsRectangle QgsMapCanvas::imageRect( const QImage &img, const QgsMapSettings &m
// This is a hack to pass QgsMapCanvasItem::setRect what it
// expects (encoding of position and size of the item)
const QgsMapToPixel &m2p = mapSettings.mapToPixel();
QgsPointXY topLeft = m2p.toMapPoint( 0, 0 );
QgsPointXY topLeft = m2p.toMapCoordinates( 0, 0 );
double res = m2p.mapUnitsPerPixel();
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + img.width()*res, topLeft.y() - img.height()*res );
return rect;
Expand Down Expand Up @@ -1558,7 +1558,7 @@ void QgsMapCanvas::wheelEvent( QWheelEvent *e )

// zoom map to mouse cursor by scaling
QgsPointXY oldCenter = center();
QgsPointXY mousePos( getCoordinateTransform()->toMapPoint( e->x(), e->y() ) );
QgsPointXY mousePos( getCoordinateTransform()->toMapCoordinates( e->x(), e->y() ) );
QgsPointXY newCenter( mousePos.x() + ( ( oldCenter.x() - mousePos.x() ) * signedWheelFactor ),
mousePos.y() + ( ( oldCenter.y() - mousePos.y() ) * signedWheelFactor ) );

Expand Down Expand Up @@ -1599,7 +1599,7 @@ void QgsMapCanvas::zoomWithCenter( int x, int y, bool zoomIn )
else
{
// transform the mouse pos to map coordinates
QgsPointXY center = getCoordinateTransform()->toMapPoint( x, y );
QgsPointXY center = getCoordinateTransform()->toMapCoordinates( x, y );
QgsRectangle r = mapSettings().visibleExtent();
r.scale( scaleFactor, &center );
setExtent( r, true );
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsmaptoolpan.cpp
Expand Up @@ -81,7 +81,7 @@ void QgsMapToolPan::canvasReleaseEvent( QgsMapMouseEvent *e )
else // add pan to mouse cursor
{
// transform the mouse pos to map coordinates
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapPoint( e->x(), e->y() );
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( e->x(), e->y() );
mCanvas->setCenter( center );
mCanvas->refresh();
}
Expand Down Expand Up @@ -127,7 +127,7 @@ void QgsMapToolPan::pinchTriggered( QPinchGesture *gesture )
QPoint pos = gesture->centerPoint().toPoint();
pos = mCanvas->mapFromGlobal( pos );
// transform the mouse pos to map coordinates
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapPoint( pos.x(), pos.y() );
QgsPointXY center = mCanvas->getCoordinateTransform()->toMapCoordinates( pos.x(), pos.y() );
QgsRectangle r = mCanvas->extent();
r.scale( 1 / gesture->totalScaleFactor(), &center );
mCanvas->setExtent( r );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsrubberband.cpp
Expand Up @@ -524,7 +524,7 @@ void QgsRubberBand::updateRect()
// This is an hack to pass QgsMapCanvasItem::setRect what it
// expects (encoding of position and size of the item)
qreal res = m2p.mapUnitsPerPixel();
QgsPointXY topLeft = m2p.toMapPoint( r.xMinimum(), r.yMinimum() );
QgsPointXY topLeft = m2p.toMapCoordinates( r.xMinimum(), r.yMinimum() );
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + r.width()*res, topLeft.y() - r.height()*res );

setRect( rect );
Expand Down
4 changes: 2 additions & 2 deletions src/quickgui/qgsquickmapsettings.cpp
Expand Up @@ -118,10 +118,10 @@ QPointF QgsQuickMapSettings::coordinateToScreen( const QgsPoint &point ) const

QgsPoint QgsQuickMapSettings::screenToCoordinate( const QPointF &point ) const
{
// use floating point precision with mapToCoordinatesF
// use floating point precision with mapToCoordinates (i.e. do not use QPointF::toPoint)
// this is to avoid rounding errors with an odd screen width or height
// and the point being set to the exact center of it
const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinatesF( point.x(), point.y() );
const QgsPointXY pp = mMapSettings.mapToPixel().toMapCoordinates( point.x(), point.y() );
return QgsPoint( pp );
}

Expand Down
12 changes: 6 additions & 6 deletions tests/src/core/testqgsmaptopixel.cpp
Expand Up @@ -28,7 +28,7 @@ class TestQgsMapToPixel: public QObject
void rotation();
void getters();
void fromScale();
void toMapPoint();
void toMapCoordinates();
};

void TestQgsMapToPixel::rotation()
Expand All @@ -40,7 +40,7 @@ void TestQgsMapToPixel::rotation()
QCOMPARE( d.x(), 5.0 ); // center doesn't move
QCOMPARE( d.y(), 5.0 );

QgsPointXY b = m2p.toMapCoordinatesF( d.x(), d.y() ); // transform back
QgsPointXY b = m2p.toMapCoordinates( d.x(), d.y() ); // transform back
QCOMPARE( p, b );

m2p.transform( &p ); // in place transform
Expand Down Expand Up @@ -106,16 +106,16 @@ void TestQgsMapToPixel::fromScale()
QGSCOMPARENEAR( m2p.mapUnitsPerPixel(), 0.000265, 0.000001 );
}

void TestQgsMapToPixel::toMapPoint()
void TestQgsMapToPixel::toMapCoordinates()
{
QgsMapToPixel m2p( 1, 5, 5, 10, 10, 90 );
QgsPointXY p = m2p.toMapPoint( 5, 5 );
QgsPointXY p = m2p.toMapCoordinates( 5, 5 );
QCOMPARE( p, QgsPointXY( 5, 5 ) );

p = m2p.toMapPoint( 10, 10 );
p = m2p.toMapCoordinates( 10, 10 );
QCOMPARE( p, QgsPointXY( 10, 10 ) );

p = m2p.toMapPoint( 20, 20 );
p = m2p.toMapCoordinates( 20, 20 );
QCOMPARE( p, QgsPointXY( 20, 20 ) );
}

Expand Down

0 comments on commit 157ba0f

Please sign in to comment.