17
17
18
18
#include " qgscomposerlegend.h"
19
19
#include " qgscomposerlegenditem.h"
20
+ #include " qgscomposermap.h"
20
21
#include " qgsmaplayer.h"
21
22
#include " qgsmaplayerregistry.h"
22
23
#include " qgsmaprenderer.h"
@@ -33,7 +34,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
33
34
, mBoxSpace( 2 )
34
35
, mLayerSpace( 2 )
35
36
, mSymbolSpace( 2 )
36
- , mIconLabelSpace( 2 )
37
+ , mIconLabelSpace( 2 ), mComposerMap( 0 )
37
38
{
38
39
// QStringList idList = layerIdList();
39
40
// mLegendModel.setLayerSet( idList );
@@ -50,7 +51,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
50
51
connect ( &mLegendModel , SIGNAL ( layersChanged () ), this , SLOT ( synchronizeWithModel () ) );
51
52
}
52
53
53
- QgsComposerLegend::QgsComposerLegend (): QgsComposerItem( 0 )
54
+ QgsComposerLegend::QgsComposerLegend (): QgsComposerItem( 0 ), mComposerMap( 0 )
54
55
{
55
56
56
57
}
@@ -390,23 +391,49 @@ void QgsComposerLegend::drawSymbolV2( QPainter* p, QgsSymbolV2* s, double curren
390
391
rasterScaleFactor = ( paintDevice->logicalDpiX () + paintDevice->logicalDpiY () ) / 2.0 / 25.4 ;
391
392
}
392
393
394
+ // consider relation to composer map for symbol sizes in mm
395
+ bool sizeInMapUnits = s->outputUnit () == QgsSymbolV2::MapUnit;
396
+ double mmPerMapUnit = 1 ;
397
+ if ( mComposerMap )
398
+ {
399
+ mmPerMapUnit = mComposerMap ->mapUnitsToMM ();
400
+ }
401
+ QgsMarkerSymbolV2* markerSymbol = dynamic_cast <QgsMarkerSymbolV2*>( s );
402
+
393
403
// Consider symbol size for point markers
394
404
double height = mSymbolHeight ;
395
405
double width = mSymbolWidth ;
396
- if ( s->type () == QgsSymbolV2::Marker )
406
+ double size = 0 ;
407
+
408
+ if ( markerSymbol )
397
409
{
398
- QgsMarkerSymbolV2* markerSymbol = dynamic_cast <QgsMarkerSymbolV2*>( s );
399
- if ( markerSymbol )
410
+ size = markerSymbol->size ();
411
+ height = size;
412
+ width = size;
413
+ if ( mComposerMap && sizeInMapUnits )
400
414
{
401
- height = markerSymbol->size ();
402
- width = markerSymbol->size ();
415
+ height *= mmPerMapUnit;
416
+ width *= mmPerMapUnit;
417
+ markerSymbol->setSize ( width );
403
418
}
404
419
}
405
420
406
421
p->save ();
407
422
p->translate ( currentXPosition, currentYCoord );
408
423
p->scale ( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor );
424
+
425
+ if ( markerSymbol && sizeInMapUnits )
426
+ {
427
+ s->setOutputUnit ( QgsSymbolV2::MM );
428
+ }
409
429
s->drawPreviewIcon ( p, QSize ( width * rasterScaleFactor, height * rasterScaleFactor ) );
430
+
431
+ if ( markerSymbol && sizeInMapUnits )
432
+ {
433
+ s->setOutputUnit ( QgsSymbolV2::MapUnit );
434
+ markerSymbol->setSize ( size );
435
+ }
436
+
410
437
p->restore ();
411
438
currentXPosition += width;
412
439
symbolHeight = height;
@@ -605,6 +632,11 @@ bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const
605
632
composerLegendElem.setAttribute ( " symbolWidth" , mSymbolWidth );
606
633
composerLegendElem.setAttribute ( " symbolHeight" , mSymbolHeight );
607
634
635
+ if ( mComposerMap )
636
+ {
637
+ composerLegendElem.setAttribute ( " map" , mComposerMap ->id () );
638
+ }
639
+
608
640
// write model properties
609
641
mLegendModel .writeXML ( composerLegendElem, doc );
610
642
@@ -655,6 +687,12 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
655
687
mSymbolWidth = itemElem.attribute ( " symbolWidth" , " 7.0" ).toDouble ();
656
688
mSymbolHeight = itemElem.attribute ( " symbolHeight" , " 14.0" ).toDouble ();
657
689
690
+ // composer map
691
+ if ( !itemElem.attribute ( " map" ).isEmpty () )
692
+ {
693
+ mComposerMap = mComposition ->getComposerMapById ( itemElem.attribute ( " map" ).toInt () );
694
+ }
695
+
658
696
// read model properties
659
697
QDomNodeList modelNodeList = itemElem.elementsByTagName ( " Model" );
660
698
if ( modelNodeList.size () > 0 )
@@ -674,3 +712,15 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
674
712
emit itemChanged ();
675
713
return true ;
676
714
}
715
+
716
+ void QgsComposerLegend::setComposerMap ( const QgsComposerMap* map )
717
+ {
718
+ mComposerMap = map;
719
+ QObject::connect ( map, SIGNAL ( destroyed ( QObject* ) ), this , SLOT ( invalidateCurrentMap () ) );
720
+ }
721
+
722
+ void QgsComposerLegend::invalidateCurrentMap ()
723
+ {
724
+ disconnect ( mComposerMap , SIGNAL ( destroyed ( QObject* ) ), this , SLOT ( invalidateCurrentMap () ) );
725
+ mComposerMap = 0 ;
726
+ }
0 commit comments