Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 72 additions & 17 deletions crates/engine/src/cmd/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7236,6 +7236,18 @@ fn renamed_statistics(text: &str, limit: usize, braces: bool, replace: &mut impl
}
}

/// The ids of `layout`'s map frames showing the map `map`.
fn frames_showing(layout: &rustgis_map::layout::Layout, map: &str) -> Vec<u32> {
layout
.elements
.iter()
.filter_map(|e| match &e.kind {
rustgis_map::layout::ElementKind::MapFrame { map: m, .. } if m.eq_ignore_ascii_case(map) => Some(e.id),
_ => None,
})
.collect()
}

/// Carries typed references to a renamed layer along with it. Map-local references and layout
/// frames stay in their map. A name still present there keeps its references, as legends do;
/// project-wide references without a map stay unchanged when another map still has that name.
Expand Down Expand Up @@ -7338,14 +7350,7 @@ fn references_follow_layer(s: &mut Session, old: &str, new: &str) {
// A name with a brace can't be written into a token: the texts keep the old one.
let braces = new.contains(['{', '}']);
for layout in &mut s.project.layouts {
let frames: Vec<u32> = layout
.elements
.iter()
.filter_map(|e| match &e.kind {
ElementKind::MapFrame { map, .. } if map.eq_ignore_ascii_case(&map_name) => Some(e.id),
_ => None,
})
.collect();
let frames = frames_showing(layout, &map_name);
// A text's layer statistics (#2853) read the map of the layout's first frame.
let texts_read_this_map = layout.first_frame().is_some_and(|f| frames.contains(&f));
for element in &mut layout.elements {
Expand Down Expand Up @@ -7488,13 +7493,68 @@ pub(crate) fn drop_layers(s: &mut Session, ids: &[u32]) {
// The tile layers drawn over them keep their place.
m.release_tiles(ids);
m.layers.retain(|l| !ids.contains(&l.id));
terrain_follows_removal(s, &names);
square_labels_follow_removal(s, &names);
for n in &names {
follow_removal(s, &names);
}

/// What follows the removal of the layers called `names` from the active map.
fn follow_removal(s: &mut Session, names: &[String]) {
terrain_follows_removal(s, names);
square_labels_follow_removal(s, names);
layouts_follow_removal(s, names);
// A legend that left it out forgets it.
for n in names {
legends_follow_layer(s, n, None);
}
}

/// After layers called `names` are removed: a table's, chart's or colorbar's layer, and a text's
/// layer statistics (#2853), keep the name, so a layer of that name brings them back, and are drawn
/// without it meanwhile: how many elements, said once. Which elements read the active map is as for
/// a rename (`references_follow_layer`).
fn layouts_follow_removal(s: &mut Session, names: &[String]) {
use rustgis_map::layout::ElementKind;

let gone: Vec<&String> = names.iter().filter(|n| named_layers(s, n) == 0).collect();
let map_name = &s.map().name;
// The removed names some element reads, each once.
let mut named: Vec<&str> = Vec::new();
let mut count = 0usize;
for layout in &s.project.layouts {
let frames = frames_showing(layout, map_name);
let texts_read_this_map = layout.first_frame().is_some_and(|f| frames.contains(&f));
for element in &layout.elements {
let reads: Vec<&str> = match &element.kind {
ElementKind::Table { frame, layer, .. } | ElementKind::Chart { frame, layer, .. } if frames.contains(frame) => {
vec![layer]
}
ElementKind::Colorbar { frame, layer: Some(layer), .. } if frames.contains(frame) => vec![layer],
ElementKind::Text { text, .. } if texts_read_this_map => {
rustgis_map::layout::stat_tokens(text).iter().map(|t| t.layer).collect()
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}
_ => continue,
};
let mut lost = false;
for name in gone.iter().filter(|n| reads.iter().any(|r| r.eq_ignore_ascii_case(n))) {
lost = true;
if !named.iter().any(|n| n.eq_ignore_ascii_case(name)) {
named.push(name.as_str());
}
}
count += usize::from(lost);
}
}
if count == 0 {
return;
}
let key = if named.len() == 1 {
"{count} layout element(s) name the removed layer {names}: they are drawn without it until a layer of that name comes back"
} else {
"{count} layout element(s) name the removed layers {names}: they are drawn without it until a layer of that name comes back"
};
let names = named.iter().map(|n| format!("'{n}'")).collect::<Vec<_>>().join(", ");
s.notify_text(crate::notice_text::NoticeText::format(key, [("count", count.to_string().into()), ("names", names.into())]), false);
}

/// After layers called `names` are removed: point labels turned square to one of them (#2263),
/// and labels curved along one (#2262), keep its name, so a layer of that name brings their line
/// back, and are placed as auto, or as without a guide, meanwhile: said once a kind, naming the
Expand Down Expand Up @@ -7596,12 +7656,7 @@ fn remove(s: &mut Session, p: &Value) -> CmdResult {
}
let next = m.layers.get(at.min(m.layers.len().saturating_sub(1))).map(|l| l.id);
s.set_layer_selection(next.into_iter().collect(), next);
terrain_follows_removal(s, &names);
square_labels_follow_removal(s, &names);
// A legend that left it out forgets it.
for name in names {
legends_follow_layer(s, &name, None);
}
follow_removal(s, &names);
Ok(Value::Null)
}

Expand Down
3 changes: 3 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ mod layout_files;
#[path = "tests/layout_title.rs"]
mod layout_title;

#[path = "tests/layout_removed_layer.rs"]
mod layout_removed_layer;

#[path = "tests/sde_connections.rs"]
mod sde_connections;

Expand Down
119 changes: 119 additions & 0 deletions crates/engine/src/tests/layout_removed_layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//! Deleting a layer says how many layout elements name it: a chart's, table's or colorbar's
//! layer, and a text's layer statistics (#3020). The elements keep the name.

use super::*;
use rustgis_map::layout::ElementKind;

/// The notices `command` gives about layout elements that lost a layer.
fn lost(s: &mut Session, command: &str, params: Value) -> Vec<String> {
let mark = s.notice_mark();
s.run(command, &params).unwrap();
s.notices_since(mark).iter().map(|n| n.text.clone()).filter(|t| t.contains("layout element(s)")).collect()
}

fn add(s: &mut Session, params: Value) {
s.run("layout.addElement", &params).unwrap();
}

fn text(s: &mut Session, text: &str) {
add(s, json!({"kind": "text", "text": text, "x": 10, "y": 10}));
}

/// The sample world with a layout holding one map frame of it.
fn with_layout() -> Session {
let mut s = world();
s.run("layout.new", &json!({"page": "a4"})).unwrap();
s
}

fn kinds(s: &Session) -> Vec<ElementKind> {
s.project.layouts[0].elements.iter().map(|e| e.kind.clone()).collect()
}

fn said(count: usize, names: &str) -> String {
let layers = if names.contains(", ") { "layers" } else { "layer" };
format!("{count} layout element(s) name the removed {layers} {names}: they are drawn without it until a layer of that name comes back")
}

#[test]
fn removing_a_layer_says_how_many_layout_elements_name_it() {
let mut s = with_layout();
add(&mut s, json!({"kind": "table", "layer": "Countries"}));
add(&mut s, json!({"kind": "chart", "layer": "Countries", "field": "pop_est", "label": "name"}));
// Two statistics of the layer in one text count it once; the layer is found ignoring case.
text(&mut s, "{count:countries} countries, {sum:Countries:pop_est} people");
let before = kinds(&s);
assert_eq!(lost(&mut s, "layer.remove", json!({"layer": "Countries"})), [said(3, "'Countries'")]);
assert_eq!(kinds(&s), before, "the elements keep the name");
assert_eq!(lost(&mut s, "edit.undo", Value::Null), Vec::<String>::new());
assert!(s.map().layer_by_name("Countries").is_some());
assert_eq!(kinds(&s), before);
}

#[test]
fn layers_removed_together_are_said_once() {
let mut s = with_layout();
s.run("layer.addDataBytes", &json!({"fileName": "dem.tif", "data": dem_b64()})).unwrap();
let dem = s.map().layers.iter().find(|l| l.is_raster()).unwrap().name.clone();
add(&mut s, json!({"kind": "colorbar", "layer": dem}));
add(&mut s, json!({"kind": "table", "layer": "Countries"}));
// One element naming both counts once.
text(&mut s, "{count:Countries} and {count:Cities}");
let said_now = lost(&mut s, "layer.remove", json!({"layer": ["Countries", "Cities", dem]}));
assert_eq!(said_now.len(), 1, "{said_now:?}");
assert!(said_now[0].starts_with("3 layout element(s) name the removed layers "), "{said_now:?}");
for name in ["'Countries'", "'Cities'", &format!("'{dem}'")] {
assert!(said_now[0].contains(name), "{said_now:?}");
}
}

#[test]
fn removing_a_layer_no_element_names_says_nothing() {
let mut s = with_layout();
add(&mut s, json!({"kind": "table", "layer": "Countries"}));
add(&mut s, json!({"kind": "colorbar"}));
// A count's layer is all that follows its colon, so `Cities:pop` is another layer's name.
text(&mut s, "Cities {count:Cities:pop} {count:Countries} {scale}");
assert_eq!(lost(&mut s, "layer.remove", json!({"layer": "Cities"})), Vec::<String>::new());
}

#[test]
fn a_name_another_layer_still_bears_says_nothing_until_the_last_goes() {
let mut s = with_layout();
add(&mut s, json!({"kind": "table", "layer": "Countries"}));
s.run("layer.rename", &json!({"layer": "Lakes", "name": "Countries"})).unwrap();
let first = s.map().layers.iter().find(|l| l.name == "Countries").unwrap().id;
assert_eq!(lost(&mut s, "layer.remove", json!({"layer": first})), Vec::<String>::new());
assert_eq!(lost(&mut s, "layer.remove", json!({"layer": "Countries"})), [said(1, "'Countries'")]);
}

#[test]
fn two_layers_of_one_name_removed_together_name_it_once() {
let mut s = with_layout();
add(&mut s, json!({"kind": "table", "layer": "Countries"}));
s.run("layer.rename", &json!({"layer": "Lakes", "name": "Countries"})).unwrap();
let both: Vec<u32> = s.map().layers.iter().filter(|l| l.name == "Countries").map(|l| l.id).collect();
assert_eq!(lost(&mut s, "layer.remove", json!({"layer": both})), [said(1, "'Countries'")]);
}

#[test]
fn elements_that_read_another_map_are_not_counted() {
let mut s = with_layout();
add(&mut s, json!({"kind": "table", "layer": "Countries"}));
text(&mut s, "{count:Countries}");
let home = s.map().name.clone();
s.run("map.new", &json!({"name": "Empty"})).unwrap();
s.active_layout = Some(0);
let frame = s.project.layouts[0].first_frame().unwrap();
s.run("layout.update", &json!({"element": frame, "map": "Empty"})).unwrap();
s.run("map.activate", &json!({"name": home})).unwrap();
assert_eq!(lost(&mut s, "layer.remove", json!({"layer": "Countries"})), Vec::<String>::new());
}

#[test]
fn removing_a_group_says_it_too() {
let mut s = with_layout();
add(&mut s, json!({"kind": "table", "layer": "Cities"}));
s.run("group.add", &json!({"name": "Places", "layers": ["Cities"]})).unwrap();
assert_eq!(lost(&mut s, "group.remove", json!({"group": "Places"})), [said(1, "'Cities'")]);
}
31 changes: 16 additions & 15 deletions docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,21 +503,22 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
it does for a chart's or table's layer (in the layouts whose first map frame shows its map, and
in a presentation's map pages of it); a text that a longer name would take past the characters a
text holds, or that a name with a brace (which a token can't hold) would break, keeps the old
name, and the rename says so; deleting it leaves them as written. A layer read in place covers
what is loaded, as a chart does, and a warning says so. A token with no figure (no such layer or
field, a field with no numbers, a raster layer, or a sum, mean, least or greatest written without
its field) stays as written, and a warning names what is missing. Both warnings come when the
text is added or changed, or its layout is made or imported, or when another map frame becomes
the first (it is given another map, the one before it is deleted or drawn over it, or its map is
deleted), not when the page is exported. A presentation map page's title is checked against the
page's map when the page is added with it, when it is changed or when the presentation is
imported. The text, its size (2 to 400 points, as text graphics on
the map), colour, bold, **Italic** (slanted, as water and ocean names are; the built-in fonts
have no italic face, so the letters are slanted 12°), **Alignment** (left, center or right within
the element) and **Letter spacing** in points (spaced-out names such as O C E A N O, or a
negative value to tighten) are the same form a text on the map has in the pane; Enter ends the
text and Shift+Enter starts a new line. Scripts set them with `italic`, `align` and
`letterSpacing` on `layout.addElement` and `layout.update`.
name, and the rename says so; deleting it leaves them as written, and when layout elements still
name it (a text's tokens, or a chart's, table's or colorbar's layer), says how many. A layer
read in place covers what is loaded, as a chart does, and a warning says so. A token with no
figure (no such layer or field, a field with no numbers, a raster layer, or a sum, mean, least
or greatest written without its field) stays as written, and a warning names what is missing.
Both warnings come when the text is added or changed, or its layout is made or imported, or when
another map frame becomes the first (it is given another map, the one before it is deleted or
drawn over it, or its map is deleted), not when the page is exported. A presentation map page's
title is checked against the page's map when the page is added with it, when it is changed or
when the presentation is imported. The text, its size (2 to 400 points, as text graphics on the
map), colour, bold, **Italic** (slanted, as water and ocean names are; the built-in fonts have
no italic face, so the letters are slanted 12°), **Alignment** (left, center or right within the
element) and **Letter spacing** in points (spaced-out names such as O C E A N O, or a negative
value to tighten) are the same form a text on the map has in the pane; Enter ends the text and
Shift+Enter starts a new line. Scripts set them with `italic`, `align` and `letterSpacing` on
`layout.addElement` and `layout.update`.
A layout's text also takes a **Font**:
Sans-serif (the default) or Serif, for a title or a classic atlas look. The serif is one installed
on the computer (Georgia or Times New Roman on Windows and macOS, Noto, DejaVu or Liberation Serif
Expand Down
Loading