Skip to content

Commit

Permalink
rename labels
Browse files Browse the repository at this point in the history
  • Loading branch information
bertiqwerty committed Jun 1, 2024
1 parent 71366ac commit 279f13b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
48 changes: 34 additions & 14 deletions rvimage/src/rvlib/menu/tools_menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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(
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion rvimage/src/rvlib/tools_data/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Item = ((String, [u8; 3]), u32)>) -> RvResult<Self> {
let mut info = Self::empty();
for ((label, color), cat_id) in it {
Expand Down

0 comments on commit 279f13b

Please sign in to comment.