36 , mAnnotation( annotation )
38 setFlag( QGraphicsItem::ItemIsSelectable,
true );
44 connect( mAnnotation, &
QgsAnnotation::moved,
this, &QgsMapCanvasAnnotationItem::setFeatureForMapPosition );
53 connect( mAnnotation, &QgsAnnotation::destroyed,
this, &QgsMapCanvasAnnotationItem::annotationDeleted );
56 setFeatureForMapPosition();
92 void QgsMapCanvasAnnotationItem::updateBoundingRect()
94 prepareGeometryChange();
97 const double fillSymbolBleed = mAnnotation && mAnnotation->
fillSymbol() ?
100 const double mmToPixelScale =
mMapCanvas->logicalDpiX() / 25.4;
104 mBoundingRect = QRectF( - fillSymbolBleed, -fillSymbolBleed,
105 mmToPixelScale * mAnnotation->
frameSizeMm().width() + fillSymbolBleed * 2,
106 mmToPixelScale * mAnnotation->
frameSizeMm().height() + fillSymbolBleed * 2 );
110 double halfSymbolSize = 0.0;
113 halfSymbolSize = scaledSymbolSize() / 2.0;
119 const QSizeF frameSize = mAnnotation ? QSizeF( mAnnotation->
frameSizeMm().width() * mmToPixelScale,
120 mAnnotation->
frameSizeMm().height() * mmToPixelScale ) : QSizeF( 0.0, 0.0 );
122 const double xMinPos = std::min( -halfSymbolSize, offset.x() - fillSymbolBleed );
123 const double xMaxPos = std::max( halfSymbolSize, offset.x() + frameSize.width() + fillSymbolBleed );
124 const double yMinPos = std::min( -halfSymbolSize, offset.y() - fillSymbolBleed );
125 const double yMaxPos = std::max( halfSymbolSize, offset.y() + frameSize.height() + fillSymbolBleed );
126 mBoundingRect = QRectF( xMinPos, yMinPos, xMaxPos - xMinPos, yMaxPos - yMinPos );
130 void QgsMapCanvasAnnotationItem::onCanvasLayersChanged()
138 else if ( !mAnnotation->
mapLayer() )
148 void QgsMapCanvasAnnotationItem::setFeatureForMapPosition()
164 mapPosition = ct.transform( mapPosition );
170 QgsRectangle searchRect( mapPosition.
x() - halfIdentifyWidth, mapPosition.
y() - halfIdentifyWidth,
171 mapPosition.
x() + halfIdentifyWidth, mapPosition.
y() + halfIdentifyWidth );
182 void QgsMapCanvasAnnotationItem::annotationDeleted()
184 mAnnotation =
nullptr;
188 void QgsMapCanvasAnnotationItem::drawSelectionBoxes( QPainter *p )
const
195 const double handlerSize = 10;
196 p->setPen( Qt::NoPen );
197 p->setBrush( QColor( 200, 200, 210, 120 ) );
198 p->drawRect( QRectF( mBoundingRect.left(), mBoundingRect.top(), handlerSize, handlerSize ) );
199 p->drawRect( QRectF( mBoundingRect.right() - handlerSize, mBoundingRect.top(), handlerSize, handlerSize ) );
200 p->drawRect( QRectF( mBoundingRect.right() - handlerSize, mBoundingRect.bottom() - handlerSize, handlerSize, handlerSize ) );
201 p->drawRect( QRectF( mBoundingRect.left(), mBoundingRect.bottom() - handlerSize, handlerSize, handlerSize ) );
206 const QPointF itemPos = mapFromScene( pos );
208 const int cursorSensitivity = 7;
211 std::fabs( itemPos.x() ) < cursorSensitivity && std::fabs( itemPos.y() ) < cursorSensitivity )
216 const double mmToPixelScale =
mMapCanvas->logicalDpiX() / 25.4;
219 const QSizeF frameSize = mAnnotation ? mAnnotation->
frameSizeMm() * mmToPixelScale : QSizeF( 0, 0 );
221 bool left, right, up, down;
222 left = std::fabs( itemPos.x() - offset.x() ) < cursorSensitivity;
223 right = std::fabs( itemPos.x() - ( offset.x() + frameSize.width() ) ) < cursorSensitivity;
224 up = std::fabs( itemPos.y() - offset.y() ) < cursorSensitivity;
225 down = std::fabs( itemPos.y() - ( offset.y() + frameSize.height() ) ) < cursorSensitivity;
231 else if ( right && up )
235 else if ( left && down )
239 else if ( right && down )
261 if ( itemPos.x() >= offset.x() && itemPos.x() <= ( offset.x() + frameSize.width() )
262 && itemPos.y() >= offset.y() && itemPos.y() <= ( offset.y() + frameSize.height() ) )
271 switch ( moveAction )
274 return Qt::ArrowCursor;
277 return Qt::SizeAllCursor;
280 return Qt::SizeVerCursor;
283 return Qt::SizeHorCursor;
286 return Qt::SizeFDiagCursor;
289 return Qt::SizeBDiagCursor;
291 return Qt::ArrowCursor;
295 double QgsMapCanvasAnnotationItem::scaledSymbolSize()
const
307 const double dpmm =
mMapCanvas->logicalDpiX() / 25.4;
313 if ( !mAnnotation || !mAnnotation->
isVisible() )
320 mAnnotation->
render( rc );
324 drawSelectionBoxes( painter );