Skip to content
5 changes: 3 additions & 2 deletions crates/ui-egui/src/graphicformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ pub fn show(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) {
named_fonts: false,
select,
};
let settled = crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("graphic_format", k), &[graphics::TOKENS], fields))
.unwrap_or_default();
let settled =
crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("graphic_format", k), &[graphics::TOKENS], Vec::new, fields))
.unwrap_or_default();
let save = |app: &mut RustGisApp, change: serde_json::Value| {
let mut p = json!({"layer": layer, "row": row});
if let (Some(p), Some(c)) = (p.as_object_mut(), change.as_object()) {
Expand Down
6 changes: 4 additions & 2 deletions crates/ui-egui/src/layoutview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,7 @@ fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li: usize) {
let (mut it, mut al, mut ls, mut fam, mut named) = (*italic, *align, *letter_spacing, *font, font_name.clone());
let (mut wr, mut va, mut line_sp, mut cs) = (*wrap, valign.kept(), *line_spacing, case.kept());
let tokens = [rustgis_engine::map::graphics::TOKENS, rustgis_engine::map::layout::PAGE_TOKENS];
let layers = || crate::textform::StatLayer::of(layout, &app.session.project.maps);
let warning = app
.layout
.text_losses
Expand All @@ -2742,8 +2743,9 @@ fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li: usize) {
named_fonts: true,
select: false,
};
let settled = crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("layout_text", li, id), &tokens, fields))
.unwrap_or_default();
let settled =
crate::textform::section(ui, "Text", |ui| crate::textform::show(ui, ("layout_text", li, id), &tokens, layers, fields))
.unwrap_or_default();
if settled.typing {
app.layout.typing.insert(key, buf);
} else if settled.text && buf != *text {
Expand Down
31 changes: 31 additions & 0 deletions crates/ui-egui/src/ribbon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3651,6 +3651,8 @@ fn layout_groups(app: &mut RustGisApp, ui: &mut Ui) {
ui.close();
}
}
let layers = crate::textform::StatLayer::of(&layout, &app.session.project.maps);
crate::textform::stat_menu(ui, &layers, |token| add(app, json!({"kind": "dynamicText", "text": token})));
});
if big(ui, app, "picture", "Picture", "Add a PNG or JPEG picture to the page.", "ui.layoutPicture", Value::Null, false, false)
.clicked()
Expand Down Expand Up @@ -6884,6 +6886,35 @@ mod tests {
}
}

/// Publish › Text › Dynamic Text › Layer statistic adds a text holding the token picked, from
/// the layers of the first frame's map (#3036); a layout with no frame has no such section.
#[test]
fn publish_dynamic_text_adds_a_layer_statistic() {
use rustgis_engine::map::layout::ElementKind;
let mut app = RustGisApp::with_sample();
app.run("layout.new", json!({"name": "Figures"})).unwrap();
let li = app.session.active_layout.unwrap();
let before = app.session.project.layouts[li].elements.len();
let mut h = tab_harness(app, layout_groups);
for step in ["Dynamic Text", "Layer statistic", "Cities", "Count {count:Cities}"] {
h.get_by_label(step).click();
h.run();
}
let elements = &h.state().session.project.layouts[li].elements;
assert_eq!(elements.len(), before + 1);
assert!(matches!(&elements.last().unwrap().kind, ElementKind::Text { text, .. } if text == "{count:Cities}"));
assert_eq!(h.state().layout.selected, Some(elements.last().unwrap().id));
assert!(h.query_by_label("Layer statistic").is_none(), "the menu closes");

let frame = h.state().session.project.layouts[li].first_frame().unwrap();
h.state_mut().run("layout.removeElement", json!({"layout": li, "element": frame})).unwrap();
h.run();
h.get_by_label("Dynamic Text").click();
h.run();
assert!(h.query_by_label_contains("Map scale").is_some(), "the menu is open");
assert!(h.query_by_label("Layer statistic").is_none(), "no frame, no layers");
}

/// New Layout › Publication Layout › a sheet › a style makes that sheet in that style, titled
/// with the map's name; a figure has no styles to pick and is made at once.
#[test]
Expand Down
167 changes: 165 additions & 2 deletions crates/ui-egui/src/textform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,76 @@ use crate::widgets::Dragged;
/// found by typing part of a name.
const MAX_FONTS_SHOWN: usize = 200;

/// A layer a layout text can show a figure of (#3036): its name and its numeric fields, which the
/// Dynamic Text menu offers as tokens (`{count:Name}`, `{sum:Name:field}`…).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StatLayer {
pub name: String,
pub fields: Vec<String>,
}

