Skip to content

Commit b88f9b5

Browse files
committedJan 8, 2018
Port pal composer test to labels
1 parent d372799 commit b88f9b5

File tree

5 files changed

+89
-79
lines changed

5 files changed

+89
-79
lines changed
 

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ ADD_PYTHON_TEST(PyQgsOptional test_qgsoptional.py)
119119
ADD_PYTHON_TEST(PyQgsOwsConnection test_qgsowsconnection.py)
120120
ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
121121
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
122-
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)
122+
ADD_PYTHON_TEST(PyQgsPalLabelingLayout test_qgspallabeling_layout.py)
123123
ADD_PYTHON_TEST(PyQgsPalLabelingPlacement test_qgspallabeling_placement.py)
124124
ADD_PYTHON_TEST(PyQgsPanelWidget test_qgspanelwidget.py)
125125
ADD_PYTHON_TEST(PyQgsPanelWidgetStack test_qgspanelwidgetstack.py)

‎tests/src/python/test_qgslayoutatlas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def testCase(self):
8585
QgsProject.instance().addMapLayers([mVectorLayer])
8686
self.layers = [mVectorLayer]
8787

88-
# create composition with composer map
88+
# create layout with layout map
8989

9090
# select epsg:2154
9191
crs = QgsCoordinateReferenceSystem()
@@ -576,7 +576,7 @@ def rotation_test(self):
576576
polygonLayer.dataProvider().addFeatures([poly])
577577
QgsProject.instance().addMapLayer(polygonLayer)
578578

579-
# Recreating the composer locally
579+
# Recreating the layout locally
580580
composition = QgsPrintLayout(QgsProject.instance())
581581
composition.initializeDefaults()
582582

‎tests/src/python/test_qgslayoutmap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def __init__(self, methodName):
7575
# assert pipe.set(rasterRenderer), 'Cannot set pipe renderer'
7676
QgsProject.instance().addMapLayers([self.raster_layer, self.vector_layer])
7777

78-
# create composition with composer map
78+
# create layout with layout map
7979
self.layout = QgsLayout(QgsProject.instance())
8080
self.layout.initializeDefaults()
8181
self.map = QgsLayoutItemMap(self.layout)
@@ -166,7 +166,7 @@ def testOverviewMapCenter(self):
166166
assert myTestResult, myMessage
167167

168168
def testMapCrs(self):
169-
# create composition with composer map
169+
# create layout with layout map
170170
map_settings = QgsMapSettings()
171171
map_settings.setLayers([self.vector_layer])
172172
layout = QgsLayout(QgsProject.instance())

‎tests/src/python/test_qgspallabeling_composer.py renamed to ‎tests/src/python/test_qgspallabeling_layout.py

Lines changed: 83 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
2-
"""QGIS unit tests for QgsPalLabeling: label rendering output via composer
2+
"""QGIS unit tests for QgsPalLabeling: label rendering output via layout
33
4-
From build dir, run: ctest -R PyQgsPalLabelingComposer -V
4+
From build dir, run: ctest -R PyQgsPalLabelingLayout -V
55
66
See <qgis-src-dir>/tests/testdata/labeling/README.rst for description.
77
@@ -28,7 +28,14 @@
2828
from qgis.PyQt.QtPrintSupport import QPrinter
2929
from qgis.PyQt.QtSvg import QSvgRenderer, QSvgGenerator
3030

31-
from qgis.core import QgsComposition, QgsMapSettings, QgsProject, QgsComposerMap, QgsVectorLayerSimpleLabeling
31+
from qgis.core import (QgsLayout,
32+
QgsLayoutItemPage,
33+
QgsLayoutSize,
34+
QgsLayoutItemMap,
35+
QgsLayoutExporter,
36+
QgsMapSettings,
37+
QgsProject,
38+
QgsVectorLayerSimpleLabeling)
3239

3340

