@@ -26,7 +26,14 @@ QgsAnnotation::QgsAnnotation( QObject* parent )
2626 : QObject( parent )
2727 , mMarkerSymbol( new QgsMarkerSymbol() )
2828{
29-
29+ QgsStringMap props;
30+ props.insert ( QStringLiteral ( " color" ), QStringLiteral ( " white" ) );
31+ props.insert ( QStringLiteral ( " style" ), QStringLiteral ( " solid" ) );
32+ props.insert ( QStringLiteral ( " style_border" ), QStringLiteral ( " solid" ) );
33+ props.insert ( QStringLiteral ( " color_border" ), QStringLiteral ( " black" ) );
34+ props.insert ( QStringLiteral ( " width_border" ), QStringLiteral ( " 0.3" ) );
35+ props.insert ( QStringLiteral ( " joinstyle" ), QStringLiteral ( " miter" ) );
36+ mFillSymbol .reset ( QgsFillSymbol::createSimple ( props ) );
3037}
3138
3239void QgsAnnotation::setVisible ( bool visible )
@@ -89,21 +96,9 @@ void QgsAnnotation::setContentsMargin( const QgsMargins& margins )
8996 emit appearanceChanged ();
9097}
9198
92- void QgsAnnotation::setFrameBorderWidth ( double width )
99+ void QgsAnnotation::setFillSymbol ( QgsFillSymbol* symbol )
93100{
94- mFrameBorderWidth = width;
95- emit appearanceChanged ();
96- }
97-
98- void QgsAnnotation::setFrameColor ( const QColor& c )
99- {
100- mFrameColor = c;
101- emit appearanceChanged ();
102- }
103-
104- void QgsAnnotation::setFrameBackgroundColor ( const QColor& c )
105- {
106- mFrameBackgroundColor = c;
101+ mFillSymbol .reset ( symbol );
107102 emit appearanceChanged ();
108103}
109104
@@ -263,17 +258,13 @@ QPointF QgsAnnotation::pointOnLineWithDistance( QPointF startPoint, QPointF dire
263258
264259void QgsAnnotation::drawFrame ( QgsRenderContext& context ) const
265260{
266- QPen framePen ( mFrameColor );
267- framePen.setWidthF ( context.convertToPainterUnits ( mFrameBorderWidth , QgsUnitTypes::RenderPixels ) );
268-
269- QPainter* p = context.painter ();
261+ if ( !mFillSymbol )
262+ return ;
270263
271- p->setPen ( framePen );
272- QBrush frameBrush ( mFrameBackgroundColor );
273- p->setBrush ( frameBrush );
274- p->setRenderHint ( QPainter::Antialiasing, context.flags () & QgsRenderContext::Antialiasing );
264+ context.painter ()->setRenderHint ( QPainter::Antialiasing, context.flags () & QgsRenderContext::Antialiasing );
275265
276266 QPolygonF poly;
267+ QList<QPolygonF> rings; // empty list
277268 for ( int i = 0 ; i < 4 ; ++i )
278269 {
279270 QLineF currentSegment = segment ( i );
@@ -286,7 +277,10 @@ void QgsAnnotation::drawFrame( QgsRenderContext& context ) const
286277 }
287278 poly << currentSegment.p2 ();
288279 }
289- p->drawPolygon ( poly );
280+
281+ mFillSymbol ->startRender ( context );
282+ mFillSymbol ->renderPolygon ( poly, &rings, nullptr , context );
283+ mFillSymbol ->stopRender ( context );
290284}
291285
292286void QgsAnnotation::drawMarkerSymbol ( QgsRenderContext& context ) const
@@ -322,12 +316,7 @@ void QgsAnnotation::_writeXml( QDomElement& itemElem, QDomDocument& doc ) const
322316 annotationElem.setAttribute ( QStringLiteral ( " frameHeight" ), qgsDoubleToString ( mFrameSize .height () ) );
323317 annotationElem.setAttribute ( QStringLiteral ( " canvasPosX" ), qgsDoubleToString ( mRelativePosition .x () ) );
324318 annotationElem.setAttribute ( QStringLiteral ( " canvasPosY" ), qgsDoubleToString ( mRelativePosition .y () ) );
325- annotationElem.setAttribute ( QStringLiteral ( " frameBorderWidth" ), qgsDoubleToString ( mFrameBorderWidth ) );
326319 annotationElem.setAttribute ( QStringLiteral ( " contentsMargin" ), mContentsMargins .toString () );
327- annotationElem.setAttribute ( QStringLiteral ( " frameColor" ), mFrameColor .name () );
328- annotationElem.setAttribute ( QStringLiteral ( " frameColorAlpha" ), mFrameColor .alpha () );
329- annotationElem.setAttribute ( QStringLiteral ( " frameBackgroundColor" ), mFrameBackgroundColor .name () );
330- annotationElem.setAttribute ( QStringLiteral ( " frameBackgroundColorAlpha" ), mFrameBackgroundColor .alpha () );
331320 annotationElem.setAttribute ( QStringLiteral ( " visible" ), isVisible () );
332321 if ( mMapLayer )
333322 {
@@ -341,6 +330,16 @@ void QgsAnnotation::_writeXml( QDomElement& itemElem, QDomDocument& doc ) const
341330 annotationElem.appendChild ( symbolElem );
342331 }
343332 }
333+ if ( mFillSymbol )
334+ {
335+ QDomElement fillElem = doc.createElement ( QStringLiteral ( " fillSymbol" ) );
336+ QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol ( QStringLiteral ( " fill symbol" ), mFillSymbol .data (), doc );
337+ if ( !symbolElem.isNull () )
338+ {
339+ fillElem.appendChild ( symbolElem );
340+ annotationElem.appendChild ( fillElem );
341+ }
342+ }
344343 itemElem.appendChild ( annotationElem );
345344}
346345
@@ -365,12 +364,7 @@ void QgsAnnotation::_readXml( const QDomElement& annotationElem, const QDomDocum
365364 mMapPositionCrs = QgsCoordinateReferenceSystem ();
366365 }
367366
368- mFrameBorderWidth = annotationElem.attribute ( QStringLiteral ( " frameBorderWidth" ), QStringLiteral ( " 0.5" ) ).toDouble ();
369367 mContentsMargins = QgsMargins::fromString ( annotationElem.attribute ( QStringLiteral ( " contentsMargin" ) ) );
370- mFrameColor .setNamedColor ( annotationElem.attribute ( QStringLiteral ( " frameColor" ), QStringLiteral ( " #000000" ) ) );
371- mFrameColor .setAlpha ( annotationElem.attribute ( QStringLiteral ( " frameColorAlpha" ), QStringLiteral ( " 255" ) ).toInt () );
372- mFrameBackgroundColor .setNamedColor ( annotationElem.attribute ( QStringLiteral ( " frameBackgroundColor" ) ) );
373- mFrameBackgroundColor .setAlpha ( annotationElem.attribute ( QStringLiteral ( " frameBackgroundColorAlpha" ), QStringLiteral ( " 255" ) ).toInt () );
374368 mFrameSize .setWidth ( annotationElem.attribute ( QStringLiteral ( " frameWidth" ), QStringLiteral ( " 50" ) ).toDouble () );
375369 mFrameSize .setHeight ( annotationElem.attribute ( QStringLiteral ( " frameHeight" ), QStringLiteral ( " 50" ) ).toDouble () );
376370 mOffsetFromReferencePoint .setX ( annotationElem.attribute ( QStringLiteral ( " offsetX" ), QStringLiteral ( " 0" ) ).toDouble () );
@@ -393,6 +387,41 @@ void QgsAnnotation::_readXml( const QDomElement& annotationElem, const QDomDocum
393387 }
394388 }
395389
390+ mFillSymbol .reset ( nullptr );
391+ QDomElement fillElem = annotationElem.firstChildElement ( QStringLiteral ( " fillSymbol" ) );
392+ if ( !fillElem.isNull () )
393+ {
394+ QDomElement symbolElem = fillElem.firstChildElement ( QStringLiteral ( " symbol" ) );
395+ if ( !symbolElem.isNull () )
396+ {
397+ QgsFillSymbol* symbol = QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem );
398+ if ( symbol )
399+ {
400+ mFillSymbol .reset ( symbol );
401+ }
402+ }
403+ }
404+ if ( !mFillSymbol )
405+ {
406+ QColor frameColor;
407+ frameColor.setNamedColor ( annotationElem.attribute ( QStringLiteral ( " frameColor" ), QStringLiteral ( " #000000" ) ) );
408+ frameColor.setAlpha ( annotationElem.attribute ( QStringLiteral ( " frameColorAlpha" ), QStringLiteral ( " 255" ) ).toInt () );
409+ QColor frameBackgroundColor;
410+ frameBackgroundColor.setNamedColor ( annotationElem.attribute ( QStringLiteral ( " frameBackgroundColor" ) ) );
411+ frameBackgroundColor.setAlpha ( annotationElem.attribute ( QStringLiteral ( " frameBackgroundColorAlpha" ), QStringLiteral ( " 255" ) ).toInt () );
412+ double frameBorderWidth = annotationElem.attribute ( QStringLiteral ( " frameBorderWidth" ), QStringLiteral ( " 0.5" ) ).toDouble ();
413+ // need to roughly convert border width from pixels to mm - just assume 96 dpi
414+ frameBorderWidth = frameBorderWidth * 25.4 / 96.0 ;
415+ QgsStringMap props;
416+ props.insert ( QStringLiteral ( " color" ), frameBackgroundColor.name () );
417+ props.insert ( QStringLiteral ( " style" ), QStringLiteral ( " solid" ) );
418+ props.insert ( QStringLiteral ( " style_border" ), QStringLiteral ( " solid" ) );
419+ props.insert ( QStringLiteral ( " color_border" ), frameColor.name () );
420+ props.insert ( QStringLiteral ( " width_border" ), QString::number ( frameBorderWidth ) );
421+ props.insert ( QStringLiteral ( " joinstyle" ), QStringLiteral ( " miter" ) );
422+ mFillSymbol .reset ( QgsFillSymbol::createSimple ( props ) );
423+ }
424+
396425 updateBalloon ();
397426 emit mapLayerChanged ();
398427}
0 commit comments