impl StatLayer {
/// The layers a text of `layout` can show figures of: those of the map its first frame shows
/// (as the page reads them), without a raster's or a graphics layer's, each with its numeric
/// fields. A name a token can't hold (`{`, `}`, or a field's `:`) is left out, and so is a layer
/// under another of its name, and a field after another of its name (case aside), which no token
/// reads.
pub fn of(layout: &rustgis_engine::map::layout::Layout, maps: &[rustgis_engine::map::Map]) -> Vec<StatLayer> {
use rustgis_engine::map::layout::ElementKind;
let Some(ElementKind::MapFrame { map, .. }) = layout.first_frame().and_then(|f| layout.element(f)).map(|e| &e.kind) else {
return Vec::new();
};
let Some(map) = maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)) else { return Vec::new() };
let holds = |name: &str, colon: bool| !name.is_empty() && !name.contains(['{', '}']) && !(colon && name.contains(':'));
map.layers
.iter()
.filter(|l| !l.graphics && l.raster.is_none() && holds(&l.name, false))
.filter(|l| map.layer_by_name(&l.name).is_some_and(|first| first.id == l.id))
.map(|l| StatLayer {
name: l.name.clone(),
fields: l
.dataset
.fields
.iter()
.enumerate()
.filter(|(i, f)| f.ty.is_numeric() && holds(&f.name, true) && l.dataset.field_index(&f.name) == Some(*i))
.map(|(_, f)| f.name.clone())
.collect(),
})
.collect()
}
}

/// The "Layer statistic" section of a Dynamic Text menu (#3036), the Format pane's and the
/// ribbon's: a submenu per layer, a count and, for each numeric field, its sum, mean, least and
/// greatest. `pick` takes the token chosen; with no layers there is no section.
pub fn stat_menu(ui: &mut Ui, layers: &[StatLayer], mut pick: impl FnMut(String)) {
if layers.is_empty() {
return;
}
ui.separator();
crate::widgets::submenu(ui, tl!("Layer statistic"), |ui| {
for layer in layers {
crate::widgets::submenu(ui, &layer.name, |ui| {
let mut add = |ui: &mut Ui, what: &str, token: String| {
if ui.button(format!("{what} {token}")).clicked() {
pick(token);
ui.close();
}
};
add(ui, tl!("Count"), format!("{{count:{}}}", layer.name));
for field in &layer.fields {
crate::widgets::submenu(ui, field, |ui| {
for (code, what) in [("sum", tl!("Sum")), ("mean", tl!("Mean")), ("min", tl!("Least")), ("max", tl!("Greatest"))] {
add(ui, what, format!("{{{code}:{}:{field}}}", layer.name));
}
});
}
});
}
});
}

/// What the form edits. A style is left out where the text doesn't have it.
pub struct Fields<'a> {
pub text: &'a mut String,
Expand Down Expand Up @@ -90,8 +160,16 @@ pub fn section<R>(ui: &mut Ui, title: &str, add: impl FnOnce(&mut Ui) -> R) -> O
pub const LABEL_W: f32 = 96.0;

