@@ -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