3441
from utilities import (
@@ -70,7 +77,7 @@ class OutputKind():
7077

7178

7279
# noinspection PyShadowingNames
73-
class TestComposerBase(TestQgsPalLabeling):
80+
class TestLayoutBase(TestQgsPalLabeling):
7481

7582
layer = None
7683
""":type: QgsVectorLayer"""
@@ -94,7 +101,7 @@ def tearDownClass(cls):
94101

95102
def setUp(self):
96103
"""Run before each test."""
97-
super(TestComposerBase, self).setUp()
104+
super(TestLayoutBase, self).setUp()
98105
self._TestImage = ''
99106
# ensure per test map settings stay encapsulated
100107
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
@@ -104,35 +111,37 @@ def setUp(self):
104111
self._ColorTols.clear()
105112

106113
def _set_up_composition(self, width, height, dpi, engine_settings):
107-
# set up composition and add map
108-
self._c = QgsComposition(QgsProject.instance())
109-
""":type: QgsComposition"""
114+
# set up layout and add map
115+
self._c = QgsLayout(QgsProject.instance())
116+
""":type: QgsLayout"""
110117
# self._c.setUseAdvancedEffects(False)
111-
self._c.setPrintResolution(dpi)
118+
self._c.renderContext().setDpi(dpi)
112119
# 600 x 400 px = 211.67 x 141.11 mm @ 72 dpi
113120
paperw = width * 25.4 / dpi
114121
paperh = height * 25.4 / dpi
115-
self._c.setPaperSize(paperw, paperh)
116-
# NOTE: do not use QgsComposerMap(self._c, 0, 0, paperw, paperh) since
122+
page = QgsLayoutItemPage(self._c)
123+
page.attemptResize(QgsLayoutSize(paperw,paperh))
124+
self._c.pageCollection().addPage(page)
125+
# NOTE: do not use QgsLayoutItemMap(self._c, 0, 0, paperw, paperh) since
117126
# it only takes integers as parameters and the composition will grow
118127
# larger based upon union of item scene rectangles and a slight buffer
119128
# see end of QgsComposition::compositionBounds()
120129
# add map as small graphics item first, then set its scene QRectF later
121-
self._cmap = QgsComposerMap(self._c, 10, 10, 10, 10)
122-
""":type: QgsComposerMap"""
130+
self._cmap = QgsLayoutItemMap(self._c)
131+
self._cmap.attemptSetSceneRect(QRectF(10, 10, 10, 10))
132+
""":type: QgsLayoutItemMap"""
123133
self._cmap.setFrameEnabled(False)
124134
self._cmap.setLayers(self._TestMapSettings.layers())
125-
self._c.addComposerMap(self._cmap)
135+
self._c.addLayoutItem(self._cmap)
126136
# now expand map to fill page and set its extent
127-
self._cmap.setSceneRect(QRectF(0, 0, paperw, paperw))
128-
self._cmap.setNewExtent(self.aoiExtent())
137+
self._cmap.attemptSetSceneRect(QRectF(0, 0, paperw, paperw))
138+
self._cmap.setExtent(self.aoiExtent())
129139
# self._cmap.updateCachedImage()
130-
self._c.setPlotStyle(QgsComposition.Print)
131140
# composition takes labeling engine settings from project
132141
QgsProject.instance().setLabelingEngineSettings(engine_settings)
133142

134143
# noinspection PyUnusedLocal
135-
def _get_composer_image(self, width, height, dpi):
144+
def _get_layout_image(self, width, height, dpi):
136145
image = QImage(QSize(width, height),
137146
self._TestMapSettings.outputImageFormat())
138147
image.fill(QColor(152, 219, 249).rgb())
@@ -144,7 +153,8 @@ def _get_composer_image(self, width, height, dpi):
144153
QPainter.Antialiasing,
145154
self._TestMapSettings.testFlag(QgsMapSettings.Antialiasing)
146155
)
147-
self._c.renderPage(p, 0)
156+
exporter=QgsLayoutExporter(self._c)
157+
exporter.renderPage(p, 0)
148158
p.end()
149159

150160
# image = self._c.printPageAsRaster(0)
@@ -161,9 +171,7 @@ def _get_composer_image(self, width, height, dpi):
161171

162172
return res, filepath
163173

164-
def _get_composer_svg_image(self, width, height, dpi):
165-
# from qgscomposer.cpp, QgsComposer::on_mActionExportAsSVG_triggered,
166-
# near end of function
174+
def _get_layout_svg_image(self, width, height, dpi):
167175
svgpath = getTempfilePath('svg')
168176
temp_size = os.path.getsize(svgpath)
169177

@@ -176,7 +184,8 @@ def _get_composer_svg_image(self, width, height, dpi):
176184
svg_g.setResolution(dpi)
177185

178186
sp = QPainter(svg_g)
179-
self._c.renderPage(sp, 0)
187+
exporter = QgsLayoutExporter(self._c)
188+
exporter.renderPage(sp, 0)
180189
sp.end()
181190

182191
if temp_size == os.path.getsize(svgpath):
@@ -206,24 +215,25 @@ def _get_composer_svg_image(self, width, height, dpi):
206215

207216
return res, filepath
208217

209-
def _get_composer_pdf_image(self, width, height, dpi):
218+
def _get_layout_pdf_image(self, width, height, dpi):
210219
pdfpath = getTempfilePath('pdf')
211220
temp_size = os.path.getsize(pdfpath)
212221

