@@ -94,44 +94,50 @@ def setUp(self):
94
94
95
95
def _set_up_composition (self , width , height , dpi ):
96
96
# set up composition and add map
97
- # TODO: how to keep embedded map from being anti-aliased twice?
98
- # self._MapSettings .setFlag(QgsMapSettings.Antialiasing, False )
99
- # self._MapSettings .setFlag(QgsMapSettings.UseAdvancedEffects , True)
97
+ self . _TestMapSettings . setFlag ( QgsMapSettings . Antialiasing , True )
98
+ self ._TestMapSettings .setFlag (QgsMapSettings .UseAdvancedEffects , True )
99
+ self ._TestMapSettings .setFlag (QgsMapSettings .ForceVectorOutput , True )
100
100
self ._c = QgsComposition (self ._TestMapSettings )
101
101
""":type: QgsComposition"""
102
+ # self._c.setUseAdvancedEffects(False)
102
103
self ._c .setPrintResolution (dpi )
103
104
# 600 x 400 px = 211.67 x 141.11 mm @ 72 dpi
104
- # TODO: figure out why this doesn't work and needs fudging
105
- # probably need sets of fudgyness per dpi group (72, 150, 300)?
106
- paperw = round ((width * 25.4 / dpi ) + 0.05 , 0 )
107
- paperh = round ((height * 25.4 / dpi ) + 0.05 , 1 )
105
+ paperw = width * 25.4 / dpi
106
+ paperh = height * 25.4 / dpi
108
107
self ._c .setPaperSize (paperw , paperh )
109
- self ._cmap = QgsComposerMap (
110
- self ._c , 0 , 0 , self ._c .paperWidth (), self ._c .paperHeight ())
108
+ # NOTE: do not use QgsComposerMap(self._c, 0, 0, paperw, paperh) since
109
+ # it only takes integers as parameters and the composition will grow
110
+ # larger based upon union of item scene rectangles and a slight buffer
111
+ # see end of QgsComposition::compositionBounds()
112
+ # add map as small graphics item first, then set its scene QRectF later
113
+ self ._cmap = QgsComposerMap (self ._c , 10 , 10 , 10 , 10 )
111
114
""":type: QgsComposerMap"""
115
+ self ._cmap .setPreviewMode (QgsComposerMap .Render )
112
116
self ._cmap .setFrameEnabled (False )
113
- self ._cmap .setNewExtent (self .aoiExtent ())
114
117
self ._c .addComposerMap (self ._cmap )
118
+ # now expand map to fill page and set its extent
119
+ self ._cmap .setSceneRect (QRectF (0 , 0 , paperw , paperw ))
120
+ self ._cmap .setNewExtent (self .aoiExtent ())
121
+ # self._cmap.updateCachedImage()
115
122
self ._c .setPlotStyle (QgsComposition .Print )
116
123
117
124
# noinspection PyUnusedLocal
118
125
def _get_composer_image (self , width , height , dpi ):
119
- image = QImage (QSize (width , height ), QImage .Format_ARGB32 )
120
- image .fill (QColor (152 , 219 , 249 ).rgb ())
121
- image .setDotsPerMeterX (dpi / 25.4 * 1000 )
122
- image .setDotsPerMeterY (dpi / 25.4 * 1000 )
123
-
124
- p = QPainter (image )
125
- p .setRenderHint (QPainter .Antialiasing , True )
126
- p .setRenderHint (QPainter .HighQualityAntialiasing , True )
127
- self ._c .renderPage (p , 0 )
128
- p .end ()
129
-
130
- # image = self._c.printPageAsRaster(0)
131
- # """:type: QImage"""
126
+ # image = QImage(QSize(width, height), QImage.Format_ARGB32)
127
+ # image.fill(QColor(152, 219, 249).rgb())
128
+ # image.setDotsPerMeterX(dpi / 25.4 * 1000)
129
+ # image.setDotsPerMeterY(dpi / 25.4 * 1000)
130
+ #
131
+ # p = QPainter(image)
132
+ # p.setRenderHint(QPainter.Antialiasing, False )
133
+ # p.setRenderHint(QPainter.HighQualityAntialiasing, False )
134
+ # self._c.renderPage(p, 0)
135
+ # p.end()
136
+
137
+ image = self ._c .printPageAsRaster (0 )
138
+ """:type: QImage"""
132
139
133
140
if image .isNull ():
134
- # something went pear-shaped
135
141
return False , ''
136
142
137
143
filepath = getTempfilePath ('png' )
@@ -144,31 +150,23 @@ def _get_composer_image(self, width, height, dpi):
144
150
145
151
def _get_composer_svg_image (self , width , height , dpi ):
146
152
# from qgscomposer.cpp, QgsComposer::on_mActionExportAsSVG_triggered,
147
- # line 1909, near end of function
153
+ # near end of function
148
154
svgpath = getTempfilePath ('svg' )
149
155
temp_size = os .path .getsize (svgpath )
150
156
151
157
svg_g = QSvgGenerator ()
152
158
# noinspection PyArgumentList
153
159
svg_g .setTitle (QgsProject .instance ().title ())
154
160
svg_g .setFileName (svgpath )
155
- # width and height in pixels
156
- # svg_w = int(self._c.paperWidth() * self._c.printResolution() / 25.4)
157
- # svg_h = int(self._c.paperHeight() * self._c.printResolution() / 25.4)
158
- svg_w = width
159
- svg_h = height
160
- svg_g .setSize (QSize (svg_w , svg_h ))
161
- svg_g .setViewBox (QRect (0 , 0 , svg_w , svg_h ))
162
- # because the rendering is done in mm, convert the dpi
163
- # svg_g.setResolution(self._c.printResolution())
161
+ svg_g .setSize (QSize (width , height ))
162
+ svg_g .setViewBox (QRect (0 , 0 , width , height ))
164
163
svg_g .setResolution (dpi )
165
164
166
165
sp = QPainter (svg_g )
167
166
self ._c .renderPage (sp , 0 )
168
167
sp .end ()
169
168
170
169
if temp_size == os .path .getsize (svgpath ):
171
- # something went pear-shaped
172
170
return False , ''
173
171
174
172
image = QImage (width , height , QImage .Format_ARGB32 )
@@ -211,7 +209,6 @@ def _get_composer_pdf_image(self, width, height, dpi):
211
209
pdf_p .end ()
212
210
213
211
if temp_size == os .path .getsize (pdfpath ):
214
- # something went pear-shaped
215
212
return False , ''
216
213
217
214
filepath = getTempfilePath ('png' )
@@ -260,10 +257,11 @@ def get_composer_output(self, kind):
260
257
def checkTest (self , ** kwargs ):
261
258
self .lyr .writeToLayer (self .layer )
262
259
res_m , self ._TestImage = self .get_composer_output (self ._TestKind )
263
- self .saveControlImage (self ._TestImage )
264
260
self .assertTrue (res_m , 'Failed to retrieve/save output from composer' )
261
+ self .saveControlImage (self ._TestImage )
265
262
mismatch = self ._Mismatch
266
- if self ._TestGroup in self ._Mismatches :
263
+ if (self ._TestGroup in self ._Mismatches
264
+ and 'PAL_NO_MISMATCH' not in os .environ ):
267
265
mismatch = self ._Mismatches [self ._TestGroup ]
268
266
self .assertTrue (* self .renderCheck (mismatch = mismatch ,
269
267
imgpath = self ._TestImage ))
@@ -295,15 +293,39 @@ def setUp(self):
295
293
self .configTest ('pal_canvas' , 'sp' )
296
294
297
295
296
+ class TestComposerSvgPoint (TestComposerPointBase , TestPointBase ):
297
+
298
+ def setUp (self ):
299
+ """Run before each test."""
300
+ super (TestComposerSvgPoint , self ).setUp ()
301
+ self ._TestKind = OutputKind .Svg
302
+ self .configTest ('pal_composer' , 'sp_svg' )
303
+
304
+
305
+ class TestComposerSvgVsComposerPoint (TestComposerPointBase , TestPointBase ):
306
+ """
307
+ Compare only to composer image, which is already compared to canvas point
308
+ """
309
+ def setUp (self ):
310
+ """Run before each test."""
311
+ super (TestComposerSvgVsComposerPoint , self ).setUp ()
312
+ self ._TestKind = OutputKind .Svg
313
+ self .configTest ('pal_composer' , 'sp_img' )
314
+
315
+
298
316
if __name__ == '__main__' :
299
317
# NOTE: unless PAL_SUITE env var is set all test class methods will be run
300
318
# SEE: test_qgspallabeling_tests.suiteTests() to define suite
301
319
st = suiteTests ()
302
320
sp_i = ['TestComposerImagePoint.' + t for t in st ['sp_suite' ]]
303
321
sp_ivs = ['TestComposerImageVsCanvasPoint.' + t for t in st ['sp_vs_suite' ]]
322
+ sp_s = ['TestComposerSvgPoint.' + t for t in st ['sp_suite' ]]
323
+ sp_svs = ['TestComposerSvgVsComposerPoint.' + t for t in st ['sp_vs_suite' ]]
304
324
suite = []
305
325
# extended separately for finer control of PAL_SUITE (comment-out undesired)
306
326
suite .extend (sp_i )
307
327
suite .extend (sp_ivs )
328
+ suite .extend (sp_s )
329
+ suite .extend (sp_svs )
308
330
res = runSuite (sys .modules [__name__ ], suite )
309
331
sys .exit (not res .wasSuccessful ())
0 commit comments