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
17 changes: 17 additions & 0 deletions crates/render/src/layout/legend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,23 @@ pub fn class_row_labels(l: &rustgis_map::Layer) -> Vec<String> {
entries.into_iter().map(|e| e.label).collect()
}

/// Whether [`class_row_labels`] lists any row for a layer, without making a classed layer's rows:
/// asked for every row of a legend's Layers section on every frame (#3306), where making a Unique
/// Values layer's would sort and merge all its classes. A raster's are made, few and from kept
/// statistics.
pub fn has_class_rows(l: &rustgis_map::Layer) -> bool {
if !legend_lists(l) {
return false;
}
if l.is_raster() {
return !every_class(l).is_empty();
}
// Every renderer but a single symbol lists a row, even with no class: Unique Values its
// fallback, Proportional its sizes, Graduated Colors its "No data".
let classed = !matches!(l.renderer, rustgis_map::Renderer::Single(_));
classed || !l.with_size_legend(Vec::new()).is_empty() || l.diagram.as_ref().is_some_and(|d| !d.categories.is_empty())
}

/// The rows of the legend of a frame's map. With `drawn` (from [`shown_legends`]) it shows what the
/// frame draws: only the layers listed there, with their classes. Without, every class of every
/// visible layer. `opts` leaves out `<all other values>` and the layers it names, either way, and
Expand Down
35 changes: 35 additions & 0 deletions crates/render/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8406,6 +8406,41 @@ fn a_legend_labels_a_layers_classes_itself() {
};
assert_eq!(labels_of(&legend_rows(&map, None, &opts(&listed, false))), ["c0", "c1", "c99"]);
assert_eq!(class_row_labels(&map.layers[0]), ["c0", "c1", "c99"]);
for l in &map.layers {
assert_eq!(has_class_rows(l), !class_row_labels(l).is_empty(), "{}", l.name);
}
// Graduated Colors with no class, as an imported style can bring: its "No data" row is one.
let mut empty = rows_map(1);
let l = &mut empty.layers[0];
l.renderer = rustgis_map::Renderer::Graduated {
field: "g".into(),
breaks: Vec::new(),
symbols: Vec::new(),
ramp: rustgis_map::Ramp::Ember,
method: rustgis_map::ClassMethod::NaturalBreaks,
min: 0.0,
classes: 0,
class_labels: Default::default(),
class_strokes: Default::default(),
class_fills: Default::default(),
};
assert_eq!(class_row_labels(l), ["No data"]);
assert!(has_class_rows(l));
// One symbol: no row of its own until a diagram brings its categories.
l.renderer = rustgis_map::Renderer::Single(rustgis_map::Symbol::default_for(GeometryType::Point, 0));
assert!(class_row_labels(l).is_empty() && !has_class_rows(l));
l.diagram = Some(rustgis_map::DiagramSettings {
kind: rustgis_map::DiagramKind::Pie,
categories: vec![rustgis_map::DiagramCategory { field: "g".into(), colour: Rgba::rgb(255, 0, 0) }],
size: rustgis_map::DiagramSize::Fixed(12.0),
start_angle: 0.0,
background: None,
stroke: rustgis_map::DiagramSettings::default_stroke(),
stroke_width: 0.75,
labels: rustgis_map::DiagramLabel::None,
});
assert_eq!(class_row_labels(l), ["g"]);
assert!(has_class_rows(l));
let mut own = ClassLabels::default();
own.by_layer.insert(
"PTS".into(),
Expand Down
166 changes: 127 additions & 39 deletions crates/ui-egui/src/layoutview/format/legend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::widgets::Dragged;
use egui::{Ui, vec2};
use rustgis_engine::map::Drawn;
use rustgis_engine::map::layout::{Element, ElementKind, Layout, LegendFit, MAX_LEGEND_COLUMNS};
use rustgis_engine::render::layout::{legend_lists, legend_place};
use rustgis_engine::render::layout::{class_row_labels, has_class_rows, legend_lists, legend_place};
use serde_json::{Value, json};

/// A legend's rows in the grid.
Expand Down Expand Up @@ -99,13 +99,26 @@ pub(super) fn legend_property_rows(app: &mut RustGisApp, ui: &mut Ui, li: usize,
}
}

/// Over this many layers, a legend's Layers section has a box that filters them by name.
const FILTER_OVER: usize = 8;

/// The height its rows scroll in, in points.
const LAYERS_HEIGHT: f32 = 320.0;

