38
38
#include " qgscomposershape.h"
39
39
40
40
#include " QFileInfo"
41
+ #include < QSvgRenderer>
41
42
#include < QTextDocument>
42
43
#include " QTextStream"
43
44
@@ -75,6 +76,7 @@ QgsProjectParser::QgsProjectParser( QDomDocument* xmlDoc, const QString& filePat
75
76
76
77
mRestrictedLayers = restrictedLayers ();
77
78
createTextAnnotationItems ();
79
+ createSvgAnnotationItems ();
78
80
}
79
81
}
80
82
@@ -86,6 +88,7 @@ QgsProjectParser::~QgsProjectParser()
86
88
{
87
89
delete mXMLDoc ;
88
90
cleanupTextAnnotationItems ();
91
+ cleanupSvgAnnotationItems ();
89
92
}
90
93
91
94
int QgsProjectParser::numberOfLayers () const
@@ -2451,16 +2454,60 @@ void QgsProjectParser::drawOverlays( QPainter* p, int dpi, int width, int height
2451
2454
2452
2455
// calculate item position
2453
2456
double xPos, yPos;
2454
- if ( !annotationPosition ( annotationElem, scaleFactor, prjExtent, width, height, itemWidth, itemHeight, xPos, yPos ) )
2457
+ if ( !annotationPosition ( annotationElem, scaleFactor, xPos, yPos ) )
2455
2458
{
2456
2459
continue ;
2457
2460
}
2458
2461
2459
2462
drawAnnotationRectangle ( p, annotationElem, scaleFactor, xPos, yPos, itemWidth, itemHeight );
2460
2463
2461
2464
// draw annotation contents
2462
- p->translate ( xPos / scaleFactor, yPos / scaleFactor );
2463
- textIt->first ->drawContents ( p, QRectF ( 0 , 0 , itemWidth, itemHeight ) );
2465
+ p->translate ( xPos, yPos );
2466
+ p->scale ( scaleFactor, scaleFactor );
2467
+ textIt->first ->drawContents ( p, QRectF ( 0 , 0 , itemWidth / scaleFactor, itemHeight / scaleFactor ) );
2468
+ p->restore ();
2469
+ }
2470
+
2471
+ // svg annotations
2472
+ QList< QPair< QSvgRenderer*, QDomElement > >::const_iterator svgIt = mSvgAnnotationElems .constBegin ();
2473
+ QDomElement annotationElem;
2474
+ for ( ; svgIt != mSvgAnnotationElems .constEnd (); ++svgIt )
2475
+ {
2476
+ annotationElem = svgIt->second ;
2477
+ int itemWidth = annotationElem.attribute ( " frameWidth" , " 0" ).toInt () * scaleFactor;
2478
+ int itemHeight = annotationElem.attribute ( " frameHeight" , " 0" ).toInt () * scaleFactor;
2479
+
2480
+ // calculate item position
2481
+ double xPos, yPos;
2482
+ if ( !annotationPosition ( annotationElem, scaleFactor, xPos, yPos ) )
2483
+ {
2484
+ continue ;
2485
+ }
2486
+
2487
+ drawAnnotationRectangle ( p, annotationElem, scaleFactor, xPos, yPos, itemWidth, itemHeight );
2488
+
2489
+ // keep width/height ratio of svg
2490
+ QRect viewBox = svgIt->first ->viewBox ();
2491
+ if ( viewBox.isValid () )
2492
+ {
2493
+ double widthRatio = ( double )( itemWidth ) / ( double )( viewBox.width () );
2494
+ double heightRatio = ( double )( itemHeight ) / ( double )( viewBox.height () );
2495
+ double renderWidth = 0 ;
2496
+ double renderHeight = 0 ;
2497
+ if ( widthRatio <= heightRatio )
2498
+ {
2499
+ renderWidth = itemWidth;
2500
+ renderHeight = viewBox.height () * itemWidth / viewBox.width () ;
2501
+ }
2502
+ else
2503
+ {
2504
+ renderHeight = itemHeight;
2505
+ renderWidth = viewBox.width () * itemHeight / viewBox.height () ;
2506
+ }
2507
+
2508
+ svgIt->first ->render ( p, QRectF ( xPos, yPos, renderWidth,
2509
+ renderHeight ) );
2510
+ }
2464
2511
}
2465
2512
}
2466
2513
@@ -2491,6 +2538,44 @@ void QgsProjectParser::createTextAnnotationItems()
2491
2538
}
2492
2539
}
2493
2540
2541
+ void QgsProjectParser::createSvgAnnotationItems ()
2542
+ {
2543
+ mSvgAnnotationElems .clear ();
2544
+ if ( !mXMLDoc )
2545
+ {
2546
+ return ;
2547
+ }
2548
+
2549
+ QDomElement qgisElem = mXMLDoc ->documentElement ();
2550
+ QDomNodeList svgAnnotationList = qgisElem.elementsByTagName ( " SVGAnnotationItem" );
2551
+ QDomElement svgAnnotationElem;
2552
+ QDomElement annotationElem;
2553
+ for ( int i = 0 ; i < svgAnnotationList.size (); ++i )
2554
+ {
2555
+ svgAnnotationElem = svgAnnotationList.at ( i ).toElement ();
2556
+ annotationElem = svgAnnotationElem.firstChildElement ( " AnnotationItem" );
2557
+ QSvgRenderer* svg = new QSvgRenderer ();
2558
+ if ( svg->load ( convertToAbsolutePath ( svgAnnotationElem.attribute ( " file" ) ) ) )
2559
+ {
2560
+ mSvgAnnotationElems .push_back ( qMakePair ( svg, annotationElem ) );
2561
+ }
2562
+ else
2563
+ {
2564
+ delete svg;
2565
+ }
2566
+ }
2567
+ }
2568
+
2569
+ void QgsProjectParser::cleanupSvgAnnotationItems ()
2570
+ {
2571
+ QList< QPair< QSvgRenderer*, QDomElement > >::const_iterator it = mSvgAnnotationElems .constBegin ();
2572
+ for ( ; it != mSvgAnnotationElems .constEnd (); ++it )
2573
+ {
2574
+ delete it->first ;
2575
+ }
2576
+ mSvgAnnotationElems .clear ();
2577
+ }
2578
+
2494
2579
void QgsProjectParser::cleanupTextAnnotationItems ()
2495
2580
{
2496
2581
QList< QPair< QTextDocument*, QDomElement > >::const_iterator it = mTextAnnotationItems .constBegin ();
@@ -2501,23 +2586,11 @@ void QgsProjectParser::cleanupTextAnnotationItems()
2501
2586
mTextAnnotationItems .clear ();
2502
2587
}
2503
2588
2504
- bool QgsProjectParser::annotationPosition ( const QDomElement& elem, double scaleFactor, const QgsRectangle& projectExtent, int width, int height,
2505
- int itemWidth, int itemHeight, double & xPos, double & yPos )
2589
+ bool QgsProjectParser::annotationPosition ( const QDomElement& elem, double scaleFactor,
2590
+ double & xPos, double & yPos )
2506
2591
{
2507
- if ( projectExtent.isEmpty () )
2508
- {
2509
- return false ;
2510
- }
2511
-
2512
- double itemMapPosX = elem.attribute ( " mapPosX" ).toDouble ();
2513
- double itemCanvasPosX = elem.attribute ( " canvasPosX" ).toDouble ();
2514
- int maxCanvasX = itemCanvasPosX / ( itemMapPosX - projectExtent.xMinimum () ) * projectExtent.width ();
2515
- xPos = width - ( maxCanvasX - itemCanvasPosX ) * scaleFactor - itemWidth / 2.0 + 100 ;
2516
-
2517
- double itemMapPosY = elem.attribute ( " mapPosY" ).toDouble ();
2518
- double itemCanvasPosY = elem.attribute ( " canvasPosY" ).toDouble ();
2519
- int maxCanvasY = itemCanvasPosY / ( projectExtent.yMaximum () - itemMapPosY ) * projectExtent.height ();
2520
- yPos = height - ( maxCanvasY - itemCanvasPosY ) * scaleFactor - itemHeight + 100 ;
2592
+ xPos = elem.attribute ( " canvasPosX" ).toDouble () / scaleFactor;
2593
+ yPos = elem.attribute ( " canvasPosY" ).toDouble () / scaleFactor;
2521
2594
return true ;
2522
2595
}
2523
2596
@@ -2535,8 +2608,5 @@ void QgsProjectParser::drawAnnotationRectangle( QPainter* p, const QDomElement&
2535
2608
framePen.setWidth ( elem.attribute ( " frameBorderWidth" , " 1" ).toInt () );
2536
2609
p->setPen ( framePen );
2537
2610
2538
- p->save ();
2539
- p->scale ( scaleFactor, scaleFactor );
2540
- p->drawRect ( QRectF ( xPos / scaleFactor, yPos / scaleFactor, itemWidth, itemHeight ) );
2541
- p->restore ();
2611
+ p->drawRect ( QRectF ( xPos, yPos, itemWidth, itemHeight ) );
2542
2612
}
0 commit comments