diff --git a/crates/engine/src/tests/layout_legend_subtitle.rs b/crates/engine/src/tests/layout_legend_subtitle.rs index d0b4ecd25a..fb3841228e 100644 --- a/crates/engine/src/tests/layout_legend_subtitle.rs +++ b/crates/engine/src/tests/layout_legend_subtitle.rs @@ -112,8 +112,8 @@ fn a_files_subtitle_past_the_limit_is_cut_and_counted() { let mut read: rustgis_map::layout::Layout = serde_json::from_value(layout).unwrap(); assert_eq!(read.clamp().elements_adjusted, 1); let e = read.element(legend as u32).unwrap(); - let kept = serde_json::to_value(&e.kind).unwrap()["Legend"]["subtitle"].as_str().unwrap().len(); - assert!(kept > 0 && kept < 100_000, "{kept}"); + let kept = serde_json::to_value(&e.kind).unwrap()["Legend"]["subtitle"].as_str().unwrap().chars().count(); + assert_eq!(kept, rustgis_map::layout::MAX_LEGEND_SUBTITLE_CHARS); } #[test] diff --git a/crates/ui-egui/src/layoutview/format/legend.rs b/crates/ui-egui/src/layoutview/format/legend.rs index d7216957f7..07998cc13c 100644 --- a/crates/ui-egui/src/layoutview/format/legend.rs +++ b/crates/ui-egui/src/layoutview/format/legend.rs @@ -192,6 +192,29 @@ pub(super) fn legend_layers(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, if !needle.is_empty() && shown.is_empty() { ui.label(tl!("No layers match your search.")); } + // Every layer shown at once, the matches alone under a filter. + let rows: Vec<&String> = shown.iter().filter_map(|&i| names.get(i)).collect(); + if !rows.is_empty() { + ui.horizontal(|ui| { + let some_out = rows.iter().any(|n| left_out(n)); + let some_listed = rows.iter().any(|n| !left_out(n)); + let others = || kept.iter().filter(|x| !rows.iter().any(|n| x.eq_ignore_ascii_case(n))); + if ui + .add_enabled(some_out, egui::Button::new(tl!("Select All"))) + .on_hover_text(tl!("List every layer shown")) + .clicked() + { + change = Some(json!({"exclude": others().collect::>()})); + } + if ui + .add_enabled(some_listed, egui::Button::new(tl!("Select None"))) + .on_hover_text(tl!("Leave out every layer shown")) + .clicked() + { + change = Some(json!({"exclude": others().chain(rows.iter().copied()).collect::>()})); + } + }); + } // The places a row swaps with the one shown above or below it. let mut swap = None; // A bar beside the rows, not floating over their arrows. @@ -286,9 +309,11 @@ pub(super) fn legend_layers(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, next.extend(&unlisted); change = Some(json!({"layerOrder": next})); } - if !layer_order.is_empty() && ui.button(tl!("Map order")).on_hover_text(tl!("List the layers in the map's order")).clicked() + // The legend as it started: its texts for layers and classes stay. + if (!layer_order.is_empty() || !kept.is_empty()) + && ui.button(tl!("Reset")).on_hover_text(tl!("List every layer again in the map's order")).clicked() { - change = Some(json!({"layerOrder": []})); + change = Some(json!({"exclude": [], "layerOrder": []})); } }); } diff --git a/crates/ui-egui/src/layoutview/tests.rs b/crates/ui-egui/src/layoutview/tests.rs index c60d624d91..d5566fabfe 100644 --- a/crates/ui-egui/src/layoutview/tests.rs +++ b/crates/ui-egui/src/layoutview/tests.rs @@ -2225,7 +2225,7 @@ fn a_legends_layers_are_moved_in_the_format_pane() { h.run_steps(3); }; assert_eq!(listed(&h), drawn); - assert!(h.query_by_label("Map order").is_none(), "no order of its own"); + assert!(h.query_by_label("Reset").is_none(), "nothing to put back"); let ends = |end: usize| (0..=last).map(|i| i == end).collect::>(); assert_eq!(disabled(&h, "up"), ends(0)); assert_eq!(disabled(&h, "down"), ends(last)); @@ -2257,11 +2257,11 @@ fn a_legends_layers_are_moved_in_the_format_pane() { ElementKind::Legend { exclude, .. } => assert_eq!(exclude, &[up[0].clone()]), _ => panic!("a legend"), } - h.get_by_label("Map order").click(); + h.get_by_label("Reset").click(); h.run_steps(3); assert!(order(&h).is_empty()); assert_eq!(listed(&h), drawn); - assert!(h.query_by_label("Map order").is_none(), "and its button gone"); + assert!(h.query_by_label("Reset").is_none(), "and its button gone"); // The keyboard's focus goes with the layer it moves: Enter twice moves it two places. h.get_by_label(&format!("Move {} up", drawn[last])).focus(); h.run_steps(2); @@ -2301,7 +2301,7 @@ fn a_legends_layers_section_opens_in_the_legends_order() { app, ); h.run_steps(3); - assert!(h.query_by_label("Map order").is_some()); + assert!(h.query_by_label("Reset").is_some()); assert!(h.query_by_role_and_label(Role::CheckBox, "Cities").is_none()); let rivers = h.get_by_role_and_label(Role::CheckBox, "Rivers").rect().min.y; let countries = h.get_by_role_and_label(Role::CheckBox, "Countries").rect().min.y; @@ -2343,7 +2343,7 @@ fn a_legend_listing_no_layer_still_goes_back_to_the_maps_order() { app, ); h.run_steps(3); - h.get_by_label("Map order").click(); + h.get_by_label("Reset").click(); h.run_steps(3); match &h.state().session.project.layouts[li].element(legend).unwrap().kind { ElementKind::Legend { layer_order, .. } => assert!(layer_order.is_empty(), "{layer_order:?}"), @@ -2398,6 +2398,17 @@ fn layer_filter<'a>(h: &'a egui_kittest::Harness<'_, RustGisApp>) -> Option, text: &str) { + layer_filter(h).unwrap().focus(); + h.run_steps(2); + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.key_press(Key::Backspace); + h.run_steps(2); + layer_filter(h).unwrap().type_text(text); + h.run_steps(3); +} + /// Over eight layers, a legend's Layers section filters them by name, in any case; ▲ and ▼ then /// swap a layer with the nearest one shown, and ticking keeps the hidden layers left out (#3306). #[test] @@ -2414,23 +2425,14 @@ fn a_legends_layers_are_filtered_by_name() { ElementKind::Legend { exclude, layer_order, .. } => (exclude.clone(), layer_order.clone()), _ => panic!("a legend"), }; - let typed = |h: &mut egui_kittest::Harness<'_, RustGisApp>, text: &str| { - layer_filter(h).unwrap().focus(); - h.run_steps(2); - h.key_press_modifiers(Modifiers::COMMAND, Key::A); - h.key_press(Key::Backspace); - h.run_steps(2); - layer_filter(h).unwrap().type_text(text); - h.run_steps(3); - }; // Spaces alone filter nothing out. - typed(&mut h, " "); + type_layer_filter(&mut h, " "); assert!(shown(&h, "Cities") && shown(&h, "Extra 007")); - typed(&mut h, "no such layer"); + type_layer_filter(&mut h, "no such layer"); assert!(!shown(&h, "Rivers")); assert!(h.query_by_label("No layers match your search.").is_some()); - assert!(h.query_by_label("Map order").is_some()); - typed(&mut h, " RI "); + assert!(h.query_by_label("Reset").is_some()); + type_layer_filter(&mut h, " RI "); assert!(shown(&h, "Rivers") && shown(&h, "Countries")); assert!(!shown(&h, "Cities") && !shown(&h, "Extra 000")); assert!(h.query_by_label("No layers match your search.").is_none()); @@ -2451,6 +2453,107 @@ fn a_legends_layers_are_filtered_by_name() { assert_eq!(kept(&h).0, ["Cities", "Rivers"]); } +/// The names a legend leaves out, and its own order. +fn legend_listing(h: &egui_kittest::Harness<'_, RustGisApp>, li: usize, legend: u32) -> (Vec, Vec) { + match &h.state().session.project.layouts[li].element(legend).unwrap().kind { + rustgis_engine::map::layout::ElementKind::Legend { exclude, layer_order, .. } => { + let mut exclude = exclude.clone(); + exclude.sort(); + (exclude, layer_order.clone()) + } + _ => panic!("a legend"), + } +} + +/// Select All and Select None tick or untick every layer shown, the matches alone under a filter, +/// each one undo step, and keep what the section doesn't list (#3321). +#[test] +fn a_legends_layers_are_all_listed_or_left_out() { + use egui::accesskit::Role; + use egui_kittest::kittest::{NodeT, Queryable}; + let (mut h, li, legend) = legend_tree(8, json!({"exclude": ["Extra 000", "Extra 001", "Lakes"]})); + // Lakes stays on the map without being listed; Extra 000 leaves it. + let layers = &mut h.state_mut().session.map_mut().layers; + layers.iter_mut().find(|l| l.name == "Lakes").unwrap().graphics = true; + layers.retain(|l| l.name != "Extra 000"); + h.run_steps(3); + let disabled = |h: &egui_kittest::Harness<'_, RustGisApp>, l: &str| h.get_by_label(l).accesskit_node().is_disabled(); + let press = |h: &mut egui_kittest::Harness<'_, RustGisApp>, l: &str| { + let depth = h.state().session.undo_depth(); + h.get_by_label(l).click(); + h.run_steps(3); + assert_eq!(h.state().session.undo_depth(), depth + 1, "{l}: one undo step"); + }; + let out = |h: &egui_kittest::Harness<'_, RustGisApp>| legend_listing(h, li, legend).0; + + press(&mut h, "Select All"); + assert_eq!(out(&h), ["Lakes"], "Lakes, not listed here, stays left out; Extra 000, gone from the map, drops out"); + assert!(disabled(&h, "Select All"), "all listed already"); + + press(&mut h, "Select None"); + let mut every: Vec = ["Cities", "Countries", "Lakes", "Rivers"].map(String::from).to_vec(); + every.extend((1..8).map(|i| format!("Extra {i:03}"))); + every.sort(); + assert_eq!(out(&h), every); + assert!(disabled(&h, "Select None"), "all left out already"); + assert!(!disabled(&h, "Select All")); + h.state_mut().run("edit.undo", json!({})).unwrap(); + h.run_steps(3); + assert_eq!(out(&h), ["Lakes"]); + + // Under a filter, the matches alone: Countries and Rivers. + type_layer_filter(&mut h, "ri"); + press(&mut h, "Select None"); + assert_eq!(out(&h), ["Countries", "Lakes", "Rivers"]); + assert!(disabled(&h, "Select None"), "every match left out, though others are listed"); + type_layer_filter(&mut h, ""); + h.get_by_role_and_label(Role::CheckBox, "Cities").click(); + h.run_steps(3); + type_layer_filter(&mut h, "ri"); + press(&mut h, "Select All"); + assert_eq!(out(&h), ["Cities", "Lakes"], "a hidden layer stays left out"); + assert!(legend_listing(&h, li, legend).1.is_empty(), "the order untouched"); + + type_layer_filter(&mut h, "no such layer"); + assert!(h.query_by_label("Select All").is_none() && h.query_by_label("Select None").is_none()); +} + +/// Reset lists every layer again in the map's order, in one undo step, and keeps the legend's own +/// texts; it shows while there is something to put back (#3321). +#[test] +fn a_legends_layers_reset_to_the_map() { + use egui_kittest::kittest::Queryable; + use rustgis_engine::map::layout::ElementKind; + let (mut h, li, legend) = legend_tree(0, json!({"classLabels": {"Countries": {"Africa": "Renamed here"}}})); + assert!(h.query_by_label("Reset").is_none(), "nothing to put back"); + for params in + [json!({"exclude": ["Cities"]}), json!({"layerOrder": ["Rivers"]}), json!({"exclude": ["Cities"], "layerOrder": ["Rivers"]})] + { + let mut p = params.clone(); + p["element"] = json!(legend); + h.state_mut().run("layout.update", p).unwrap(); + h.run_steps(3); + let before = legend_listing(&h, li, legend); + let depth = h.state().session.undo_depth(); + h.get_by_label("Reset").click(); + h.run_steps(3); + assert_eq!(legend_listing(&h, li, legend), (Vec::new(), Vec::new()), "{params}"); + assert_eq!(h.state().session.undo_depth(), depth + 1, "{params}"); + assert!(h.query_by_label("Reset").is_none(), "{params}"); + h.state_mut().run("edit.undo", json!({})).unwrap(); + h.run_steps(3); + assert_eq!(legend_listing(&h, li, legend), before, "{params}"); + h.get_by_label("Reset").click(); + h.run_steps(3); + } + match &h.state().session.project.layouts[li].element(legend).unwrap().kind { + ElementKind::Legend { class_labels, .. } => { + assert_eq!(class_labels.of("Countries").and_then(|o| o.get("Africa")).map(|s| s.as_str()), Some("Renamed here")) + } + _ => panic!("a legend"), + } +} + /// A layer drawn by classes expands to the classes as the legend labels them, and stays expanded /// when moved; a layer drawn in one symbol has no arrow, nor a web layer (#3306). #[test] @@ -2502,7 +2605,7 @@ fn a_hundred_layers_scroll_in_the_legends_section() { use egui_kittest::kittest::Queryable; let (mut h, ..) = legend_tree(100, json!({"layerOrder": ["Extra 000"]})); let top = layer_filter(&h).unwrap().rect().max.y; - let below = h.get_by_label("Map order").rect().min.y; + let below = h.get_by_label("Reset").rect().min.y; assert!(below - top < 360.0, "{top} {below}"); layer_filter(&h).unwrap().focus(); h.run_steps(2); diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index b46d0b27df..11dec9685a 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -552,15 +552,17 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu picture lands afterwards, rather than leave the file silently incomplete. The browser cannot wait: export again once the pictures have arrived. - **Its Layers section**: a checkbox for each layer of the frame's map (and each WMS or WMTS - layer with a legend): untick one to leave it out of the legend while the map still draws it - (a mask, a highlight, a basemap-like layer). Scripts give the same with - `"exclude": ["Graticule"]` on `layout.addElement` or `layout.update` (`[]` lists every layer - again); a renamed layer stays left out. The section lists the layers in the order the legend - draws them; ▲ and ▼ move one a place, each move one undo step, and **Map order** returns to - the map's order. The rows scroll in a box of their own, and over eight layers **Filter - layers** keeps those whose name contains the text, in any case; ▲ and ▼ then swap a layer - with the nearest one shown. The arrow beside a layer drawn by classes shows its classes as - the legend labels them, every one it can list, to read only. + layer with a legend): untick one to leave it out of the legend while the map still draws it (a + mask, a highlight, a basemap-like layer). Scripts give the same with `"exclude": ["Graticule"]` + on `layout.addElement` or `layout.update` (`[]` lists every layer again); a renamed layer stays + left out. The section lists the layers in the order the legend draws them; ▲ and ▼ move one a + place, each move one undo step. **Select All** and **Select None** tick or untick every layer + shown, and **Reset** lists every layer again in the map's order, keeping the legend's own + texts; each is one undo step. The rows scroll in a box of their own, and over eight layers + **Filter layers** keeps those whose name contains the text, in any case; **Select All** and + **Select None** then act on those alone, and ▲ and ▼ swap a layer with the nearest one shown. + The arrow beside a layer drawn by classes shows its classes as the legend labels them, every + one it can list, to read only. - **Layer order** (`"layerOrder"`, or ▲ and ▼ in the Layers section): a legend lists its layers as the map draws them, top first, unless it is given its own order: `layout.update {"element": 3, "layerOrder": ["Rivers", "Roads"]}` lists those layers first, in