213222
p = QPrinter()
214223
p.setOutputFormat(QPrinter.PdfFormat)
215224
p.setOutputFileName(pdfpath)
216-
p.setPaperSize(QSizeF(self._c.paperWidth(), self._c.paperHeight()),
225+
p.setPaperSize(QSizeF(self._c.pageCollection().page(0).sizeWithUnits().width(), self._c.pageCollection().page(0).sizeWithUnits().height()),
217226
QPrinter.Millimeter)
218227
p.setFullPage(True)
219228
p.setColorMode(QPrinter.Color)
220-
p.setResolution(self._c.printResolution())
229+
p.setResolution(self._c.renderContext().dpi())
221230

222231
pdf_p = QPainter(p)
223232
# page_mm = p.pageRect(QPrinter.Millimeter)
224233
# page_px = p.pageRect(QPrinter.DevicePixel)
225234
# self._c.render(pdf_p, page_px, page_mm)
226-
self._c.renderPage(pdf_p, 0)
235+
exporter = QgsLayoutExporter(self._c)
236+
exporter.renderPage(pdf_p, 0)
227237
pdf_p.end()
228238

229239
if temp_size == os.path.getsize(pdfpath):
@@ -254,13 +264,13 @@ def _get_composer_pdf_image(self, width, height, dpi):
254264
else:
255265
return False, ''
256266

257-
qDebug("_get_composer_pdf_image call: {0}".format(' '.join(call)))
267+
qDebug("_get_layout_pdf_image call: {0}".format(' '.join(call)))
258268
res = False
259269
try:
260270
subprocess.check_call(call)
261271
res = True
262272
except subprocess.CalledProcessError as e:
263-
qDebug("_get_composer_pdf_image failed!\n"
273+
qDebug("_get_layout_pdf_image failed!\n"
264274
"cmd: {0}\n"
265275
"returncode: {1}\n"
266276
"message: {2}".format(e.cmd, e.returncode, e.message))
@@ -271,17 +281,17 @@ def _get_composer_pdf_image(self, width, height, dpi):
271281

272282
return res, filepath
273283

274-
def get_composer_output(self, kind):
284+
def get_layout_output(self, kind):
275285
ms = self._TestMapSettings
276286
osize = ms.outputSize()
277287
width, height, dpi = osize.width(), osize.height(), ms.outputDpi()
278288
self._set_up_composition(width, height, dpi, ms.labelingEngineSettings())
279289
if kind == OutputKind.Svg:
280-
return self._get_composer_svg_image(width, height, dpi)
290+
return self._get_layout_svg_image(width, height, dpi)
281291
elif kind == OutputKind.Pdf:
282-
return self._get_composer_pdf_image(width, height, dpi)
292+
return self._get_layout_pdf_image(width, height, dpi)
283293
else: # OutputKind.Img
284-
return self._get_composer_image(width, height, dpi)
294+
return self._get_layout_image(width, height, dpi)
285295

286296
# noinspection PyUnusedLocal
287297
def checkTest(self, **kwargs):
@@ -296,8 +306,8 @@ def checkTest(self, **kwargs):
296306
qDebug('MapSettings type: {0}'.format(settings_type))
297307
qDebug(mapSettingsString(ms))
298308

299-
res_m, self._TestImage = self.get_composer_output(self._TestKind)
300-
self.assertTrue(res_m, 'Failed to retrieve/save output from composer')
309+
res_m, self._TestImage = self.get_layout_output(self._TestKind)
310+
self.assertTrue(res_m, 'Failed to retrieve/save output from layout')
301311
self.saveControlImage(self._TestImage)
302312
mismatch = 0
303313
if 'PAL_NO_MISMATCH' not in os.environ:
@@ -315,146 +325,146 @@ def checkTest(self, **kwargs):
315325
imgpath=self._TestImage))
316326

317327

318-
class TestComposerPointBase(TestComposerBase):
328+
class TestLayoutPointBase(TestLayoutBase):
319329

320330
@classmethod
321331
def setUpClass(cls):
322-
TestComposerBase.setUpClass()
332+
TestLayoutBase.setUpClass()
323333
cls.layer = TestQgsPalLabeling.loadFeatureLayer('point')
324334

325335

326-
class TestComposerImagePoint(TestComposerPointBase, TestPointBase):
336+
class TestLayoutImagePoint(TestLayoutPointBase, TestPointBase):
327337

