QGIS API Documentation 3.39.0-Master (7b5d8bea57d)
Loading...
Searching...
No Matches
qgsannotationrectitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsannotationrectitem.cpp
3 ----------------
4 begin : July 2024
5 copyright : (C) 2024 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsapplication.h"
20#include "qgsgeometry.h"
21#include "qgsrendercontext.h"
24#include "qgspainting.h"
25#include "qgsfillsymbol.h"
26#include "qgssymbollayerutils.h"
27#include "qgsfillsymbollayer.h"
28#include "qgslinesymbollayer.h"
29#include "qgscalloutsregistry.h"
30#include "qgslinestring.h"
31#include "qgspolygon.h"
32#include "qgsunittypes.h"
33
36 , mBounds( bounds )
37{
38 mBackgroundSymbol = std::make_unique< QgsFillSymbol >( QgsSymbolLayerList
39 {
40 new QgsSimpleFillSymbolLayer( QColor( 255, 255, 255 ), Qt::BrushStyle::SolidPattern, QColor( 0, 0, 0 ), Qt::PenStyle::NoPen )
41 } );
42 QgsSimpleLineSymbolLayer *borderSymbol = new QgsSimpleLineSymbolLayer( QColor( 0, 0, 0 ) );
43 borderSymbol->setPenJoinStyle( Qt::MiterJoin );
44 mFrameSymbol = std::make_unique< QgsFillSymbol >( QgsSymbolLayerList{ borderSymbol } );
45}
46
48
63
65{
66 QgsRectangle bounds = mBounds;
68 {
69 try
70 {
71 bounds = context.coordinateTransform().transformBoundingBox( mBounds );
72 }
73 catch ( QgsCsException & )
74 {
75 return;
76 }
77 }
78
79 QRectF painterBounds;
80
81 switch ( mPlacementMode )
82 {
84 painterBounds = context.mapToPixel().transformBounds( bounds.toRectF() );
85 break;
86
88 {
89 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
90 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
91
92 if ( callout() && !calloutAnchor().isEmpty() )
93 {
94 QgsGeometry anchor = calloutAnchor();
95
96 const double calloutOffsetWidthPixels = context.convertToPainterUnits( offsetFromCallout().width(), offsetFromCalloutUnit() );
97 const double calloutOffsetHeightPixels = context.convertToPainterUnits( offsetFromCallout().height(), offsetFromCalloutUnit() );
98
99 QPointF anchorPoint = anchor.asQPointF();
100 if ( context.coordinateTransform().isValid() )
101 {
102 double x = anchorPoint.x();
103 double y = anchorPoint.y();
104 double z = 0.0;
105 context.coordinateTransform().transformInPlace( x, y, z );
106 anchorPoint = QPointF( x, y );
107 }
108
109 context.mapToPixel().transformInPlace( anchorPoint.rx(), anchorPoint.ry() );
110
111 painterBounds = QRectF( anchorPoint.x() + calloutOffsetWidthPixels,
112 anchorPoint.y() + calloutOffsetHeightPixels, widthPixels, heightPixels );
113 }
114 else
115 {
116 QPointF center = bounds.center().toQPointF();
117
118 context.mapToPixel().transformInPlace( center.rx(), center.ry() );
119 painterBounds = QRectF( center.x() - widthPixels * 0.5,
120 center.y() - heightPixels * 0.5,
121 widthPixels, heightPixels );
122 }
123 break;
124 }
125
127 {
128 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
129 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
130
131 QPointF center = bounds.center().toQPointF();
132 center.rx() *= context.outputSize().width();
133 center.ry() *= context.outputSize().height();
134
135 painterBounds = QRectF( center.x() - widthPixels * 0.5,
136 center.y() - heightPixels * 0.5,
137 widthPixels, heightPixels );
138 break;
139 }
140 }
141
142 if ( painterBounds.width() < 1 || painterBounds.height() < 1 )
143 return;
144
145 if ( mDrawBackground && mBackgroundSymbol )
146 {
147 mBackgroundSymbol->startRender( context );
148 mBackgroundSymbol->renderPolygon( painterBounds, nullptr, nullptr, context );
149 mBackgroundSymbol->stopRender( context );
150 }
151
153 {
154 QgsCallout::QgsCalloutContext calloutContext;
155 renderCallout( context, painterBounds, 0, calloutContext, feedback );
156 }
157
158 renderInBounds( context, painterBounds, feedback );
159
160 if ( mDrawFrame && mFrameSymbol )
161 {
162 mFrameSymbol->startRender( context );
163 mFrameSymbol->renderPolygon( painterBounds, nullptr, nullptr, context );
164 mFrameSymbol->stopRender( context );
165 }
166}
167
168QList<QgsAnnotationItemNode> QgsAnnotationRectItem::nodesV2( const QgsAnnotationItemEditContext &context ) const
169{
170 QList<QgsAnnotationItemNode> res;
171 switch ( mPlacementMode )
172 {
174 {
175 res =
176 {
177 QgsAnnotationItemNode( QgsVertexId( 0, 0, 0 ), QgsPointXY( mBounds.xMinimum(), mBounds.yMinimum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeBDiagCursor ),
178 QgsAnnotationItemNode( QgsVertexId( 0, 0, 1 ), QgsPointXY( mBounds.xMaximum(), mBounds.yMinimum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeFDiagCursor ),
179 QgsAnnotationItemNode( QgsVertexId( 0, 0, 2 ), QgsPointXY( mBounds.xMaximum(), mBounds.yMaximum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeBDiagCursor ),
180 QgsAnnotationItemNode( QgsVertexId( 0, 0, 3 ), QgsPointXY( mBounds.xMinimum(), mBounds.yMaximum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeFDiagCursor ),
181 };
182
183 QgsPointXY calloutNodePoint;
184 if ( !calloutAnchor().isEmpty() )
185 {
186 calloutNodePoint = calloutAnchor().asPoint();
187 }
188 else
189 {
190 calloutNodePoint = mBounds.center();
191 }
192 res.append( QgsAnnotationItemNode( QgsVertexId( 1, 0, 0 ), calloutNodePoint, Qgis::AnnotationItemNodeType::CalloutHandle ) );
193
194 return res;
195 }
196
198 {
199 res =
200 {
202 };
203
204 QgsPointXY calloutNodePoint;
205 if ( !calloutAnchor().isEmpty() )
206 {
207 calloutNodePoint = calloutAnchor().asPoint();
208 }
209 else
210 {
211 calloutNodePoint = QgsPointXY( context.currentItemBounds().xMinimum(), context.currentItemBounds().yMinimum() );
212 }
213 res.append( QgsAnnotationItemNode( QgsVertexId( 1, 0, 0 ), calloutNodePoint, Qgis::AnnotationItemNodeType::CalloutHandle ) );
214
215 return res;
216 }
217
219 {
220 return
221 {
224 };
225 }
226 }
228}
229
231{
232 switch ( operation->type() )
233 {
235 {
236 QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
237 if ( moveOperation->nodeId().part == 0 )
238 {
239 switch ( mPlacementMode )
240 {
242 {
243 switch ( moveOperation->nodeId().vertex )
244 {
245 case 0:
246 mBounds = QgsRectangle( moveOperation->after().x(),
247 moveOperation->after().y(),
248 mBounds.xMaximum(),
249 mBounds.yMaximum() );
250 break;
251 case 1:
252 mBounds = QgsRectangle( mBounds.xMinimum(),
253 moveOperation->after().y(),
254 moveOperation->after().x(),
255 mBounds.yMaximum() );
256 break;
257 case 2:
258 mBounds = QgsRectangle( mBounds.xMinimum(),
259 mBounds.yMinimum(),
260 moveOperation->after().x(),
261 moveOperation->after().y() );
262 break;
263 case 3:
264 mBounds = QgsRectangle( moveOperation->after().x(),
265 mBounds.yMinimum(),
266 mBounds.xMaximum(),
267 moveOperation->after().y() );
268 break;
269 default:
270 break;
271 }
273 }
274
276 {
277 mBounds = QgsRectangle::fromCenterAndSize( moveOperation->after(),
278 mBounds.width(),
279 mBounds.height() );
281 }
282
284 {
285 const double deltaX = moveOperation->translationXPixels() / context.renderContext().outputSize().width();
286 const double deltaY = moveOperation->translationYPixels() / context.renderContext().outputSize().height();
287 mBounds = QgsRectangle::fromCenterAndSize( QgsPointXY( mBounds.center().x() + deltaX, mBounds.center().y() + deltaY ),
288 mBounds.width(), mBounds.height() );
290 }
291 }
292 }
293 else if ( moveOperation->nodeId().part == 1 )
294 {
295 setCalloutAnchor( QgsGeometry::fromPoint( moveOperation->after() ) );
296 if ( !callout() )
297 {
298 setCallout( QgsApplication::calloutRegistry()->defaultCallout() );
299 }
301 }
302 break;
303 }
304
306 {
307 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
308 switch ( mPlacementMode )
309 {
310
312 mBounds = QgsRectangle( mBounds.xMinimum() + moveOperation->translationX(),
313 mBounds.yMinimum() + moveOperation->translationY(),
314 mBounds.xMaximum() + moveOperation->translationX(),
315 mBounds.yMaximum() + moveOperation->translationY() );
316 break;
317
319 {
320 if ( callout() && !calloutAnchor().isEmpty() )
321 {
322 const double xOffset = context.renderContext().convertFromPainterUnits( moveOperation->translationXPixels(), offsetFromCalloutUnit() );
323 const double yOffset = context.renderContext().convertFromPainterUnits( moveOperation->translationYPixels(), offsetFromCalloutUnit() );
324 setOffsetFromCallout( QSizeF( offsetFromCallout().width() + xOffset, offsetFromCallout().height() + yOffset ) );
325 }
326 else
327 {
328 mBounds = QgsRectangle( mBounds.xMinimum() + moveOperation->translationX(),
329 mBounds.yMinimum() + moveOperation->translationY(),
330 mBounds.xMaximum() + moveOperation->translationX(),
331 mBounds.yMaximum() + moveOperation->translationY() );
332 }
333 break;
334 }
335
337 {
338 const double deltaX = moveOperation->translationXPixels() / context.renderContext().outputSize().width();
339 const double deltaY = moveOperation->translationYPixels() / context.renderContext().outputSize().height();
340 mBounds = QgsRectangle::fromCenterAndSize( QgsPointXY( mBounds.center().x() + deltaX, mBounds.center().y() + deltaY ),
341 mBounds.width(), mBounds.height() );
342 break;
343 }
344 }
346 }
347
350 break;
351 }
353}
354
356{
357 switch ( operation->type() )
358 {
360 {
361 QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
362 if ( moveOperation->nodeId().part == 0 )
363 {
364 switch ( mPlacementMode )
365 {
367 {
368 QgsRectangle modifiedBounds = mBounds;
369 switch ( moveOperation->nodeId().vertex )
370 {
371 case 0:
372 modifiedBounds.setXMinimum( moveOperation->after().x() );
373 modifiedBounds.setYMinimum( moveOperation->after().y() );
374 break;
375 case 1:
376 modifiedBounds.setXMaximum( moveOperation->after().x() );
377 modifiedBounds.setYMinimum( moveOperation->after().y() );
378 break;
379 case 2:
380 modifiedBounds.setXMaximum( moveOperation->after().x() );
381 modifiedBounds.setYMaximum( moveOperation->after().y() );
382 break;
383 case 3:
384 modifiedBounds.setXMinimum( moveOperation->after().x() );
385 modifiedBounds.setYMaximum( moveOperation->after().y() );
386 break;
387 default:
388 break;
389 }
391 }
393 {
394 const QgsRectangle currentBounds = context.currentItemBounds();
395 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( moveOperation->after(), currentBounds.width(), currentBounds.height() );
397 }
399 {
400 const QgsRectangle currentBounds = context.currentItemBounds();
401 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( currentBounds.center() + ( moveOperation->after() - moveOperation->before() ),
402 currentBounds.width(), currentBounds.height() );
404 }
405 }
406 }
407 else
408 {
409 QgsAnnotationItemEditOperationMoveNode *moveOperation = dynamic_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
410 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
411 }
412 break;
413 }
414
416 {
417 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
418 switch ( mPlacementMode )
419 {
421 {
422 const QgsRectangle modifiedBounds( mBounds.xMinimum() + moveOperation->translationX(),
423 mBounds.yMinimum() + moveOperation->translationY(),
424 mBounds.xMaximum() + moveOperation->translationX(),
425 mBounds.yMaximum() + moveOperation->translationY() );
427 }
428
430 {
431 if ( callout() && !calloutAnchor().isEmpty() )
432 {
433 QgsGeometry anchor = calloutAnchor();
434
435 const double calloutOffsetWidthPixels = context.renderContext().convertToPainterUnits( offsetFromCallout().width(), offsetFromCalloutUnit() )
436 + moveOperation->translationXPixels();
437 const double calloutOffsetHeightPixels = context.renderContext().convertToPainterUnits( offsetFromCallout().height(), offsetFromCalloutUnit() )
438 + moveOperation->translationYPixels();
439
440 QPointF anchorPoint = anchor.asQPointF();
441 if ( context.renderContext().coordinateTransform().isValid() )
442 {
443 double x = anchorPoint.x();
444 double y = anchorPoint.y();
445 double z = 0.0;
447 anchorPoint = QPointF( x, y );
448 }
449
450 context.renderContext().mapToPixel().transformInPlace( anchorPoint.rx(), anchorPoint.ry() );
451
452 const double textOriginXPixels = anchorPoint.x() + calloutOffsetWidthPixels;
453 const double textOriginYPixels = anchorPoint.y() + calloutOffsetHeightPixels;
454
455 const double widthPixels = context.renderContext().convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
456 const double heightPixels = context.renderContext().convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
457
458 QgsLineString ls( QVector<QgsPointXY> { QgsPointXY( textOriginXPixels, textOriginYPixels ),
459 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels ),
460 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels + heightPixels ),
461 QgsPointXY( textOriginXPixels, textOriginYPixels + heightPixels ),
462 QgsPointXY( textOriginXPixels, textOriginYPixels )
463 } );
464
465 QgsGeometry g( new QgsPolygon( ls.clone() ) );
466 g.transform( context.renderContext().mapToPixel().transform().inverted() );
469 }
470 else
471 {
472 const QgsRectangle currentBounds = context.currentItemBounds();
473 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( mBounds.center() + QgsVector( moveOperation->translationX(), moveOperation->translationY() ),
474 currentBounds.width(), currentBounds.height() );
476 }
477 }
478
480 {
481 const QgsRectangle currentBounds = context.currentItemBounds();
482 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( currentBounds.center() + QgsVector( moveOperation->translationX(), moveOperation->translationY() ),
483 currentBounds.width(), currentBounds.height() );
485 }
486 }
487 break;
488 }
489
492 break;
493 }
494 return nullptr;
495}
496
498{
500 switch ( mPlacementMode )
501 {
503 {
504 bounds = mBounds;
505 if ( callout() && !calloutAnchor().isEmpty() )
506 {
507 QgsGeometry anchor = calloutAnchor();
509 }
510 break;
511 }
512
514 if ( callout() && !calloutAnchor().isEmpty() )
515 {
517 }
518 else
519 {
520 bounds = QgsRectangle( mBounds.center(), mBounds.center() );
521 }
522 break;
523
525 bounds = mBounds;
526 break;
527 }
528
529 return bounds;
530}
531
533{
534 switch ( mPlacementMode )
535 {
538
540 {
541 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
542 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
543
544 QRectF boundsInPixels;
545 if ( callout() && !calloutAnchor().isEmpty() )
546 {
547 QgsGeometry anchor = calloutAnchor();
548
549 const double calloutOffsetWidthPixels = context.convertToPainterUnits( offsetFromCallout().width(), offsetFromCalloutUnit() );
550 const double calloutOffsetHeightPixels = context.convertToPainterUnits( offsetFromCallout().height(), offsetFromCalloutUnit() );
551
552 QPointF anchorPoint = anchor.asQPointF();
553 if ( context.coordinateTransform().isValid() )
554 {
555 double x = anchorPoint.x();
556 double y = anchorPoint.y();
557 double z = 0.0;
558 context.coordinateTransform().transformInPlace( x, y, z );
559 anchorPoint = QPointF( x, y );
560 }
561
562 context.mapToPixel().transformInPlace( anchorPoint.rx(), anchorPoint.ry() );
563
564 QgsRectangle textRect( anchorPoint.x() + calloutOffsetWidthPixels,
565 anchorPoint.y() + calloutOffsetHeightPixels,
566 anchorPoint.x() + calloutOffsetWidthPixels + widthPixels,
567 anchorPoint.y() + calloutOffsetHeightPixels + heightPixels );
568 QgsRectangle anchorRect( anchorPoint.x(), anchorPoint.y(), anchorPoint.x(), anchorPoint.y() );
569 anchorRect.combineExtentWith( textRect );
570
571 boundsInPixels = anchorRect.toRectF();
572 }
573 else
574 {
575 QPointF center = mBounds.center().toQPointF();
576 if ( context.coordinateTransform().isValid() )
577 {
578 double x = center.x();
579 double y = center.y();
580 double z = 0.0;
581 context.coordinateTransform().transformInPlace( x, y, z );
582 center = QPointF( x, y );
583 }
584
585 context.mapToPixel().transformInPlace( center.rx(), center.ry() );
586 boundsInPixels = QRectF( center.x() - widthPixels * 0.5,
587 center.y() - heightPixels * 0.5,
588 widthPixels, heightPixels );
589 }
590 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
591 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
592 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
593 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
594
595 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
597 return textRect;
598 }
599
601 {
602 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
603 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
604
605 QRectF boundsInPixels;
606
607 const double centerMapX = context.mapExtent().xMinimum() + mBounds.center().x() * context.mapExtent().width();
608 const double centerMapY = context.mapExtent().yMaximum() - mBounds.center().y() * context.mapExtent().height();
609 QPointF center( centerMapX, centerMapY );
610 if ( context.coordinateTransform().isValid() )
611 {
612 double x = centerMapX;
613 double y = centerMapY;
614 double z = 0.0;
615 context.coordinateTransform().transformInPlace( x, y, z );
616 center = QPointF( x, y );
617 }
618
619 context.mapToPixel().transformInPlace( center.rx(), center.ry() );
620 boundsInPixels = QRectF( center.x() - widthPixels * 0.5,
621 center.y() - heightPixels * 0.5,
622 widthPixels, heightPixels );
623
624 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
625 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
626 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
627 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
628
629 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
631 return textRect;
632 }
633 }
635}
636
638{
639 mBounds = bounds;
640}
641
643{
644 return mBackgroundSymbol.get();
645}
646
648{
649 mBackgroundSymbol.reset( symbol );
650}
651
653{
654 return mFrameSymbol.get();
655}
656
658{
659 mFrameSymbol.reset( symbol );
660}
661
663{
664 if ( const QgsAnnotationRectItem *otherRect = dynamic_cast< const QgsAnnotationRectItem * >( other ) )
665 {
666 setPlacementMode( otherRect->mPlacementMode );
667 setFixedSize( otherRect->mFixedSize );
668 setFixedSizeUnit( otherRect->mFixedSizeUnit );
669
670 setBackgroundEnabled( otherRect->mDrawBackground );
671 if ( otherRect->mBackgroundSymbol )
672 setBackgroundSymbol( otherRect->mBackgroundSymbol->clone() );
673
674 setFrameEnabled( otherRect->mDrawFrame );
675 if ( otherRect->mFrameSymbol )
676 setFrameSymbol( otherRect->mFrameSymbol->clone() );
677 }
678
680}
681
682bool QgsAnnotationRectItem::writeCommonProperties( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
683{
684 element.setAttribute( QStringLiteral( "xMin" ), qgsDoubleToString( mBounds.xMinimum() ) );
685 element.setAttribute( QStringLiteral( "xMax" ), qgsDoubleToString( mBounds.xMaximum() ) );
686 element.setAttribute( QStringLiteral( "yMin" ), qgsDoubleToString( mBounds.yMinimum() ) );
687 element.setAttribute( QStringLiteral( "yMax" ), qgsDoubleToString( mBounds.yMaximum() ) );
688 element.setAttribute( QStringLiteral( "sizeMode" ), qgsEnumValueToKey( mPlacementMode ) );
689 element.setAttribute( QStringLiteral( "fixedWidth" ), qgsDoubleToString( mFixedSize.width() ) );
690 element.setAttribute( QStringLiteral( "fixedHeight" ), qgsDoubleToString( mFixedSize.height() ) );
691 element.setAttribute( QStringLiteral( "fixedSizeUnit" ), QgsUnitTypes::encodeUnit( mFixedSizeUnit ) );
692
693 element.setAttribute( QStringLiteral( "backgroundEnabled" ), mDrawBackground ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
694 if ( mBackgroundSymbol )
695 {
696 QDomElement backgroundElement = document.createElement( QStringLiteral( "backgroundSymbol" ) );
697 backgroundElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "backgroundSymbol" ), mBackgroundSymbol.get(), document, context ) );
698 element.appendChild( backgroundElement );
699 }
700
701 element.setAttribute( QStringLiteral( "frameEnabled" ), mDrawFrame ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
702 if ( mFrameSymbol )
703 {
704 QDomElement frameElement = document.createElement( QStringLiteral( "frameSymbol" ) );
705 frameElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "frameSymbol" ), mFrameSymbol.get(), document, context ) );
706 element.appendChild( frameElement );
707 }
708
709 return QgsAnnotationItem::writeCommonProperties( element, document, context );
710}
711
712bool QgsAnnotationRectItem::readCommonProperties( const QDomElement &element, const QgsReadWriteContext &context )
713{
714 mBounds.setXMinimum( element.attribute( QStringLiteral( "xMin" ) ).toDouble() );
715 mBounds.setXMaximum( element.attribute( QStringLiteral( "xMax" ) ).toDouble() );
716 mBounds.setYMinimum( element.attribute( QStringLiteral( "yMin" ) ).toDouble() );
717 mBounds.setYMaximum( element.attribute( QStringLiteral( "yMax" ) ).toDouble() );
718
719 mPlacementMode = qgsEnumKeyToValue( element.attribute( QStringLiteral( "sizeMode" ) ), Qgis::AnnotationPlacementMode::SpatialBounds );
720
721 mFixedSize = QSizeF(
722 element.attribute( QStringLiteral( "fixedWidth" ) ).toDouble(),
723 element.attribute( QStringLiteral( "fixedHeight" ) ).toDouble()
724 );
725 mFixedSizeUnit = QgsUnitTypes::decodeRenderUnit( element.attribute( QStringLiteral( "fixedSizeUnit" ) ) );
726
727 mDrawBackground = element.attribute( QStringLiteral( "backgroundEnabled" ), QStringLiteral( "0" ) ).toInt();
728 const QDomElement backgroundSymbolElem = element.firstChildElement( QStringLiteral( "backgroundSymbol" ) ).firstChildElement();
729 if ( !backgroundSymbolElem.isNull() )
730 {
731 setBackgroundSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( backgroundSymbolElem, context ) );
732 }
733
734 mDrawFrame = element.attribute( QStringLiteral( "frameEnabled" ), QStringLiteral( "0" ) ).toInt();
735 const QDomElement frameSymbolElem = element.firstChildElement( QStringLiteral( "frameSymbol" ) ).firstChildElement();
736 if ( !frameSymbolElem.isNull() )
737 {
738 setFrameSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( frameSymbolElem, context ) );
739 }
740
741 return QgsAnnotationItem::readCommonProperties( element, context );
742}
743
745{
746 return mFixedSize;
747}
748
749void QgsAnnotationRectItem::setFixedSize( const QSizeF &size )
750{
751 mFixedSize = size;
752}
753
755{
756 return mFixedSizeUnit;
757}
758
760{
761 mFixedSizeUnit = unit;
762}
763
765{
766 return mPlacementMode;
767}
768
770{
771 mPlacementMode = mode;
772}
@ VertexHandle
Node is a handle for manipulating vertices.
@ CalloutHandle
Node is a handle for manipulating callouts.
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
@ SupportsCallouts
Item supports callouts.
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2351
@ Invalid
Operation has invalid parameters for the item, no change occurred.
@ Success
Item was modified successfully.
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2302
RenderUnit
Rendering size units.
Definition qgis.h:4756
AnnotationPlacementMode
Annotation item placement modes.
Definition qgis.h:2312
@ SpatialBounds
Item is rendered inside fixed spatial bounds, and size will depend on map scale.
@ FixedSize
Item is rendered at a fixed size, regardless of map scale. Item's location is georeferenced to a spat...
@ RelativeToMapFrame
Items size and placement is relative to the map's frame, and the item will always be rendered in the ...
@ Reverse
Reverse/inverse transform (from destination to source)
Abstract base class for annotation item edit operations.
@ TranslateItem
Translate (move) an item.
virtual Type type() const =0
Returns the operation type.
Encapsulates the context for an annotation item edit operation.
QgsRectangle currentItemBounds() const
Returns the current rendered bounds of the item, in the annotation layer's CRS.
QgsRenderContext renderContext() const
Returns the render context associated with the edit operation.
Annotation item edit operation consisting of moving a node.
double translationYPixels() const
Returns the y-axis translation, in pixels.
QgsPoint before() const
Returns the node position before the move occurred (in layer coordinates).
QgsPoint after() const
Returns the node position after the move occurred (in layer coordinates).
double translationXPixels() const
Returns the x-axis translation, in pixels.
QgsVertexId nodeId() const
Returns the associated node ID.
Encapsulates the transient results of an in-progress annotation edit operation.
Annotation item edit operation consisting of translating (moving) an item.
double translationY() const
Returns the y-axis translation, in layer units.
double translationX() const
Returns the x-axis translation, in layer units.
double translationYPixels() const
Returns the y-axis translation, in pixels.
double translationXPixels() const
Returns the x-axis translation, in pixels.
Contains information about a node used for editing an annotation item.
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
virtual bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes common properties from the base class into an XML element.
void setCallout(QgsCallout *callout)
Sets the item's callout renderer, responsible for drawing item callouts.
void setOffsetFromCallout(const QSizeF &offset)
Sets the offset of the annotation item from the calloutAnchor().
QgsGeometry calloutAnchor() const
Returns the callout's anchor geometry.
Qgis::RenderUnit offsetFromCalloutUnit() const
Returns the units for the offsetFromCallout().
virtual void copyCommonProperties(const QgsAnnotationItem *other)
Copies common properties from the base class from an other item.
void setCalloutAnchor(const QgsGeometry &anchor)
Sets the callout's anchor geometry.
QSizeF offsetFromCallout() const
Returns the (optional) offset of the annotation item from the calloutAnchor().
QgsCallout * callout() const
Returns the item's callout renderer, responsible for drawing item callouts.
virtual bool readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context)
Reads common properties from the base class from the given DOM element.
void renderCallout(QgsRenderContext &context, const QRectF &rect, double angle, QgsCallout::QgsCalloutContext &calloutContext, QgsFeedback *feedback)
Renders the item's callout.
Abstract base class for annotation items which render annotations in a rectangular shape.
virtual void renderInBounds(QgsRenderContext &context, const QRectF &painterRect, QgsFeedback *feedback)=0
Renders the item to the specified render context.
Qgis::AnnotationItemFlags flags() const override
Returns item flags.
void copyCommonProperties(const QgsAnnotationItem *other) override
Copies common properties from the base class from an other item.
QgsRectangle bounds() const
Returns the bounds of the item.
void render(QgsRenderContext &context, QgsFeedback *feedback) override
Renders the item to the specified render context.
QgsAnnotationItemEditOperationTransientResults * transientEditResultsV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Retrieves the results of a transient (in progress) edit operation on the item.
Qgis::AnnotationItemEditOperationResult applyEditV2(QgsAbstractAnnotationItemEditOperation *operation, const QgsAnnotationItemEditContext &context) override
Applies an edit operation to the item.
~QgsAnnotationRectItem() override
bool readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context) override
Reads common properties from the base class from the given DOM element.
Qgis::RenderUnit fixedSizeUnit() const
Returns the units to use for fixed item sizes, when the placementMode() is Qgis::AnnotationPlacementM...
QSizeF fixedSize() const
Returns the fixed size to use for the item, when the placementMode() is Qgis::AnnotationPlacementMode...
void setFixedSizeUnit(Qgis::RenderUnit unit)
Sets the unit to use for fixed item sizes, when the placementMode() is Qgis::AnnotationPlacementMode:...
QgsAnnotationRectItem(const QgsRectangle &bounds)
Constructor for QgsAnnotationRectItem, rendering the annotation within the specified bounds geometry.
void setBackgroundSymbol(QgsFillSymbol *symbol)
Sets the symbol used to render the item's background.
void setFrameSymbol(QgsFillSymbol *symbol)
Sets the symbol used to render the item's frame.
void setBounds(const QgsRectangle &bounds)
Sets the bounds of the item.
void setPlacementMode(Qgis::AnnotationPlacementMode mode)
Sets the placement mode for the item.
bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes common properties from the base class into an XML element.
const QgsFillSymbol * frameSymbol() const
Returns the symbol used to render the item's frame.
void setBackgroundEnabled(bool enabled)
Sets whether the item's background should be rendered.
QList< QgsAnnotationItemNode > nodesV2(const QgsAnnotationItemEditContext &context) const override
Returns the nodes for the item, used for editing the item.
void setFrameEnabled(bool enabled)
Sets whether the item's frame should be rendered.
QgsRectangle boundingBox() const override
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
void setFixedSize(const QSizeF &size)
Sets the fixed size to use for the item, when the placementMode() is Qgis::AnnotationPlacementMode::F...
Qgis::AnnotationPlacementMode placementMode() const
Returns the placement mode for the item.
const QgsFillSymbol * backgroundSymbol() const
Returns the symbol used to render the item's background.
static QgsCalloutRegistry * calloutRegistry()
Returns the application's callout registry, used for managing callout types.
Contains additional contextual information about the context in which a callout is being rendered.
Definition qgscallout.h:249
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Custom exception class for Coordinate Reference System related exceptions.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
QPointF asQPointF() const
Returns contents of the geometry as a QPointF if wkbType is WKBPoint, otherwise returns a null QPoint...
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static QgsGeometry fromPoint(const QgsPoint &point)
Creates a new geometry from a QgsPoint object.
Line string geometry type, with support for z-dimension and m-values.
QRectF transformBounds(const QRectF &bounds) const
Transforms a bounding box from map coordinates to device coordinates.
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
void transformInPlace(double &x, double &y) const
Transforms map coordinates to device coordinates.
A class to represent a 2D point.
Definition qgspointxy.h:60
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:165
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition qgspoint.cpp:104
double x
Definition qgspoint.h:52
double y
Definition qgspoint.h:53
Polygon geometry type.
Definition qgspolygon.h:33
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
void setYMinimum(double y)
Set the minimum y value.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
void setXMinimum(double x)
Set the minimum x value.
double width() const
Returns the width of the rectangle.
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
double xMaximum() const
Returns the x maximum value (right side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
void setYMaximum(double y)
Set the maximum y value.
QgsPointXY center() const
Returns the center point of the rectangle.
void setXMaximum(double x)
Set the maximum x value.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
static QgsRectangle fromCenterAndSize(const QgsPointXY &center, double width, double height)
Creates a new rectangle, given the specified center point and width and height.
double height() const
Returns the height of the rectangle.
Contains information about the context of a rendering operation.
double convertFromPainterUnits(double size, Qgis::RenderUnit unit) const
Converts a size from painter units (pixels) to the specified render unit.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QSize outputSize() const
Returns the size of the resulting rendered image, in pixels.
QgsRectangle mapExtent() const
Returns the original extent of the map being rendered.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Renders polygons using a single fill and stroke color.
A simple line symbol layer, which renders lines using a line in a variety of styles (e....
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
A class to represent a vector.
Definition qgsvector.h:30
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:6028
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:5735
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6009
#define BUILTIN_UNREACHABLE
Definition qgis.h:6472
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition qgssymbol.h:30
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30
int vertex
Vertex number.
Definition qgsvertexid.h:94
int part
Part number.
Definition qgsvertexid.h:88