1717
1818#include " qgscomposerlegend.h"
1919#include " qgscomposerlegenditem.h"
20+ #include " qgscomposermap.h"
2021#include " qgsmaplayer.h"
2122#include " qgsmaplayerregistry.h"
2223#include " qgsmaprenderer.h"
@@ -33,7 +34,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
3334 , mBoxSpace( 2 )
3435 , mLayerSpace( 2 )
3536 , mSymbolSpace( 2 )
36- , mIconLabelSpace( 2 )
37+ , mIconLabelSpace( 2 ), mComposerMap( 0 )
3738{
3839 // QStringList idList = layerIdList();
3940 // mLegendModel.setLayerSet( idList );
@@ -50,7 +51,7 @@ QgsComposerLegend::QgsComposerLegend( QgsComposition* composition )
5051 connect ( &mLegendModel , SIGNAL ( layersChanged () ), this , SLOT ( synchronizeWithModel () ) );
5152}
5253
53- QgsComposerLegend::QgsComposerLegend (): QgsComposerItem( 0 )
54+ QgsComposerLegend::QgsComposerLegend (): QgsComposerItem( 0 ), mComposerMap( 0 )
5455{
5556
5657}
@@ -394,23 +395,49 @@ void QgsComposerLegend::drawSymbolV2( QPainter* p, QgsSymbolV2* s, double curren
394395 rasterScaleFactor = ( paintDevice->logicalDpiX () + paintDevice->logicalDpiY () ) / 2.0 / 25.4 ;
395396 }
396397
398+ // consider relation to composer map for symbol sizes in mm
399+ bool sizeInMapUnits = s->outputUnit () == QgsSymbolV2::MapUnit;
400+ double mmPerMapUnit = 1 ;
401+ if ( mComposerMap )
402+ {
403+ mmPerMapUnit = mComposerMap ->mapUnitsToMM ();
404+ }
405+ QgsMarkerSymbolV2* markerSymbol = dynamic_cast <QgsMarkerSymbolV2*>( s );
406+
397407 // Consider symbol size for point markers
398408 double height = mSymbolHeight ;
399409 double width = mSymbolWidth ;
400- if ( s->type () == QgsSymbolV2::Marker )
410+ double size = 0 ;
411+
412+ if ( markerSymbol )
401413 {
402- QgsMarkerSymbolV2* markerSymbol = dynamic_cast <QgsMarkerSymbolV2*>( s );
403- if ( markerSymbol )
414+ size = markerSymbol->size ();
415+ height = size;
416+ width = size;
417+ if ( mComposerMap && sizeInMapUnits )
404418 {
405- height = markerSymbol->size ();
406- width = markerSymbol->size ();
419+ height *= mmPerMapUnit;
420+ width *= mmPerMapUnit;
421+ markerSymbol->setSize ( width );
407422 }
408423 }
409424
410425 p->save ();
411426 p->translate ( currentXPosition, currentYCoord );
412427 p->scale ( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor );
428+
429+ if ( markerSymbol && sizeInMapUnits )
430+ {
431+ s->setOutputUnit ( QgsSymbolV2::MM );
432+ }
413433 s->drawPreviewIcon ( p, QSize ( width * rasterScaleFactor, height * rasterScaleFactor ) );
434+
435+ if ( markerSymbol && sizeInMapUnits )
436+ {
437+ s->setOutputUnit ( QgsSymbolV2::MapUnit );
438+ markerSymbol->setSize ( size );
439+ }
440+
414441 p->restore ();
415442 currentXPosition += width;
416443 symbolHeight = height;
@@ -609,6 +636,11 @@ bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const
609636 composerLegendElem.setAttribute ( " symbolWidth" , mSymbolWidth );
610637 composerLegendElem.setAttribute ( " symbolHeight" , mSymbolHeight );
611638
639+ if ( mComposerMap )
640+ {
641+ composerLegendElem.setAttribute ( " map" , mComposerMap ->id () );
642+ }
643+
612644 // write model properties
613645 mLegendModel .writeXML ( composerLegendElem, doc );
614646
@@ -659,6 +691,12 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
659691 mSymbolWidth = itemElem.attribute ( " symbolWidth" , " 7.0" ).toDouble ();
660692 mSymbolHeight = itemElem.attribute ( " symbolHeight" , " 14.0" ).toDouble ();
661693
694+ // composer map
695+ if ( !itemElem.attribute ( " map" ).isEmpty () )
696+ {
697+ mComposerMap = mComposition ->getComposerMapById ( itemElem.attribute ( " map" ).toInt () );
698+ }
699+
662700 // read model properties
663701 QDomNodeList modelNodeList = itemElem.elementsByTagName ( " Model" );
664702 if ( modelNodeList.size () > 0 )
@@ -678,3 +716,15 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
678716 emit itemChanged ();
679717 return true ;
680718}
719+
720+ void QgsComposerLegend::setComposerMap ( const QgsComposerMap* map )
721+ {
722+ mComposerMap = map;
723+ QObject::connect ( map, SIGNAL ( destroyed ( QObject* ) ), this , SLOT ( invalidateCurrentMap () ) );
724+ }
725+
726+ void QgsComposerLegend::invalidateCurrentMap ()
727+ {
728+ disconnect ( mComposerMap , SIGNAL ( destroyed ( QObject* ) ), this , SLOT ( invalidateCurrentMap () ) );
729+ mComposerMap = 0 ;
730+ }
0 commit comments