QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
35#include <QString>
36
37using namespace Qt::StringLiterals;
38
41 , mBounds( bounds )
42{
43 mBackgroundSymbol = std::make_unique< QgsFillSymbol >(
44 QgsSymbolLayerList { new QgsSimpleFillSymbolLayer( QColor( 255, 255, 255 ), Qt::BrushStyle::SolidPattern, QColor( 0, 0, 0 ), Qt::PenStyle::NoPen ) }
45 );
46 QgsSimpleLineSymbolLayer *borderSymbol = new QgsSimpleLineSymbolLayer( QColor( 0, 0, 0 ) );
47 borderSymbol->setPenJoinStyle( Qt::MiterJoin );
48 mFrameSymbol = std::make_unique< QgsFillSymbol >( QgsSymbolLayerList { borderSymbol } );
49}
50
52
66
68{
69 QgsRectangle bounds = mBounds;
71 {
72 try
73 {
74 bounds = context.coordinateTransform().transformBoundingBox( mBounds );
75 }
76 catch ( QgsCsException & )
77 {
78 return;
79 }
80 }
81
82 QRectF painterBounds;
83
84 switch ( mPlacementMode )
85 {
87 painterBounds = context.mapToPixel().transformBounds( bounds.toRectF() );
88 break;
89
91 {
92 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
93 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
94
95 if ( callout() && !calloutAnchor().isEmpty() )
96 {
97 QgsGeometry anchor = calloutAnchor();
98
99 const double calloutOffsetWidthPixels = context.convertToPainterUnits( offsetFromCallout().width(), offsetFromCalloutUnit() );
100 const double calloutOffsetHeightPixels = context.convertToPainterUnits( offsetFromCallout().height(), offsetFromCalloutUnit() );
101
102 QPointF anchorPoint = anchor.asQPointF();
103 if ( context.coordinateTransform().isValid() )
104 {
105 double x = anchorPoint.x();
106 double y = anchorPoint.y();
107 double z = 0.0;
108 context.coordinateTransform().transformInPlace( x, y, z );
109 anchorPoint = QPointF( x, y );
110 }
111
112 context.mapToPixel().transformInPlace( anchorPoint.rx(), anchorPoint.ry() );
113
114 painterBounds = QRectF( anchorPoint.x() + calloutOffsetWidthPixels, anchorPoint.y() + calloutOffsetHeightPixels, widthPixels, heightPixels );
115 }
116 else
117 {
118 QPointF center = bounds.center().toQPointF();
119
120 context.mapToPixel().transformInPlace( center.rx(), center.ry() );
121 painterBounds = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, 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, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
136 break;
137 }
138 }
139
140 if ( painterBounds.width() < 1 || painterBounds.height() < 1 )
141 return;
142
143 if ( mDrawBackground && mBackgroundSymbol )
144 {
145 mBackgroundSymbol->startRender( context );
146 mBackgroundSymbol->renderPolygon( painterBounds, nullptr, nullptr, context );
147 mBackgroundSymbol->stopRender( context );
148 }
149
151 {
152 QgsCallout::QgsCalloutContext calloutContext;
153 renderCallout( context, painterBounds, 0, calloutContext, feedback );
154 }
155
156 renderInBounds( context, painterBounds, feedback );
157
158 if ( mDrawFrame && mFrameSymbol )
159 {
160 mFrameSymbol->startRender( context );
161 mFrameSymbol->renderPolygon( painterBounds, nullptr, nullptr, context );
162 mFrameSymbol->stopRender( context );
163 }
164}
165
166QList<QgsAnnotationItemNode> QgsAnnotationRectItem::nodesV2( const QgsAnnotationItemEditContext &context ) const
167{
168 QList<QgsAnnotationItemNode> res;
169 switch ( mPlacementMode )
170 {
172 {
173 res = {
174 QgsAnnotationItemNode( QgsVertexId( 0, 0, 0 ), QgsPointXY( mBounds.xMinimum(), mBounds.yMinimum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeBDiagCursor ),
175 QgsAnnotationItemNode( QgsVertexId( 0, 0, 1 ), QgsPointXY( mBounds.xMaximum(), mBounds.yMinimum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeFDiagCursor ),
176 QgsAnnotationItemNode( QgsVertexId( 0, 0, 2 ), QgsPointXY( mBounds.xMaximum(), mBounds.yMaximum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeBDiagCursor ),
177 QgsAnnotationItemNode( QgsVertexId( 0, 0, 3 ), QgsPointXY( mBounds.xMinimum(), mBounds.yMaximum() ), Qgis::AnnotationItemNodeType::VertexHandle, Qt::CursorShape::SizeFDiagCursor ),
178 };
179
180 QgsPointXY calloutNodePoint;
181 if ( !calloutAnchor().isEmpty() )
182 {
183 calloutNodePoint = calloutAnchor().asPoint();
184 }
185 else
186 {
187 calloutNodePoint = mBounds.center();
188 }
189 res.append( QgsAnnotationItemNode( QgsVertexId( 1, 0, 0 ), calloutNodePoint, Qgis::AnnotationItemNodeType::CalloutHandle ) );
190
191 return res;
192 }
193
195 {
196 res = { QgsAnnotationItemNode( QgsVertexId( 0, 0, 0 ), mBounds.center(), Qgis::AnnotationItemNodeType::VertexHandle ) };
197
198 QgsPointXY calloutNodePoint;
199 if ( !calloutAnchor().isEmpty() )
200 {
201 calloutNodePoint = calloutAnchor().asPoint();
202 }
203 else
204 {
205 calloutNodePoint = QgsPointXY( context.currentItemBounds().xMinimum(), context.currentItemBounds().yMinimum() );
206 }
207 res.append( QgsAnnotationItemNode( QgsVertexId( 1, 0, 0 ), calloutNodePoint, Qgis::AnnotationItemNodeType::CalloutHandle ) );
208
209 return res;
210 }
211
213 {
215 }
216 }
218}
219
221{
222 switch ( operation->type() )
223 {
225 {
226 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
227 if ( moveOperation->nodeId().part == 0 )
228 {
229 switch ( mPlacementMode )
230 {
232 {
233 switch ( moveOperation->nodeId().vertex )
234 {
235 case 0:
236 mBounds = QgsRectangle( moveOperation->after().x(), moveOperation->after().y(), mBounds.xMaximum(), mBounds.yMaximum() );
237 break;
238 case 1:
239 mBounds = QgsRectangle( mBounds.xMinimum(), moveOperation->after().y(), moveOperation->after().x(), mBounds.yMaximum() );
240 break;
241 case 2:
242 mBounds = QgsRectangle( mBounds.xMinimum(), mBounds.yMinimum(), moveOperation->after().x(), moveOperation->after().y() );
243 break;
244 case 3:
245 mBounds = QgsRectangle( moveOperation->after().x(), mBounds.yMinimum(), mBounds.xMaximum(), moveOperation->after().y() );
246 break;
247 default:
248 break;
249 }
251 }
252
254 {
255 mBounds = QgsRectangle::fromCenterAndSize( moveOperation->after(), mBounds.width(), mBounds.height() );
257 }
258
260 {
261 const double deltaX = moveOperation->translationXPixels() / context.renderContext().outputSize().width();
262 const double deltaY = moveOperation->translationYPixels() / context.renderContext().outputSize().height();
263 mBounds = QgsRectangle::fromCenterAndSize( QgsPointXY( mBounds.center().x() + deltaX, mBounds.center().y() + deltaY ), mBounds.width(), mBounds.height() );
265 }
266 }
267 }
268 else if ( moveOperation->nodeId().part == 1 )
269 {
270 setCalloutAnchor( QgsGeometry::fromPoint( moveOperation->after() ) );
271 if ( !callout() )
272 {
273 setCallout( QgsApplication::calloutRegistry()->defaultCallout() );
274 }
276 }
277 break;
278 }
279
281 {
282 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
283 switch ( mPlacementMode )
284 {
286 mBounds = QgsRectangle(
287 mBounds.xMinimum() + moveOperation->translationX(),
288 mBounds.yMinimum() + moveOperation->translationY(),
289 mBounds.xMaximum() + moveOperation->translationX(),
290 mBounds.yMaximum() + moveOperation->translationY()
291 );
292 break;
293
295 {
296 if ( callout() && !calloutAnchor().isEmpty() )
297 {
298 const double xOffset = context.renderContext().convertFromPainterUnits( moveOperation->translationXPixels(), offsetFromCalloutUnit() );
299 const double yOffset = context.renderContext().convertFromPainterUnits( moveOperation->translationYPixels(), offsetFromCalloutUnit() );
300 setOffsetFromCallout( QSizeF( offsetFromCallout().width() + xOffset, offsetFromCallout().height() + yOffset ) );
301 }
302 else
303 {
304 mBounds = QgsRectangle(
305 mBounds.xMinimum() + moveOperation->translationX(),
306 mBounds.yMinimum() + moveOperation->translationY(),
307 mBounds.xMaximum() + moveOperation->translationX(),
308 mBounds.yMaximum() + moveOperation->translationY()
309 );
310 }
311 break;
312 }
313
315 {
316 const double deltaX = moveOperation->translationXPixels() / context.renderContext().outputSize().width();
317 const double deltaY = moveOperation->translationYPixels() / context.renderContext().outputSize().height();
318 mBounds = QgsRectangle::fromCenterAndSize( QgsPointXY( mBounds.center().x() + deltaX, mBounds.center().y() + deltaY ), mBounds.width(), mBounds.height() );
319 break;
320 }
321 }
323 }
324
328 break;
329 }
331}
332
334{
335 switch ( operation->type() )
336 {
338 {
339 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
340 if ( moveOperation->nodeId().part == 0 )
341 {
342 switch ( mPlacementMode )
343 {
345 {
346 QgsRectangle modifiedBounds = mBounds;
347 switch ( moveOperation->nodeId().vertex )
348 {
349 case 0:
350 modifiedBounds.setXMinimum( moveOperation->after().x() );
351 modifiedBounds.setYMinimum( moveOperation->after().y() );
352 break;
353 case 1:
354 modifiedBounds.setXMaximum( moveOperation->after().x() );
355 modifiedBounds.setYMinimum( moveOperation->after().y() );
356 break;
357 case 2:
358 modifiedBounds.setXMaximum( moveOperation->after().x() );
359 modifiedBounds.setYMaximum( moveOperation->after().y() );
360 break;
361 case 3:
362 modifiedBounds.setXMinimum( moveOperation->after().x() );
363 modifiedBounds.setYMaximum( moveOperation->after().y() );
364 break;
365 default:
366 break;
367 }
369 }
371 {
372 const QgsRectangle currentBounds = context.currentItemBounds();
373 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( moveOperation->after(), currentBounds.width(), currentBounds.height() );
375 }
377 {
378 const QgsRectangle currentBounds = context.currentItemBounds();
379 const QgsRectangle newBounds = QgsRectangle::fromCenterAndSize( currentBounds.center() + ( moveOperation->after() - moveOperation->before() ), currentBounds.width(), currentBounds.height() );
381 }
382 }
383 }
384 else
385 {
386 QgsAnnotationItemEditOperationMoveNode *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationMoveNode * >( operation );
387 return new QgsAnnotationItemEditOperationTransientResults( QgsGeometry( moveOperation->after().clone() ) );
388 }
389 break;
390 }
391
393 {
394 QgsAnnotationItemEditOperationTranslateItem *moveOperation = qgis::down_cast< QgsAnnotationItemEditOperationTranslateItem * >( operation );
395 switch ( mPlacementMode )
396 {
398 {
399 const QgsRectangle modifiedBounds(
400 mBounds.xMinimum() + moveOperation->translationX(),
401 mBounds.yMinimum() + moveOperation->translationY(),
402 mBounds.xMaximum() + moveOperation->translationX(),
403 mBounds.yMaximum() + moveOperation->translationY()
404 );
406 }
407
409 {
410 if ( callout() && !calloutAnchor().isEmpty() )
411 {
412 QgsGeometry anchor = calloutAnchor();
413
414 const double calloutOffsetWidthPixels = context.renderContext().convertToPainterUnits( offsetFromCallout().width(), offsetFromCalloutUnit() ) + moveOperation->translationXPixels();
415 const double calloutOffsetHeightPixels = context.renderContext().convertToPainterUnits( offsetFromCallout().height(), offsetFromCalloutUnit() ) + moveOperation->translationYPixels();
416
417 QPointF anchorPoint = anchor.asQPointF();
418 if ( context.renderContext().coordinateTransform().isValid() )
419 {
420 double x = anchorPoint.x();
421 double y = anchorPoint.y();
422 double z = 0.0;
424 anchorPoint = QPointF( x, y );
425 }
426
427 context.renderContext().mapToPixel().transformInPlace( anchorPoint.rx(), anchorPoint.ry() );
428
429 const double textOriginXPixels = anchorPoint.x() + calloutOffsetWidthPixels;
430 const double textOriginYPixels = anchorPoint.y() + calloutOffsetHeightPixels;
431
432 const double widthPixels = context.renderContext().convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
433 const double heightPixels = context.renderContext().convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
434
435 QgsLineString ls(
436 QVector<QgsPointXY> {
437 QgsPointXY( textOriginXPixels, textOriginYPixels ),
438 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels ),
439 QgsPointXY( textOriginXPixels + widthPixels, textOriginYPixels + heightPixels ),
440 QgsPointXY( textOriginXPixels, textOriginYPixels + heightPixels ),
441 QgsPointXY( textOriginXPixels, textOriginYPixels )
442 }
443 );
444
445 QgsGeometry g( new QgsPolygon( ls.clone() ) );
446 g.transform( context.renderContext().mapToPixel().transform().inverted() );
449 }
450 else
451 {
452 const QgsRectangle currentBounds = context.currentItemBounds();
453 const QgsRectangle newBounds
454 = QgsRectangle::fromCenterAndSize( mBounds.center() + QgsVector( moveOperation->translationX(), moveOperation->translationY() ), currentBounds.width(), currentBounds.height() );
456 }
457 }
458
460 {
461 const QgsRectangle currentBounds = context.currentItemBounds();
462 const QgsRectangle newBounds
463 = QgsRectangle::fromCenterAndSize( currentBounds.center() + QgsVector( moveOperation->translationX(), moveOperation->translationY() ), currentBounds.width(), currentBounds.height() );
465 }
466 }
467 break;
468 }
469
473 break;
474 }
475 return nullptr;
476}
477
479{
481 switch ( mPlacementMode )
482 {
484 {
485 bounds = mBounds;
486 if ( callout() && !calloutAnchor().isEmpty() )
487 {
488 QgsGeometry anchor = calloutAnchor();
489 bounds.combineExtentWith( anchor.boundingBox() );
490 }
491 break;
492 }
493
495 if ( callout() && !calloutAnchor().isEmpty() )
496 {
498 }
499 else
500 {
501 bounds = QgsRectangle( mBounds.center(), mBounds.center() );
502 }
503 break;
504
506 bounds = mBounds;
507 break;
508 }
509
510 return bounds;
511}
512
514{
515 switch ( mPlacementMode )
516 {
519
521 {
522 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
523 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
524
525 QRectF boundsInPixels;
526 if ( callout() && !calloutAnchor().isEmpty() )
527 {
528 QgsGeometry anchor = calloutAnchor();
529
530 const double calloutOffsetWidthPixels = context.convertToPainterUnits( offsetFromCallout().width(), offsetFromCalloutUnit() );
531 const double calloutOffsetHeightPixels = context.convertToPainterUnits( offsetFromCallout().height(), offsetFromCalloutUnit() );
532
533 QPointF anchorPoint = anchor.asQPointF();
534 if ( context.coordinateTransform().isValid() )
535 {
536 double x = anchorPoint.x();
537 double y = anchorPoint.y();
538 double z = 0.0;
539 context.coordinateTransform().transformInPlace( x, y, z );
540 anchorPoint = QPointF( x, y );
541 }
542
543 context.mapToPixel().transformInPlace( anchorPoint.rx(), anchorPoint.ry() );
544
546 textRect( anchorPoint.x() + calloutOffsetWidthPixels, anchorPoint.y() + calloutOffsetHeightPixels, anchorPoint.x() + calloutOffsetWidthPixels + widthPixels, anchorPoint.y() + calloutOffsetHeightPixels + heightPixels );
547 QgsRectangle anchorRect( anchorPoint.x(), anchorPoint.y(), anchorPoint.x(), anchorPoint.y() );
548 anchorRect.combineExtentWith( textRect );
549
550 boundsInPixels = anchorRect.toRectF();
551 }
552 else
553 {
554 QPointF center = mBounds.center().toQPointF();
555 if ( context.coordinateTransform().isValid() )
556 {
557 double x = center.x();
558 double y = center.y();
559 double z = 0.0;
560 context.coordinateTransform().transformInPlace( x, y, z );
561 center = QPointF( x, y );
562 }
563
564 context.mapToPixel().transformInPlace( center.rx(), center.ry() );
565 boundsInPixels = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
566 }
567 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
568 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
569 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
570 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
571
572 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
574 return textRect;
575 }
576
578 {
579 const double widthPixels = context.convertToPainterUnits( mFixedSize.width(), mFixedSizeUnit );
580 const double heightPixels = context.convertToPainterUnits( mFixedSize.height(), mFixedSizeUnit );
581
582 QRectF boundsInPixels;
583
584 const double centerMapX = context.mapExtent().xMinimum() + mBounds.center().x() * context.mapExtent().width();
585 const double centerMapY = context.mapExtent().yMaximum() - mBounds.center().y() * context.mapExtent().height();
586 QPointF center( centerMapX, centerMapY );
587 if ( context.coordinateTransform().isValid() )
588 {
589 double x = centerMapX;
590 double y = centerMapY;
591 double z = 0.0;
592 context.coordinateTransform().transformInPlace( x, y, z );
593 center = QPointF( x, y );
594 }
595
596 context.mapToPixel().transformInPlace( center.rx(), center.ry() );
597 boundsInPixels = QRectF( center.x() - widthPixels * 0.5, center.y() - heightPixels * 0.5, widthPixels, heightPixels );
598
599 const QgsPointXY topLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.top() );
600 const QgsPointXY topRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.top() );
601 const QgsPointXY bottomLeft = context.mapToPixel().toMapCoordinates( boundsInPixels.left(), boundsInPixels.bottom() );
602 const QgsPointXY bottomRight = context.mapToPixel().toMapCoordinates( boundsInPixels.right(), boundsInPixels.bottom() );
603
604 const QgsRectangle boundsMapUnits = QgsRectangle( topLeft.x(), bottomLeft.y(), bottomRight.x(), topRight.y() );
606 return textRect;
607 }
608 }
610}
611
613{
614 mBounds = bounds;
615}
616
618{
619 return mBackgroundSymbol.get();
620}
621
623{
624 mBackgroundSymbol.reset( symbol );
625}
626
628{
629 return mFrameSymbol.get();
630}
631
633{
634 mFrameSymbol.reset( symbol );
635}
636
638{
639 if ( const QgsAnnotationRectItem *otherRect = dynamic_cast< const QgsAnnotationRectItem * >( other ) )
640 {
641 setPlacementMode( otherRect->mPlacementMode );
642 setFixedSize( otherRect->mFixedSize );
643 setFixedSizeUnit( otherRect->mFixedSizeUnit );
644
645 setBackgroundEnabled( otherRect->mDrawBackground );
646 if ( otherRect->mBackgroundSymbol )
647 setBackgroundSymbol( otherRect->mBackgroundSymbol->clone() );
648
649 setFrameEnabled( otherRect->mDrawFrame );
650 if ( otherRect->mFrameSymbol )
651 setFrameSymbol( otherRect->mFrameSymbol->clone() );
652 }
653
655}
656
657bool QgsAnnotationRectItem::writeCommonProperties( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
658{
659 element.setAttribute( u"xMin"_s, qgsDoubleToString( mBounds.xMinimum() ) );
660 element.setAttribute( u"xMax"_s, qgsDoubleToString( mBounds.xMaximum() ) );
661 element.setAttribute( u"yMin"_s, qgsDoubleToString( mBounds.yMinimum() ) );
662 element.setAttribute( u"yMax"_s, qgsDoubleToString( mBounds.yMaximum() ) );
663 element.setAttribute( u"sizeMode"_s, qgsEnumValueToKey( mPlacementMode ) );
664 element.setAttribute( u"fixedWidth"_s, qgsDoubleToString( mFixedSize.width() ) );
665 element.setAttribute( u"fixedHeight"_s, qgsDoubleToString( mFixedSize.height() ) );
666 element.setAttribute( u"fixedSizeUnit"_s, QgsUnitTypes::encodeUnit( mFixedSizeUnit ) );
667
668 element.setAttribute( u"backgroundEnabled"_s, mDrawBackground ? u"1"_s : u"0"_s );
669 if ( mBackgroundSymbol )
670 {
671 QDomElement backgroundElement = document.createElement( u"backgroundSymbol"_s );
672 backgroundElement.appendChild( QgsSymbolLayerUtils::saveSymbol( u"backgroundSymbol"_s, mBackgroundSymbol.get(), document, context ) );
673 element.appendChild( backgroundElement );
674 }
675
676 element.setAttribute( u"frameEnabled"_s, mDrawFrame ? u"1"_s : u"0"_s );
677 if ( mFrameSymbol )
678 {
679 QDomElement frameElement = document.createElement( u"frameSymbol"_s );
680 frameElement.appendChild( QgsSymbolLayerUtils::saveSymbol( u"frameSymbol"_s, mFrameSymbol.get(), document, context ) );
681 element.appendChild( frameElement );
682 }
683
684 return QgsAnnotationItem::writeCommonProperties( element, document, context );
685}
686
687bool QgsAnnotationRectItem::readCommonProperties( const QDomElement &element, const QgsReadWriteContext &context )
688{
689 mBounds.setXMinimum( element.attribute( u"xMin"_s ).toDouble() );
690 mBounds.setXMaximum( element.attribute( u"xMax"_s ).toDouble() );
691 mBounds.setYMinimum( element.attribute( u"yMin"_s ).toDouble() );
692 mBounds.setYMaximum( element.attribute( u"yMax"_s ).toDouble() );
693
694 mPlacementMode = qgsEnumKeyToValue( element.attribute( u"sizeMode"_s ), Qgis::AnnotationPlacementMode::SpatialBounds );
695
696 mFixedSize = QSizeF( element.attribute( u"fixedWidth"_s ).toDouble(), element.attribute( u"fixedHeight"_s ).toDouble() );
697 mFixedSizeUnit = QgsUnitTypes::decodeRenderUnit( element.attribute( u"fixedSizeUnit"_s ) );
698
699 mDrawBackground = element.attribute( u"backgroundEnabled"_s, u"0"_s ).toInt();
700 const QDomElement backgroundSymbolElem = element.firstChildElement( u"backgroundSymbol"_s ).firstChildElement();
701 if ( !backgroundSymbolElem.isNull() )
702 {
703 setBackgroundSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( backgroundSymbolElem, context ).release() );
704 }
705
706 mDrawFrame = element.attribute( u"frameEnabled"_s, u"0"_s ).toInt();
707 const QDomElement frameSymbolElem = element.firstChildElement( u"frameSymbol"_s ).firstChildElement();
708 if ( !frameSymbolElem.isNull() )
709 {
710 setFrameSymbol( QgsSymbolLayerUtils::loadSymbol< QgsFillSymbol >( frameSymbolElem, context ).release() );
711 }
712
713 return QgsAnnotationItem::readCommonProperties( element, context );
714}
715
717{
718 return mFixedSize;
719}
720
721void QgsAnnotationRectItem::setFixedSize( const QSizeF &size )
722{
723 mFixedSize = size;
724}
725
727{
728 return mFixedSizeUnit;
729}
730
732{
733 mFixedSizeUnit = unit;
734}
735
737{
738 return mPlacementMode;
739}
740
742{
743 mPlacementMode = mode;
744}
@ VertexHandle
Node is a handle for manipulating vertices.
Definition qgis.h:2597
@ CalloutHandle
Node is a handle for manipulating callouts.
Definition qgis.h:2598
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
Definition qgis.h:2554
@ SupportsCallouts
Item supports callouts.
Definition qgis.h:2556
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
Definition qgis.h:2608
@ Invalid
Operation has invalid parameters for the item, no change occurred.
Definition qgis.h:2610
@ Success
Item was modified successfully.
Definition qgis.h:2609
QFlags< AnnotationItemFlag > AnnotationItemFlags
Annotation item flags.
Definition qgis.h:2559
RenderUnit
Rendering size units.
Definition qgis.h:5340
AnnotationPlacementMode
Annotation item placement modes.
Definition qgis.h:2569
@ SpatialBounds
Item is rendered inside fixed spatial bounds, and size will depend on map scale.
Definition qgis.h:2570
@ FixedSize
Item is rendered at a fixed size, regardless of map scale. Item's location is georeferenced to a spat...
Definition qgis.h:2571
@ RelativeToMapFrame
Items size and placement is relative to the map's frame, and the item will always be rendered in the ...
Definition qgis.h:2572
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2766
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:248
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:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition qgspoint.cpp:138
double x
Definition qgspoint.h:56
double y
Definition qgspoint.h:57
Polygon geometry type.
Definition qgspolygon.h:37
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:34
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:7176
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6893
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7157
#define BUILTIN_UNREACHABLE
Definition qgis.h:7540
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition qgssymbol.h:30
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:34
int vertex
Vertex number.
Definition qgsvertexid.h:99
int part
Part number.
Definition qgsvertexid.h:93