// private QRectF QgsMapCanvasItem::toCanvasCoordinates( const QRectF& rect ) { QPointF tl( toCanvasCoordinates( rect.topLeft() ) ); QPointF bl( toCanvasCoordinates( rect.bottomLeft() ) ); QPointF br( toCanvasCoordinates( rect.bottomRight() ) ); QPointF tr( toCanvasCoordinates( rect.topRight() ) ); double xmin = std::min( tl.x(), std::min( bl.x(), std::min( br.x(), tr.x() ) ) ); double ymin = std::min( tl.y(), std::min( bl.y(), std::min( br.y(), tr.y() ) ) ); double xmax = std::max( tl.x(), std::max( bl.x(), std::max( br.x(), tr.x() ) ) ); double ymax = std::max( tl.y(), std::max( bl.y(), std::max( br.y(), tr.y() ) ) ); return QRectF( QPointF( xmin, ymin ), QPointF( xmax, ymax ) ); }
void QgsHighlight::paintPoint( QPainter *p, QgsPoint point ) { QPolygonF r( 5 ); double d = mMapCanvas->extent().width() * 0.005; r[0] = toCanvasCoordinates( point + QgsVector( -d, -d ) ) - pos(); r[1] = toCanvasCoordinates( point + QgsVector( d, -d ) ) - pos(); r[2] = toCanvasCoordinates( point + QgsVector( d, d ) ) - pos(); r[3] = toCanvasCoordinates( point + QgsVector( -d, d ) ) - pos(); r[4] = r[0]; p->drawPolygon( r ); }
void QgsMapToolRotateFeature::canvasMoveEvent( QMouseEvent * e ) { if ( mCtrl ) { if ( !mAnchorPoint ) { return; } mAnchorPoint->setCenter( toMapCoordinates( e->pos() ) ); mStartPointMapCoords = toMapCoordinates( e->pos() ); mStPoint = e->pos(); return; } if ( mRubberBand ) { double XDistance = e->pos().x() - mStPoint.x(); double YDistance = e->pos().y() - mStPoint.y(); mRotation = atan2( YDistance, XDistance ) * ( 180 / PI ); mRotation = mRotation - mRotationOffset; mStPoint = toCanvasCoordinates( mStartPointMapCoords ); double offsetX = mStPoint.x() - mRubberBand->x(); double offsetY = mStPoint.y() - mRubberBand->y(); mRubberBand->setTransform( QTransform().translate( offsetX, offsetY ).rotate( mRotation ).translate( -1 * offsetX, -1 * offsetY ) ); mRubberBand->update(); } }
void QgsMapCanvasAnnotationItem::updatePosition() { if ( !mAnnotation ) return; if ( mAnnotation->hasFixedMapPosition() ) { QgsCoordinateTransform t( mAnnotation->mapPositionCrs(), mMapCanvas->mapSettings().destinationCrs(), QgsProject::instance() ); QgsPointXY coord = mAnnotation->mapPosition(); try { coord = t.transform( coord ); } catch ( QgsCsException & ) {} setPos( toCanvasCoordinates( coord ) ); } else { //relative position double x = mAnnotation->relativePosition().x() * mMapCanvas->width(); double y = mAnnotation->relativePosition().y() * mMapCanvas->height(); setPos( x, y ); } updateBoundingRect(); }
void QgsMapCanvasItem::setRect( const QgsRectangle& rect, bool resetRotation ) { mRect = rect; //updatePosition(); QRectF r; // empty rect by default if ( !mRect.isEmpty() ) { // rect encodes origin of the item (xMin,yMax from map to canvas units) // and size (rect size / map units per pixel) r.setTopLeft( toCanvasCoordinates( QPointF( mRect.xMinimum(), mRect.yMaximum() ) ) ); const QgsMapToPixel* m2p = mMapCanvas->getCoordinateTransform(); double res = m2p->mapUnitsPerPixel(); r.setSize( QSizeF( mRect.width() / res, mRect.height() / res ) ); } // set position in canvas where the item will have coordinate (0,0) prepareGeometryChange(); setPos( r.topLeft() ); mItemSize = QSizeF( r.width() + 2, r.height() + 2 ); if ( resetRotation ) { mRectRotation = mMapCanvas->rotation(); setRotation( 0 ); } // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height())); update(); }
void QgsHighlight::paintPolygon( QPainter *p, QgsPolygon polygon ) { QPolygonF poly; // just ring outlines, no fill p->setPen( mPen ); p->setBrush( Qt::NoBrush ); for ( int i = 0; i < polygon.size(); i++ ) { QPolygonF ring( polygon[i].size() + 1 ); for ( int j = 0; j < polygon[i].size(); j++ ) { ring[ j ] = toCanvasCoordinates( polygon[i][j] ) - pos(); } ring[ polygon[i].size()] = ring[ 0 ]; p->drawPolygon( ring ); if ( i == 0 ) poly = ring; else poly = poly.subtracted( ring ); } // just fill, no outline p->setPen( Qt::NoPen ); p->setBrush( mBrush ); p->drawPolygon( poly ); }
void QgsAnnotationItem::setMapPosition( const QgsPoint& pos ) { mMapPosition = pos; setPos( toCanvasCoordinates( mMapPosition ) ); setMapPositionCrs( mMapCanvas->mapSettings().destinationCrs() ); }
void QgsHighlight::paintPolygon( QPainter *p, QgsPolygon polygon ) { // OddEven fill rule by default QPainterPath path; p->setPen( mPen ); p->setBrush( mBrush ); for ( int i = 0; i < polygon.size(); i++ ) { if ( polygon[i].empty() ) continue; QPolygonF ring; ring.reserve( polygon[i].size() + 1 ); for ( int j = 0; j < polygon[i].size(); j++ ) { //adding point only if it is more than a pixel appart from the previous one const QPointF cur = toCanvasCoordinates( polygon[i][j] ) - pos(); if ( 0 == j || std::abs( ring.back().x() - cur.x() ) > 1 || std::abs( ring.back().y() - cur.y() ) > 1 ) { ring.push_back( cur ); } } ring.push_back( ring[ 0 ] ); path.addPolygon( ring ); } p->drawPath( path ); }
void QgsGCPCanvasItem::updatePosition() { if ( !mDataPoint ) { return; } setPos( toCanvasCoordinates( mIsGCPSource ? mDataPoint->pixelCoords() : mDataPoint->mapCoords() ) ); }
void QgsHighlight::paintPoint( QPainter *p, QgsPoint point ) { QPolygonF r( 5 ); if ( mLayer ) { point = mMapCanvas->mapRenderer()->layerToMapCoordinates( mLayer, point ); } double d = mMapCanvas->extent().width() * 0.005; r[0] = toCanvasCoordinates( point + QgsVector( -d, -d ) ) - pos(); r[1] = toCanvasCoordinates( point + QgsVector( d, -d ) ) - pos(); r[2] = toCanvasCoordinates( point + QgsVector( d, d ) ) - pos(); r[3] = toCanvasCoordinates( point + QgsVector( -d, d ) ) - pos(); r[4] = r[0]; p->drawPolygon( r ); }
void QgsAnnotationItem::updatePosition() { if ( mMapPositionFixed ) { setPos( toCanvasCoordinates( mMapPosition ) ); } else { mMapPosition = toMapCoordinates( pos().toPoint() ); } }
void QgsHighlight::paintLine( QPainter *p, QgsPolyline line ) { QPolygonF polygon( line.size() ); for ( int i = 0; i < line.size(); i++ ) { polygon[i] = toCanvasCoordinates( line[i] ) - pos(); } p->drawPolyline( polygon ); }
void QgsAnnotationItem::updatePosition() { if ( mMapPositionFixed ) { QgsCoordinateTransform t( mMapPositionCrs, mMapCanvas->mapSettings().destinationCrs() ); setPos( toCanvasCoordinates( t.transform( mMapPosition ) ) ); } else { mMapPosition = toMapCoordinates( pos().toPoint() ); } }
void QgsMapCanvasItem::setRect( const QgsRectangle& rect ) { mRect = rect; //updatePosition(); QRectF r; // empty rect by default if ( !mRect.isEmpty() ) { r.setTopLeft( toCanvasCoordinates( QgsPoint( mRect.xMinimum(), mRect.yMinimum() ) ) ); r.setBottomRight( toCanvasCoordinates( QgsPoint( mRect.xMaximum(), mRect.yMaximum() ) ) ); r = r.normalized(); } // set position in canvas where the item will have coordinate (0,0) prepareGeometryChange(); setPos( r.topLeft() ); mItemSize = QSizeF( r.width() + 2, r.height() + 2 ); // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height())); update(); }
/*! Draw the shape in response to an update event. */ void QgsRubberBand::paint( QPainter* p ) { QList<QgsPoint> currentList; if ( mPoints.size() > 0 ) { p->setBrush( mBrush ); for ( int i = 0; i < mPoints.size(); ++i ) { QVector<QPointF> pts; QList<QgsPoint>::const_iterator it = mPoints.at( i ).constBegin(); for ( ; it != mPoints.at( i ).constEnd(); ++it ) { pts.append( toCanvasCoordinates( QgsPoint( it->x() + mTranslationOffsetX, it->y() + mTranslationOffsetY ) ) - pos() ); } switch ( mGeometryType ) { case QGis::Polygon: { mPen.setWidth( mWidth ); p->setPen( mPen ); p->drawPolygon( pts ); } break; case QGis::Point: { mPen.setWidth( 1 ); p->setPen( mPen ); QVector<QPointF>::const_iterator ptIt = pts.constBegin(); for ( ; ptIt != pts.constEnd(); ++ptIt ) { p->drawEllipse(( *ptIt ).x() - mWidth / 2, ( *ptIt ).y() - mWidth / 2, mWidth, mWidth ); } } break; case QGis::Line: default: { mPen.setWidth( mWidth ); p->setPen( mPen ); p->drawPolyline( pts ); } break; } } } }
void QgsGpsMarker::paint( QPainter* p ) { if ( ! mSvg.isValid() ) { return; } // this needs to be done when the canvas is repainted to make for smoother map rendering // if not done the map could be panned, but the cursor position won't be updated until the next valid GPS fix is received QPointF pt = toCanvasCoordinates( mCenter ); setPos( pt ); float myHalfSize = mSize / 2.0; mSvg.render( p, QRectF( 0 - myHalfSize , 0 - myHalfSize, mSize, mSize ) ); }
void QgsHighlight::paintLine( QPainter *p, QgsPolyline line ) { QPolygonF polygon( line.size() ); for ( int i = 0; i < line.size(); i++ ) { if ( mLayer ) { line[i] = mMapCanvas->mapRenderer()->layerToMapCoordinates( mLayer, line[i] ); } polygon[i] = toCanvasCoordinates( line[i] ) - pos(); } p->drawPolyline( polygon ); }
void QgsMapToolRotateFeature::updateRubberband( double rotation ) { if ( mRotationActive ) { mRotation = rotation; mStPoint = toCanvasCoordinates( mStartPointMapCoords ); double offsetX = mStPoint.x() - mRubberBand->x(); double offsetY = mStPoint.y() - mRubberBand->y(); if ( mRubberBand ) { mRubberBand->setTransform( QTransform().translate( offsetX, offsetY ).rotate( mRotation ).translate( -1 * offsetX, -1 * offsetY ) ); mRubberBand->update(); } } }
void QgsMapCanvasItem::setRect( const QgsRectangle& rect ) { mRect = rect; //updatePosition(); QRectF r; // empty rect by default if ( !mRect.isEmpty() ) { r = toCanvasCoordinates( mRect.toRectF() ); r = r.normalized(); } // set position in canvas where the item will have coordinate (0,0) prepareGeometryChange(); setPos( r.topLeft() ); // TODO: compute from (0,0) using toMapCoordinates ? mItemSize = QSizeF( r.width() + 2, r.height() + 2 ); // QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height())); update(); }
void QgsGpsMarker::setCenter( const QgsPoint& point ) { //transform to map crs if ( mMapCanvas && mMapCanvas->mapRenderer() ) { QgsCoordinateTransform t( mWgs84CRS, mMapCanvas->mapRenderer()->destinationCrs() ); try { mCenter = t.transform( point ); } catch ( QgsCsException &e ) //silently ignore transformation exceptions { Q_UNUSED( e ); return; } } else { mCenter = point; } QPointF pt = toCanvasCoordinates( mCenter ); setPos( pt ); }
void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter ) { if ( !mAdvancedDigitizingDockWidget->cadEnabled() ) return; QgsRectangle mapRect = mMapCanvas->extent(); if ( rect() != mapRect ) setRect( mapRect ); int nPoints = mAdvancedDigitizingDockWidget->pointsCount(); if ( !nPoints ) return; bool previousPointExist, penulPointExist; const QgsPointXY curPoint = mAdvancedDigitizingDockWidget->currentPoint(); const QgsPointXY prevPoint = mAdvancedDigitizingDockWidget->previousPoint( &previousPointExist ); const QgsPointXY penulPoint = mAdvancedDigitizingDockWidget->penultimatePoint( &penulPointExist ); const bool snappedToVertex = mAdvancedDigitizingDockWidget->snappedToVertex(); const QList<QgsPointXY> snappedSegment = mAdvancedDigitizingDockWidget->snappedSegment(); const bool hasSnappedSegment = snappedSegment.count() == 2; const bool curPointExist = mapRect.contains( curPoint ); const double mupp = mMapCanvas->getCoordinateTransform()->mapUnitsPerPixel(); if ( mupp == 0 ) return; QPointF curPointPix, prevPointPix, penulPointPix, snapSegmentPix1, snapSegmentPix2; if ( curPointExist ) { curPointPix = toCanvasCoordinates( curPoint ); } if ( previousPointExist ) { prevPointPix = toCanvasCoordinates( prevPoint ); } if ( penulPointExist ) { penulPointPix = toCanvasCoordinates( penulPoint ); } if ( hasSnappedSegment ) { snapSegmentPix1 = toCanvasCoordinates( snappedSegment[0] ); snapSegmentPix2 = toCanvasCoordinates( snappedSegment[1] ); } painter->setRenderHint( QPainter::Antialiasing ); painter->setCompositionMode( QPainter::CompositionMode_Difference ); // Draw point snap if ( curPointExist && snappedToVertex ) { painter->setPen( mSnapPen ); painter->drawEllipse( curPointPix, 10, 10 ); } // Draw segment snap if ( hasSnappedSegment && !snappedToVertex ) { painter->setPen( mSnapPen ); painter->drawLine( snapSegmentPix1, snapSegmentPix2 ); if ( curPointExist ) { painter->setPen( mSnapLinePen ); painter->drawLine( snapSegmentPix1, curPointPix ); } } // Draw segment par/per input if ( mAdvancedDigitizingDockWidget->additionalConstraint() != QgsAdvancedDigitizingDockWidget::NoConstraint && hasSnappedSegment ) { painter->setPen( mConstruction2Pen ); painter->drawLine( snapSegmentPix1, snapSegmentPix2 ); } // Draw angle if ( nPoints > 1 ) { double a0, a; if ( mAdvancedDigitizingDockWidget->constraintAngle()->relative() && nPoints > 2 ) { a0 = std::atan2( -( prevPoint.y() - penulPoint.y() ), prevPoint.x() - penulPoint.x() ); } else { a0 = 0; } if ( mAdvancedDigitizingDockWidget->constraintAngle()->isLocked() ) { a = a0 - mAdvancedDigitizingDockWidget->constraintAngle()->value() * M_PI / 180; } else { a = std::atan2( -( curPoint.y() - prevPoint.y() ), curPoint.x() - prevPoint.x() ); } painter->setPen( mConstruction2Pen ); painter->drawArc( QRectF( prevPointPix.x() - 20, prevPointPix.y() - 20, 40, 40 ), static_cast<int>( 16 * -a0 * 180 / M_PI ), static_cast<int>( 16 * ( a0 - a ) * 180 / M_PI ) ); painter->drawLine( prevPointPix, prevPointPix + 60 * QPointF( std::cos( a0 ), std::sin( a0 ) ) ); 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 ) ) ); } } // Draw distance if ( nPoints > 1 && mAdvancedDigitizingDockWidget->constraintDistance()->isLocked() ) { painter->setPen( mLockedPen ); double r = mAdvancedDigitizingDockWidget->constraintDistance()->value() / mupp; painter->drawEllipse( prevPointPix, r, r ); } // Draw x if ( mAdvancedDigitizingDockWidget->constraintX()->isLocked() ) { double x = 0.0; bool draw = true; painter->setPen( mLockedPen ); if ( mAdvancedDigitizingDockWidget->constraintX()->relative() ) { if ( nPoints > 1 ) { x = mAdvancedDigitizingDockWidget->constraintX()->value() / mupp + prevPointPix.x(); } else { draw = false; } } else { x = toCanvasCoordinates( QgsPointXY( mAdvancedDigitizingDockWidget->constraintX()->value(), 0 ) ).x(); } if ( draw ) { painter->drawLine( QPointF( x, 0 ), QPointF( x, boundingRect().height() ) ); } } // Draw y if ( mAdvancedDigitizingDockWidget->constraintY()->isLocked() ) { double y = 0.0; bool draw = true; painter->setPen( mLockedPen ); if ( mAdvancedDigitizingDockWidget->constraintY()->relative() ) { if ( nPoints > 1 ) { // y is reversed! y = -mAdvancedDigitizingDockWidget->constraintY()->value() / mupp + prevPointPix.y(); } else { draw = false; } } else { y = toCanvasCoordinates( QgsPointXY( 0, mAdvancedDigitizingDockWidget->constraintY()->value() ) ).y(); } if ( draw ) { painter->drawLine( QPointF( 0, y ), QPointF( boundingRect().width(), y ) ); } } // Draw constr if ( mAdvancedDigitizingDockWidget->additionalConstraint() == QgsAdvancedDigitizingDockWidget::NoConstraint ) { if ( curPointExist && previousPointExist ) { painter->setPen( mConstruction2Pen ); painter->drawLine( prevPointPix, curPointPix ); } if ( previousPointExist && penulPointExist ) { painter->setPen( mConstruction1Pen ); painter->drawLine( penulPointPix, prevPointPix ); } } if ( curPointExist ) { painter->setPen( mCursorPen ); painter->drawLine( curPointPix + QPointF( -5, -5 ), curPointPix + QPointF( +5, +5 ) ); painter->drawLine( curPointPix + QPointF( -5, +5 ), curPointPix + QPointF( +5, -5 ) ); } QgsPointLocator::Match match = mAdvancedDigitizingDockWidget->mapPointMatch(); if ( match.isValid() ) { mSnapIndicator->setMatch( match ); mSnapIndicator->setVisible( true ); } else mSnapIndicator->setVisible( false ); }
/*! Draw the shape in response to an update event. */ void QgsRubberBand::paint( QPainter* p ) { if ( !mPoints.isEmpty() ) { p->setBrush( mBrush ); p->setPen( mPen ); Q_FOREACH ( const QList<QgsPoint>& line, mPoints ) { QVector<QPointF> pts; Q_FOREACH ( const QgsPoint& pt, line ) { const QPointF cur = toCanvasCoordinates( QgsPoint( pt.x() + mTranslationOffsetX, pt.y() + mTranslationOffsetY ) ) - pos(); if ( pts.empty() || std::abs( pts.back().x() - cur.x() ) > 1 || std::abs( pts.back().y() - cur.y() ) > 1 ) pts.append( cur ); } switch ( mGeometryType ) { case QgsWkbTypes::PolygonGeometry: { p->drawPolygon( pts ); } break; case QgsWkbTypes::PointGeometry: { Q_FOREACH ( QPointF pt, pts ) { double x = pt.x(); double y = pt.y(); qreal s = ( mIconSize - 1 ) / 2.0; switch ( mIconType ) { case ICON_NONE: break; case ICON_CROSS: p->drawLine( QLineF( x - s, y, x + s, y ) ); p->drawLine( QLineF( x, y - s, x, y + s ) ); break; case ICON_X: p->drawLine( QLineF( x - s, y - s, x + s, y + s ) ); p->drawLine( QLineF( x - s, y + s, x + s, y - s ) ); break; case ICON_BOX: p->drawLine( QLineF( x - s, y - s, x + s, y - s ) ); p->drawLine( QLineF( x + s, y - s, x + s, y + s ) ); p->drawLine( QLineF( x + s, y + s, x - s, y + s ) ); p->drawLine( QLineF( x - s, y + s, x - s, y - s ) ); break; case ICON_FULL_BOX: p->drawRect( x - s, y - s, mIconSize, mIconSize ); break; case ICON_CIRCLE: p->drawEllipse( x - s, y - s, mIconSize, mIconSize ); break; } } } break; case QgsWkbTypes::LineGeometry: default: { p->drawPolyline( pts ); } break; }
void QgsMapToolRotatePointSymbols::canvasPressEvent( QMouseEvent *e ) { if ( !mCanvas ) { return; } mActiveLayer = currentVectorLayer(); if ( !mActiveLayer ) { notifyNotVectorLayer(); return; } if ( !mActiveLayer->isEditable() ) { notifyNotEditableLayer(); return; } if ( mActiveLayer->geometryType() != QGis::Point ) { return; } //find the closest feature to the pressed position QgsMapCanvasSnapper canvasSnapper( mCanvas ); QList<QgsSnappingResult> snapResults; if ( canvasSnapper.snapToCurrentLayer( e->pos(), snapResults, QgsSnapper::SnapToVertex, -1 ) != 0 || snapResults.size() < 1 ) { QMessageBox::critical( 0, tr( "No point feature" ), tr( "No point feature was detected at the clicked position. Please click closer to the feature or enhance the search tolerance under Settings->Options->Digitizing->Serch radius for vertex edits" ) ); return; //error during snapping } mFeatureNumber = snapResults.at( 0 ).snappedAtGeometry; //get list with renderer rotation attributes if ( layerRotationAttributes( mActiveLayer, mCurrentRotationAttributes ) != 0 ) { return; } if ( mCurrentRotationAttributes.size() < 1 ) { QMessageBox::critical( 0, tr( "No rotation Attributes" ), tr( "The active point layer does not have a rotation attribute" ) ); return; } mSnappedPoint = toCanvasCoordinates( snapResults.at( 0 ).snappedVertex ); //find out initial arrow direction QgsFeature pointFeature; if ( !mActiveLayer->featureAtId( mFeatureNumber, pointFeature, false, true ) ) { return; } const QgsAttributeMap pointFeatureAttributes = pointFeature.attributeMap(); const QgsAttributeMap::const_iterator attIt = pointFeatureAttributes.find( mCurrentRotationAttributes.at( 0 ) ); if ( attIt == pointFeatureAttributes.constEnd() ) { return; } mCurrentRotationFeature = attIt.value().toDouble(); createPixmapItem( pointFeature ); if ( mRotationItem ) { mRotationItem->setPointLocation( snapResults.at( 0 ).snappedVertex ); } mCurrentMouseAzimut = calculateAzimut( e->pos() ); setPixmapItemRotation(( int )( mCurrentMouseAzimut ) ); mRotating = true; }
void QgsPointRotationItem::setPointLocation( const QgsPoint &p ) { QPointF transformedPoint = toCanvasCoordinates( p ); setPos( transformedPoint.x() - mPixmap.width() / 2.0, transformedPoint.y() - mPixmap.height() / 2.0 ); }
void QgsAnnotationItem::setMapPosition( const QgsPoint& pos ) { mMapPosition = pos; setPos( toCanvasCoordinates( mMapPosition ) ); }
/*! Draw the shape in response to an update event. */ void QgsRubberBand::paint( QPainter* p ) { QList<QgsPoint> currentList; if ( mPoints.size() > 0 ) { p->setBrush( mBrush ); mPen.setWidth( mWidth ); p->setPen( mPen ); for ( int i = 0; i < mPoints.size(); ++i ) { QVector<QPointF> pts; QList<QgsPoint>::const_iterator it = mPoints.at( i ).constBegin(); for ( ; it != mPoints.at( i ).constEnd(); ++it ) { pts.append( toCanvasCoordinates( QgsPoint( it->x() + mTranslationOffsetX, it->y() + mTranslationOffsetY ) ) - pos() ); } switch ( mGeometryType ) { case QGis::Polygon: { p->drawPolygon( pts ); } break; case QGis::Point: { QVector<QPointF>::const_iterator ptIt = pts.constBegin(); for ( ; ptIt != pts.constEnd(); ++ptIt ) { double x = (*ptIt).x(); double y = (*ptIt).y(); qreal s = ( mIconSize - 1 ) / 2; switch ( mIconType ) { case ICON_NONE: break; case ICON_CROSS: p->drawLine( QLineF( x - s, y, x + s, y ) ); p->drawLine( QLineF( x, y - s, x, y + s ) ); break; case ICON_X: p->drawLine( QLineF( x - s, y - s, x + s, y + s ) ); p->drawLine( QLineF( x - s, y + s, x + s, y - s ) ); break; case ICON_BOX: p->drawLine( QLineF( x - s, y - s, x + s, y - s ) ); p->drawLine( QLineF( x + s, y - s, x + s, y + s ) ); p->drawLine( QLineF( x + s, y + s, x - s, y + s ) ); p->drawLine( QLineF( x - s, y + s, x - s, y - s ) ); break; case ICON_CIRCLE: p->drawEllipse( x - s, y - s, mIconSize, mIconSize ); break; } } } break; case QGis::Line: default: { p->drawPolyline( pts ); } break; } } } }
void QgsMapToolRotateFeature::canvasReleaseEvent( QgsMapMouseEvent *e ) { if ( !mCanvas ) { return; } QgsVectorLayer *vlayer = currentVectorLayer(); if ( !vlayer ) { deleteRotationWidget(); deleteRubberband(); notifyNotVectorLayer(); return; } if ( e->button() == Qt::RightButton ) { cancel(); return; } // place anchor point on CTRL + click if ( e->modifiers() & Qt::ControlModifier ) { if ( !mAnchorPoint ) { return; } mAnchorPoint->setCenter( toMapCoordinates( e->pos() ) ); mStartPointMapCoords = toMapCoordinates( e->pos() ); mStPoint = e->pos(); return; } deleteRotationWidget(); // Initialize rotation if not yet active if ( !mRotationActive ) { mRotation = 0; mRotationOffset = 0; deleteRubberband(); mInitialPos = e->pos(); if ( !vlayer->isEditable() ) { notifyNotEditableLayer(); return; } QgsPointXY layerCoords = toLayerCoordinates( vlayer, e->pos() ); double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapSettings() ); QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius, layerCoords.x() + searchRadius, layerCoords.y() + searchRadius ); if ( vlayer->selectedFeatureCount() == 0 ) { QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( selectRect ).setNoAttributes() ); //find the closest feature QgsGeometry pointGeometry = QgsGeometry::fromPointXY( layerCoords ); if ( pointGeometry.isNull() ) { return; } double minDistance = std::numeric_limits<double>::max(); QgsFeature cf; QgsFeature f; while ( fit.nextFeature( f ) ) { if ( f.hasGeometry() ) { double currentDistance = pointGeometry.distance( f.geometry() ); if ( currentDistance < minDistance ) { minDistance = currentDistance; cf = f; } } } if ( minDistance == std::numeric_limits<double>::max() ) { emit messageEmitted( tr( "Could not find a nearby feature in the current layer." ) ); return; } QgsRectangle bound = cf.geometry().boundingBox(); mStartPointMapCoords = toMapCoordinates( vlayer, bound.center() ); if ( !mAnchorPoint ) { mAnchorPoint = qgis::make_unique<QgsVertexMarker>( mCanvas ); } mAnchorPoint->setIconType( QgsVertexMarker::ICON_CROSS ); mAnchorPoint->setCenter( mStartPointMapCoords ); mStPoint = toCanvasCoordinates( mStartPointMapCoords ); mRotatedFeatures.clear(); mRotatedFeatures << cf.id(); //todo: take the closest feature, not the first one... mRubberBand = createRubberBand( vlayer->geometryType() ); mRubberBand->setToGeometry( cf.geometry(), vlayer ); } else { mRotatedFeatures = vlayer->selectedFeatureIds(); mRubberBand = createRubberBand( vlayer->geometryType() ); QgsFeature feat; QgsFeatureIterator it = vlayer->getSelectedFeatures(); while ( it.nextFeature( feat ) ) { mRubberBand->addGeometry( feat.geometry(), vlayer ); } } mRubberBand->show(); double XDistance = mInitialPos.x() - mAnchorPoint->x(); double YDistance = mInitialPos.y() - mAnchorPoint->y(); mRotationOffset = std::atan2( YDistance, XDistance ) * ( 180 / M_PI ); createRotationWidget(); if ( e->modifiers() & Qt::ShiftModifier ) { if ( mRotationWidget ) { mRotationWidget->setMagnet( 45 ); } } mRotationActive = true; return; } applyRotation( mRotation ); }
void QgsPointMarkerItem::setPointLocation( const QgsPoint& p ) { mLocation = toCanvasCoordinates( p ); }
void QgsMapToolRotateFeature::canvasPressEvent( QMouseEvent * e ) { mRotation = 0; if ( mCtrl ) { return; } delete mRubberBand; mRubberBand = 0; mInitialPos = e->pos(); QgsVectorLayer* vlayer = currentVectorLayer(); if ( !vlayer ) { notifyNotVectorLayer(); return; } if ( !vlayer->isEditable() ) { notifyNotEditableLayer(); return; } QgsPoint layerCoords = toLayerCoordinates( vlayer, e->pos() ); double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapSettings() ); QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius, layerCoords.x() + searchRadius, layerCoords.y() + searchRadius ); if ( vlayer->selectedFeatureCount() == 0 ) { QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( selectRect ).setSubsetOfAttributes( QgsAttributeList() ) ); //find the closest feature QgsGeometry* pointGeometry = QgsGeometry::fromPoint( layerCoords ); if ( !pointGeometry ) { return; } double minDistance = std::numeric_limits<double>::max(); QgsFeature cf; QgsFeature f; while ( fit.nextFeature( f ) ) { if ( f.geometry() ) { double currentDistance = pointGeometry->distance( *f.geometry() ); if ( currentDistance < minDistance ) { minDistance = currentDistance; cf = f; } } } delete pointGeometry; if ( minDistance == std::numeric_limits<double>::max() ) { return; } QgsRectangle bound = cf.geometry()->boundingBox(); mStartPointMapCoords = toMapCoordinates( vlayer, bound.center() ); if ( !mAnchorPoint ) { mAnchorPoint = new QgsVertexMarker( mCanvas ); } mAnchorPoint->setIconType( QgsVertexMarker::ICON_CROSS ); mAnchorPoint->setCenter( mStartPointMapCoords ); mStPoint = toCanvasCoordinates( mStartPointMapCoords ); mRotatedFeatures.clear(); mRotatedFeatures << cf.id(); //todo: take the closest feature, not the first one... mRubberBand = createRubberBand( vlayer->geometryType() ); mRubberBand->setToGeometry( cf.geometry(), vlayer ); } else { mRotatedFeatures = vlayer->selectedFeaturesIds(); mRubberBand = createRubberBand( vlayer->geometryType() ); QgsFeature feat; QgsFeatureIterator it = vlayer->selectedFeaturesIterator(); while ( it.nextFeature( feat ) ) { mRubberBand->addGeometry( feat.geometry(), vlayer ); } } mRubberBand->setColor( QColor( 255, 0, 0, 65 ) ); mRubberBand->setWidth( 2 ); mRubberBand->show(); double XDistance = mInitialPos.x() - mAnchorPoint->x(); double YDistance = mInitialPos.y() - mAnchorPoint->y() ; mRotationOffset = atan2( YDistance, XDistance ) * ( 180 / PI ); }