1use tracing::info;
2
3use super::Manipulate;
4use crate::{
5 annotations_accessor_mut,
6 events::Events,
7 file_util::PathPair,
8 history::{History, Record},
9 make_tool_transform,
10 result::trace_ok_err,
11 tools_data::{
12 attributes_data::set_attrmap_val,
13 parameters::{ParamMap, ParamVal},
14 AttributesToolData,
15 },
16 tools_data_accessors,
17 world::World,
18 world_annotations_accessor,
19};
20use std::mem;
21const MISSING_DATA_MSG: &str = "Missing data for Attributes";
22pub const ACTOR_NAME: &str = "Attributes";
23annotations_accessor_mut!(
24 ACTOR_NAME,
25 attributes_mut,
26 "Attribute didn't work",
27 ParamMap
28);
29world_annotations_accessor!(ACTOR_NAME, attributes, "Attribute didn't work", ParamMap);
30tools_data_accessors!(
31 ACTOR_NAME,
32 MISSING_DATA_MSG,
33 attributes_data,
34 AttributesToolData,
35 attributes,
36 attributes_mut
37);
38
39fn propagate_annos(
40 mut annos: ParamMap,
41 attr_names: &[String],
42 to_propagate: &[(usize, ParamVal)],
43) -> ParamMap {
44 for (attr_idx, val) in to_propagate {
45 if let Some(attr_val) = annos.get_mut(&attr_names[*attr_idx]) {
46 *attr_val = val.clone();
47 }
48 }
49 annos
50}
51
52fn get_buffers(world: &World) -> Vec<String> {
53 let annos = get_annos(world);
54 let data = get_specific(world);
55 if let (Some(data), Some(annos)) = (data, annos) {
56 data.attr_names()
57 .iter()
58 .map(|attr_name| {
59 if let Some(attrval) = annos.get(attr_name) {
60 attrval.to_string()
61 } else {
62 "".to_string()
63 }
64 })
65 .collect()
66 } else {
67 vec![]
68 }
69}
70fn propagate_buffer(
71 mut attribute_buffer: Vec<String>,
72 to_propagate: &[(usize, ParamVal)],
73) -> Vec<String> {
74 for (attr_idx, val) in to_propagate {
75 attribute_buffer[*attr_idx] = val.to_string();
76 }
77 attribute_buffer
78}
79fn file_change(mut world: World) -> World {
80 use_currentimageshape_for_annos(&mut world);
81 let attr_buffers = get_buffers(&world);
82 let annos = get_annos_mut(&mut world).map(mem::take);
83 let data = get_specific_mut(&mut world);
84
85 if let (Some(data), Some(mut annos)) = (data, annos) {
86 for (attr_name, attr_val) in data.attr_names().iter().zip(data.attr_vals().iter()) {
87 if !annos.contains(attr_name) {
88 set_attrmap_val(&mut annos, attr_name, attr_val);
89 }
90 }
91
92 let attr_buffers = propagate_buffer(attr_buffers, &data.to_propagate_attr_val);
94 for (i, buffer) in attr_buffers.into_iter().enumerate() {
95 *data.attr_value_buffer_mut(i) = buffer;
96 }
97
98 annos = propagate_annos(annos, data.attr_names(), &data.to_propagate_attr_val);
99
100 if let Some(annos_) = get_annos_mut(&mut world) {
101 *annos_ = annos;
102 }
103 }
104 let current = get_annos(&world).cloned();
105 if let Some(data) = get_specific_mut(&mut world) {
106 data.current_attr_map = current;
107 }
108 world
109}
110fn add_attribute(
111 mut world: World,
112 mut history: History,
113 suppress_exists_err: bool,
114) -> (World, History) {
115 let attr_map_tmp = get_annos_mut(&mut world).map(mem::take);
116 let data = get_specific_mut(&mut world);
117
118 if let (Some(mut attr_map_tmp), Some(data)) = (attr_map_tmp, data) {
119 let new_attr_name = data.new_attr_name.clone();
120 if data.attr_names().contains(&new_attr_name) && !suppress_exists_err {
121 tracing::error!("New attribute {new_attr_name} could not be created, already exists");
122 } else {
123 let new_attr_val = data.new_attr_val.clone();
124 for (_, (val_map, _)) in data.anno_iter_mut() {
125 set_attrmap_val(val_map, &new_attr_name, &new_attr_val);
126 }
127 set_attrmap_val(&mut attr_map_tmp, &new_attr_name, &new_attr_val);
128 if let Some(a) = get_annos_mut(&mut world) {
129 a.clone_from(&attr_map_tmp);
130 }
131 if let Some(data) = get_specific_mut(&mut world) {
132 data.current_attr_map = Some(attr_map_tmp);
133 data.push(new_attr_name, new_attr_val);
134 history.push(Record::new(world.clone(), ACTOR_NAME));
135 }
136 }
137 }
138 if let Some(data) = get_specific_mut(&mut world) {
139 data.options.is_addition_triggered = false;
140 data.new_attr_name = String::new();
141 data.new_attr_val = ParamVal::default();
142 }
143 (world, history)
144}
145
146fn check_remove(mut world: World, mut history: History) -> (World, History) {
147 if let Some(removal_idx) = get_specific(&world).map(|d| d.options.removal_idx) {
148 let data = get_specific_mut(&mut world);
149 if let (Some(data), Some(removal_idx)) = (data, removal_idx) {
150 data.remove_attr(removal_idx);
151 history.push(Record::new(world.clone(), ACTOR_NAME));
152 }
153 if let Some(removal_idx) = get_specific_mut(&mut world).map(|d| &mut d.options.removal_idx)
154 {
155 *removal_idx = None;
156 }
157 }
158 (world, history)
159}
160
161#[derive(Clone, Copy, Debug)]
162pub struct Attributes;
163
164impl Manipulate for Attributes {
165 fn new() -> Self
166 where
167 Self: Sized,
168 {
169 Self
170 }
171
172 fn on_activate(&mut self, mut world: World) -> World {
173 let data = get_data_mut(&mut world);
174 if let Some(data) = trace_ok_err(data) {
175 data.menu_active = true;
176 }
177 file_change(world)
178 }
179 fn on_deactivate(&mut self, mut world: World) -> World {
180 let data = get_data_mut(&mut world);
181 if let Some(data) = trace_ok_err(data) {
182 data.menu_active = false;
183 }
184 world
185 }
186 fn on_filechange(&mut self, world: World, history: History) -> (World, History) {
187 (file_change(world), history)
188 }
189 fn events_tf(
190 &mut self,
191 mut world: World,
192 mut history: History,
193 _event: &Events,
194 ) -> (World, History) {
195 let is_addition_triggered = get_specific(&world).map(|d| d.options.is_addition_triggered);
196 if is_addition_triggered == Some(true) {
197 (world, history) = add_attribute(world, history, false);
199 }
200 let attr_data = get_specific_mut(&mut world);
201 if let Some(attr_data) = attr_data {
202 if let Some(rename_src_idx) = attr_data.options.rename_src_idx {
203 let from_name = &attr_data.attr_names()[rename_src_idx].clone();
204 let to_name = &attr_data.new_attr_name.clone();
205 tracing::info!("Rename attribute {from_name} to {to_name}");
206 attr_data.rename(from_name, to_name);
207 attr_data.options.rename_src_idx = None;
208 }
209 }
210 let is_update_triggered = get_specific(&world).map(|d| d.options.is_update_triggered);
211 if is_update_triggered == Some(true) {
212 info!("update attr");
213 let current_from_menu_clone =
214 get_specific(&world).and_then(|d| d.current_attr_map.clone());
215 if let (Some(mut cfm), Some(anno)) =
216 (current_from_menu_clone, get_annos_mut(&mut world))
217 {
218 *anno = mem::take(&mut cfm);
219 }
220 if let Some(update_current_attr_map) =
221 get_specific_mut(&mut world).map(|d| &mut d.options.is_update_triggered)
222 {
223 *update_current_attr_map = false;
224 }
225 }
226 (world, history) = check_remove(world, history);
227
228 let is_export_triggered =
229 get_specific(&world).map(|d| d.options.import_export_trigger.export_triggered());
230 if is_export_triggered == Some(true) {
231 let ssh_cfg = world.data.meta_data.ssh_cfg.clone();
232 let attr_data = get_specific(&world);
233 let export_only_opened_folder =
234 attr_data.map(|d| d.options.export_only_opened_folder) == Some(true);
235 let key_filter = if export_only_opened_folder {
236 world
237 .data
238 .meta_data
239 .opened_folder
240 .as_ref()
241 .map(PathPair::path_relative)
242 } else {
243 None
244 };
245 let annos_str = get_specific(&world)
246 .and_then(|d| trace_ok_err(d.serialize_annotations(key_filter)));
247 if let (Some(annos_str), Some(data)) = (annos_str, get_specific(&world)) {
248 if trace_ok_err(data.export_path.conn.write(
249 &annos_str,
250 &data.export_path.path,
251 ssh_cfg.as_ref(),
252 ))
253 .is_some()
254 {
255 info!("exported annotations to {:?}", data.export_path.path);
256 }
257 }
258 if let Some(export_triggered) =
259 get_specific_mut(&mut world).map(|d| &mut d.options.import_export_trigger)
260 {
261 export_triggered.untrigger_export();
262 }
263 }
264 let is_import_triggered =
265 get_specific(&world).map(|d| d.options.import_export_trigger.import_triggered());
266 if is_import_triggered == Some(true) {
267 tracing::info!("import attr tiggered");
268 let ssh_cfg = world.data.meta_data.ssh_cfg.clone();
269 let cur_prj = world.data.meta_data.prj_path().map(|p| p.to_path_buf());
270 let attr_data = get_specific_mut(&mut world);
271 let imported_map = attr_data.and_then(|data| {
272 let in_path = &data.export_path.path;
273 tracing::info!("importing attributes from {in_path:?}");
274 let json_str = trace_ok_err(data.export_path.conn.read(in_path, ssh_cfg.as_ref()));
275 if let Some(s) = json_str {
276 trace_ok_err(AttributesToolData::deserialize_annotations(
277 &s,
278 cur_prj.as_deref(),
279 ))
280 } else {
281 None
282 }
283 });
284 if let Some(imported_map) = &imported_map {
285 for (_, (attr_map, _)) in imported_map.iter() {
287 for (attr_name, attr_val) in attr_map.iter() {
288 let data = get_specific_mut(&mut world);
289 if let Some(d) = data {
290 d.new_attr_name = attr_name.clone();
291 d.new_attr_val = attr_val.clone().reset();
292 }
293 tracing::debug!("inserting attr {attr_name} with value {attr_val}");
294 (world, history) = add_attribute(world, history, true);
295 }
296 }
297 }
298 if let Some(imported_map) = imported_map {
299 let data = get_specific_mut(&mut world);
300 if let Some(d) = data {
301 d.merge_map(imported_map);
302 }
303 }
304 let annos = get_annos(&world).cloned();
305 let attr_buffer = get_buffers(&world);
306 if let (Some(data), Some(annos)) = (get_specific_mut(&mut world), annos) {
307 data.current_attr_map = Some(annos);
308 data.set_new_attr_value_buffer(attr_buffer);
309 }
310 }
311 if let Some(import_trigger) =
312 get_specific_mut(&mut world).map(|d| &mut d.options.import_export_trigger)
313 {
314 import_trigger.untrigger_import();
315 }
316 make_tool_transform!(self, world, history, event, [])
317 }
318}
319#[cfg(test)]
320use {
321 crate::tracing_setup::init_tracing_for_tests, crate::types::ViewImage, image::DynamicImage,
322 std::collections::HashMap, std::fs, std::path::Path,
323};
324#[cfg(test)]
325pub(super) fn test_data() -> (World, History) {
326 use std::path::Path;
327
328 use crate::ToolsDataMap;
329
330 let im_test = DynamicImage::ImageRgb8(ViewImage::new(64, 64));
331 let mut world = World::from_real_im(
332 im_test,
333 ToolsDataMap::new(),
334 None,
335 Some("superimage.png".to_string()),
336 Path::new("superimage.png"),
337 Some(0),
338 );
339 world.data.meta_data.flags.is_loading_screen_active = Some(false);
340
341 let history = History::default();
342 (world, history)
343}
344#[test]
345fn test_import_export() {
346 init_tracing_for_tests();
347 fn test(testpath: &Path) {
348 let (mut world, history) = test_data();
349 let data = get_specific_mut(&mut world).unwrap();
350 let json_str = fs::read_to_string(testpath).unwrap();
351 let reference_data = AttributesToolData::deserialize_annotations(&json_str, None).unwrap();
352 tracing::debug!("reference_data: {:?}", reference_data);
353 data.export_path.path = testpath.to_path_buf();
354 data.options.import_export_trigger.trigger_import();
355 let events = Events::default();
356 let (world, _) = Attributes {}.events_tf(world, history, &events);
357 let annos = world.data.tools_data_map[ACTOR_NAME]
358 .specifics
359 .attributes()
360 .unwrap()
361 .anno_iter()
362 .collect::<HashMap<_, _>>();
363 tracing::debug!("annos: {:?}", annos);
364 for k in reference_data.keys() {
365 tracing::debug!("k: {:?}", k);
366 let (annos, _) = annos.get(k).unwrap();
367 let (ref_annos, _) = &reference_data[k];
368 assert_eq!(annos, ref_annos);
369 }
370 let current = get_annos(&world).unwrap();
371 for v in current.values() {
372 assert!(v.is_default());
373 }
374 }
375 let testpath = Path::new("resources/test_data/attr_import.json");
376 test(testpath);
377 let testpath = Path::new("resources/test_data/attr_import_untagged.json");
378 test(testpath);
379}
380
381#[test]
382fn test_add() {
383 init_tracing_for_tests();
384 let mut attr_tool = Attributes::new();
385 let events = Events::default();
386 let (mut world, history) = test_data();
387 let attr_data = get_specific_mut(&mut world).unwrap();
388 attr_data.options.is_addition_triggered = true;
389 attr_data.new_attr_name = "a attr".to_string();
390 attr_data.new_attr_val = ParamVal::Int(Some(1));
391 let (mut world, history) = attr_tool.events_tf(world, history, &events);
392 let attr_data = get_specific_mut(&mut world).unwrap();
393 attr_data.options.is_addition_triggered = true;
394 attr_data.new_attr_name = "c attr".to_string();
395 attr_data.new_attr_val = ParamVal::Int(Some(2));
396 let (mut world, history) = attr_tool.events_tf(world, history, &events);
397 let attr_data = get_specific_mut(&mut world).unwrap();
398 attr_data.options.is_addition_triggered = true;
399 attr_data.new_attr_name = "b attr".to_string();
400 attr_data.new_attr_val = ParamVal::Int(Some(3));
401 let (world, _) = attr_tool.events_tf(world, history, &events);
402 let data = get_specific(&world).unwrap();
403 let cam = data.current_attr_map.as_ref().unwrap();
404 let c_attr_val = cam.get("c attr").unwrap();
405 assert_eq!(c_attr_val, &ParamVal::Int(Some(2)));
406 let b_attr_val = cam.get("b attr").unwrap();
407 assert_eq!(b_attr_val, &ParamVal::Int(Some(3)));
408 let a_attr_val = cam.get("a attr").unwrap();
409 assert_eq!(a_attr_val, &ParamVal::Int(Some(1)));
410
411 assert_eq!(data.attr_names(), &["a attr", "b attr", "c attr"]);
413 assert_eq!(
414 data.attr_vals(),
415 &[
416 ParamVal::Int(Some(1)),
417 ParamVal::Int(Some(3)),
418 ParamVal::Int(Some(2)),
419 ]
420 );
421}
422#[test]
423fn test_rm_add() {
424 init_tracing_for_tests();
425 let (mut world, history) = test_data();
426 let attr_data = get_specific_mut(&mut world).unwrap();
427 attr_data.options.is_addition_triggered = true;
428 attr_data.new_attr_name = "test_attr".to_string();
429 attr_data.new_attr_val = ParamVal::Str("123".into());
430 let (mut world, history) = add_attribute(world, history, false);
431 let attr_data = get_specific_mut(&mut world).unwrap();
432 attr_data.options.removal_idx = Some(0);
433 let (mut world, _) = check_remove(world, history);
434 let attr_data = get_specific_mut(&mut world).unwrap();
435 assert!(!attr_data.options.is_addition_triggered);
436 assert!(attr_data.options.removal_idx.is_none());
437 assert_eq!(
438 attr_data.current_attr_map.as_ref().map(|cam| cam.len()),
439 Some(0)
440 );
441}