Repository navigation
feat(labels): set a point layer's obstacle weight in the Labeling pane (#3526) #3609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -984,8 +984,9 @@ fn square_to(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, c: &Class) { | |||||
| /// Where labels crowd: every one shown, over the others, rather than some left out; the class's | ||||||
| /// weight, the heavier winning where labels compete; its clearance, the room its labels keep free | ||||||
| /// of others; whether it removes duplicates, one label of a text where its features lie closer than | ||||||
| /// so far (#2847); and callouts, which move a crowded label to a free spot nearby with a line back | ||||||
| /// to its feature, no longer than so far. | ||||||
| /// so far (#2847); callouts, which move a crowded label to a free spot nearby with a line back | ||||||
| /// to its feature, no longer than so far; and, for a layer with points, the weight of its points as | ||||||
| /// obstacles for labels: the layer's, whichever class is shown (#3526). | ||||||
| fn crowding(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, c: &Class) { | ||||||
| use rustgis_engine::map::symbology::{ | ||||||
| CALLOUT_REACH, DEFAULT_LABEL_DUPLICATES, LABEL_CLEARANCE, LABEL_DUPLICATES, LABEL_WEIGHT, callout_reach, label_clearance, | ||||||
|
|
@@ -1019,6 +1020,26 @@ fn crowding(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, c: &Class) { | |||||
| } | ||||||
| }); | ||||||
| ui.end_row(); | ||||||
| // The layer's points as obstacles (#3526), for the layers the engine takes it for, whatever | ||||||
| // the class shown; 0 is none. | ||||||
| if l.takes_obstacle_weight() { | ||||||
| let input_label = ui.label(tl!("Labels keep off these points: weight")); | ||||||
| // As the weight: scoped to its layer, written when let go. | ||||||
| ui.push_id(("label_obstacle_weight", l.instance_id()), |ui| { | ||||||
| let mut v = f64::from(l.labels.obstacle_weight); | ||||||
| let drag = crate::widgets::Dragged::lend(ui, egui::Id::unique(("label_obstacle_weight_drag", l.instance_id())), &mut v); | ||||||
| let r = ui | ||||||
| .add(egui::DragValue::new(&mut v).update_while_editing(false).range(LABEL_WEIGHT).speed(0.05).max_decimals(0)) | ||||||
| .labelled_by(input_label.id) | ||||||
| .on_hover_text(tl!( | ||||||
| "A label whose class's weight isn't above this isn't placed over one of this layer's points, except its own feature's. 0: the points are no obstacle." | ||||||
| )); | ||||||
| if drag.settled(ui, &r, &mut v) && label_weight(v) != l.labels.obstacle_weight { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low-confidence nit:
Suggested change
Category: quality |
||||||
| run(app, "labels.placement", c.params(json!({"obstacleWeight": label_weight(v)}))); | ||||||
| } | ||||||
| }); | ||||||
| ui.end_row(); | ||||||
| } | ||||||
| let input_label = ui.label(tl!("Clearance")); | ||||||
| // As the weight: scoped to its layer and class, written when let go (#2828). | ||||||
| ui.push_id(("label_clearance", l.instance_id(), c.name), |ui| { | ||||||
|
|
@@ -1822,6 +1843,58 @@ mod tests { | |||||
| assert_eq!(h.state().session.undo_depth(), steps, "nothing to undo"); | ||||||
| } | ||||||
|
|
||||||
| /// A point layer's obstacle weight (#3526) is shown and set in Position, under Weight: typed, | ||||||
| /// a fraction rounded; a layer of lines has no such row. | ||||||
| #[test] | ||||||
| fn an_obstacle_weight_is_set_in_the_pane() { | ||||||
| use egui::accesskit::Role; | ||||||
| const LABEL: &str = "Labels keep off these points: weight"; | ||||||
| let mut h = harness(with_cities()); | ||||||
| h.run(); | ||||||
| let cities = layer_id(h.state(), "Cities"); | ||||||
| h.state_mut().session.run("layer.select", &json!({"layer": cities})).unwrap(); | ||||||
| h.state_mut().session.run("labels.placement", &json!({"layer": cities, "obstacleWeight": 4})).unwrap(); | ||||||
| h.run(); | ||||||
| open_tab(&mut h, "Position"); | ||||||
| let weight = | ||||||
| |h: &egui_kittest::Harness<'_, RustGisApp>| h.state().session.map().layer_by_name("Cities").unwrap().labels.obstacle_weight; | ||||||
| assert_eq!(h.get_by_role_and_label(Role::SpinButton, LABEL).accesskit_node().numeric_value(), Some(4.0), "the layer's weight"); | ||||||
| let steps = h.state().session.undo_depth(); | ||||||
| h.get_by_role_and_label(Role::SpinButton, LABEL).focus(); | ||||||
| h.run(); | ||||||
| h.get_by_role_and_label(Role::SpinButton, LABEL).type_text("7.6"); | ||||||
| h.key_press(egui::Key::Enter); | ||||||
| h.run(); | ||||||
| assert_eq!(weight(&h), 8, "a fraction rounded"); | ||||||
| assert_eq!(h.state().session.undo_depth(), steps + 1, "one step"); | ||||||
| // The class's own weight is left alone. | ||||||
| let class_weight = h.state().session.map().layer_by_name("Cities").unwrap().labels.default.weight; | ||||||
| assert_eq!(class_weight, rustgis_engine::map::symbology::DEFAULT_LABEL_WEIGHT); | ||||||
| // A layer of lines takes no obstacle weight, so the row isn't shown. | ||||||
| let rivers = layer_id(h.state(), "Rivers"); | ||||||
| h.state_mut().session.run("layer.select", &json!({"layer": rivers})).unwrap(); | ||||||
| h.run(); | ||||||
| assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_none(), "no row for lines"); | ||||||
| // Nor for polygons, a graphics layer, or a cluster or heat map layer: the engine takes the | ||||||
| // weight of none of them. | ||||||
| let countries = layer_id(h.state(), "Countries"); | ||||||
| h.state_mut().session.run("layer.select", &json!({"layer": countries})).unwrap(); | ||||||
| h.run(); | ||||||
| assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_none(), "no row for polygons"); | ||||||
| h.state_mut().session.run("layer.select", &json!({"layer": cities})).unwrap(); | ||||||
| h.state_mut().session.map_mut().layer_mut(cities).unwrap().aggregation = | ||||||
| Some(rustgis_engine::map::effects::Aggregation { hexagons: true, size: 20.0 }); | ||||||
| h.run(); | ||||||
| assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_none(), "no row for a cluster layer"); | ||||||
| h.state_mut().session.map_mut().layer_mut(cities).unwrap().aggregation = None; | ||||||
| h.state_mut().session.map_mut().layer_mut(cities).unwrap().graphics = true; | ||||||
| h.run(); | ||||||
| assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_none(), "no row for a graphics layer"); | ||||||
| h.state_mut().session.map_mut().layer_mut(cities).unwrap().graphics = false; | ||||||
| h.run(); | ||||||
| assert!(h.query_by_role_and_label(Role::SpinButton, LABEL).is_some(), "the row is back for points"); | ||||||
| } | ||||||
|
|
||||||
| /// A label class's clearance (#2828) is shown and set in Position, under Weight: typed, a | ||||||
| /// fraction kept; dragged, one step when let go; another layer chosen mid-drag, nothing. | ||||||
| #[test] | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A layer that already holds an obstacle weight and then stops taking one (it becomes a cluster or heat map layer, or a graphics layer) loses its row, so the user can't see or clear the weight from the pane. The weight stays saved, and the project keeps requiring
label-obstacleson that account, so the requirement has no visible cause. The PR describes this as a known limit, so this is a follow-up, not a blocker. A small fix is to keep the row while a weight is set, so it can be set to 0 (whichlabels.placementalways accepts):Category: quality