Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When selecting overlapping annotations, prefer the smallest #58124

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/gui/annotations/qgsmaptoolmodifyannotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ const QgsRenderedAnnotationItemDetails *QgsMapToolModifyAnnotation::findClosestI
{
const QgsRenderedAnnotationItemDetails *closestItem = nullptr;
double closestItemDistance = std::numeric_limits< double >::max();
int closestItemZ = 0;
double closestItemArea = std::numeric_limits< double >::max();

for ( const QgsRenderedAnnotationItemDetails *item : items )
{
Expand All @@ -735,11 +735,11 @@ const QgsRenderedAnnotationItemDetails *QgsMapToolModifyAnnotation::findClosestI

const QgsRectangle itemBounds = item->boundingBox();
const double itemDistance = itemBounds.contains( mapPoint ) ? 0 : itemBounds.distance( mapPoint );
if ( !closestItem || itemDistance < closestItemDistance || ( itemDistance == closestItemDistance && annotationItem->zIndex() > closestItemZ ) )
if ( !closestItem || itemDistance < closestItemDistance || ( itemDistance == closestItemDistance && itemBounds.area() < closestItemArea ) )
{
closestItem = item;
closestItemDistance = itemDistance;
closestItemZ = annotationItem->zIndex();
closestItemArea = itemBounds.area();
bounds = itemBounds;
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/src/app/testqgsmaptooleditannotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ void TestQgsMapToolEditAnnotation::testSelectItem()
item1->setZIndex( 1 );
const QString i1id = layer->addItem( item1 );

QgsAnnotationPolygonItem *item2 = new QgsAnnotationPolygonItem( new QgsPolygon( new QgsLineString( QVector<QgsPoint> { QgsPoint( 1, 4 ), QgsPoint( 5, 4 ), QgsPoint( 5, 9 ), QgsPoint( 1, 9 ), QgsPoint( 1, 4 ) } ) ) );
item2->setZIndex( 2 );
QgsAnnotationPolygonItem *item2 = new QgsAnnotationPolygonItem( new QgsPolygon( new QgsLineString( QVector<QgsPoint> { QgsPoint( 1, 4 ), QgsPoint( 1.6, 4 ), QgsPoint( 1.6, 4.6 ), QgsPoint( 1, 4.6 ), QgsPoint( 1, 4 ) } ) ) );
item2->setZIndex( 0 );
const QString i2id = layer->addItem( item2 );

QgsAnnotationPolygonItem *item3 = new QgsAnnotationPolygonItem( new QgsPolygon( new QgsLineString( QVector<QgsPoint> { QgsPoint( 7, 1 ), QgsPoint( 8, 1 ), QgsPoint( 8, 2 ), QgsPoint( 7, 2 ), QgsPoint( 7, 1 ) } ) ) );
Expand Down Expand Up @@ -131,7 +131,7 @@ void TestQgsMapToolEditAnnotation::testSelectItem()
QCOMPARE( spy.count(), 1 );
QCOMPARE( spy.at( 0 ).at( 1 ).toString(), i1id );

// overlapping items, highest z order should be selected
// overlapping items, smallest area item should be selected
utils.mouseMove( 1.5, 4.5 );
utils.mouseClick( 1.5, 4.5, Qt::LeftButton, Qt::KeyboardModifiers(), true );
QCOMPARE( spy.count(), 2 );
Expand Down