@@ -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
@@ -192,7 +185,11 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
192
185
QPointF newCanvasPos = item->pos () + ( e->pos () - mLastMousePosition );
193
186
if ( annotation->hasFixedMapPosition () )
194
187
{
195
- annotation->setFrameOffsetFromReferencePoint ( annotation->frameOffsetFromReferencePoint () + ( e->pos () - mLastMousePosition ) );
188
+ const double pixelToMmScale = 25.4 / mCanvas ->logicalDpiX ();
189
+ const double deltaX = pixelToMmScale * ( e->pos ().x () - mLastMousePosition .x () );
190
+ const double deltaY = pixelToMmScale * ( e->pos ().y () - mLastMousePosition .y () );
191
+ annotation->setFrameOffsetFromReferencePointMm ( QPointF ( annotation->frameOffsetFromReferencePointMm ().x () + deltaX,
192
+ annotation->frameOffsetFromReferencePointMm ().y () + deltaY ) );
196
193
annotation->setRelativePosition ( QPointF ( newCanvasPos.x () / mCanvas ->width (),
197
194
newCanvasPos.y () / mCanvas ->height () ) );
198
195
}
@@ -209,9 +206,12 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
209
206
else if ( mCurrentMoveAction != QgsMapCanvasAnnotationItem::NoAction )
210
207
{
211
208
// handle the frame resize actions
212
- QSizeF size = annotation->frameSize ();
213
- double xmin = annotation->frameOffsetFromReferencePoint ().x ();
214
- double ymin = annotation->frameOffsetFromReferencePoint ().y ();
209
+
210
+ const double pixelToMmScale = 25.4 / mCanvas ->logicalDpiX ();
211
+
212
+ QSizeF size = annotation->frameSizeMm ();
213
+ double xmin = annotation->frameOffsetFromReferencePointMm ().x ();
214
+ double ymin = annotation->frameOffsetFromReferencePointMm ().y ();
215
215
double xmax = xmin + size.width ();
216
216
double ymax = ymin + size.height ();
217
217
double relPosX = annotation->relativePosition ().x ();
@@ -221,27 +221,27 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
221
221
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightDown ||
222
222
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightUp )
223
223
{
224
- xmax += e->pos ().x () - mLastMousePosition .x ();
224
+ xmax += pixelToMmScale * ( e->pos ().x () - mLastMousePosition .x () );
225
225
}
226
226
if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeft ||
227
227
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftDown ||
228
228
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftUp )
229
229
{
230
- xmin += e->pos ().x () - mLastMousePosition .x ();
230
+ xmin += pixelToMmScale * ( e->pos ().x () - mLastMousePosition .x () );
231
231
relPosX = ( relPosX * mCanvas ->width () + e->pos ().x () - mLastMousePosition .x () ) / static_cast <double >( mCanvas ->width () );
232
232
}
233
233
if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameUp ||
234
234
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftUp ||
235
235
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightUp )
236
236
{
237
- ymin += e->pos ().y () - mLastMousePosition .y ();
237
+ ymin += pixelToMmScale * ( e->pos ().y () - mLastMousePosition .y () );
238
238
relPosY = ( relPosY * mCanvas ->height () + e->pos ().y () - mLastMousePosition .y () ) / static_cast <double >( mCanvas ->height () );
239
239
}
240
240
if ( mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameDown ||
241
241
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameLeftDown ||
242
242
mCurrentMoveAction == QgsMapCanvasAnnotationItem::ResizeFrameRightDown )
243
243
{
244
- ymax += e->pos ().y () - mLastMousePosition .y ();
244
+ ymax += pixelToMmScale * ( e->pos ().y () - mLastMousePosition .y () );
245
245
}
246
246
247
247
// switch min / max if necessary
@@ -259,8 +259,8 @@ void QgsMapToolAnnotation::canvasMoveEvent( QgsMapMouseEvent *e )
259
259
ymin = tmp;
260
260
}
261
261
262
- annotation->setFrameOffsetFromReferencePoint ( QPointF ( xmin, ymin ) );
263
- annotation->setFrameSize ( QSizeF ( xmax - xmin, ymax - ymin ) );
262
+ annotation->setFrameOffsetFromReferencePointMm ( QPointF ( xmin, ymin ) );
263
+ annotation->setFrameSizeMm ( QSizeF ( xmax - xmin, ymax - ymin ) );
264
264
annotation->setRelativePosition ( QPointF ( relPosX, relPosY ) );
265
265
item->update ();
266
266
QgsProject::instance ()->setDirty ( true );
@@ -349,7 +349,7 @@ void QgsMapToolAnnotation::toggleTextItemVisibilities()
349
349
QList<QgsMapCanvasAnnotationItem *> itemList = annotationItems ();
350
350
Q_FOREACH ( QgsMapCanvasAnnotationItem *item, itemList )
351
351
{
352
- QgsTextAnnotation *textItem = dynamic_cast <QgsTextAnnotation *>( item->annotation () );
352
+ QgsTextAnnotation *textItem = qobject_cast <QgsTextAnnotation *>( item->annotation () );
353
353
if ( textItem )
354
354
{
355
355
textItem->setVisible ( !textItem->isVisible () );
0 commit comments