Skip to content

Commit

Permalink
Merge pull request #5 from RIMS-Code/edit_saturation_curve
Browse files Browse the repository at this point in the history
allow to edit saturation curve
  • Loading branch information
trappitsch authored May 2, 2024
2 parents 1e09cc9 + a8eefa1 commit e690cc9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,22 +396,25 @@ impl eframe::App for TemplateApp {
ui.add_space(VERTICAL_SPACE);

ui.horizontal(|ui| {
if ui.button("Add").clicked() {
if ui.button("Add or Update")
.on_hover_text("Add or update the saturation curve with the given title.")
.clicked() {
self.error_saturation.clear();

if self.sat_tmp_title.is_empty() {
self.error_saturation = "Title cannot be empty.".to_owned();
} else {
for entry in &self.saturation_curves {
if entry.title.eq(&self.sat_tmp_title) {
self.error_saturation = "Title already exists.".to_owned();
break;
}
}
}

// create a new saturation curve object to push to the vec if no previous error
if self.error_saturation.is_empty() {
// check if entry already exists
let mut index_exists: Option<usize> = None;
for (index, entry) in self.saturation_curves.clone().iter().enumerate() {
if entry.title.eq(&self.sat_tmp_title) {
index_exists = Some(index);
break;
}
}
match SaturationCurve::new_from_parts(
&self.sat_tmp_title,
&self.sat_tmp_notes,
Expand All @@ -423,7 +426,10 @@ impl eframe::App for TemplateApp {
&self.sat_tmp_ydat_unc,
) {
Ok(sc) => {
self.saturation_curves.push(sc);
match index_exists {
Some(index) => self.saturation_curves[index] = sc,
None => self.saturation_curves.push(sc),
};
self.sat_tmp_title.clear();
self.sat_tmp_notes.clear();
self.sat_tmp_fit = true;
Expand Down Expand Up @@ -465,6 +471,18 @@ impl eframe::App for TemplateApp {
self.saturation_curves.swap(it, it + 1);
}

// Edit button
if ui.button("Edit entry").clicked() {
self.sat_tmp_title = val.title.clone();
self.sat_tmp_notes = val.notes.clone();
self.sat_tmp_unit = val.units.clone();
self.sat_tmp_fit = val.fit;
self.sat_tmp_xdat = val.get_xdat();
self.sat_tmp_xdat_unc = val.get_xdat_unc();
self.sat_tmp_ydat = val.get_ydat();
self.sat_tmp_ydat_unc = val.get_ydat_unc();
}

// Delete button
if ui.button("Delete").clicked() {
self.saturation_curves.remove(it);
Expand Down
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,46 @@ impl SaturationCurve {
ydat_unc,
})
}

pub fn get_xdat(&self) -> String {
self.xdat
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(", ")
}

pub fn get_xdat_unc(&self) -> String {
if let Some(xdat_unc) = &self.xdat_unc {
xdat_unc
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(", ")
} else {
String::new()
}
}

pub fn get_ydat(&self) -> String {
self.ydat
.iter()
.map(|y| y.to_string())
.collect::<Vec<String>>()
.join(", ")
}

pub fn get_ydat_unc(&self) -> String {
if let Some(ydat_unc) = &self.ydat_unc {
ydat_unc
.iter()
.map(|y| y.to_string())
.collect::<Vec<String>>()
.join(", ")
} else {
String::new()
}
}
}

#[derive(Debug, PartialEq, serde::Deserialize, serde::Serialize)]
Expand Down

0 comments on commit e690cc9

Please sign in to comment.