Skip to content
Permalink
Browse files

Fix advanced digitizing display for rotated map canvas

  • Loading branch information
jdugge committed Feb 19, 2019
1 parent 7980cef commit ef912d2732325656f46d60ff5f81d89057e2dd2b
Showing with 29 additions and 17 deletions.
  1. +29 −17 src/gui/qgsadvanceddigitizingcanvasitem.cpp
@@ -38,7 +38,16 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
if ( !mAdvancedDigitizingDockWidget->cadEnabled() )
return;

QgsRectangle mapRect = mMapCanvas->extent();
// Use visible polygon rather than extent to properly handle rotated maps
QPolygonF mapPoly = mMapCanvas->mapSettings().visiblePolygon();
double canvasWidth = QLineF( mapPoly[0], mapPoly[1] ).length();
double canvasHeight = QLineF( mapPoly[0], mapPoly[3] ).length();
QgsRectangle mapRect = QgsRectangle( mapPoly[0],
QgsPointXY(
mapPoly[0].x() + canvasWidth,
mapPoly[0].y() - canvasHeight
)
);
if ( rect() != mapRect )
setRect( mapRect );

@@ -54,12 +63,16 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
const QList<QgsPointXY> snappedSegment = mAdvancedDigitizingDockWidget->snappedSegment();
const bool hasSnappedSegment = snappedSegment.count() == 2;

const bool curPointExist = mapRect.contains( curPoint );
const bool curPointExist = mapPoly.containsPoint( curPoint.toQPointF(), Qt::OddEvenFill );

const double mupp = mMapCanvas->getCoordinateTransform()->mapUnitsPerPixel();
if ( mupp == 0 )
return;

const double canvasRotation = mMapCanvas->rotation();
const double canvasRotationRad = canvasRotation * M_PI / 180;
const double canvasMaxDimension = std::max( canvasWidth / mupp, canvasHeight / mupp );

QPointF curPointPix, prevPointPix, penulPointPix, snapSegmentPix1, snapSegmentPix2;

if ( curPointExist )
@@ -116,19 +129,19 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
double a0, a;
if ( mAdvancedDigitizingDockWidget->constraintAngle()->relative() && nPoints > 2 )
{
a0 = std::atan2( -( prevPoint.y() - penulPoint.y() ), prevPoint.x() - penulPoint.x() );
a0 = std::atan2( -( prevPoint.y() - penulPoint.y() ), prevPoint.x() - penulPoint.x() ) + canvasRotationRad;
}
else
{
a0 = 0;
a0 = canvasRotationRad;
}
if ( mAdvancedDigitizingDockWidget->constraintAngle()->isLocked() )
{
a = a0 - mAdvancedDigitizingDockWidget->constraintAngle()->value() * M_PI / 180;
}
else
{
a = std::atan2( -( curPoint.y() - prevPoint.y() ), curPoint.x() - prevPoint.x() );
a = std::atan2( -( curPoint.y() - prevPoint.y() ), curPoint.x() - prevPoint.x() ) + canvasRotationRad;
}
painter->setPen( mConstruction2Pen );
painter->drawArc( QRectF( prevPointPix.x() - 20,
@@ -143,9 +156,8 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
if ( mAdvancedDigitizingDockWidget->constraintAngle()->isLocked() )
{
painter->setPen( mLockedPen );
double d = std::max( boundingRect().width(), boundingRect().height() );
painter->drawLine( prevPointPix - d * QPointF( std::cos( a ), std::sin( a ) ),
prevPointPix + d * QPointF( std::cos( a ), std::sin( a ) ) );
painter->drawLine( prevPointPix - canvasMaxDimension * QPointF( std::cos( a ), std::sin( a ) ),
prevPointPix + canvasMaxDimension * QPointF( std::cos( a ), std::sin( a ) ) );
}
}

@@ -167,7 +179,7 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
{
if ( nPoints > 1 )
{
x = mAdvancedDigitizingDockWidget->constraintX()->value() / mupp + prevPointPix.x();
x = mAdvancedDigitizingDockWidget->constraintX()->value() + prevPoint.x();
}
else
{
@@ -176,12 +188,12 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
}
else
{
x = toCanvasCoordinates( QgsPointXY( mAdvancedDigitizingDockWidget->constraintX()->value(), 0 ) ).x();
x = mAdvancedDigitizingDockWidget->constraintX()->value();
}
if ( draw )
{
painter->drawLine( QPointF( x, 0 ),
QPointF( x, boundingRect().height() ) );
painter->drawLine( toCanvasCoordinates( QgsPointXY( x, mapPoly[0].y() ) ) - canvasMaxDimension * QPointF( std::sin( -canvasRotationRad ), std::cos( -canvasRotationRad ) ),
toCanvasCoordinates( QgsPointXY( x, mapPoly[0].y() ) ) + canvasMaxDimension * QPointF( std::sin( -canvasRotationRad ), std::cos( -canvasRotationRad ) ) );
}
}

@@ -195,8 +207,7 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
{
if ( nPoints > 1 )
{
// y is reversed!
y = -mAdvancedDigitizingDockWidget->constraintY()->value() / mupp + prevPointPix.y();
y = mAdvancedDigitizingDockWidget->constraintY()->value() + prevPoint.y();
}
else
{
@@ -205,12 +216,13 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
}
else
{
y = toCanvasCoordinates( QgsPointXY( 0, mAdvancedDigitizingDockWidget->constraintY()->value() ) ).y();
y = mAdvancedDigitizingDockWidget->constraintY()->value();
}
if ( draw )
{
painter->drawLine( QPointF( 0, y ),
QPointF( boundingRect().width(), y ) );
painter->drawLine( toCanvasCoordinates( QgsPointXY( mapPoly[0].x(), y ) ) - canvasMaxDimension * QPointF( std::cos( -canvasRotationRad ), -std::sin( -canvasRotationRad ) ),
toCanvasCoordinates( QgsPointXY( mapPoly[0].x(), y ) ) + canvasMaxDimension * QPointF( std::cos( -canvasRotationRad ), -std::sin( -canvasRotationRad ) ) );

}
}

0 comments on commit ef912d2

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