Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tweak cursors and resizing behavior
  • Loading branch information
YoannQDQ authored and nyalldawson committed May 7, 2023
1 parent 28af4c0 commit 3c3bfb8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 80 deletions.
96 changes: 46 additions & 50 deletions src/app/qgsmaptoolannotation.cpp
Expand Up @@ -34,12 +34,13 @@
#include "qgsexception.h"
#include "qgsannotationmanager.h"
#include "qgsmapmouseevent.h"
#include "qgsapplication.h"


QgsMapToolAnnotation::QgsMapToolAnnotation( QgsMapCanvas *canvas )
: QgsMapTool( canvas )
{
mCursor = QCursor( Qt::ArrowCursor );
mCursor = QgsApplication::getThemeCursor( QgsApplication::CapturePoint );
}

QgsMapTool::Flags QgsMapToolAnnotation::flags() const
Expand Down Expand Up @@ -81,7 +82,6 @@ void QgsMapToolAnnotation::canvasReleaseEvent( QgsMapMouseEvent *e )
Q_UNUSED( e )

mCurrentMoveAction = QgsMapCanvasAnnotationItem::NoAction;
mCanvas->setCursor( mCursor );
}

void QgsMapToolAnnotation::canvasPressEvent( QgsMapMouseEvent *e )
Expand All @@ -93,56 +93,50 @@ void QgsMapToolAnnotation::canvasPressEvent( QgsMapMouseEvent *e )

mLastMousePosition = e->pos();

QgsMapCanvasAnnotationItem *item = selectedItem();
// Check if we clicked on an existing item
QgsMapCanvasAnnotationItem *item = itemAtPos( e->pos() );
if ( item )
{
mCurrentMoveAction = item->moveActionForPosition( e->pos() );
if ( mCurrentMoveAction != QgsMapCanvasAnnotationItem::NoAction )
if ( !item->isSelected() )
{
return;
mCanvas->scene()->clearSelection();
item->setSelected( true );
}
return;
}

if ( !item || mCurrentMoveAction == QgsMapCanvasAnnotationItem::NoAction )
// Otherwise create new one
mCanvas->scene()->clearSelection();

QgsAnnotation *annotation = createItem();
if ( annotation )
{
//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
const 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 )
{
//otherwise create new one
QgsAnnotation *annotation = createItem();
if ( annotation )
if ( QgsMapCanvasAnnotationItem *annotationItem = dynamic_cast< QgsMapCanvasAnnotationItem * >( item ) )
{
const 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 ( annotationItem->annotation() == annotation )
{
if ( QgsMapCanvasAnnotationItem *annotationItem = dynamic_cast< QgsMapCanvasAnnotationItem * >( item ) )
{
if ( annotationItem->annotation() == annotation )
{
annotationItem->setSelected( true );
break;
}
}
annotationItem->setSelected( true );
break;
}
}
}
}


}

void QgsMapToolAnnotation::keyPressEvent( QKeyEvent *e )
Expand All @@ -152,11 +146,10 @@ void QgsMapToolAnnotation::keyPressEvent( QKeyEvent *e )
{
if ( e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete )
{
const QCursor neutralCursor( item->cursorShapeForAction( QgsMapCanvasAnnotationItem::NoAction ) );
QgsProject::instance()->annotationManager()->removeAnnotation( item->annotation() );
if ( mCanvas )
{
mCanvas->setCursor( neutralCursor );
mCanvas->setCursor( mCursor );
e->ignore();
}
}
Expand Down Expand Up @@ -191,14 +184,12 @@ bool QgsMapToolAnnotation::populateContextMenuWithEvent( QMenu *menu, QgsMapMous
void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
{
QgsMapCanvasAnnotationItem *item = selectedItem();
if ( !item )
return;

QgsAnnotation *annotation = item->annotation();
if ( !annotation )
return;
QgsAnnotation *annotation = nullptr;
if ( item )
annotation = item->annotation();

if ( e->buttons() & Qt::LeftButton )
if ( annotation && ( e->buttons() & Qt::LeftButton ) )
{
if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::MoveMapPosition )
{
Expand Down Expand Up @@ -295,12 +286,15 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
QgsProject::instance()->setDirty( true );
}
}
else if ( item )
else if ( mCanvas )
{
const QgsMapCanvasAnnotationItem::MouseMoveAction moveAction = item->moveActionForPosition( e->pos() );
if ( mCanvas )
if ( ( item = itemAtPos( e->pos() ) ) )
{
mCanvas->setCursor( QCursor( item->cursorShapeForAction( item->moveActionForPosition( e->pos() ) ) ) );
}
else
{
mCanvas->setCursor( QCursor( item->cursorShapeForAction( moveAction ) ) );
mCanvas->setCursor( mCursor );
}
}
mLastMousePosition = e->pos();
Expand Down Expand Up @@ -334,7 +328,9 @@ QgsMapCanvasAnnotationItem *QgsMapToolAnnotation::itemAtPos( QPointF pos ) const
for ( ; gIt != graphicItems.end(); ++gIt )
{
QgsMapCanvasAnnotationItem *annotationItem = dynamic_cast<QgsMapCanvasAnnotationItem *>( *gIt );
if ( annotationItem )
// Consider only the topmost item that has a move action for the position
// (i.e. cursor is over the frame or over the anchor point)
if ( annotationItem && annotationItem->moveActionForPosition( pos ) != QgsMapCanvasAnnotationItem::NoAction )
{
return annotationItem;
}
Expand Down
65 changes: 35 additions & 30 deletions src/gui/qgsmapcanvasannotationitem.cpp
Expand Up @@ -229,37 +229,42 @@ QgsMapCanvasAnnotationItem::MouseMoveAction QgsMapCanvasAnnotationItem::moveActi
itemPos.y() + cursorSensitivity >= offset.y() &&
itemPos.y() - cursorSensitivity <= ( offset.y() + frameSize.height() ) );

if ( left && up )
{
return ResizeFrameLeftUp;
}
else if ( right && up )
{
return ResizeFrameRightUp;
}
else if ( left && down )
{
return ResizeFrameLeftDown;
}
else if ( right && down )
{
return ResizeFrameRightDown;
}
if ( left && inframe )
{
return ResizeFrameLeft;
}
if ( right && inframe )
{
return ResizeFrameRight;
}
if ( up && inframe )
{
return ResizeFrameUp;
}
if ( down && inframe )
// Resize actions are only available if the item is selected
// Otherwise, mouse handles are not visible
if ( isSelected() )
{
return ResizeFrameDown;
if ( left && up )
{
return ResizeFrameLeftUp;
}
else if ( right && up )
{
return ResizeFrameRightUp;
}
else if ( left && down )
{
return ResizeFrameLeftDown;
}
else if ( right && down )
{
return ResizeFrameRightDown;
}
if ( left && inframe )
{
return ResizeFrameLeft;
}
if ( right && inframe )
{
return ResizeFrameRight;
}
if ( up && inframe )
{
return ResizeFrameUp;
}
if ( down && inframe )
{
return ResizeFrameDown;
}
}

//finally test if pos is in the frame area
Expand Down

0 comments on commit 3c3bfb8

Please sign in to comment.