22
22
QgsVectorLayer ,
23
23
QgsMapLayerRegistry ,
24
24
QgsMarkerSymbolV2 ,
25
- QgsSingleSymbolRendererV2
25
+ QgsSingleSymbolRendererV2 ,
26
+ QgsRectangle
26
27
)
27
28
from qgis .testing import (start_app ,
28
29
unittest
37
38
38
39
class TestQgsComposerLegend (unittest .TestCase ):
39
40
40
- def __init__ (self , methodName ):
41
- """Run once on class initialization."""
42
- unittest .TestCase .__init__ (self , methodName )
43
- point_path = os .path .join (TEST_DATA_DIR , 'points.shp' )
44
- self .point_layer = QgsVectorLayer (point_path , 'points' , 'ogr' )
45
- QgsMapLayerRegistry .instance ().addMapLayers ([self .point_layer ])
46
-
47
41
def testInitialSizeSymbolMapUnits (self ):
48
42
"""Test initial size of legend with a symbol size in map units"""
49
43
44
+ point_path = os .path .join (TEST_DATA_DIR , 'points.shp' )
45
+ point_layer = QgsVectorLayer (point_path , 'points' , 'ogr' )
46
+ QgsMapLayerRegistry .instance ().addMapLayers ([point_layer ])
47
+
50
48
marker_symbol = QgsMarkerSymbolV2 .createSimple ({'color' : '#ff0000' , 'outline_style' : 'no' , 'size' : '5' , 'size_unit' : 'MapUnit' })
51
49
52
- self . point_layer .setRendererV2 (QgsSingleSymbolRendererV2 (marker_symbol ))
50
+ point_layer .setRendererV2 (QgsSingleSymbolRendererV2 (marker_symbol ))
53
51
54
52
s = QgsMapSettings ()
55
- s .setLayers ([self . point_layer .id ()])
53
+ s .setLayers ([point_layer .id ()])
56
54
s .setCrsTransformEnabled (False )
57
55
composition = QgsComposition (s )
58
56
composition .setPaperSize (297 , 210 )
59
57
60
58
composer_map = QgsComposerMap (composition , 20 , 20 , 80 , 80 )
61
59
composer_map .setFrameEnabled (True )
62
60
composition .addComposerMap (composer_map )
63
- composer_map .setNewExtent (self . point_layer .extent ())
61
+ composer_map .setNewExtent (point_layer .extent ())
64
62
65
63
legend = QgsComposerLegend (composition )
66
64
legend .setSceneRect (QRectF (120 , 20 , 80 , 80 ))
@@ -77,6 +75,46 @@ def testInitialSizeSymbolMapUnits(self):
77
75
result , message = checker .testComposition ()
78
76
self .assertTrue (result , message )
79
77
78
+ QgsMapLayerRegistry .instance ().removeMapLayers ([point_layer ])
79
+
80
+ def testResizeWithMapContent (self ):
81
+ """Test test legend resizes to match map content"""
82
+
83
+ point_path = os .path .join (TEST_DATA_DIR , 'points.shp' )
84
+ point_layer = QgsVectorLayer (point_path , 'points' , 'ogr' )
85
+ QgsMapLayerRegistry .instance ().addMapLayers ([point_layer ])
86
+
87
+ s = QgsMapSettings ()
88
+ s .setLayers ([point_layer .id ()])
89
+ s .setCrsTransformEnabled (False )
90
+ composition = QgsComposition (s )
91
+ composition .setPaperSize (297 , 210 )
92
+
93
+ composer_map = QgsComposerMap (composition , 20 , 20 , 80 , 80 )
94
+ composer_map .setFrameEnabled (True )
95
+ composition .addComposerMap (composer_map )
96
+ composer_map .setNewExtent (point_layer .extent ())
97
+
98
+ legend = QgsComposerLegend (composition )
99
+ legend .setSceneRect (QRectF (120 , 20 , 80 , 80 ))
100
+ legend .setFrameEnabled (True )
101
+ legend .setFrameOutlineWidth (2 )
102
+ legend .setBackgroundColor (QColor (200 , 200 , 200 ))
103
+ legend .setTitle ('' )
104
+ legend .setLegendFilterByMapEnabled (True )
105
+ composition .addComposerLegend (legend )
106
+ legend .setComposerMap (composer_map )
107
+
108
+ composer_map .setNewExtent (QgsRectangle (- 102.51 , 41.16 , - 102.36 , 41.30 ))
109
+
110
+ checker = QgsCompositionChecker (
111
+ 'composer_legend_size_content' , composition )
112
+ checker .setControlPathPrefix ("composer_legend" )
113
+ result , message = checker .testComposition ()
114
+ self .assertTrue (result , message )
115
+
116
+ QgsMapLayerRegistry .instance ().removeMapLayers ([point_layer ])
117
+
80
118
81
119
if __name__ == '__main__' :
82
120
unittest .main ()
0 commit comments