diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 8958219b52..b3cc32e982 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -26,7 +26,7 @@ pub(crate) fn map_key(s: &Session) -> MapKey { /// The project (`Session::opened`) and the map in view a dialog or a form was opened on: layer ids /// are a map's own, so what it captured is only good while both are still in view. -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct MapOwner { project: u64, map: MapKey, diff --git a/crates/ui-egui/src/publicationwizard/map.rs b/crates/ui-egui/src/publicationwizard/map.rs new file mode 100644 index 0000000000..7b9c3fc77d --- /dev/null +++ b/crates/ui-egui/src/publicationwizard/map.rs @@ -0,0 +1,415 @@ +//! The wizard's map step. +use super::*; +use crate::dialogs::MapOwner; +use rustgis_engine::map::Layer; + +/// Which area the map frame shows. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Area { + /// The view, left to `layout.publication`. + View, + /// The features a layer shows (hidden or not), in the map it was picked in. The layer by its + /// [`Layer::instance_id`](rustgis_engine::map::Layer::instance_id): Undo hands its map-local + /// id to the next layer added. + Layer { map: MapOwner, layer: u64 }, + /// The selected features of the map it was picked in. + Selection { map: MapOwner }, +} + +/// The name `layout.publication` gives when none is: data, not translated. +const DEFAULT_NAME: &str = "Publication"; + +/// What a side with no length (a point, a straight line) is widened to, in degrees; on the ±180 or +/// ±90 edge half of it is cut off again. +const MIN_SIDE: f64 = 0.01; + +/// What a pick names in the session now. +enum Held<'a> { + View, + Layer(&'a Layer), + Selection, +} + +/// What `area` names now, or why it no longer holds: the one check the step, the review and Create +/// share, so a stale pick reads the same in the three. It reads no places, as it runs every frame. +fn held<'a>(s: &'a Session, area: &Area) -> Result, &'static str> { + let map = s.map(); + match area { + Area::View => Ok(Held::View), + Area::Layer { map: owner, .. } | Area::Selection { map: owner } if !owner.holds(s) => { + Err(tl!("The map was switched or renamed since the map extent was picked: pick it again.")) + } + Area::Layer { layer, .. } => map + .layers + .iter() + .find(|l| l.instance_id() == *layer) + .map(Held::Layer) + .ok_or_else(|| tl!("The layer picked is no longer in the map: pick the map extent again.")), + Area::Selection { .. } if map.selection_count() == 0 => Err(tl!("Nothing is selected: pick the map extent again.")), + Area::Selection { .. } => Ok(Held::Selection), + } +} + +/// The map step: the area the map frame shows and the layout's name. +pub(super) fn map_step(ui: &mut egui::Ui, s: &Session, d: &mut Draft) { + let map = s.map(); + // In the map's layer order, top first, by what costs nothing a frame: whether it has rows, a + // raster or a source. Its places are read at Create ([`Layer::extent`] walks every + // longitude/latitude row each time), where one with none to frame is refused. + let framable = || map.layers.iter().filter(|l| !l.dataset.features.is_empty() || l.raster.is_some() || l.source.is_some()); + egui::Grid::new("publication_wizard_map").num_columns(2).spacing(egui::vec2(8.0, 8.0)).show(ui, |ui| { + // A row for each choice, so the label sits beside the first one. + ui.label(tl!("Map extent")); + if ui.radio(d.area == Area::View, tl!("View")).on_hover_text(tl!("What the map view shows.")).clicked() { + d.area = Area::View; + } + ui.end_row(); + ui.label(""); + // A pick that no longer holds marks nothing. + let held = held(s, &d.area); + ui.horizontal(|ui| { + let picked = match held { + Ok(Held::Layer(l)) => Some(l), + _ => None, + }; + let top = framable().next().map(Layer::instance_id); + let radio = ui.add_enabled(top.is_some(), egui::RadioButton::new(picked.is_some(), tl!("Layer"))); + if radio.clicked() + && let Some(layer) = picked.map(Layer::instance_id).or(top) + { + d.area = Area::Layer { map: MapOwner::of(s), layer }; + } + if let Some(picked) = picked { + egui::ComboBox::from_id_salt("publication_wizard_map_layer") + .selected_text(picked.name.as_str()) + .width(ui.available_width().min(180.0)) + .truncate() + .show_ui(ui, |ui| { + for l in framable() { + if ui.selectable_label(l.instance_id() == picked.instance_id(), l.name.as_str()).clicked() { + d.area = Area::Layer { map: MapOwner::of(s), layer: l.instance_id() }; + } + } + }) + .response + .labelled_by(radio.id); + } + }); + ui.end_row(); + ui.label(""); + let selection = egui::RadioButton::new(matches!(held, Ok(Held::Selection)), tl!("Selection")); + if ui + .add_enabled(map.selection_count() > 0, selection) + .on_hover_text(tl!("The selected features.")) + .on_disabled_hover_text(tl!("Nothing is selected in the map.")) + .clicked() + { + d.area = Area::Selection { map: MapOwner::of(s) }; + } + ui.end_row(); + let label = ui.label(tl!("Name")); + ui.add(egui::TextEdit::singleline(&mut d.name).hint_text(DEFAULT_NAME).desired_width(field_width(ui))).labelled_by(label.id); + ui.end_row(); + }); +} + +/// How the review names the layout: as typed, or as the command names one given no name, saying +/// when it numbers it because a layout has that name. +pub(super) fn name_label(s: &Session, name: &str) -> String { + let name = Some(name.trim()).filter(|n| !n.is_empty()).unwrap_or(DEFAULT_NAME); + if s.project.layouts.iter().any(|l| l.name.eq_ignore_ascii_case(name)) { + i18n::fmt("{name} (numbered: a layout has that name)", &[("name", name)]) + } else { + name.to_string() + } +} + +/// How the review names the area. +pub(super) fn area_label(s: &Session, area: &Area) -> String { + match held(s, area) { + Ok(Held::View) => tl!("The view").to_string(), + Ok(Held::Layer(l)) => i18n::fmt("Layer: {layer}", &[("layer", &l.name)]), + Ok(Held::Selection) => tl!("Selected features").to_string(), + Err(_) => tl!("(none)").to_string(), + } +} + +/// `e` as `layout.publication` takes it: cut to ±180 and ±90, a side of no length widened to +/// [`MIN_SIDE`]; none when nothing is left to frame. +fn framed(e: Extent) -> Option<[f64; 4]> { + if e.is_empty() { + return None; + } + let side = |min: f64, max: f64, limit: f64| { + let pad = if max > min { 0.0 } else { MIN_SIDE / 2.0 }; + let (min, max) = ((min - pad).max(-limit), (max + pad).min(limit)); + (min < max).then_some((min, max)) + }; + let ((west, east), (south, north)) = (side(e.min_x, e.max_x, 180.0)?, side(e.min_y, e.max_y, 90.0)?); + Some([west, south, east, north]) +} + +/// The `extent` to send for `area`, none for the view; a pick that no longer holds is an error +/// rather than another area framed. Read once, at Create, on the UI thread where +/// `layout.publication` runs next: it may walk the whole layer, or the whole selection. +pub(super) fn area_extent(s: &Session, area: &Area) -> Result, String> { + let extent = match held(s, area)? { + Held::View => return Ok(None), + Held::Layer(l) => l.shown_extent(), + Held::Selection => s.map().selection_extent(), + }; + framed(extent).map(Some).ok_or_else(|| tl!("There is nothing to frame there: pick another map extent.").to_string()) +} + +#[cfg(test)] +mod tests { + use super::harness::*; + use super::*; + use egui::accesskit::Role; + use egui_kittest::kittest::{NodeT, Queryable}; + use rustgis_engine::data::{Dataset, GeometryType}; + + fn close(got: Option<[f64; 4]>, want: [f64; 4]) { + let got = got.expect("an extent"); + assert!(got.iter().zip(want).all(|(g, w)| (g - w).abs() < 1e-9), "{got:?} is not {want:?}"); + } + + /// A side of no length (a point, a straight line) is widened to 0.01°, the rest is left as it + /// is; what lies past ±180 and ±90 is cut; nothing left to frame is none. + #[test] + fn an_extent_is_widened_and_cut_to_what_the_command_takes() { + close(framed(Extent::new(10.0, 20.0, 10.0, 20.0)), [9.995, 19.995, 10.005, 20.005]); + close(framed(Extent::new(10.0, 20.0, 12.0, 20.0)), [10.0, 19.995, 12.0, 20.005]); + close(framed(Extent::new(10.0, 20.0, 10.0, 22.0)), [9.995, 20.0, 10.005, 22.0]); + close(framed(Extent::new(180.0, 90.0, 180.0, 90.0)), [179.995, 89.995, 180.0, 90.0]); + close(framed(Extent::new(-190.0, -95.0, 200.0, 91.0)), [-180.0, -90.0, 180.0, 90.0]); + assert_eq!(framed(Extent::EMPTY), None); + assert_eq!(framed(Extent::new(190.0, 0.0, 200.0, 1.0)), None); + } + + /// The view sends no extent; a layer sends the extent of the features it shows; the + /// selection, that of the selected features. + #[test] + fn each_area_sends_its_own_extent() { + let mut app = app(); + let map = MapOwner::of(&app.session); + assert_eq!(area_extent(&app.session, &Area::View), Ok(None)); + app.run("layer.definitionQuery", json!({"layer": "Cities", "where": "pop_max > 10000000"})).unwrap(); + let (id, shown, all) = { + let l = app.session.map().layers.iter().find(|l| l.name == "Cities").unwrap(); + (l.instance_id(), l.shown_extent(), l.extent()) + }; + assert_ne!(shown, all, "the query narrows it"); + assert_eq!(area_extent(&app.session, &Area::Layer { map: map.clone(), layer: id }), Ok(framed(shown))); + app.run("select.byAttributes", json!({"layer": "Cities", "where": "name = 'Tokyo'"})).unwrap(); + let selected = app.session.map().selection_extent(); + let got = area_extent(&app.session, &Area::Selection { map }).unwrap(); + assert_eq!(got, framed(selected)); + assert!(got.is_some_and(|[w, s, e, n]| w < e && s < n), "one point, framed: {got:?}"); + } + + /// The step starts on the view; Layer picks one from the list; Selection is off with nothing + /// selected. + #[test] + fn the_step_picks_the_view_a_layer_or_the_selection() { + let mut app = app(); + open(&mut app); + app.dialogs.publication.as_mut().unwrap().step = Step::Map; + let mut h = harness(app); + let area = |h: &egui_kittest::Harness<'static, RustGisApp>| h.state().dialogs.publication.as_ref().unwrap().area.clone(); + assert_eq!(area(&h), Area::View); + assert!(h.get_by_role_and_label(Role::RadioButton, "Selection").accesskit_node().is_disabled()); + h.get_by_role_and_label(Role::RadioButton, "Layer").click(); + h.run_steps(4); + let top = h.state().session.map().layers.first().unwrap().instance_id(); + assert_eq!(h.state().session.map().layers[0].name, "Cities", "the layer drawn on top"); + assert!(matches!(area(&h), Area::Layer { layer, .. } if layer == top), "{:?}", area(&h)); + h.state_mut().run("select.byAttributes", json!({"layer": "Cities", "where": "name = 'Tokyo'"})).unwrap(); + h.run_steps(2); + h.get_by_role_and_label(Role::RadioButton, "Selection").click(); + h.run_steps(4); + assert!(matches!(area(&h), Area::Selection { .. }), "{:?}", area(&h)); + } + + /// A long layer name is cut to the list's width, inside the window at its smallest. + #[test] + fn the_layer_list_stays_in_the_window() { + let mut app = app(); + open(&mut app); + let long = "A layer with a very long name ".repeat(6); + app.run("layer.rename", json!({"layer": "Cities", "name": long})).unwrap(); + let map = MapOwner::of(&app.session); + let instance = app.session.map().layers[0].instance_id(); + let d = app.dialogs.publication.as_mut().unwrap(); + (d.area, d.step) = (Area::Layer { map, layer: instance }, Step::Map); + let h = harness_sized(app, egui::vec2(420.0, 600.0)); + let list = h.get_by_role(Role::ComboBox).rect(); + assert!(list.right() <= 420.0, "the list runs to {}", list.right()); + } + + /// The review names what Create will make: a selection cleared since reads as none, and a name + /// another layout has (the default's too, case aside) is said to be numbered. + #[test] + fn the_review_says_a_cleared_selection_and_a_taken_name() { + let mut app = app(); + open(&mut app); + app.run("select.byAttributes", json!({"layer": "Cities", "where": "name = 'Tokyo'"})).unwrap(); + let d = app.dialogs.publication.as_mut().unwrap(); + (d.area, d.step) = (Area::Selection { map: MapOwner::of(&app.session) }, Step::Review); + let mut h = harness(app); + assert!(h.query_by_label("Selected features").is_some()); + assert!(h.query_by_label("Publication").is_some()); + h.state_mut().run("select.clear", Value::Null).unwrap(); + h.state_mut().run("layout.new", json!({"name": "publication"})).unwrap(); + h.state_mut().dialogs.open = Some("publicationWizard"); + h.run_steps(4); + assert!(h.query_by_label("Selected features").is_none()); + assert!(h.query_by_label("Publication (numbered: a layout has that name)").is_some()); + h.state_mut().dialogs.publication.as_mut().unwrap().name = " Publication ".into(); + h.run_steps(4); + assert!(h.query_by_label("Publication (numbered: a layout has that name)").is_some()); + } + + /// A layer is offered for having rows, its places left unread until Create: one with none to + /// frame is refused there; a layer with no rows isn't offered. + #[test] + fn a_layer_is_offered_by_its_rows_and_its_places_read_at_create() { + let mut app = app(); + open(&mut app); + let mut unplaced = Dataset::new("No places", GeometryType::Point, vec![]); + unplaced.push(None, vec![]); + app.session.map_mut().add_dataset(unplaced); + app.session.map_mut().add_dataset(Dataset::new("No rows", GeometryType::Point, vec![])); + app.dialogs.publication.as_mut().unwrap().step = Step::Map; + let mut h = harness(app); + h.get_by_role_and_label(Role::RadioButton, "Layer").click(); + h.run_steps(4); + let state = h.state(); + let area = state.dialogs.publication.as_ref().unwrap().area.clone(); + assert_eq!(area_label(&state.session, &area), "Layer: No places"); + assert_eq!(area_extent(&state.session, &area), Err("There is nothing to frame there: pick another map extent.".to_string())); + } + + /// A pick that no longer holds reads the same everywhere: no choice marked on the step, + /// "(none)" in the review, and Create says why instead of framing something else. + #[test] + fn a_pick_that_no_longer_holds_reads_the_same_everywhere() { + const SWITCHED: &str = "The map was switched or renamed since the map extent was picked: pick it again."; + const GONE: &str = "The layer picked is no longer in the map: pick the map extent again."; + const CLEARED: &str = "Nothing is selected: pick the map extent again."; + type Change = fn(&mut RustGisApp); + fn run(app: &mut RustGisApp, id: &str, p: Value) { + app.run(id, p).unwrap(); + } + let cases: [(&str, bool, Change, &str); 9] = [ + ("another map, layer", true, |a| run(a, "map.new", json!({})), SWITCHED), + ("another map, selection", false, |a| run(a, "map.new", json!({})), SWITCHED), + ("map renamed, layer", true, |a| run(a, "map.rename", json!({"name": "Renamed"})), SWITCHED), + ("map renamed, selection", false, |a| run(a, "map.rename", json!({"name": "Renamed"})), SWITCHED), + ("project replaced, layer", true, |a| replace_project(a), SWITCHED), + ("project replaced, selection", false, |a| replace_project(a), SWITCHED), + ("layer removed", true, |a| run(a, "layer.remove", json!({"layer": "Rivers copy"})), GONE), + ( + "paste undone, its id reused", + true, + |a| { + run(a, "edit.undo", Value::Null); + run(a, "layer.copy", json!({"layer": "Lakes"})); + run(a, "layer.paste", json!({})); + }, + GONE, + ), + ("selection cleared", false, |a| run(a, "select.clear", Value::Null), CLEARED), + ]; + for (case, layer, change, why) in cases { + let mut app = app(); + run(&mut app, "select.byAttributes", json!({"layer": "Cities", "where": "name = 'Tokyo'"})); + run(&mut app, "layer.copy", json!({"layer": "Rivers"})); + run(&mut app, "layer.paste", json!({})); + let map = MapOwner::of(&app.session); + let area = if layer { Area::Layer { map, layer: app.session.map().layers[0].instance_id() } } else { Area::Selection { map } }; + change(&mut app); + assert_eq!(area_label(&app.session, &area), "(none)", "{case}"); + assert_eq!(area_extent(&app.session, &area), Err(why.to_string()), "{case}"); + open(&mut app); + let d = app.dialogs.publication.as_mut().unwrap(); + (d.area, d.step) = (area, Step::Map); + let h = harness(app); + for name in ["View", "Layer", "Selection"] { + let radio = h.get_by_role_and_label(Role::RadioButton, name); + assert_ne!(radio.accesskit_node().toggled(), Some(egui::accesskit::Toggled::True), "{case}: {name}"); + } + assert!(h.query_by_role(Role::ComboBox).is_none(), "{case}: no layer list"); + } + } + + /// A project opened over this one, its map named as the old one was. + fn replace_project(app: &mut RustGisApp) { + let name = app.session.map().name.clone(); + app.run("project.new", json!({})).unwrap(); + app.run("map.rename", json!({"name": name})).unwrap(); + } + + /// Create frames the layer picked and names the layout as typed; a figure takes both too. + #[test] + fn create_frames_the_area_and_names_the_layout() { + for size in ["a4", "figure-1col"] { + let mut app = app(); + open(&mut app); + let map = MapOwner::of(&app.session); + app.run("select.byAttributes", json!({"layer": "Cities", "where": "name = 'Tokyo'"})).unwrap(); + let [w, s, e, n] = framed(app.session.map().selection_extent()).unwrap(); + { + let d = app.dialogs.publication.as_mut().unwrap(); + d.size = size.into(); + d.area = Area::Selection { map }; + d.name = " Tokyo sheet ".into(); + d.step = Step::Review; + } + let mut h = harness(app); + click(&mut h, "Create"); + let l = h.state().session.project.layouts.last().unwrap(); + assert_eq!(l.name, "Tokyo sheet", "{size}"); + let center = l.elements.iter().find_map(|el| match &el.kind { + rustgis_engine::map::layout::ElementKind::MapFrame { center, .. } if el.name == "Map Frame" => Some(*center), + _ => None, + }); + let c = center.expect("the map frame"); + assert!(c.x > w - 0.01 && c.x < e + 0.01 && c.y > s - 0.01 && c.y < n + 0.01, "{size}: {c:?}"); + } + } + + /// An area gone at Create is said, and the wizard stays with nothing made. + #[test] + fn create_with_the_area_gone_is_said_and_makes_nothing() { + let mut app = app(); + open(&mut app); + let map = MapOwner::of(&app.session); + { + let d = app.dialogs.publication.as_mut().unwrap(); + d.area = Area::Selection { map }; + d.step = Step::Review; + } + let before = app.session.project.layouts.len(); + let mut h = harness(app); + let said = h.state().session.notice_mark(); + click(&mut h, "Create"); + let app = h.state(); + assert_eq!(app.session.project.layouts.len(), before); + assert_eq!(app.session.notice_mark(), said + 1); + assert_eq!(app.dialogs.open, Some("publicationWizard")); + } + + /// The name goes only when typed, for a sheet and for a figure. + #[test] + fn the_name_is_sent_only_when_typed() { + let app = app(); + let mut d = Draft::new(&app.session); + d.name = " ".into(); + assert!(d.params().get("name").is_none()); + d.name = "Rivers".into(); + assert_eq!(d.params()["name"], "Rivers"); + d.size = "figure-1col".into(); + assert_eq!(d.params(), json!({"profile": "figure-1col", "name": "Rivers"})); + } +} diff --git a/crates/ui-egui/src/publicationwizard/mod.rs b/crates/ui-egui/src/publicationwizard/mod.rs index fe7fef2888..6081d3b5f4 100644 --- a/crates/ui-egui/src/publicationwizard/mod.rs +++ b/crates/ui-egui/src/publicationwizard/mod.rs @@ -1,7 +1,8 @@ -//! Publication Layout Wizard: `layout.publication` asked step by step (size, style, texts, sources -//! and reference, accent), then made at once, as the New Layout › Publication Layout menu makes it -//! (#2850). +//! Publication Layout Wizard: `layout.publication` asked step by step (size, map, style, texts, +//! sources and reference, accent), then made at once, as the New Layout › Publication Layout menu +//! makes it (#2850). mod accent; +mod map; mod review; mod size; mod sources; @@ -11,12 +12,14 @@ mod texts; use crate::textform::StatLayer; use crate::{RustGisApp, i18n, theme::Tokens}; use accent::accent_step; +use map::{Area, area_extent, map_step}; use review::review_step; use rustgis_engine::Session; use rustgis_engine::cmd::layout::{ MAX_PUBLICATION_LINES, MAX_PUBLICATION_TEXT, PUBLICATION_DEFAULT_REFERENCE, PUBLICATION_HEADINGS, PUBLICATION_PROFILES, publication_style_accent, }; +use rustgis_engine::geom::Extent; use rustgis_engine::map::Map; use rustgis_engine::map::layout::PAGES; use serde_json::{Value, json}; @@ -29,6 +32,7 @@ use texts::texts_step; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Step { Size, + Map, Style, Texts, Sources, @@ -88,6 +92,9 @@ pub struct Draft { /// A page size's id (`a4`, from [`PAGES`]) or a figure's profile id (`figure-1col`). pub size: String, pub orientation: Orientation, + pub area: Area, + /// The layout's name; empty leaves it to the command. + pub name: String, pub style: String, pub title: String, /// Whether the title was typed; until then it is the active map's name, as the menu's. @@ -126,6 +133,8 @@ impl Draft { step: Step::Size, size: "a4".into(), orientation: Orientation::Auto, + area: Area::View, + name: String::new(), style: "classic".into(), title: s.map().name.clone(), title_typed: false, @@ -160,9 +169,9 @@ impl Draft { /// The steps this draft goes through, in order. fn steps(&self) -> &'static [Step] { if self.figure() { - return &[Step::Size, Step::Review]; + return &[Step::Size, Step::Map, Step::Review]; } - &[Step::Size, Step::Style, Step::Texts, Step::Sources, Step::Accent, Step::Review] + &[Step::Size, Step::Map, Step::Style, Step::Texts, Step::Sources, Step::Accent, Step::Review] } /// The step `by` places from this one, if there is one. @@ -181,6 +190,9 @@ impl Draft { (false, Orientation::Landscape) => format!("{}-landscape", self.size), }; let mut p = json!({"profile": profile}); + if !self.name.trim().is_empty() { + p["name"] = json!(self.name.trim()); + } if self.figure() { return p; } @@ -334,6 +346,7 @@ fn size_label(id: &str) -> String { fn step_name(step: Step) -> &'static str { match step { Step::Size => tl_raw!("Size"), + Step::Map => tl_raw!("Map"), Step::Style => tl_raw!("Style"), Step::Texts => tl_raw!("Texts"), Step::Sources => tl_raw!("Sources and reference"), @@ -346,6 +359,7 @@ fn step_name(step: Step) -> &'static str { fn step_label(step: Step) -> &'static str { match step { Step::Size => tl!("Size"), + Step::Map => tl!("Map"), Step::Style => tl!("Style"), Step::Texts => tl!("Texts"), Step::Sources => tl!("Sources"), @@ -469,11 +483,12 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { .auto_shrink([false, false]) .show(ui, |ui| match d.step { Step::Size => size_step(ui, &mut d), + Step::Map => map_step(ui, &app.session, &mut d), Step::Style => style_step(ui, &mut d, t.accent), Step::Texts => texts_step(ui, &mut d, app.session.map()), Step::Sources => sources_step(ui, &mut d, app.session.map()), Step::Accent => accent_step(ui, &mut d, t.accent), - Step::Review => review_step(ui, &d), + Step::Review => review_step(ui, &app.session, &d), }); }); }); @@ -499,8 +514,17 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { }); }); // A layout the command refuses is said (a map with no extent), and the wizard stays. - if create && crate::ribbon::make_publication(app, d.params()) { - close = true; + if create { + match area_extent(&app.session, &d.area) { + Ok(extent) => { + let mut params = d.params(); + if let Some(extent) = extent { + params["extent"] = json!(extent); + } + close |= crate::ribbon::make_publication(app, params); + } + Err(e) => app.session.notify(e, true), + } } if !open || close { app.dialogs.open = None; @@ -581,6 +605,7 @@ mod tests { assert_eq!(d.title, name); assert!(d.subtitle.is_empty() && d.note.is_empty() && d.sources.is_empty() && d.author.is_empty()); assert!(d.accent.is_none() && !d.no_author); + assert!(d.area == Area::View && d.name.is_empty()); assert_eq!(app.dialogs.open, Some("publicationWizard")); app.dialogs.publication.as_mut().unwrap().subtitle = "Kept".into(); open(&mut app); @@ -618,8 +643,8 @@ mod tests { assert_eq!(d.params(), json!({"profile": "figure-1col"})); } - /// A sheet goes through the six steps; a figure goes from its size to the review, the steps - /// it takes nothing from skipped both ways. Back is off on the first step; Next becomes + /// A sheet goes through the seven steps; a figure goes from its size to its map and the review, + /// the steps it takes nothing from skipped both ways. Back is off on the first step; Next becomes /// Create on the last. #[test] fn a_sheet_takes_every_step_and_a_figure_skips_what_it_doesnt_draw() { @@ -627,22 +652,26 @@ mod tests { open(&mut app); let mut h = harness(app); assert!(h.get_by_role_and_label(Role::Button, "Back").accesskit_node().is_disabled(), "Back is off"); - for want in [Step::Style, Step::Texts, Step::Sources, Step::Accent, Step::Review] { + for want in [Step::Map, Step::Style, Step::Texts, Step::Sources, Step::Accent, Step::Review] { click(&mut h, "Next"); assert_eq!(step(&h), want); } assert!(h.query_by_role_and_label(Role::Button, "Next").is_none()); assert!(h.query_by_role_and_label(Role::Button, "Create").is_some()); - for want in [Step::Accent, Step::Sources, Step::Texts, Step::Style, Step::Size] { + for want in [Step::Accent, Step::Sources, Step::Texts, Step::Style, Step::Map, Step::Size] { click(&mut h, "Back"); assert_eq!(step(&h), want); } h.state_mut().dialogs.publication.as_mut().unwrap().size = "figure-2col".into(); h.run_steps(2); - click(&mut h, "Next"); - assert_eq!(step(&h), Step::Review); - click(&mut h, "Back"); - assert_eq!(step(&h), Step::Size); + for want in [Step::Map, Step::Review] { + click(&mut h, "Next"); + assert_eq!(step(&h), want); + } + for want in [Step::Map, Step::Size] { + click(&mut h, "Back"); + assert_eq!(step(&h), want); + } } /// Create makes the layout in one step, closes the wizard and opens the Publish tab, as the @@ -700,7 +729,7 @@ mod tests { /// The names the step list holds, in order, and the one it marks as the current step. fn listed(h: &egui_kittest::Harness<'static, RustGisApp>) -> (Vec<&'static str>, Vec<&'static str>) { let (mut names, mut current) = (Vec::new(), Vec::new()); - for name in ["Size", "Style", "Texts", "Sources", "Accent", "Review"] { + for name in ["Size", "Map", "Style", "Texts", "Sources", "Accent", "Review"] { if let Some(n) = h.query_by_role_and_label(Role::Button, name) { names.push(name); if n.accesskit_node().toggled() == Some(egui::accesskit::Toggled::True) { @@ -735,13 +764,13 @@ mod tests { let mut app = app(); open(&mut app); let mut h = harness(app); - assert_eq!(listed(&h), (vec!["Size", "Style", "Texts", "Sources", "Accent", "Review"], vec!["Size"])); + assert_eq!(listed(&h), (vec!["Size", "Map", "Style", "Texts", "Sources", "Accent", "Review"], vec!["Size"])); click(&mut h, "Next"); - assert_eq!(listed(&h).1, ["Style"]); + assert_eq!(listed(&h).1, ["Map"]); click(&mut h, "Back"); h.state_mut().dialogs.publication.as_mut().unwrap().size = "figure-1col".into(); h.run_steps(2); - assert_eq!(listed(&h), (vec!["Size", "Review"], vec!["Size"])); + assert_eq!(listed(&h), (vec!["Size", "Map", "Review"], vec!["Size"])); } /// A step clicked in the list is the one shown, first, middle or last, keeping what was @@ -772,13 +801,13 @@ mod tests { for _ in 0..30 { h.key_press(egui::Key::Tab); h.run_steps(2); - for name in ["Size", "Style", "Texts", "Sources", "Accent", "Review"] { + for name in ["Size", "Map", "Style", "Texts", "Sources", "Accent", "Review"] { if h.query_by_role_and_label(Role::Button, name).is_some_and(|n| n.accesskit_node().is_focused()) { reached.push(name); } } } - for name in ["Size", "Style", "Texts", "Sources", "Accent", "Review"] { + for name in ["Size", "Map", "Style", "Texts", "Sources", "Accent", "Review"] { assert!(reached.contains(&name), "{name} not reached: {reached:?}"); } } @@ -799,9 +828,9 @@ mod tests { assert!((415.0..=460.0).contains(&small.width()) && (355.0..=420.0).contains(&small.height()), "smallest {small:?}"); let next = h.get_by_role_and_label(Role::Button, "Next").rect(); assert!(small.contains_rect(next), "Next at {next:?} outside {small:?}"); - assert_eq!(listed(&h).0.len(), 6, "the list stays"); + assert_eq!(listed(&h).0.len(), 7, "the list stays"); // Each step wraps or scrolls inside that size: none makes the window wider or taller. - for _ in 0..5 { + for _ in 0..6 { click(&mut h, "Next"); // Within the point a high-density screen's rounding of a wrapped line may add. let now = window(&h).size(); @@ -824,7 +853,7 @@ mod tests { let larger = at(&h); let moved = larger.right_bottom() - before.right_bottom(); assert!(moved.y > 50.0, "Next moved by {moved:?}"); - for _ in 0..4 { + for _ in 0..5 { click(&mut h, "Next"); assert_eq!(at(&h), larger, "{:?}", step(&h)); } @@ -838,7 +867,7 @@ mod tests { let mut h = harness(app); let at = |h: &egui_kittest::Harness<'static, RustGisApp>| h.get_by_role_and_label(Role::Button, "Next").rect(); let first = at(&h); - for _ in 0..4 { + for _ in 0..5 { click(&mut h, "Next"); assert_eq!(at(&h), first, "{:?}", step(&h)); } @@ -859,7 +888,7 @@ mod tests { assert_ne!(next, "Next", "{code}: the language changed"); let at = |h: &egui_kittest::Harness<'static, RustGisApp>| h.get_by_role_and_label(Role::Button, &next).rect(); let first = at(&h); - for _ in 0..4 { + for _ in 0..5 { h.get_by_role_and_label(Role::Button, &next).click(); h.run_steps(4); assert_eq!(at(&h), first, "{code}: {:?}", step(&h)); @@ -890,7 +919,7 @@ mod tests { let text = h.get_by_label(about).rect(); assert!(text.right() <= 320.0, "{text:?} runs off a screen 320 wide"); assert_eq!(listed(&h).0, Vec::<&str>::new(), "the list is hidden"); - assert!(h.query_by_label("Step 2 of 6: Style").is_some(), "the heading still says the step"); + assert!(h.query_by_label("Step 3 of 7: Style").is_some(), "the heading still says the step"); } /// A screen that narrows while the wizard is open (a phone turned, a browser resized) keeps diff --git a/crates/ui-egui/src/publicationwizard/review.rs b/crates/ui-egui/src/publicationwizard/review.rs index 897edc3d63..b9cc236678 100644 --- a/crates/ui-egui/src/publicationwizard/review.rs +++ b/crates/ui-egui/src/publicationwizard/review.rs @@ -1,8 +1,9 @@ //! The wizard's review step. +use super::map::{area_label, name_label}; use super::*; /// The review step: what was picked, step by step. -pub(super) fn review_step(ui: &mut egui::Ui, d: &Draft) { +pub(super) fn review_step(ui: &mut egui::Ui, s: &Session, d: &Draft) { let none = tl!("(none)"); let or_none = |t: &str| if t.trim().is_empty() { none.to_string() } else { t.trim().to_string() }; egui::Grid::new("publication_wizard_review").num_columns(2).spacing(egui::vec2(12.0, 6.0)).show(ui, |ui| { @@ -13,6 +14,8 @@ pub(super) fn review_step(ui: &mut egui::Ui, d: &Draft) { ui.end_row(); }; row(tl!("Size"), size_label(&d.size)); + row(tl!("Map extent"), area_label(s, &d.area)); + row(tl!("Name"), name_label(s, &d.name)); if d.figure() { return; } diff --git a/crates/ui-egui/src/publicationwizard/texts.rs b/crates/ui-egui/src/publicationwizard/texts.rs index ebfdcacd80..006f0c2456 100644 --- a/crates/ui-egui/src/publicationwizard/texts.rs +++ b/crates/ui-egui/src/publicationwizard/texts.rs @@ -69,8 +69,9 @@ mod tests { h.state_mut().run("map.new", json!({"name": "Rivers"})).unwrap(); h.run_steps(2); assert_eq!(h.state().dialogs.publication.as_ref().unwrap().title, "Rivers"); - click(&mut h, "Next"); - click(&mut h, "Next"); + for _ in 0..3 { + click(&mut h, "Next"); + } h.get_by_label("Title").focus(); h.run_steps(1); h.get_by_label("Title").type_text(" basins"); diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index bb6400ce4c..d3aa07e0eb 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -40,20 +40,26 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu datum under it, at 7 pt. A figure's title and sources belong in its caption, so they are left out, and the result says so. - **Step by step**: **Publication Layout Wizard…**, at the end of the same menu, asks for the size - (a sheet turned Auto, Portrait or Landscape, or a figure), the style (each beside an outline of - where its title, map and footer go, and its column or panel if it has one, with the accent - colour picked, or else its own, where the sheet takes it, which Classic's doesn't; a click on - the outline picks the style), the texts (title, the active map's name until one is typed; - subtitle, note and credit line, or none; an Editorial sheet's column heading), the sources and - reference (each one per line, with its heading; the reference can be left out) and the accent - colour, then shows what was picked; **Create** makes the layout as the menu does. **Insert - Statistic**, on the texts and sources steps, adds a layer figure of the active map to the field - the cursor was last in there (the note or the sources, where it was in none). A figure goes from - its size to the review, as it takes no style, texts or accent. A field left empty is left out - (for the credit line, the reference and the headings, the command's default applies); the title - is the exception: empty, it means no title. A layout the command refuses is said, and the wizard - stays open. The window can be resized. Its steps are listed on the left, and a click goes to - one; a narrow screen leaves the list out, and the heading still says which step this is. + (a sheet turned Auto, Portrait or Landscape, or a figure), the map extent and the layout's name, + the style (each beside an outline of where its title, map and footer go, and its column or panel + if it has one, with the accent colour picked, or else its own, where the sheet takes it, which + Classic's doesn't; a click on the outline picks the style), the texts (title, the active map's + name until one is typed; subtitle, note and credit line, or none; an Editorial sheet's column + heading), the sources and reference (each one per line, with its heading; the reference can be + left out) and the accent colour, then shows what was picked; **Create** makes the layout as the + menu does. **Insert Statistic**, on the texts and sources steps, adds a layer figure of the + active map to the field the cursor was last in there (the note or the sources, where it was in + none). The map extent is the view, the features one layer shows (hidden or not, in their + longitude/latitude box) or the selected features (a point or a straight line is widened to + 0.01°, and what lies past ±180° or ±90° is left out of the frame; an area with nothing to frame, + one gone by Create, or one picked before the map or the project was switched or the map renamed, + is said and nothing is made); without a name the layout is `Publication`; the review says when a + layout has that name, so the new one is numbered. A figure goes from its size to its map and the + review, as it takes no style, texts or accent. A field left empty is left out (for the credit + line, the reference and the headings, the command's default applies); the title is the + exception: empty, it means no title. A layout the command refuses is said, and the wizard stays + open. The window can be resized. Its steps are listed on the left, and a click goes to one; a + narrow screen leaves the list out, and the heading still says which step this is. - **The map**: the `extent` given (or the view) fitted north-up, without tilt, at a round scale, two significant figures, rounded up so it still fits within the strips the graticule's labels take inside the frame. A graticule's step follows the extent (2° for a state, 15′ for a city),