QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
22#include "qgsapplication.h"
23#include "qgscalloutsregistry.h"
24#include "qgsfillsymbol.h"
25#include "qgsfillsymbollayer.h"
26#include "qgsgeometry.h"
27#include "qgslinestring.h"
28#include "qgslinesymbollayer.h"
29#include "qgspainting.h"
30#include "qgspolygon.h"
31#include "qgsrendercontext.h"
32#include "qgssymbollayerutils.h"
33#include "qgsunittypes.h"
34
37 , mBounds( bounds )
38{
39 mBackgroundSymbol = std::make_unique< QgsFillSymbol >( QgsSymbolLayerList
40 {
41 new QgsSimpleFillSymbolLayer( QColor( 255, 255, 255 ), Qt::BrushStyle::SolidPattern, QColor( 0, 0, 0 ), Qt::PenStyle::NoPen )
42 } );
43 QgsSimpleLineSymbolLayer *borderSymbol = new QgsSimpleLineSymbolLayer( QColor( 0, 0, 0 ) );
44 borderSymbol->setPenJoinStyle( Qt::MiterJoin );
45 mFrameSymbol = std::make_unique< QgsFillSymbol >( QgsSymbolLayerList{ borderSymbol } );
46}
47
49
64
66{
67 QgsRectangle bounds = mBounds;
69 {
70 try
71 {
72 bounds = context.coordinateTransform().transformBoundingBox( mBounds );
73 }
74 catch ( QgsCsException & )
75 {
76 return;
77 }
78 }
79
80 QRectF painterBounds;
81
82 switch ( mPlacementMode )
83 {
85 painterBounds = context.mapToPixel().transformBounds( bounds.toRectF() );
86 break;
87
89 {
90 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
91 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
92
93 if ( callout() && !calloutAnchor().isEmpty() )
94 {
95 QgsGeometry anchor = calloutAnchor();
96
97 const double calloutOffsetWidthPixels = context.convertToPainterUnits( offsetFromCallout().width(), offsetFromCalloutUnit() );
98 const double calloutOffsetHeightPixels = context.convertToPainterUnits( offsetFromCallout().height(), offsetFromCalloutUnit() );
99
100 QPointF anchorPoint = anchor.asQPointF();
101 if ( context.coordinateTransform().isValid() )
102 {
103 double x = anchorPoint.x();
104 double y = anchorPoint.y();
105 double z = 0.0;
106 context.coordinateTransform().transformInPlace( x, y, z );
107 anchorPoint = QPointF( x, y );
108 }
109
110 context.mapToPixel().transformInPlace( anchorPoint.rx(), anchorPoint.ry() );
111
112 painterBounds = QRectF( anchorPoint.x() + calloutOffsetWidthPixels,
113 anchorPoint.y() + calloutOffsetHeightPixels, widthPixels, heightPixels );
114 }
115 else
116 {
117 QPointF center = bounds.center().toQPointF();
118
119 context.mapToPixel().transformInPlace( center.rx(), center.ry() );
120 painterBounds = QRectF( center.x() - widthPixels * 0.5,
121 center.y() - heightPixels * 0.5,
122 widthPixels, heightPixels );
123 }
124 break;
125 }
126
128 {
129 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
130 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
131
132 QPointF center = bounds.center().toQPointF();
133 center.rx() *= context.outputSize().width();
134 center.ry() *= context.outputSize().height();
135
136 painterBounds = QRectF( center.x() - widthPixels * 0.5,
137 center.y() - heightPixels * 0.5,
138 widthPixels, heightPixels );
139 break;
140 }
141 }
142
143 if ( painterBounds.width() < 1 || painterBounds.height() < 1 )
144 return;
145
146 if ( mDrawBackground && mBackgroundSymbol )
147 {
148 mBackgroundSymbol->startRender( context );
149 mBackgroundSymbol->renderPolygon( painterBounds, nullptr, nullptr, context );
150 mBackgroundSymbol->stopRender( context );
151 }
152
154 {
155 QgsCallout::QgsCalloutContext calloutContext;
156 renderCallout( context, painterBounds, 0, calloutContext, feedback );
157 }
158
159 renderInBounds( context, painterBounds, feedback );
160
161 if ( mDrawFrame && mFrameSymbol )
162 {
163 mFrameSymbol->startRender( context );
164 mFrameSymbol->renderPolygon( painterBounds, nullptr, nullptr, context );
165 mFrameSymbol->stopRender( context );
166 }
167}
168
169QList<QgsAnnotationItemNode> QgsAnnotationRectItem::nodesV2( const QgsAnnotationItemEditContext &context ) const
170{
171 QList<QgsAnnotationItemNode> res;
172 switch ( mPlacementMode )
173 {
175 {
176 res =
177 {
178 QgsAnnotationItemNode( QgsVertexId( 0, 0, 0 ), QgsPointXY( mBounds.xMinimum(), mBounds.yMinimum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeBDiagCursor ),
179 QgsAnnotationItemNode( QgsVertexId( 0, 0, 1 ), QgsPointXY( mBounds.xMaximum(), mBounds.yMinimum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeFDiagCursor ),
180 QgsAnnotationItemNode( QgsVertexId( 0, 0, 2 ), QgsPointXY( mBounds.xMaximum(), mBounds.yMaximum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeBDiagCursor ),
181 QgsAnnotationItemNode( QgsVertexId( 0, 0, 3 ), QgsPointXY( mBounds.xMinimum(), mBounds.yMaximum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeFDiagCursor ),
182 };
183
184 QgsPointXY calloutNodePoint;
185 if ( !calloutAnchor().isEmpty() )
186 {
187 calloutNodePoint = calloutAnchor().asPoint();
188 }
189 else
190 {
191 calloutNodePoint = mBounds.center();
192 }
193 res.append( QgsAnnotationItemNode( QgsVertexId( 1, 0, 0 ), calloutNodePoint, Qgis::AnnotationItemNodeType::CalloutHandle ) );
194
195 return res;
196 }
197
199 {
200 res =
201 {
203 };
204
205 QgsPointXY calloutNodePoint;
206 if ( !calloutAnchor().isEmpty() )
207 {
208 calloutNodePoint = calloutAnchor().asPoint();
209 }
210 else
211 {
212 calloutNodePoint = QgsPointXY( context.currentItemBounds().xMinimum(), context.currentItemBounds().yMinimum() );
213 }
214 res.append( QgsAnnotationItemNode( QgsVertexId( 1, 0, 0 ), calloutNodePoint, Qgis::AnnotationItemNodeType::CalloutHandle ) );
215
216 return res;
217 }
218
220 {
221 return
222 {
225 };
226 }
227 }
229}
230
232{
233 switch ( operation->type() )
234 {
236 {
237 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
238 if ( moveOperation->nodeId().part == 0 )
239 {
240 switch ( mPlacementMode )
241 {
243 {
244 switch ( moveOperation->nodeId().vertex )
245 {
246 case 0:
247 mBounds = QgsRectangle( moveOperation->after().x(),
248 moveOperation->after().y(),
249 mBounds.xMaximum(),
250 mBounds.yMaximum() );
251 break;
252 case 1:
253 mBounds = QgsRectangle( mBounds.xMinimum(),
254 moveOperation->after().y(),
255 moveOperation->after().x(),
256 mBounds.yMaximum() );
257 break;
258 case 2:
259 mBounds = QgsRectangle( mBounds.xMinimum(),
260 mBounds.yMinimum(),
261 moveOperation->after().x(),
262 moveOperation->after().y() );
263 break;
264 case 3:
265 mBounds = QgsRectangle( moveOperation->after().x(),
266 mBounds.yMinimum(),
267 mBounds.xMaximum(),
268 moveOperation->after().y() );
269 break;
270 default:
271 break;
272 }
274 }
275
277 {
278 mBounds = QgsRectangle::fromCenterAndSize( moveOperation->after(),
279 mBounds.width(),
280 mBounds.height() );
282 }
283
285 {
286 const double deltaX = moveOperation->translationXPixels() / context.renderContext().outputSize().width();
287 const double deltaY = moveOperation->translationYPixels() / context.renderContext().outputSize().height();
288 mBounds = QgsRectangle::fromCenterAndSize( QgsPointXY( mBounds.center().x() + deltaX, mBounds.center().y() + deltaY ),
289 mBounds.width(), mBounds.height() );
291 }
292 }
293 }
294 else if ( moveOperation->nodeId().part == 1 )
295 {
296 setCalloutAnchor( QgsGeometry::fromPoint( moveOperation->after() ) );
297 if ( !callout() )
298 {
299 setCallout( QgsApplication::calloutRegistry()->defaultCallout() );
300 }
302 }
303 break;
304 }
305
307 {
308 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
309 switch ( mPlacementMode )
310 {
311
313 mBounds = QgsRectangle( mBounds.xMinimum() + moveOperation->translationX(),
314 mBounds.yMinimum() + moveOperation->translationY(),
315 mBounds.xMaximum() + moveOperation->translationX(),
316 mBounds.yMaximum() + moveOperation->translationY() );
317 break;
318
320 {
321 if ( callout() && !calloutAnchor().isEmpty() )
322 {
323 const double xOffset = context.renderContext().convertFromPainterUnits( moveOperation->translationXPixels(), offsetFromCalloutUnit() );
324 const double yOffset = context.renderContext().convertFromPainterUnits( moveOperation->translationYPixels(), offsetFromCalloutUnit() );
325 setOffsetFromCallout( QSizeF( offsetFromCallout().width() + xOffset, offsetFromCallout().height() + yOffset ) );
326 }
327 else
328 {
329 mBounds = QgsRectangle( mBounds.xMinimum() + moveOperation->translationX(),
330 mBounds.yMinimum() + moveOperation->translationY(),
331 mBounds.xMaximum() + moveOperation->translationX(),
332 mBounds.yMaximum() + moveOperation->translationY() );
333 }
334 break;
335 }
336
338 {
339 const double deltaX = moveOperation->translationXPixels() / context.renderContext().outputSize().width();
340 const double deltaY = moveOperation->translationYPixels() / context.renderContext().outputSize().height();
341 mBounds = QgsRectangle::fromCenterAndSize( QgsPointXY( mBounds.center().x() + deltaX, mBounds.center().y() + deltaY ),
342 mBounds.width(), mBounds.height() );
343 break;
344 }
345 }
347 }
348
351 break;
352 }
354}
355
357{
358 switch ( operation->type() )
359 {
361 {
362 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
363 if ( moveOperation->nodeId().part == 0 )
364 {
365 switch ( mPlacementMode )
366 {
368 {
369 QgsRectangle modifiedBounds = mBounds;
370 switch ( moveOperation->nodeId().vertex )
371 {
372 case 0:
373 modifiedBounds.setXMinimum( moveOperation->after().x() );
374 modifiedBounds.setYMinimum( moveOperation->after().y() );
375 break;
376 case 1:
377 modifiedBounds.setXMaximum( moveOperation->after().x() );
378 modifiedBounds.setYMinimum( moveOperation->after().y() );
379 break;
380 case 2:
381 modifiedBounds.setXMaximum( moveOperation->after().x() );
382 modifiedBounds.setYMaximum( moveOperation->after().y() );
383 break;
384 case 3:
385 modifiedBounds.setXMinimum( moveOperation->after().x() );
386 modifiedBounds.setYMaximum( moveOperation->after().y() );
387 break;
388 default:
389 break;
390 }
392 }
394 {
395 const QgsRectangle currentBounds = context.currentItemBounds();
396 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( moveOperation->after(), currentBounds.width(), currentBounds.height() );
398 }
400 {
401 const QgsRectangle currentBounds = context.currentItemBounds();
402 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( currentBounds.center() + ( moveOperation->after() - moveOperation->before() ),
403 currentBounds.width(), currentBounds.height() );
405 }
406 }
407 }
408 else
409 {
410 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
411 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
412 }
413 break;
414 }
415
417 {
418 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
419 switch ( mPlacementMode )
420 {
422 {
423 const QgsRectangle modifiedBounds( mBounds.xMinimum() + moveOperation->translationX(),
424 mBounds.yMinimum() + moveOperation->translationY(),
425 mBounds.xMaximum() + moveOperation->translationX(),
426 mBounds.yMaximum() + moveOperation->translationY() );
428 }
429
431 {
432 if ( callout() && !calloutAnchor().isEmpty() )
433 {
434 QgsGeometry anchor = calloutAnchor();
435
436 const double calloutOffsetWidthPixels = context.renderContext().convertToPainterUnits( offsetFromCallout().width(), offsetFromCalloutUnit() )
437 + moveOperation->translationXPixels();
438 const double calloutOffsetHeightPixels = context.renderContext().convertToPainterUnits( offsetFromCallout().height(), offsetFromCalloutUnit() )
439 + moveOperation->translationYPixels();
440
441 QPointF anchorPoint = anchor.asQPointF();
442 if ( context.renderContext().coordinateTransform().isValid() )
443 {
444 double x = anchorPoint.x();
445 double y = anchorPoint.y();
446 double z = 0.0;
448 anchorPoint = QPointF( x, y );
449 }
450
451 context.renderContext().mapToPixel().transformInPlace( anchorPoint.rx(), anchorPoint.ry() );
452
453 const double textOriginXPixels = anchorPoint.x() + calloutOffsetWidthPixels;
454 const double textOriginYPixels = anchorPoint.y() + calloutOffsetHeightPixels;
455
456 const double widthPixels = context.renderContext().convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
457 const double heightPixels = context.renderContext().convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
458
459 QgsLineString ls( QVector<QgsPointXY> { QgsPointXY( textOriginXPixels, textOriginYPixels ),
460 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels ),
461 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels + heightPixels ),
462 QgsPointXY( textOriginXPixels, textOriginYPixels + heightPixels ),
463 QgsPointXY( textOriginXPixels, textOriginYPixels )
464 } );
465
466 QgsGeometry g( new QgsPolygon( ls.clone() ) );
467 g.transform( context.renderContext().mapToPixel().transform().inverted() );
470 }
471 else
472 {
473 const QgsRectangle currentBounds = context.currentItemBounds();
474 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( mBounds.center() + QgsVector( moveOperation->translationX(), moveOperation->translationY() ),
475 currentBounds.width(), currentBounds.height() );
477 }
478 }
479
481 {
482 const QgsRectangle currentBounds = context.currentItemBounds();
483 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( currentBounds.center() + QgsVector( moveOperation->translationX(), moveOperation->translationY() ),
484 currentBounds.width(), currentBounds.height() );
486 }
487 }
488 break;
489 }
490
493 break;
494 }
495 return nullptr;
496}
497
499{
501 switch ( mPlacementMode )
502 {
504 {
505 bounds = mBounds;
506 if ( callout() && !calloutAnchor().isEmpty() )
507 {
508 QgsGeometry anchor = calloutAnchor();
509 bounds.combineExtentWith( anchor.boundingBox() );
510 }
511 break;
512 }
513
515 if ( callout() && !calloutAnchor().isEmpty() )
516 {
518 }
519 else
520 {
521 bounds = QgsRectangle( mBounds.center(), mBounds.center() );
522 }
523 break;
524
526 bounds = mBounds;
527 break;
528 }
529
530 return bounds;
531}
532
534{
535 switch ( mPlacementMode )
536 {
539
541 {
542 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
543 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
544
545 QRectF boundsInPixels;
546 if ( callout() && !calloutAnchor().isEmpty() )
547 {
548 QgsGeometry anchor = calloutAnchor();
549
550 const double calloutOffsetWidthPixels = context.convertToPainterUnits( offsetFromCallout().width(), offsetFromCalloutUnit() );
551 const double calloutOffsetHeightPixels = context.convertToPainterUnits( offsetFromCallout().height(), offsetFromCalloutUnit() );
552
553 QPointF anchorPoint = anchor.asQPointF();
554 if ( context.coordinateTransform().isValid() )
555 {
556 double x = anchorPoint.x();
557 double y = anchorPoint.y();
558 double z = 0.0;
559 context.coordinateTransform().transformInPlace( x, y, z );
560 anchorPoint = QPointF( x, y );
561 }
562
563 context.mapToPixel().transformInPlace( anchorPoint.rx(), anchorPoint.ry() );
564
565 QgsRectangle textRect( anchorPoint.x() + calloutOffsetWidthPixels,
566 anchorPoint.y() + calloutOffsetHeightPixels,
567 anchorPoint.x() + calloutOffsetWidthPixels + widthPixels,
568 anchorPoint.y() + calloutOffsetHeightPixels + heightPixels );
569 QgsRectangle anchorRect( anchorPoint.x(), anchorPoint.y(), anchorPoint.x(), anchorPoint.y() );
570 anchorRect.combineExtentWith( textRect );
571
572 boundsInPixels = anchorRect.toRectF();
573 }
574 else
575 {
576 QPointF center = mBounds.center().toQPointF();
577 if ( context.coordinateTransform().isValid() )
578 {
579 double x = center.x();
580 double y = center.y();
581 double z = 0.0;
582 context.coordinateTransform().transformInPlace( x, y, z );
583 center = QPointF( x, y );
584 }
585
586 context.mapToPixel().transformInPlace( center.rx(), center.ry() );
587 boundsInPixels = QRectF( center.x() - widthPixels * 0.5,
588 center.y() - heightPixels * 0.5,
589 widthPixels, heightPixels );
590 }
591 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
592 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
593 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
594 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
595
596 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
598 return textRect;
599 }
600
602 {
603 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
604 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
605
606 QRectF boundsInPixels;
607
608 const double centerMapX = context.mapExtent().xMinimum() + mBounds.center().x() * context.mapExtent().width();
609 const double centerMapY = context.mapExtent().yMaximum() - mBounds.center().y() * context.mapExtent().height();
610 QPointF center( centerMapX, centerMapY );
611 if ( context.coordinateTransform().isValid() )
612 {
613 double x = centerMapX;
614 double y = centerMapY;
615 double z = 0.0;
616 context.coordinateTransform().transformInPlace( x, y, z );
617 center = QPointF( x, y );
618 }
619
620 context.mapToPixel().transformInPlace( center.rx(), center.ry() );
621 boundsInPixels = QRectF( center.x() - widthPixels * 0.5,
622 center.y() - heightPixels * 0.5,
623 widthPixels, heightPixels );
624
625 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
626 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
627 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
628 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
629
630 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
632 return textRect;
633 }
634 }
636}
637
639{
640 mBounds = bounds;
641}
642
644{
645 return mBackgroundSymbol.get();
646}
647
649{
650 mBackgroundSymbol.reset( symbol );
651}
652
654{
655 return mFrameSymbol.get();
656}
657
659{
660 mFrameSymbol.reset( symbol );
661}
662
664{
665 if ( const QgsAnnotationRectItem *otherRect = dynamic_cast< const QgsAnnotationRectItem * >( other ) )
666 {
667 setPlacementMode( otherRect->mPlacementMode );
668 setFixedSize( otherRect->mFixedSize );
669 setFixedSizeUnit( otherRect->mFixedSizeUnit );
670
671 setBackgroundEnabled( otherRect->mDrawBackground );
672 if ( otherRect->mBackgroundSymbol )
673 setBackgroundSymbol( otherRect->mBackgroundSymbol->clone() );
674
675 setFrameEnabled( otherRect->mDrawFrame );
676 if ( otherRect->mFrameSymbol )
677 setFrameSymbol( otherRect->mFrameSymbol->clone() );
678 }
679
681}
682
683bool QgsAnnotationRectItem::writeCommonProperties( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
684{
685 element.setAttribute( QStringLiteral( "xMin" ), qgsDoubleToString( mBounds.xMinimum() ) );
686 element.setAttribute( QStringLiteral( "xMax" ), qgsDoubleToString( mBounds.xMaximum() ) );
687 element.setAttribute( QStringLiteral( "yMin" ), qgsDoubleToString( mBounds.yMinimum() ) );
688 element.setAttribute( QStringLiteral( "yMax" ), qgsDoubleToString( mBounds.yMaximum() ) );
689 element.setAttribute( QStringLiteral( "sizeMode" ), qgsEnumValueToKey( mPlacementMode ) );
690 element.setAttribute( QStringLiteral( "fixedWidth" ), qgsDoubleToString( mFixedSize.width() ) );
691 element.setAttribute( QStringLiteral( "fixedHeight" ), qgsDoubleToString( mFixedSize.height() ) );
692 element.setAttribute( QStringLiteral( "fixedSizeUnit" ), QgsUnitTypes::encodeUnit( mFixedSizeUnit ) );
693
694 element.setAttribute( QStringLiteral( "backgroundEnabled" ), mDrawBackground ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
695 if ( mBackgroundSymbol )
696 {
697 QDomElement backgroundElement = document.createElement( QStringLiteral( "backgroundSymbol" ) );
698 backgroundElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "backgroundSymbol" ), mBackgroundSymbol.get(), document, context ) );
699 element.appendChild( backgroundElement );
700 }
701
702 element.setAttribute( QStringLiteral( "frameEnabled" ), mDrawFrame ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
703 if ( mFrameSymbol )
704 {
705 QDomElement frameElement = document.createElement( QStringLiteral( "frameSymbol" ) );
706 frameElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "frameSymbol" ), mFrameSymbol.get(), document, context ) );
707 element.appendChild( frameElement );
708 }
709
710 return QgsAnnotationItem::writeCommonProperties( element, document, context );
711}
712
713bool QgsAnnotationRectItem::readCommonProperties( const QDomElement &element, const QgsReadWriteContext &context )
714{
715 mBounds.setXMinimum( element.attribute( QStringLiteral( "xMin" ) ).toDouble() );
716 mBounds.setXMaximum( element.attribute( QStringLiteral( "xMax" ) ).toDouble() );
717 mBounds.setYMinimum( element.attribute( QStringLiteral( "yMin" ) ).toDouble() );
718 mBounds.setYMaximum( element.attribute( QStringLiteral( "yMax" ) ).toDouble() );
719
720 mPlacementMode = qgsEnumKeyToValue( element.attribute( QStringLiteral( "sizeMode" ) ), Qgis::AnnotationPlacementMode::SpatialBounds );
721
722 mFixedSize = QSizeF(
723 element.attribute( QStringLiteral( "fixedWidth" ) ).toDouble(),
724 element.attribute( QStringLiteral( "fixedHeight" ) ).toDouble()
725 );
726 mFixedSizeUnit = QgsUnitTypes::decodeRenderUnit( element.attribute( QStringLiteral( "fixedSizeUnit" ) ) );
727
728 mDrawBackground = element.attribute( QStringLiteral( "backgroundEnabled" ), QStringLiteral( "0" ) ).toInt();
729 const QDomElement backgroundSymbolElem = element.firstChildElement( QStringLiteral( "backgroundSymbol" ) ).firstChildElement();
730 if ( !backgroundSymbolElem.isNull() )
731 {
732 setBackgroundSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( backgroundSymbolElem, context ).release() );
733 }
734
735 mDrawFrame = element.attribute( QStringLiteral( "frameEnabled" ), QStringLiteral( "0" ) ).toInt();
736 const QDomElement frameSymbolElem = element.firstChildElement( QStringLiteral( "frameSymbol" ) ).firstChildElement();
737 if ( !frameSymbolElem.isNull() )
738 {
739 setFrameSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( frameSymbolElem, context ).release() );
740 }
741
742 return QgsAnnotationItem::readCommonProperties( element, context );
743}
744
746{
747 return mFixedSize;
748}
749
750void QgsAnnotationRectItem::setFixedSize( const QSizeF &size )
751{
752 mFixedSize = size;
753}
754
756{
757 return mFixedSizeUnit;
758}
759
761{
762 mFixedSizeUnit = unit;
763}
764
766{
767 return mPlacementMode;
768}
769
771{
772 mPlacementMode = mode;
773}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2508
@ CalloutHandle
Node is a handle for manipulating callouts.
Definition qgis.h:2509
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
Definition qgis.h:2465
@ SupportsCallouts
Item supports callouts.
Definition qgis.h:2467
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2519
@ Invalid
Operation has invalid parameters for the item, no change occurred.
Definition qgis.h:2521
@ Success
Item was modified successfully.
Definition qgis.h:2520
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2470
RenderUnit
Rendering size units.
Definition qgis.h:5183
AnnotationPlacementMode
Annotation item placement modes.
Definition qgis.h:2480
@ SpatialBounds
Item is rendered inside fixed spatial bounds, and size will depend on map scale.
Definition qgis.h:2481
@ FixedSize
Item is rendered at a fixed size, regardless of map scale. Item's location is georeferenced to a spat...
Definition qgis.h:2482
@ RelativeToMapFrame
Items size and placement is relative to the map's frame, and the item will always be rendered in the ...
Definition qgis.h:2483
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2673
Abstract base class for annotation item edit operations.
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.
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.
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:250
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.
Represents a 2D point.
Definition qgspointxy.h:60
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition qgspoint.cpp:108
double x
Definition qgspoint.h:52
double y
Definition qgspoint.h:53
Polygon geometry type.
Definition qgspolygon.h:33
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
double xMinimum
double yMinimum
void setYMinimum(double y)
Set the minimum y value.
void setXMinimum(double x)
Set the minimum x value.
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
double yMaximum
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.
QgsPointXY center
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 std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
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.
Represent a 2-dimensional vector.
Definition qgsvector.h:31
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:6817
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6524
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6798
#define BUILTIN_UNREACHABLE
Definition qgis.h:7208
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