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
16 changes: 8 additions & 8 deletions crates/engine/src/cmd/layout/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,14 @@ pub(in crate::cmd::layout) fn chart_update(
Ok(Some((o, Some(to.name.clone()), said)))
}

/// Says [`chart_notices`] of the chart `id` of layout `li`: what it leaves out of what it is told
/// to draw (an existing loss made said) and whether its box is too small for it. It reads each
/// feature of the chart's layer once, on the command's thread, as drawing the chart does on every
/// draw of the page: run when a chart is added, told what to draw or coloured by classes; resized
/// or restyled, it reads them too, for [`say_chart_crowded`] to say the last alone: whether the
/// box is too small depends on the bars, and the Layout view reads them on this thread as well
/// when it draws the edited page. An export
/// says [`chart_notices`] of what its pages drew.
/// Says [`chart_notices`] of the chart `id` of layout `li`: what it leaves out of what it is told to
/// draw (an existing loss made said) and whether its box is too small for it. It reads the chart's
/// layer, its definition query over every feature and then the features it keeps, on the command's
/// thread, as drawing the chart does on every draw of the page: run when a chart is added, told what to
/// draw or coloured by classes; resized or restyled, it reads them too, for [`say_chart_crowded`] to
/// say the last alone: whether the box is too small depends on the bars, and the Layout view reads them
/// on this thread as well when it draws the edited page. An export says [`chart_notices`] of what its
/// pages drew.
pub(in crate::cmd::layout) fn say_chart_losses(s: &mut Session, li: usize, id: u32) {
say_chart(s, li, id, false);
}
Expand Down
9 changes: 5 additions & 4 deletions crates/engine/src/cmd/layout/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,15 @@ pub(in crate::cmd::layout) fn held_rows(p: &Value) -> Option<String> {

/// Says the rows and columns the box of the table `id` of layout `li` leaves out (#3416), and a
/// sort field its layer no longer has (#3409), measured as [`super::text::text_fit_loss`] measures
/// a text; nothing of another element.
/// a text; nothing of another element. With a definition query on the table's layer, the count
/// reads it over every feature, once, on the command's thread and without a look at Cancel, as
/// drawing the table does on every draw of the page.
pub(in crate::cmd::layout) fn say_table_cut(s: &mut Session, li: usize, id: u32) {
let Some(l) = s.project.layouts.get(li) else { return };
// The counts don't depend on the resolution: the box, its columns and its rows scale with it.
// Nothing is drawn here, so a layer read in place counts the rows it holds now.
let mut cut = rustgis_render::layout::tables_cut(l, &s.project.maps, 300.0);
cut.retain(|t| t.id == id);
for note in rustgis_render::layout::table_notices(&cut) {
let cut = rustgis_render::layout::table_cut(l, id, &s.project.maps, 300.0);
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
for note in rustgis_render::layout::table_notices(cut.as_slice()) {
s.warn(note);
}
}
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 @@ -193,6 +193,9 @@ mod layer_ids;
#[path = "tests/layout_table.rs"]
mod layout_table;

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

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

Expand Down
178 changes: 178 additions & 0 deletions crates/engine/src/tests/layout_definition_query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//! A layout table and chart list only what their layer's definition query keeps (#3478).

use super::*;
use rustgis_data::{Dataset, Field, FieldType, Geometry, GeometryType, Value as Data};

/// `region`, `n` and whether the query `shown = 1` keeps the row.
const SHOPS: [(&str, i64, bool); 7] =
[("E", 40, true), ("N", 90, false), ("S", 10, true), ("E", 70, false), ("N", 30, true), ("W", 80, false), ("S", 20, true)];

/// A blank layout "Shops page" over a map with the points layer "Shops" holding `rows`, and the
/// layer "Pages" of two features to run a series over.
fn session(rows: &[(&str, i64, bool)]) -> Session {
let mut s = Session::new(sample::world_project());
let point = || Some(Geometry::Point(rustgis_data::Coord::new(0.0, 0.0)));
let mut ds = Dataset::new(
"Shops",
GeometryType::Point,
vec![Field::new("region", FieldType::Text), Field::new("n", FieldType::Integer), Field::new("shown", FieldType::Integer)],
);
for (region, n, shown) in rows {
ds.push(point(), vec![Data::Text((*region).into()), Data::Int(*n), Data::Int(i64::from(*shown))]);
}
s.map_mut().add_dataset(ds);
let mut pages = Dataset::new("Pages", GeometryType::Point, vec![Field::new("p", FieldType::Integer)]);
for p in [1, 2] {
pages.push(point(), vec![Data::Int(p)]);
}
s.map_mut().add_dataset(pages);
s.run("layout.new", &json!({"name": "Shops page"})).unwrap();
s.run("layout.addFrame", &json!({"x": 5, "y": 5, "w": 80, "h": 50})).unwrap();
s
}

/// The rows the query keeps, as a layer of their own.
fn kept() -> Vec<(&'static str, i64, bool)> {
SHOPS.into_iter().filter(|r| r.2).collect()
}

fn query(s: &mut Session, text: &str) {
s.run("layer.definitionQuery", &json!({"layer": "Shops", "where": text})).unwrap();
}

/// Adds an element of Shops, large enough for every row, with `extra` keys; its id.
fn add(s: &mut Session, kind: &str, extra: &Value) -> u64 {
let mut p = json!({"kind": kind, "layer": "Shops", "x": 100, "y": 10, "w": 90, "h": 90});
if kind == "chart" {
p["field"] = json!("n");
}
if let (Some(p), Some(e)) = (p.as_object_mut(), extra.as_object()) {
p.extend(e.clone());
}
s.run("layout.addElement", &p).unwrap()["element"].as_u64().unwrap()
}

fn page(s: &mut Session) -> Vec<u8> {
crate::cmd::layout::layout_png(s, 0, 96.0, None).unwrap()
}

/// The page with an element of `extra` keys over all of Shops under `text`.
fn queried(kind: &str, extra: &Value, text: &str) -> Vec<u8> {
let mut s = session(&SHOPS);
query(&mut s, text);
add(&mut s, kind, extra);
page(&mut s)
}

/// The same element over a layer holding `rows` alone, without a query.
fn holding(kind: &str, extra: &Value, rows: &[(&str, i64, bool)]) -> Vec<u8> {
let mut s = session(rows);
add(&mut s, kind, extra);
page(&mut s)
}

#[test]
fn a_table_and_a_chart_draw_only_what_the_query_keeps() {
for (kind, extra) in [
("table", json!({})),
("table", json!({"sort": "n"})),
("table", json!({"sort": "n", "descending": true, "rows": 2})),
("chart", json!({"label": "region"})),
("chart", json!({"label": "region", "bars": 2, "other": true})),
("chart", json!({"group": "region", "statistic": "sum"})),
("chart", json!({"group": "region", "statistic": "count"})),
("chart", json!({"group": "region", "statistic": "mean", "values": true})),
("chart", json!({"group": "region", "statistic": "sum", "stack": "shown"})),
] {
let want = holding(kind, &extra, &kept());
assert_ne!(want, holding(kind, &extra, &SHOPS), "{kind} {extra}: the hidden rows show in a drawing");
assert_eq!(queried(kind, &extra, "shown = 1"), want, "{kind} {extra}");
// A query keeping nothing leaves what an empty layer draws.
assert_eq!(queried(kind, &extra, "n < 0"), holding(kind, &extra, &[]), "{kind} {extra}");
}
}

#[test]
fn a_query_that_is_blank_or_does_not_read_keeps_every_row() {
for kind in ["table", "chart"] {
let want = holding(kind, &json!({}), &SHOPS);
for text in ["", " "] {
assert_eq!(queried(kind, &json!({}), text), want, "{kind} '{text}'");
}
let mut s = session(&SHOPS);
s.map_mut().layers.iter_mut().find(|l| l.name == "Shops").unwrap().definition_query = Some("n = = 1".into());
add(&mut s, kind, &json!({}));
assert_eq!(page(&mut s), want, "{kind}");
}
}

#[test]
fn a_table_of_the_selected_rows_leaves_out_the_hidden_ones() {
let extra = json!({"selectedOnly": true});
let selected = |rows: &[(&str, i64, bool)], text: Option<&str>, selection: Vec<usize>| {
let mut s = session(rows);
if let Some(text) = text {
query(&mut s, text);
}
s.map_mut().layers.iter_mut().find(|l| l.name == "Shops").unwrap().selection = selection;
add(&mut s, "table", &extra);
page(&mut s)
};
// Rows 1 and 3 are hidden; 0 and 4 are the first and third of those kept.
assert_eq!(selected(&SHOPS, Some("shown = 1"), vec![0, 1, 3, 4]), selected(&kept(), None, vec![0, 2]));
assert_eq!(selected(&SHOPS, Some("shown = 1"), vec![1, 3]), selected(&kept(), None, vec![]));
}

#[test]
fn a_series_page_lists_what_the_query_keeps_on_it() {
for kind in ["table", "chart"] {
let mut s = session(&SHOPS);
// The first page keeps the rows up to 10.
query(&mut s, "n <= @page_number * 10");
add(&mut s, kind, &json!({}));
s.run("layout.series", &json!({"layer": "Pages"})).unwrap();
let mut want = session(&[("S", 10, true)]);
add(&mut want, kind, &json!({}));
want.run("layout.series", &json!({"layer": "Pages"})).unwrap();
assert_eq!(page(&mut s), page(&mut want), "{kind}");
}
}

#[test]
fn undo_gives_the_hidden_rows_back() {
for kind in ["table", "chart"] {
let mut s = session(&SHOPS);
add(&mut s, kind, &json!({}));
let all = page(&mut s);
query(&mut s, "shown = 1");
assert_eq!(page(&mut s), holding(kind, &json!({}), &kept()), "{kind}");
s.run("edit.undo", &Value::Null).unwrap();
assert_eq!(page(&mut s), all, "{kind}");
}
}

/// What exporting the page says of its tables and charts.
fn exported(s: &mut Session) -> Vec<String> {
let dir = std::env::temp_dir().join(format!("rustgis-3478-{}-{:?}", std::process::id(), std::thread::current().id()));
std::fs::create_dir_all(&dir).unwrap();
let mark = s.notice_mark();
s.run("layout.export", &json!({"path": dir.join("page.pdf"), "dpi": 36})).unwrap();
std::fs::remove_dir_all(&dir).unwrap();
s.notices_since(mark).iter().map(|n| n.text.clone()).filter(|t| t.contains("Table") || t.contains("Chart")).collect()
}

#[test]
fn what_is_said_to_be_left_out_counts_the_kept_rows_alone() {
let mut s = session(&SHOPS);
query(&mut s, "shown = 1");
// Four rows kept of the six asked for: all of them fit, and nothing is cut.
add(&mut s, "table", &json!({"rows": 6}));
let bars = add(&mut s, "chart", &json!({"bars": 3}));
let said = exported(&mut s);
assert_eq!(said.len(), 1, "{said:?}");
assert!(said[0].contains(&format!("({bars}): the chart draws 3 of 4 bars")), "{said:?}");
// A box for one row of the four.
let small = add(&mut s, "table", &json!({"h": 11}));
let said = exported(&mut s);
assert!(said.iter().any(|t| t.contains(&format!("({small}): its box shows 1 of 4 rows"))), "{said:?}");
}
6 changes: 3 additions & 3 deletions crates/map/src/layout/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl Shade {
}

/// The values of a field a chart groups or stacks its bars by, each numbered as it first comes
/// ([`ElementKind::chart_data`]). Each name is held once, in `index`; a text is looked up as it
/// ([`ElementKind::chart_data_of`]). Each name is held once, in `index`; a text is looked up as it
/// is, and only a new value's name is copied. A number or a yes/no is looked up by its value in
/// `by_value` first, so it is written out once per value, not per feature: a value first seen goes
/// through `index` by its name, so values written alike (3 and 3.0) stay one. No value, or only
Expand Down Expand Up @@ -326,7 +326,7 @@ pub(super) fn add_cell(
s.merge(shade);
}

/// A stacked chart's key and stretches ([`ElementKind::chart_data`]) from the total of each pair of
/// A stacked chart's key and stretches ([`ElementKind::chart_data_of`]) from the total of each pair of
/// a group value and a stack value (`cells`), the bar each group is drawn in (`bar_of`, an Other
/// bar's groups all in the last of `bars`) and the stack values' names; `coloured`, each pair's
/// colour gives a value the one its features share over the bars shown. The key holds the values
Expand Down Expand Up @@ -405,7 +405,7 @@ pub(super) fn stack_bars(
(key, stretches, kept, gathered, whole)
}

/// A chart's group value looked up by itself rather than by its name ([`ElementKind::chart_data`]):
/// A chart's group value looked up by itself rather than by its name ([`ElementKind::chart_data_of`]):
/// a whole number, a number's bits, or a yes/no. The same key is always written alike, so a bar
/// found by it is the bar of that name.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
Expand Down
Loading
Loading