void QgsMapToolAnnotation::canvasPressEvent( QgsMapMouseEvent *e ) { if ( !mCanvas ) { return; } mLastMousePosition = e->pos(); QgsMapCanvasAnnotationItem *item = selectedItem(); if ( item ) { mCurrentMoveAction = item->moveActionForPosition( e->pos() ); if ( mCurrentMoveAction != QgsMapCanvasAnnotationItem::NoAction ) { return; } } if ( !item || mCurrentMoveAction == QgsMapCanvasAnnotationItem::NoAction ) { //select a new item if there is one at this position mCanvas->scene()->clearSelection(); QgsMapCanvasAnnotationItem *existingItem = itemAtPos( e->pos() ); if ( existingItem ) { existingItem->setSelected( true ); } else { //otherwise create new one QgsAnnotation *annotation = createItem(); if ( annotation ) { QgsPointXY mapPos = transformCanvasToAnnotation( toMapCoordinates( e->pos() ), annotation ); annotation->setMapPosition( mapPos ); annotation->setMapPositionCrs( mCanvas->mapSettings().destinationCrs() ); annotation->setRelativePosition( QPointF( e->pos().x() / mCanvas->width(), e->pos().y() / mCanvas->height() ) ); annotation->setFrameSizeMm( QSizeF( 50, 25 ) ); QgsProject::instance()->annotationManager()->addAnnotation( annotation ); // select newly added item const auto constItems = mCanvas->items(); for ( QGraphicsItem *item : constItems ) { if ( QgsMapCanvasAnnotationItem *annotationItem = dynamic_cast< QgsMapCanvasAnnotationItem * >( item ) ) { if ( annotationItem->annotation() == annotation ) { annotationItem->setSelected( true ); break; } } } } } } }
void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e ) { QgsMapCanvasAnnotationItem *item = selectedItem(); if ( !item || !item->annotation() ) return; QgsAnnotation *annotation = item->annotation(); if ( e->buttons() & Qt::LeftButton ) { if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::MoveMapPosition ) { QgsPointXY mapPos = transformCanvasToAnnotation( e->snapPoint(), annotation ); annotation->setMapPosition( mapPos ); annotation->setRelativePosition( QPointF( e->pos().x() / mCanvas->width(), e->pos().y() / mCanvas->height() ) ); item->update(); QgsProject::instance()->setDirty( true ); } else if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::MoveFramePosition ) { QPointF newCanvasPos = item->pos() + ( e->pos() - mLastMousePosition ); if ( annotation->hasFixedMapPosition() ) { const double pixelToMmScale = 25.4 / mCanvas->logicalDpiX(); const double deltaX = pixelToMmScale * ( e->pos().x() - mLastMousePosition.x() ); const double deltaY = pixelToMmScale * ( e->pos().y() - mLastMousePosition.y() ); annotation->setFrameOffsetFromReferencePointMm( QPointF( annotation->frameOffsetFromReferencePointMm().x() + deltaX, annotation->frameOffsetFromReferencePointMm().y() + deltaY ) ); annotation->setRelativePosition( QPointF( newCanvasPos.x() / mCanvas->width(), newCanvasPos.y() / mCanvas->height() ) ); } else { QgsPointXY mapPos = transformCanvasToAnnotation( toMapCoordinates( newCanvasPos.toPoint() ), annotation ); annotation->setMapPosition( mapPos ); annotation->setRelativePosition( QPointF( newCanvasPos.x() / mCanvas->width(), newCanvasPos.y() / mCanvas->height() ) ); } item->update(); QgsProject::instance()->setDirty( true ); } else if ( mCurrentMoveAction != QgsMapCanvasAnnotationItem::NoAction ) { //handle the frame resize actions const double pixelToMmScale = 25.4 / mCanvas->logicalDpiX(); QSizeF size = annotation->frameSizeMm(); double xmin = annotation->frameOffsetFromReferencePointMm().x(); double ymin = annotation->frameOffsetFromReferencePointMm().y(); double xmax = xmin + size.width(); double ymax = ymin + size.height(); double relPosX = annotation->relativePosition().x(); double relPosY = annotation->relativePosition().y(); if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRight || mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightDown || mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightUp ) { xmax += pixelToMmScale * ( e->pos().x() - mLastMousePosition.x() ); } if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeft || mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftDown || mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftUp ) { xmin += pixelToMmScale * ( e->pos().x() - mLastMousePosition.x() ); relPosX = ( relPosX * mCanvas->width() + e->pos().x() - mLastMousePosition.x() ) / static_cast<double>( mCanvas->width() ); } if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameUp || mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftUp || mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightUp ) { ymin += pixelToMmScale * ( e->pos().y() - mLastMousePosition.y() ); relPosY = ( relPosY * mCanvas->height() + e->pos().y() - mLastMousePosition.y() ) / static_cast<double>( mCanvas->height() ); } if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameDown || mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftDown || mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightDown ) { ymax += pixelToMmScale * ( e->pos().y() - mLastMousePosition.y() ); } //switch min / max if necessary double tmp; if ( xmax < xmin ) { tmp = xmax; xmax = xmin; xmin = tmp; } if ( ymax < ymin ) { tmp = ymax; ymax = ymin; ymin = tmp; } annotation->setFrameOffsetFromReferencePointMm( QPointF( xmin, ymin ) ); annotation->setFrameSizeMm( QSizeF( xmax - xmin, ymax - ymin ) ); annotation->setRelativePosition( QPointF( relPosX, relPosY ) ); item->update(); QgsProject::instance()->setDirty( true ); } } else if ( item ) { QgsMapCanvasAnnotationItem::MouseMoveAction moveAction = item->moveActionForPosition( e->pos() ); if ( mCanvas ) { mCanvas->setCursor( QCursor( item->cursorShapeForAction( moveAction ) ) ); } } mLastMousePosition = e->pos(); }