1818#include " qgscomposerlegend.h"
1919#include " qgsmaplayer.h"
2020#include " qgsmaplayerregistry.h"
21+ #include " qgsmaprenderer.h"
2122#include " qgssymbol.h"
2223#include < QDomDocument>
2324#include < QDomElement>
@@ -91,29 +92,41 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
9192
9293 maxXCoord = 2 * mBoxSpace + textWidthMillimeters ( mTitleFont , mTitle );
9394
94- // draw layer items
95+ // draw only visible layer items
96+ QgsMapRenderer* theMapRenderer = mComposition ->mapRenderer ();
97+ QStringList visibleLayerIds;
98+ if (theMapRenderer)
99+ {
100+ visibleLayerIds = theMapRenderer->layerSet ();
101+ }
102+
103+
95104 for ( int i = 0 ; i < numLayerItems; ++i )
96105 {
97106 currentLayerItem = rootItem->child ( i );
98107 if ( currentLayerItem )
99108 {
100- // Let the user omit the layer title item by having an empty layer title string
101- if (!currentLayerItem-> text (). isEmpty ( ))
109+ QString currentLayerId = currentLayerItem-> data (). toString ();
110+ if (visibleLayerIds. contains (currentLayerId ))
102111 {
103- currentYCoordinate += mLayerSpace ;
104- currentYCoordinate += fontAscentMillimeters ( mLayerFont );
105-
106- // draw layer Item
107- if ( painter )
112+ // Let the user omit the layer title item by having an empty layer title string
113+ if (!currentLayerItem->text ().isEmpty ())
108114 {
109- drawText ( painter, mBoxSpace , currentYCoordinate, currentLayerItem->text (), mLayerFont );
115+ currentYCoordinate += mLayerSpace ;
116+ currentYCoordinate += fontAscentMillimeters ( mLayerFont );
117+
118+ // draw layer Item
119+ if ( painter )
120+ {
121+ drawText ( painter, mBoxSpace , currentYCoordinate, currentLayerItem->text (), mLayerFont );
122+ }
110123 }
111- }
112124
113- maxXCoord = std::max ( maxXCoord, 2 * mBoxSpace + textWidthMillimeters ( mLayerFont , currentLayerItem->text () ) );
125+ maxXCoord = std::max ( maxXCoord, 2 * mBoxSpace + textWidthMillimeters ( mLayerFont , currentLayerItem->text () ) );
114126
115- // and child items
116- drawLayerChildItems ( painter, currentLayerItem, currentYCoordinate, maxXCoord );
127+ // and child items
128+ drawLayerChildItems ( painter, currentLayerItem, currentYCoordinate, maxXCoord );
129+ }
117130 }
118131 }
119132
@@ -133,6 +146,17 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
133146
134147 size.setHeight ( currentYCoordinate );
135148 size.setWidth ( maxXCoord );
149+
150+ // adjust box if width or height is to small
151+ if (painter && currentYCoordinate > rect ().width ())
152+ {
153+ setSceneRect ( QRectF ( transform ().dx (), transform ().dy (), rect ().width (), currentYCoordinate));
154+ }
155+ if (painter && maxXCoord > rect ().height ())
156+ {
157+ setSceneRect ( QRectF ( transform ().dx (), transform ().dy (), maxXCoord, rect ().height ()));
158+ }
159+
136160 return size;
137161}
138162
0 commit comments