From 279f13b7e8778182dd3d05e3d8bcefd48946b8b3 Mon Sep 17 00:00:00 2001 From: Behrang Shafei <50267830+bertiqwerty@users.noreply.github.com> Date: Sat, 1 Jun 2024 10:59:26 +0200 Subject: [PATCH] rename labels --- rvimage/src/rvlib/menu/tools_menus.rs | 48 +++++++++++++++++++-------- rvimage/src/rvlib/tools_data/core.rs | 10 +++++- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/rvimage/src/rvlib/menu/tools_menus.rs b/rvimage/src/rvlib/menu/tools_menus.rs index 5f4ad1e..9e9d6f5 100644 --- a/rvimage/src/rvlib/menu/tools_menus.rs +++ b/rvimage/src/rvlib/menu/tools_menus.rs @@ -40,18 +40,27 @@ fn removable_rows( } to_be_removed } +enum LabelEditMode { + Add, + Rename, +} fn new_label_text( ui: &mut Ui, new_label: &mut String, are_tools_active: &mut bool, -) -> Option { - let label_field = text_edit_singleline(ui, new_label, are_tools_active); - if label_field.lost_focus() { - Some(new_label.clone()) - } else { - None - } +) -> Option<(String, LabelEditMode)> { + text_edit_singleline(ui, new_label, are_tools_active); + ui.horizontal(|ui| { + if ui.button("add").clicked() { + Some((new_label.clone(), LabelEditMode::Add)) + } else if ui.button("rename").clicked() { + Some((new_label.clone(), LabelEditMode::Rename)) + } else { + None + } + }) + .inner } fn show_inactive_tool_menu( @@ -88,17 +97,28 @@ where let mut show_only_change = false; let new_label = new_label_text(ui, &mut label_info.new_label, are_tools_active); let default_label = label_info.find_default(); - if let (Some(default_label), Some(new_label)) = (default_label, new_label.as_ref()) { + if let (Some(default_label), Some((new_label, _))) = (default_label, new_label.as_ref()) { info!("replaced default '{default_label}' label by '{new_label}'"); default_label.clone_from(new_label); label_change = true; - } else if let Some(new_label) = new_label { - if let Err(e) = label_info.push(new_label, None, None) { - warn!("{e:?}"); - return LabelMenuResult::default(); + } else if let Some((new_label, edit_mode)) = new_label { + match edit_mode { + LabelEditMode::Add => { + if let Err(e) = label_info.push(new_label, None, None) { + warn!("{e:?}"); + return LabelMenuResult::default(); + } + label_change = true; + new_idx = label_info.len() - 1; + } + LabelEditMode::Rename => { + if let Err(e) = label_info.rename_label(label_info.cat_idx_current, new_label) { + warn!("{e:?}"); + return LabelMenuResult::default(); + } + label_change = true; + } } - label_change = true; - new_idx = label_info.len() - 1; } let mut show_only_current = label_info.show_only_current; let mut to_be_removed = None; diff --git a/rvimage/src/rvlib/tools_data/core.rs b/rvimage/src/rvlib/tools_data/core.rs index f415414..fa74871 100644 --- a/rvimage/src/rvlib/tools_data/core.rs +++ b/rvimage/src/rvlib/tools_data/core.rs @@ -12,7 +12,7 @@ use super::label_map::LabelMap; pub const OUTLINE_THICKNESS_CONVERSION: TPtF = 10.0; -const DEFAULT_LABEL: &str = "foreground"; +const DEFAULT_LABEL: &str = "rvimage_fg"; fn color_dist(c1: [u8; 3], c2: [u8; 3]) -> f32 { let square_d = |i| (c1[i] as f32 - c2[i] as f32).powi(2); @@ -302,6 +302,14 @@ impl LabelInfo { Ok(()) } } + pub fn rename_label(&mut self, idx: usize, label: String) -> RvResult<()> { + if self.labels.contains(&label) { + Err(rverr!("label '{label}' already exists")) + } else { + self.labels[idx] = label; + Ok(()) + } + } pub fn from_iter(it: impl Iterator) -> RvResult { let mut info = Self::empty(); for ((label, color), cat_id) in it {