328338
def setUp(self):
329339
"""Run before each test."""
330-
super(TestComposerImagePoint, self).setUp()
340+
super(TestLayoutImagePoint, self).setUp()
331341
self._TestKind = OutputKind.Img
332342
self.configTest('pal_composer', 'sp_img')
333343

334344

335-
class TestComposerImageVsCanvasPoint(TestComposerPointBase, TestPointBase):
345+
class TestLayoutImageVsCanvasPoint(TestLayoutPointBase, TestPointBase):
336346

337347
def setUp(self):
338348
"""Run before each test."""
339-
super(TestComposerImageVsCanvasPoint, self).setUp()
349+
super(TestLayoutImageVsCanvasPoint, self).setUp()
340350
self._TestKind = OutputKind.Img
341351
self.configTest('pal_canvas', 'sp')
342352

343353

344-
class TestComposerSvgPoint(TestComposerPointBase, TestPointBase):
354+
class TestLayoutSvgPoint(TestLayoutPointBase, TestPointBase):
345355

346356
def setUp(self):
347357
"""Run before each test."""
348-
super(TestComposerSvgPoint, self).setUp()
358+
super(TestLayoutSvgPoint, self).setUp()
349359
self._TestKind = OutputKind.Svg
350360
self.configTest('pal_composer', 'sp_svg')
351361

352362

353-
class TestComposerSvgVsComposerPoint(TestComposerPointBase, TestPointBase):
363+
class TestLayoutSvgVsLayoutPoint(TestLayoutPointBase, TestPointBase):
354364

355365
"""
356-
Compare only to composer image, which is already compared to canvas point
366+
Compare only to layout image, which is already compared to canvas point
357367
"""
358368

359369
def setUp(self):
360370
"""Run before each test."""
361-
super(TestComposerSvgVsComposerPoint, self).setUp()
371+
super(TestLayoutSvgVsLayoutPoint, self).setUp()
362372
self._TestKind = OutputKind.Svg
363373
self.configTest('pal_composer', 'sp_img')
364374
self._ColorTol = 4
365375

366376

367-
class TestComposerPdfPoint(TestComposerPointBase, TestPointBase):
377+
class TestLayoutPdfPoint(TestLayoutPointBase, TestPointBase):
368378

369379
def setUp(self):
370380
"""Run before each test."""
371-
super(TestComposerPdfPoint, self).setUp()
381+
super(TestLayoutPdfPoint, self).setUp()
372382
self._TestKind = OutputKind.Pdf
373383
self.configTest('pal_composer', 'sp_pdf')
374384

375385

376-
class TestComposerPdfVsComposerPoint(TestComposerPointBase, TestPointBase):
386+
class TestLayoutPdfVsLayoutPoint(TestLayoutPointBase, TestPointBase):
377387

378388
"""
379-
Compare only to composer image, which is already compared to canvas point
389+
Compare only to layout image, which is already compared to canvas point
380390
"""
381391

382392
def setUp(self):
383393
"""Run before each test."""
384-
super(TestComposerPdfVsComposerPoint, self).setUp()
394+
super(TestLayoutPdfVsLayoutPoint, self).setUp()
385395
self._TestKind = OutputKind.Pdf
386396
self.configTest('pal_composer', 'sp_img')
387397
self._Mismatch = 50
388398
self._ColorTol = 18
389399

390400

391-
class TestComposerLineBase(TestComposerBase):
401+
class TestLayoutLineBase(TestLayoutBase):
392402

393403
@classmethod
394404
def setUpClass(cls):
395-
TestComposerBase.setUpClass()
405+
TestLayoutBase.setUpClass()
396406
cls.layer = TestQgsPalLabeling.loadFeatureLayer('line')
397407

398408

399-
class TestComposerImageLine(TestComposerLineBase, TestLineBase):
409+
class TestLayoutImageLine(TestLayoutLineBase, TestLineBase):
400410

401411
def setUp(self):
402412
"""Run before each test."""
403-
super(TestComposerImageLine, self).setUp()
413+
super(TestLayoutImageLine, self).setUp()
404414
self._TestKind = OutputKind.Img
405415
self.configTest('pal_composer_line', 'sp_img')
406416

407417

408-
class TestComposerImageVsCanvasLine(TestComposerLineBase, TestLineBase):
418+
class TestLayoutImageVsCanvasLine(TestLayoutLineBase, TestLineBase):
409419

410420
def setUp(self):
411421
"""Run before each test."""
412-
super(TestComposerImageVsCanvasLine, self).setUp()
422+
super(TestLayoutImageVsCanvasLine, self).setUp()
413423
self._TestKind = OutputKind.Img
414424
self.configTest('pal_canvas_line', 'sp')
415425

