@@ -50,26 +50,19 @@ QDialog *QgsMapToolAnnotation::createItemEditor( QgsMapCanvasAnnotationItem *ite
50
50
51
51
QgsAnnotation *annotation = item->annotation ();
52
52
53
- QgsTextAnnotation *tItem = dynamic_cast <QgsTextAnnotation *>( annotation );
54
- if ( tItem )
53
+ if ( qobject_cast<QgsTextAnnotation *>( annotation ) )
55
54
{
56
55
return new QgsTextAnnotationDialog ( item );
57
56
}
58
-
59
- QgsFormAnnotation *fItem = dynamic_cast <QgsFormAnnotation *>( annotation );
60
- if ( fItem )
57
+ else if ( qobject_cast<QgsFormAnnotation *>( annotation ) )
61
58
{
62
59
return new QgsFormAnnotationDialog ( item );
63
60
}
64
-
65
- QgsHtmlAnnotation *hItem = dynamic_cast <QgsHtmlAnnotation *>( annotation );
66
- if ( hItem )
61
+ else if ( qobject_cast<QgsHtmlAnnotation *>( annotation ) )
67
62
{
68
63
return new QgsHtmlAnnotationDialog ( item );
69
64
}
70
-
71
- QgsSvgAnnotation *sItem = dynamic_cast <QgsSvgAnnotation *>( annotation );
72
- if ( sItem )
65
+ else if ( qobject_cast<QgsSvgAnnotation *>( annotation ) )
73
66
{
74
67
return new QgsSvgAnnotationDialog ( item );
75
68
}
@@ -124,7 +117,7 @@ void QgsMapToolAnnotation::canvasPressEvent( QgsMapMouseEvent *e )
124
117
annotation->setMapPositionCrs ( mCanvas ->mapSettings ().destinationCrs () );
125
118
annotation->setRelativePosition ( QPointF ( e->pos ().x () / mCanvas ->width (),
126
119
e->pos ().y () / mCanvas ->height () ) );
127
- annotation->setFrameSize ( QSizeF ( 200 , 100 ) );
120
+ annotation->setFrameSizeMm ( QSizeF ( 50 , 25 ) );
128
121
129
122
QgsProject::instance ()->annotationManager ()->addAnnotation ( annotation );
130
123
@@ -193,7 +186,11 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
193
186
QPointF newCanvasPos = item->pos () + ( e->pos () - mLastMousePosition );
194
187
if ( annotation->hasFixedMapPosition () )
195
188
{
196
- annotation->setFrameOffsetFromReferencePoint ( annotation->frameOffsetFromReferencePoint () + ( e->pos () - mLastMousePosition ) );
189
+ const double pixelToMmScale = 25.4 / mCanvas ->logicalDpiX ();
190
+ const double deltaX = pixelToMmScale * ( e->pos ().x () - mLastMousePosition .x () );
191
+ const double deltaY = pixelToMmScale * ( e->pos ().y () - mLastMousePosition .y () );
192
+ annotation->setFrameOffsetFromReferencePointMm ( QPointF ( annotation->frameOffsetFromReferencePointMm ().x () + deltaX,
193
+ annotation->frameOffsetFromReferencePointMm ().y () + deltaY ) );
197
194
annotation->setRelativePosition ( QPointF ( newCanvasPos.x () / mCanvas ->width (),
198
195
newCanvasPos.y () / mCanvas ->height () ) );
199
196
}
@@ -210,9 +207,12 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
210
207
else if ( mCurrentMoveAction != QgsMapCanvasAnnotationItem::NoAction )
211
208
{
212
209
// handle the frame resize actions
213
- QSizeF size = annotation->frameSize ();
214
- double xmin = annotation->frameOffsetFromReferencePoint ().x ();
215
- double ymin = annotation->frameOffsetFromReferencePoint ().y ();
210
+
211
+ const double pixelToMmScale = 25.4 / mCanvas ->logicalDpiX ();
212
+
213
+ QSizeF size = annotation->frameSizeMm ();
214
+ double xmin = annotation->frameOffsetFromReferencePointMm ().x ();
215
+ double ymin = annotation->frameOffsetFromReferencePointMm ().y ();
216
216
double xmax = xmin + size.width ();
217
217
double ymax = ymin + size.height ();
218
218
double relPosX = annotation->relativePosition ().x ();
@@ -222,27 +222,27 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
222
222
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightDown ||
223
223
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightUp )
224
224
{
225
- xmax += e->pos ().x () - mLastMousePosition .x ();
225
+ xmax += pixelToMmScale * ( e->pos ().x () - mLastMousePosition .x () );
226
226
}
227
227
if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeft ||
228
228
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftDown ||
229
229
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftUp )
230
230
{
231
- xmin += e->pos ().x () - mLastMousePosition .x ();
231
+ xmin += pixelToMmScale * ( e->pos ().x () - mLastMousePosition .x () );
232
232
relPosX = ( relPosX * mCanvas ->width () + e->pos ().x () - mLastMousePosition .x () ) / static_cast <double >( mCanvas ->width () );
233
233
}
234
234
if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameUp ||
235
235
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftUp ||
236
236
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightUp )
237
237
{
238
- ymin += e->pos ().y () - mLastMousePosition .y ();
238
+ ymin += pixelToMmScale * ( e->pos ().y () - mLastMousePosition .y () );
239
239
relPosY = ( relPosY * mCanvas ->height () + e->pos ().y () - mLastMousePosition .y () ) / static_cast <double >( mCanvas ->height () );
240
240
}
241
241
if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameDown ||
242
242
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftDown ||
243
243
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightDown )
244
244
{
245
- ymax += e->pos ().y () - mLastMousePosition .y ();
245
+ ymax += pixelToMmScale * ( e->pos ().y () - mLastMousePosition .y () );
246
246
}
247
247
248
248
// switch min / max if necessary
@@ -260,8 +260,8 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
260
260
ymin = tmp;
261
261
}
262
262
263
- annotation->setFrameOffsetFromReferencePoint ( QPointF ( xmin, ymin ) );
264
- annotation->setFrameSize ( QSizeF ( xmax - xmin, ymax - ymin ) );
263
+ annotation->setFrameOffsetFromReferencePointMm ( QPointF ( xmin, ymin ) );
264
+ annotation->setFrameSizeMm ( QSizeF ( xmax - xmin, ymax - ymin ) );
265
265
annotation->setRelativePosition ( QPointF ( relPosX, relPosY ) );
266
266
item->update ();
267
267
QgsProject::instance ()->setDirty ( true );
@@ -351,7 +351,7 @@ void QgsMapToolAnnotation::toggleTextItemVisibilities()
351
351
const auto constItemList = itemList;
352
352
for ( QgsMapCanvasAnnotationItem *item : constItemList )
353
353
{
354
- QgsTextAnnotation *textItem = dynamic_cast <QgsTextAnnotation *>( item->annotation () );
354
+ QgsTextAnnotation *textItem = qobject_cast <QgsTextAnnotation *>( item->annotation () );
355
355
if ( textItem )
356
356
{
357
357
textItem->setVisible ( !textItem->isVisible () );
0 commit comments