added saving after marking

This commit is contained in:
2022-02-07 10:22:19 +01:00
parent 2938cedfdb
commit df6a619b2d
2 changed files with 45 additions and 1 deletions

View File

@@ -106,13 +106,20 @@ fn build_ui(app: &Application) {
let file = dialog.file().expect("Couldn't get file"); let file = dialog.file().expect("Couldn't get file");
eprintln!("Open"); eprintln!("Open");
let filename = file.path().expect("Couldn't get file path"); let filename = file.path().expect("Couldn't get file path");
let contents = fs::read_to_string(filename).expect("Something went wrong reading the file"); let contents = fs::read_to_string(filename.clone()).expect("Something went wrong reading the file");
eprintln!("{}", contents); eprintln!("{}", contents);
let new_dataset : Vec<AnnotationZStack> = serde_json::from_str(&contents).unwrap(); let new_dataset : Vec<AnnotationZStack> = serde_json::from_str(&contents).unwrap();
let mut state = state.borrow_mut(); let mut state = state.borrow_mut();
state.replace_foucs_stacks(new_dataset); state.replace_foucs_stacks(new_dataset);
state.save_path = Some(
filename.clone().as_path()
.to_str()
.expect("failed to convert filname to str")
.to_string()
);
eprintln!("{}", state.save_path.clone().unwrap());
image_ui.update(&state); image_ui.update(&state);
} }
dialog.close(); dialog.close();
@@ -147,6 +154,7 @@ fn build_ui(app: &Application) {
let mut state = state.borrow_mut(); let mut state = state.borrow_mut();
state.mark_focus(); state.mark_focus();
state.save();
state.skip(); state.skip();
image_ui.update(&state); image_ui.update(&state);
})); }));

View File

@@ -1,4 +1,8 @@
use std::io::Write;
use std::{collections::HashMap, fs::File};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::constants::NONE_STRING_OPTION; use crate::constants::NONE_STRING_OPTION;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -6,6 +10,7 @@ pub struct State {
stacks: Vec<AnnotationZStack>, stacks: Vec<AnnotationZStack>,
stack_index: Option<usize>, stack_index: Option<usize>,
pub image_index: Option<usize>, pub image_index: Option<usize>,
pub save_path: Option<String>,
} }
impl State { impl State {
@@ -14,6 +19,7 @@ impl State {
stacks: Vec::new(), stacks: Vec::new(),
stack_index: None, stack_index: None,
image_index: None, image_index: None,
save_path: None,
} }
} }
pub fn set_image_index(&mut self, image_index: Option<usize>) { pub fn set_image_index(&mut self, image_index: Option<usize>) {
@@ -86,6 +92,29 @@ impl State {
} }
} }
pub fn save(&self) {
if let Some(path) = self.save_path.clone() {
match File::create(path) {
Ok(mut file) => {
let contents =
serde_json::to_string(&self.stacks).expect("Could not serialize data.");
match file.write(contents.as_bytes()) {
Ok(_) => {}
Err(e) => {
eprintln!("an error occured while saving: {}", e.to_string());
}
}
}
Err(e) => {
eprintln!("{}", e.to_string());
}
};
} else {
// TODO: error dialogue
eprintln!("No save path specified");
}
}
pub fn previous(&mut self) { pub fn previous(&mut self) {
let len = self.stacks.len(); let len = self.stacks.len();
if len == 0 { if len == 0 {
@@ -100,12 +129,18 @@ impl State {
pub struct AnnotationZStack { pub struct AnnotationZStack {
pub images: Vec<AnnotationImage>, pub images: Vec<AnnotationImage>,
pub best_index: Option<usize>, pub best_index: Option<usize>,
#[serde(flatten)]
extra: HashMap<String, Value>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnnotationImage { pub struct AnnotationImage {
pub image_path: String, pub image_path: String,
pub neighbours: [Option<String>; 8], pub neighbours: [Option<String>; 8],
#[serde(flatten)]
extra: HashMap<String, Value>,
} }
impl AnnotationImage { impl AnnotationImage {
@@ -118,6 +153,7 @@ impl AnnotationImage {
AnnotationImage { AnnotationImage {
image_path, image_path,
neighbours: _neighbours, neighbours: _neighbours,
extra: HashMap::new(),
} }
} }
} }