18
18
#include " qgscomposerlegend.h"
19
19
#include " qgsmaplayer.h"
20
20
#include " qgsmaplayerregistry.h"
21
+ #include " qgsmaprenderer.h"
21
22
#include " qgssymbol.h"
22
23
#include < QDomDocument>
23
24
#include < QDomElement>
@@ -91,29 +92,41 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
91
92
92
93
maxXCoord = 2 * mBoxSpace + textWidthMillimeters ( mTitleFont , mTitle );
93
94
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
+
95
104
for ( int i = 0 ; i < numLayerItems; ++i )
96
105
{
97
106
currentLayerItem = rootItem->child ( i );
98
107
if ( currentLayerItem )
99
108
{
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 ))
102
111
{
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 ())
108
114
{
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
+ }
110
123
}
111
- }
112
124
113
- maxXCoord = std::max ( maxXCoord, 2 * mBoxSpace + textWidthMillimeters ( mLayerFont , currentLayerItem->text () ) );
125
+ maxXCoord = std::max ( maxXCoord, 2 * mBoxSpace + textWidthMillimeters ( mLayerFont , currentLayerItem->text () ) );
114
126
115
- // and child items
116
- drawLayerChildItems ( painter, currentLayerItem, currentYCoordinate, maxXCoord );
127
+ // and child items
128
+ drawLayerChildItems ( painter, currentLayerItem, currentYCoordinate, maxXCoord );
129
+ }
117
130
}
118
131
}
119
132
@@ -133,6 +146,17 @@ QSizeF QgsComposerLegend::paintAndDetermineSize( QPainter* painter )
133
146
134
147
size.setHeight ( currentYCoordinate );
135
148
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
+
136
160
return size;
137
161
}
138
162
0 commit comments