/// `text` as the Layers filter compares it: lower case a character at a time, as
/// `str::to_lowercase` turns a capital sigma into one letter or another by its place, and the final
/// sigma as the other.
fn folded(text: &str) -> String {
text.chars().flat_map(char::to_lowercase).map(|c| if c == 'ς' { 'σ' } else { c }).collect()
}

/// The layers a legend lists, below the grid.
pub(super) fn legend_layers(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, e: &Element, update: &dyn Fn(&mut RustGisApp, Value)) {
let id = e.id;
// The layers a legend lists: a checkbox for each vector layer of its frame's map, each raster
// drawn by classes (#1679) and each WMS or WMTS layer with a legend, ticked when listed. Names
// gone from the map drop out of the list as it's written back.
if let ElementKind::Legend { frame, exclude, layer_order, .. } = &e.kind {
if let ElementKind::Legend { frame, exclude, layer_order, class_labels, .. } = &e.kind {
let map = layout.element(*frame).and_then(|f| match &f.kind {
ElementKind::MapFrame { map, .. } => app.session.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(map)),
_ => None,
Expand Down Expand Up @@ -136,61 +149,136 @@ pub(super) fn legend_layers(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout,
let kept: Vec<String> = exclude.iter().filter(|x| on_map(x)).cloned().collect();
// The same for the names ordered that aren't listed here.
let unlisted: Vec<&String> = layer_order.iter().filter(|x| on_map(x) && !names.iter().any(|n| n.eq_ignore_ascii_case(x))).collect();
// What a row or button asks for, sent once the map is no longer read.
let mut change = None;
// With none to list, still there to drop the legend's own order.
if !names.is_empty() || !layer_order.is_empty() {
ui.add_space(4.0);
let open = !exclude.is_empty() || !layer_order.is_empty();
egui::CollapsingHeader::new(tl!("Layers")).id_salt(("legend_layers", id)).default_open(open).show(ui, |ui| {
// By layout too, as element ids repeat: the section's rows, scroll and open classes
// take their ids from it.
egui::CollapsingHeader::new(tl!("Layers")).id_salt(("legend_layers", &layout.name, id)).default_open(open).show(ui, |ui| {
let left_out = |n: &str| exclude.iter().any(|x| x.eq_ignore_ascii_case(n));
// The place a row moves with, up or down.
// The filter is the view's, not the project's: kept by egui, and read only while
// its box shows, so a list grown short again hides nothing.
let mut needle = String::new();
if names.len() > FILTER_OVER {
// By layout too: element ids repeat from one to another.
let key = egui::Id::unique(("legend_layers_filter", &layout.name, id));
let mut text = ui.data(|d| d.get_temp::<String>(key)).unwrap_or_default();
let r = ui.add(egui::TextEdit::singleline(&mut text).hint_text(tl!("Filter layers")).desired_width(f32::INFINITY));
// Named once typed in too, when its hint is gone.
r.widget_info(|| egui::WidgetInfo::labeled(egui::Role::TextInput, true, tl!("Filter layers")));
needle = folded(text.trim());
ui.data_mut(|d| d.insert_temp(key, text));
}
// The places in `names` of the rows shown.
let shown: Vec<usize> = names.iter().enumerate().filter(|(_, n)| folded(n).contains(&needle)).map(|(i, _)| i).collect();
if !needle.is_empty() && shown.is_empty() {
ui.label(tl!("No layers match your search."));
}
// The places a row swaps with the one shown above or below it.
let mut swap = None;
for (i, n) in names.iter().enumerate() {
// Ids from the layer, not the row's place (`push_id` would mix it in), so the
// keyboard's focus moves with its layer.
let row_id = ui.make_persistent_id(n.to_ascii_lowercase());
ui.scope_builder(egui::UiBuilder::new().scope_id(row_id), |ui| {
// The arrows on the right, the name in what they leave.
let row = vec2(ui.available_width(), ui.spacing().interact_size.y);
ui.allocate_ui_with_layout(row, egui::Layout::right_to_left(egui::Align::Center), |ui| {
// Named for its layer, so each row's arrows tell apart to a screen reader.
let arrow = |ui: &mut Ui, glyph: &str, on: bool, hover: &str, label: String| {
let r = ui.add_enabled(on, egui::Button::new(glyph).small());
r.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, &label));
r.on_hover_text(hover).clicked()
};
let args = [("name", n.as_str())];
if arrow(ui, "▼", i + 1 < names.len(), tl!("Move down"), crate::i18n::fmt("Move {name} down", &args)) {
swap = Some((i, i + 1));
}
if arrow(ui, "▲", i > 0, tl!("Move up"), crate::i18n::fmt("Move {name} up", &args)) {
swap = Some((i - 1, i));
}
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Truncate);
let mut listed = !left_out(n);
if ui.checkbox(&mut listed, n.as_str()).on_hover_text(tl!("List this layer in the legend")).changed() {
let mut next: Vec<&String> = kept.iter().filter(|x| !x.eq_ignore_ascii_case(n)).collect();
if !listed {
next.push(n);
// A bar beside the rows, not floating over their arrows.
ui.scope(|ui| {
ui.spacing_mut().scroll = egui::style::ScrollStyle::solid();
egui::ScrollArea::vertical()
.id_salt(("legend_layers_scroll", id))
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
.max_height(LAYERS_HEIGHT)
.auto_shrink([false, true])
.show(ui, |ui| {
for (k, (i, n)) in shown.iter().filter_map(|&i| names.get(i).map(|n| (i, n))).enumerate() {
let above = k.checked_sub(1).and_then(|k| shown.get(k)).copied();
let below = shown.get(k + 1).copied();
// The map's layers of this name: a web layer has no classes.
let layers = || map.into_iter().flat_map(|m| &m.layers).filter(|l| l.name.eq_ignore_ascii_case(n));
let classed = layers().any(has_class_rows);
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
// Ids from the layer, not the row's place (`push_id` would mix it in), so
// the keyboard's focus and the row's expansion move with its layer.
let row_id = ui.make_persistent_id(n.to_ascii_lowercase());
ui.scope_builder(egui::UiBuilder::new().scope_id(row_id), |ui| {
let mut classes = egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
row_id.with("classes"),
false,
);
// The arrows on the right, the name in what they leave.
let row = vec2(ui.available_width(), ui.spacing().interact_size.y);
ui.allocate_ui_with_layout(row, egui::Layout::right_to_left(egui::Align::Center), |ui| {
// Named for its layer, so each row's arrows tell apart to a screen reader.
let arrow = |ui: &mut Ui, glyph: &str, on: bool, hover: &str, label: String| {
let r = ui.add_enabled(on, egui::Button::new(glyph).small());
r.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, &label));
r.on_hover_text(hover).clicked()
};
let args = [("name", n.as_str())];
if arrow(ui, "▼", below.is_some(), tl!("Move down"), crate::i18n::fmt("Move {name} down", &args))
{
swap = below.map(|j| (i, j));
}
if arrow(ui, "▲", above.is_some(), tl!("Move up"), crate::i18n::fmt("Move {name} up", &args)) {
swap = above.map(|j| (j, i));
}
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Truncate);
let open = classes.is_open();
let (glyph, hover, label) = if open {
("▾", tl!("Hide classes"), crate::i18n::fmt("Hide the classes of {name}", &args))
} else {
("▸", tl!("Show classes"), crate::i18n::fmt("Show the classes of {name}", &args))
};
// Drawn unseen without classes, so the checkboxes line up.
let toggle = ui.add_visible(classed, egui::Button::new(glyph).small().frame(false));
if classed {
toggle.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &label));
if toggle.on_hover_text(hover).clicked() {
classes.toggle(ui);
}
}
let mut listed = !left_out(n);
if ui
.checkbox(&mut listed, n.as_str())
.on_hover_text(tl!("List this layer in the legend"))
.changed()
{
let mut next: Vec<&String> = kept.iter().filter(|x| !x.eq_ignore_ascii_case(n)).collect();
if !listed {
next.push(n);
}
change = Some(json!({"exclude": next}));
}
});
});
if classed && classes.is_open() {
// As the legend labels them: its own text for a class, else the layer's.
let own = class_labels.of(n);
ui.indent("classes", |ui| {
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Truncate);
for label in layers().flat_map(class_row_labels) {
ui.label(own.and_then(|o| o.get(&label)).unwrap_or(&label));
}
});
}
update(app, json!({"exclude": next}));
}
});
classes.store(ui.ctx());
});
}
});
});
}
});
if let Some((a, b)) = swap {
let mut next: Vec<&String> = names.iter().collect();
next.swap(a, b);
// Those ordered but not listed stay ordered, after these.
next.extend(&unlisted);
update(app, json!({"layerOrder": next}));
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()
{
update(app, json!({"layerOrder": []}));
change = Some(json!({"layerOrder": []}));
}
});
}
if let Some(change) = change {
update(app, change);
}
}
}
Loading
Loading