Skip to content

Commit f774908

Browse files
authored
Merge pull request #703 from meyerlor/fix/print-template-order-not-saved
Fix print template order not persisted on reload
2 parents f650804 + a968803 commit f774908

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

lizmap/table_manager/layouts.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,26 @@ def load_qgis_layouts(self, data: dict):
7171
else:
7272
legacy_print_checkbox = False
7373

74-
# For all layouts in the project already loaded
75-
for layout in QgsProject.instance().layoutManager().printLayouts():
74+
# Build a lookup of QGIS layouts by name
75+
qgis_layouts_by_name = {
76+
layout.name(): layout
77+
for layout in QgsProject.instance().layoutManager().printLayouts()
78+
}
79+
80+
# Determine the display order: use saved order from config, then append any new layouts
81+
if data and data.get(self.label_dictionary_list()):
82+
cfg_order = [item.get('layout') for item in data.get(self.label_dictionary_list())]
83+
# Keep only names that still exist in the project
84+
ordered_names = [name for name in cfg_order if name in qgis_layouts_by_name]
85+
# Append new layouts not in the saved config
86+
for name in qgis_layouts_by_name:
87+
if name not in ordered_names:
88+
ordered_names.append(name)
89+
else:
90+
ordered_names = list(qgis_layouts_by_name.keys())
91+
92+
for layout_name in ordered_names:
93+
layout = qgis_layouts_by_name[layout_name]
7694
# TODO check for report ?
7795
LOGGER.debug(" * reading layout {}".format(layout.name()))
7896
row = self.table.rowCount()

tests/test_table_manager.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,71 @@ def test_layout_definitions(self, data: Path, layer: QgsVectorLayer):
312312

313313
self.assertDictEqual({}, table_manager.wfs_fields_used())
314314

315+
def test_layout_order_preservation(self, data: Path, layer: QgsVectorLayer):
316+
"""Test that layout order is preserved from config when loading."""
317+
table = QTableWidget()
318+
definitions = LayoutsDefinitions()
319+
320+
QgsProject.instance().read(str(data.joinpath("print.qgs")))
321+
322+
table_manager = TableManagerLayouts(None, definitions, None, table, None, None, None)
323+
324+
self.assertEqual(table_manager.table.rowCount(), 0)
325+
326+
# Create a config with layouts in a different order than QGIS returns them
327+
# QGIS layouts in print.qgs are: "A4 Landscape", "Cadastre", "Local planning", "Economy"
328+
# We'll specify a different order in the config
329+
cfg = {
330+
"list": [
331+
{
332+
"layout": "Cadastre",
333+
"enabled": True,
334+
"formats_available": ("pdf",),
335+
"default_format": "pdf",
336+
"dpi_available": ("100",),
337+
"default_dpi": "100",
338+
},
339+
{
340+
"layout": "Local planning",
341+
"enabled": True,
342+
"formats_available": ("pdf",),
343+
"default_format": "pdf",
344+
"dpi_available": ("100",),
345+
"default_dpi": "100",
346+
},
347+
{
348+
"layout": "A4 Landscape",
349+
"enabled": False,
350+
"formats_available": ("pdf", "png"),
351+
"default_format": "pdf",
352+
"dpi_available": ("100", "300"),
353+
"default_dpi": "300",
354+
},
355+
# Note: "Economy" is not in the config, it should appear at the end
356+
]
357+
}
358+
359+
table_manager.load_qgis_layouts(cfg)
360+
self.assertEqual(table_manager.table.rowCount(), 4)
361+
362+
# Export and verify the order matches the config order, not QGIS order
363+
data_output = table_manager.to_json(LwcVersions.latest())
364+
365+
# The list should be in the order: Cadastre, Local planning, A4 Landscape, Economy
366+
# Economy should be at the end because it wasn't in the config
367+
layout_names = [item["layout"] for item in data_output["list"]]
368+
expected_order = ["Cadastre", "Local planning", "A4 Landscape", "Economy"]
369+
self.assertEqual(layout_names, expected_order)
370+
371+
# Verify that the properties are preserved correctly
372+
self.assertEqual(data_output["list"][0]["layout"], "Cadastre")
373+
self.assertTrue(data_output["list"][0]["enabled"])
374+
self.assertEqual(data_output["list"][2]["layout"], "A4 Landscape")
375+
self.assertFalse(data_output["list"][2]["enabled"])
376+
# Economy should have default values since it wasn't in the config
377+
self.assertEqual(data_output["list"][3]["layout"], "Economy")
378+
self.assertTrue(data_output["list"][3]["enabled"])
379+
315380
def test_dataviz_definitions(self, layer: QgsVectorLayer):
316381
"""Test dataviz collections keys."""
317382
table_manager = TableManager(None, DatavizDefinitions(), None, QTableWidget(), None, None, None, None)

0 commit comments

Comments
 (0)