46
46
TestCase ,
47
47
unittest ,
48
48
unitTestDataPath ,
49
+ getTempfilePath ,
50
+ renderMapToImage ,
49
51
loadTestFonts ,
50
52
getTestFont ,
51
53
openInBrowserTab
@@ -208,7 +210,9 @@ def getBaseMapSettings(cls):
208
210
ms .setBackgroundColor (QColor (152 , 219 , 249 ))
209
211
ms .setOutputSize (QSize (420 , 280 ))
210
212
ms .setOutputDpi (72 )
211
- ms .setFlag (QgsMapSettings .Antialiasing )
213
+ ms .setFlag (QgsMapSettings .Antialiasing , True )
214
+ ms .setFlag (QgsMapSettings .UseAdvancedEffects , False )
215
+ ms .setFlag (QgsMapSettings .ForceVectorOutput , False ) # no caching?
212
216
ms .setDestinationCrs (crs )
213
217
ms .setCrsTransformEnabled (False )
214
218
ms .setMapUnits (crs .mapUnits ()) # meters
@@ -217,7 +221,7 @@ def getBaseMapSettings(cls):
217
221
218
222
def cloneMapSettings (self , oms ):
219
223
"""
220
- :param oms: QgsMapSettings
224
+ :param QgsMapSettings oms: Other QgsMapSettings
221
225
:rtype: QgsMapSettings
222
226
"""
223
227
ms = QgsMapSettings ()
@@ -292,81 +296,43 @@ def saveControlImage(self, tmpimg=''):
292
296
or 'Vs' in self ._TestGroup ):
293
297
return
294
298
imgpath = self .controlImagePath ()
295
- # print "saveControlImage: {0}".format(imgpath)
296
299
testdir = os .path .dirname (imgpath )
297
300
if not os .path .exists (testdir ):
298
301
os .makedirs (testdir )
299
302
imgbasepath = \
300
303
os .path .join (testdir ,
301
304
os .path .splitext (os .path .basename (imgpath ))[0 ])
305
+ # remove any existing control images
302
306
for f in glob .glob (imgbasepath + '.*' ):
303
307
if os .path .exists (f ):
304
308
os .remove (f )
305
- if tmpimg and os .path .exists (tmpimg ):
306
- shutil .copyfile (tmpimg , imgpath )
307
- else :
308
- print '\n saveControlImage.render(): entered'
309
- print '{0}.{1}' .format (self ._TestGroup , self ._TestFunction )
309
+ qDebug ('Control image for {0}.{1}' .format (self ._TestGroup ,
310
+ self ._TestFunction ))
310
311
312
+ if not tmpimg :
313
+ # TODO: this can be deprecated, when per-base-test-class rendering
314
+ # in checkTest() is verified OK for all classes
315
+ qDebug ('Rendering control to: {0}' .format (imgpath ))
311
316
ms = self ._MapSettings # class settings
312
317
""":type: QgsMapSettings"""
318
+ settings_type = 'Class'
313
319
if self ._TestMapSettings is not None :
314
320
ms = self ._TestMapSettings # per test settings
315
- print 'self._MapSettings...'
316
- print 'ms.layers(): {0}' .format (
317
- [self ._MapRegistry .mapLayer (i ).name () for i in ms .layers ()]
318
- )
319
- print 'ms.outputSize(): {0} x {1}' .format (
320
- ms .outputSize ().width (), ms .outputSize ().height ())
321
- print 'ms.outputDpi(): {0}' .format (ms .outputDpi ())
322
- print 'ms.mapUnits(): {0}' .format (ms .mapUnits ())
323
- print 'ms.extent(): {0}' .format (ms .extent ().toString ())
324
- print 'ms.hasCrsTransformEnabled(): {0}' .format (
325
- ms .hasCrsTransformEnabled ())
326
- print 'ms.destinationCrs(): {0}' .format (
327
- ms .destinationCrs ().authid ())
328
-
329
- # pal = QgsPalLabeling()
330
- pal = self ._Pal .clone () # or custom settings are lost
331
- pal .init (ms )
332
- r = QgsMapRenderer ()
333
- r .setLabelingEngine (pal )
334
-
335
- # this seems too redundant
336
- r .setOutputSize (ms .outputSize (), ms .outputDpi ())
337
- r .setMapUnits (ms .mapUnits ())
338
- r .setExtent (ms .extent ())
339
- r .setProjectionsEnabled (ms .hasCrsTransformEnabled ())
340
- r .setDestinationCrs (ms .destinationCrs ())
341
- r .setLayerSet (ms .layers ())
342
-
343
- ctx = r .rendererContext ()
344
- ctx .setDrawEditingInformation (
345
- ms .testFlag (QgsMapSettings .DrawEditingInfo ))
346
- ctx .setForceVectorOutput (
347
- ms .testFlag (QgsMapSettings .ForceVectorOutput ))
348
- ctx .setUseAdvancedEffects (
349
- ms .testFlag (QgsMapSettings .UseAdvancedEffects ))
350
-
351
- image = QImage (ms .outputSize (), QImage .Format_ARGB32 )
352
- image .fill (ms .backgroundColor ().rgb ())
353
- image .setDotsPerMeterX (ms .outputDpi () / 25.4 * 1000 )
354
- image .setDotsPerMeterY (ms .outputDpi () / 25.4 * 1000 )
355
-
356
- p = QPainter (image )
357
- r .render (p )
358
- p .end ()
359
-
360
- if not image .save (imgpath , 'png' ):
361
- os .unlink (imgpath )
362
-
363
- # delete extraneous world file (always generated)
364
- # wrld_file = imgbasepath + '.PNGw'
365
- # if os.path.exists(wrld_file):
366
- # os.remove(wrld_file)
367
-
368
- if not os .path .exists (imgpath ):
369
- raise OSError ('Control image not created: {0}' .format (imgpath ))
321
+ settings_type = 'Test'
322
+ qDebug ('MapSettings type: {0}' .format (settings_type ))
323
+
324
+ img = renderMapToImage (ms , parallel = False )
325
+ """:type: QImage"""
326
+ tmpimg = getTempfilePath ('png' )
327
+ if not img .save (tmpimg , 'png' ):
328
+ os .unlink (tmpimg )
329
+ raise OSError ('Control not created for: {0}' .format (imgpath ))
330
+
331
+ if tmpimg and os .path .exists (tmpimg ):
332
+ qDebug ('Copying control to: {0}' .format (imgpath ))
333
+ shutil .copyfile (tmpimg , imgpath )
334
+ else :
335
+ raise OSError ('Control not copied to: {0}' .format (imgpath ))
370
336
371
337
def renderCheck (self , mismatch = 0 , colortol = 0 , imgpath = '' , grpprefix = '' ):
372
338
"""Check rendered map canvas or existing image against control image
@@ -385,7 +351,10 @@ def renderCheck(self, mismatch=0, colortol=0, imgpath='', grpprefix=''):
385
351
chk .setControlPathPrefix ('expected_' + grpprefix )
386
352
chk .setControlName (self ._Test )
387
353
chk .setColorTolerance (colortol )
388
- chk .setMapSettings (self ._MapSettings )
354
+ ms = self ._MapSettings # class settings
355
+ if self ._TestMapSettings is not None :
356
+ ms = self ._TestMapSettings # per test settings
357
+ chk .setMapSettings (ms )
389
358
# noinspection PyUnusedLocal
390
359
res = False
391
360
if imgpath :
0 commit comments