/// The form, its fields told apart by `id` (an element, a dialog). `tokens` are the dynamic text
/// lists the text expands where it is shown, each a section of the Dynamic Text menu.
pub fn show(ui: &mut Ui, id: impl std::hash::Hash + std::fmt::Debug, tokens: &[&[(&str, &str)]], f: Fields<'_>) -> Settled {
/// lists the text expands where it is shown, each a section of the Dynamic Text menu; `layers`
/// gives the layers whose figures it can show, a section of their own (none for a text graphic),
/// and is asked only while the menu is open.
pub fn show(
ui: &mut Ui,
id: impl std::hash::Hash + std::fmt::Debug,
tokens: &[&[(&str, &str)]],
layers: impl FnOnce() -> Vec<StatLayer>,
f: Fields<'_>,
) -> Settled {
let id = egui::Id::unique(id);
let mut out = Settled::default();
let hint = tl!("Shift+Enter starts a new line.");
Expand Down Expand Up @@ -125,6 +203,10 @@ pub fn show(ui: &mut Ui, id: impl std::hash::Hash + std::fmt::Debug, tokens: &[&
}
}
}
stat_menu(ui, &layers(), |token| {
f.text.push_str(&token);
out.text = true;
});
});
out.typing = r.has_focus() && !out.text;
egui::Grid::new(id.with("style")).num_columns(2).min_col_width(LABEL_W).spacing(vec2(8.0, 6.0)).show(ui, |ui| {
Expand Down Expand Up @@ -411,6 +493,8 @@ mod tests {
/// Whether the vertical alignment, line spacing and case are offered, as a layout text's
/// form offers them.
blocks: bool,
/// The layers whose figures the text can show.
layers: Vec<StatLayer>,
settled: Vec<&'static str>,
}

Expand All @@ -423,6 +507,7 @@ mod tests {
ui,
"form",
&[rustgis_engine::map::graphics::TOKENS, rustgis_engine::map::layout::PAGE_TOKENS],
|| f.layers.clone(),
Fields {
text: &mut f.text,
size: &mut size,
Expand Down Expand Up @@ -490,6 +575,84 @@ mod tests {
assert!(h.query_by_label("Page number {page}").is_none(), "the menu closes");
}

/// The layers offered are those of the first frame's map, with their numeric fields; a raster
/// layer, a graphics layer and a name a token can't hold are left out (#3036).
#[test]
fn the_layers_offered_are_the_first_frames_with_their_numeric_fields() {
use rustgis_engine::data::{Dataset, Field, FieldType, GeometryType};
use rustgis_engine::map::{
Map,
layout::{ElementKind, Layout},
};
let layer = |name: &str, fields: Vec<(&str, FieldType)>| {
Dataset::new(name, GeometryType::None, fields.into_iter().map(|(n, t)| Field::new(n, t)).collect())
};
let mut map = Map::new("Main");
// A token reads the first layer of a name, case aside, so one under it is not offered
// (a layer is added above the others).
map.add_dataset(layer("Under", vec![("other", FieldType::Double)]));
map.add_dataset(layer(
"Cities",
vec![
("pop", FieldType::Double),
("name", FieldType::Text),
("a:b", FieldType::Double),
// A token reads the first field of a name, case aside: neither of these is offered.
("Pop", FieldType::Double),
("Name", FieldType::Double),
],
));
map.add_dataset(layer("Bad {name}", vec![("x", FieldType::Double)]));
map.layers.iter_mut().find(|l| l.name == "Under").unwrap_or_else(|| panic!("a layer")).name = "cities".into();
let mut other = Map::new("Other");
other.add_dataset(layer("Rivers", vec![("km", FieldType::Double)]));
let mut layout = Layout::new("L", 100.0, 100.0);
assert_eq!(StatLayer::of(&layout, &[map.clone(), other.clone()]), Vec::new(), "no frame, no layers");
let frame = |m: &str| -> ElementKind {
serde_json::from_value(serde_json::json!({"MapFrame": {"map": m, "center": {"x": 0, "y": 0}, "scale": 1000000}}))
.unwrap_or_else(|e| panic!("{e}"))
};
layout.add("Frame", (0.0, 0.0, 50.0, 50.0), frame("main"));
assert_eq!(
StatLayer::of(&layout, &[map.clone(), other.clone()]),
[StatLayer { name: "Cities".into(), fields: vec!["pop".into()] }]
);
let mut second = Layout::new("L2", 100.0, 100.0);
second.add("Frame", (0.0, 0.0, 50.0, 50.0), frame("Other"));
assert_eq!(StatLayer::of(&second, &[map, other]), [StatLayer { name: "Rivers".into(), fields: vec!["km".into()] }]);
}

/// A layer statistic is added from a submenu per layer, a count and the four figures of each
/// numeric field (#3036); a text with no layers to read has no such section.
#[test]
fn a_layer_statistic_is_added_from_its_layers_submenu() {
let cities = StatLayer { name: "Cities".into(), fields: vec!["pop".into(), "area".into()] };
let mut h = harness(Form { text: "".into(), size: 12.0, layers: vec![cities], ..Form::default() });
h.run();
for (path, text) in [
(&["Cities", "Count {count:Cities}"][..], "{count:Cities}"),
(&["Cities", "pop", "Sum {sum:Cities:pop}"][..], "{count:Cities}{sum:Cities:pop}"),
(&["Cities", "area", "Greatest {max:Cities:area}"][..], "{count:Cities}{sum:Cities:pop}{max:Cities:area}"),
] {
h.get_by_label("Insert Dynamic Text").click();
h.run();
h.get_by_label("Layer statistic").click();
h.run();
for step in path {
h.get_by_label(step).click();
h.run();
}
assert_eq!(h.state().text, text);
}
assert_eq!(h.state().settled, ["text", "text", "text"]);
assert!(h.query_by_label("Layer statistic").is_none(), "the menu closes");
let mut none = harness(Form { text: "T".into(), size: 12.0, ..Form::default() });
none.run();
none.get_by_label("Insert Dynamic Text").click();
none.run();
assert!(none.query_by_label("Layer statistic").is_none(), "no layers, no section");
}

/// Colour and boldness show only where the text has them; the size stays in the one range
/// text is drawn at.
#[test]
Expand Down
6 changes: 5 additions & 1 deletion docs/user-guide/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,11 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu
**Insert Dynamic Text** menu, under the text, lists each with what it shows. A layer's figures
fill in too (#2853): `{count:Cities}` is how many features the layer has, and `{sum:Cities:pop}`,
`{mean:Cities:pop}`, `{min:Cities:pop}` and `{max:Cities:pop}` add up, average or pick the least
or greatest number of a field, read as a chart reads its values. They follow the layer's
or greatest number of a field, read as a chart reads its values. **Layer statistic** writes them,
in a layout text's **Insert Dynamic Text** menu and in **Publish › Text › Dynamic Text** (which
adds a text holding one): a submenu for each layer of the first map frame's map, with **Count**
and, under each numeric field, **Sum**, **Mean**, **Least** and **Greatest** (raster layers,
graphics layers and names a token can't hold are not listed). They follow the layer's
definition query (one that no longer compiles keeps every feature, as the map draws it), take the
map of the layout's first map frame (in a series, the whole layer, not the page's part of it, so
a definition query that reads the series page's values keeps nothing there), and are written as a
Expand Down
Loading