416426

417-
class TestComposerSvgLine(TestComposerLineBase, TestLineBase):
427+
class TestLayoutSvgLine(TestLayoutLineBase, TestLineBase):
418428

419429
def setUp(self):
420430
"""Run before each test."""
421-
super(TestComposerSvgLine, self).setUp()
431+
super(TestLayoutSvgLine, self).setUp()
422432
self._TestKind = OutputKind.Svg
423433
self.configTest('pal_composer_line', 'sp_svg')
424434

425435

426-
class TestComposerSvgVsComposerLine(TestComposerLineBase, TestLineBase):
436+
class TestLayoutSvgVsLayoutLine(TestLayoutLineBase, TestLineBase):
427437

428438
"""
429-
Compare only to composer image, which is already compared to canvas line
439+
Compare only to layout image, which is already compared to canvas line
430440
"""
431441

432442
def setUp(self):
433443
"""Run before each test."""
434-
super(TestComposerSvgVsComposerLine, self).setUp()
444+
super(TestLayoutSvgVsLayoutLine, self).setUp()
435445
self._TestKind = OutputKind.Svg
436446
self.configTest('pal_composer_line', 'sp_img')
437447
self._ColorTol = 4
438448

439449

440-
class TestComposerPdfLine(TestComposerLineBase, TestLineBase):
450+
class TestLayoutPdfLine(TestLayoutLineBase, TestLineBase):
441451

442452
def setUp(self):
443453
"""Run before each test."""
444-
super(TestComposerPdfLine, self).setUp()
454+
super(TestLayoutPdfLine, self).setUp()
445455
self._TestKind = OutputKind.Pdf
446456
self.configTest('pal_composer_line', 'sp_pdf')
447457

448458

449-
class TestComposerPdfVsComposerLine(TestComposerLineBase, TestLineBase):
459+
class TestLayoutPdfVsLayoutLine(TestLayoutLineBase, TestLineBase):
450460

451461
"""
452-
Compare only to composer image, which is already compared to canvas line
462+
Compare only to layout image, which is already compared to canvas line
453463
"""
454464

455465
def setUp(self):
456466
"""Run before each test."""
457-
super(TestComposerPdfVsComposerLine, self).setUp()
467+
super(TestLayoutPdfVsLayoutLine, self).setUp()
458468
self._TestKind = OutputKind.Pdf
459469
self.configTest('pal_composer_line', 'sp_img')
460470
self._Mismatch = 50
@@ -465,12 +475,12 @@ def setUp(self):
465475
# NOTE: unless PAL_SUITE env var is set all test class methods will be run
466476
# SEE: test_qgspallabeling_tests.suiteTests() to define suite
467477
st = suiteTests()
468-
sp_i = ['TestComposerImagePoint.' + t for t in st['sp_suite']]
469-
sp_ivs = ['TestComposerImageVsCanvasPoint.' + t for t in st['sp_vs_suite']]
470-
sp_s = ['TestComposerSvgPoint.' + t for t in st['sp_suite']]
471-
sp_svs = ['TestComposerSvgVsComposerPoint.' + t for t in st['sp_vs_suite']]
472-
sp_p = ['TestComposerPdfPoint.' + t for t in st['sp_suite']]
473-
sp_pvs = ['TestComposerPdfVsComposerPoint.' + t for t in st['sp_vs_suite']]
478+
sp_i = ['TestLayoutImagePoint.' + t for t in st['sp_suite']]
479+
sp_ivs = ['TestLayoutImageVsCanvasPoint.' + t for t in st['sp_vs_suite']]
480+
sp_s = ['TestLayoutSvgPoint.' + t for t in st['sp_suite']]
481+
sp_svs = ['TestLayoutSvgVsLayoutPoint.' + t for t in st['sp_vs_suite']]
482+
sp_p = ['TestLayoutPdfPoint.' + t for t in st['sp_suite']]
483+
sp_pvs = ['TestLayoutPdfVsLayoutPoint.' + t for t in st['sp_vs_suite']]
474484
suite = []
475485

476486
# extended separately for finer control of PAL_SUITE (comment-out undesired)

‎tests/src/python/test_qgspallabeling_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def test_curved_placement_below(self):
314314
def suiteTests():
315315
"""
316316
Use to define which tests are run when PAL_SUITE is set.
317-
Use sp_vs_suite for comparison of server and composer outputs to canvas
317+
Use sp_vs_suite for comparison of server and layout outputs to canvas
318318
"""
319319
sp_suite = [
320320
# 'test_default_label',

0 commit comments

Comments
 (0)
Please sign in to comment.