change image paths are handled relative from the data file

This commit is contained in:
2022-02-09 16:34:26 +01:00
parent 94a41b5455
commit 2c26155044
3 changed files with 62 additions and 35 deletions

View File

@@ -13,6 +13,7 @@ pub use crate::ui::ImageUI;
use std::cell::{RefCell}; use std::cell::{RefCell};
use std::fs; use std::fs;
use std::path::Path;
use std::sync::{Arc}; use std::sync::{Arc};
use adw::{prelude::*, Application}; use adw::{prelude::*, Application};
@@ -113,13 +114,20 @@ fn build_ui(app: &Application) {
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( state.file_name = filename.clone().as_path().file_name().map(|x| x.to_str().expect("failed to convert filname to str").to_string());
filename.clone().as_path() state.root_path = filename.clone().as_path().parent().map(|x| x.to_str().expect("failed to convert filname to str").to_string());
.to_str()
.expect("failed to convert filname to str") match (state.root_path.clone(), state.file_name.clone()) {
.to_string() (Some(root_path), Some(file_name)) => {
); let path = Path::new(&root_path).join(Path::new(&file_name));
eprintln!("{}", state.save_path.clone().unwrap()); eprintln!("{:?}", path);
}
(_,_) => {
eprintln!("Path not properly set");
}
}
image_ui.update(&state); image_ui.update(&state);
} }
dialog.close(); dialog.close();

View File

@@ -1,4 +1,5 @@
use std::io::Write; use std::io::Write;
use std::path::Path;
use std::{collections::HashMap, fs::File}; use std::{collections::HashMap, fs::File};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -10,7 +11,8 @@ 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>, pub file_name: Option<String>,
pub root_path: Option<String>,
} }
impl State { impl State {
@@ -19,7 +21,8 @@ impl State {
stacks: Vec::new(), stacks: Vec::new(),
stack_index: None, stack_index: None,
image_index: None, image_index: None,
save_path: None, file_name: None,
root_path: None,
} }
} }
pub fn set_image_index(&mut self, image_index: Option<usize>) { pub fn set_image_index(&mut self, image_index: Option<usize>) {
@@ -93,25 +96,29 @@ impl State {
} }
pub fn save(&self) { pub fn save(&self) {
if let Some(path) = self.save_path.clone() { match (self.root_path.clone(), self.file_name.clone()) {
match File::create(path) { (Some(root_path), Some(file_name)) => {
Ok(mut file) => { let path = Path::new(&root_path).join(Path::new(&file_name));
let contents = match File::create(path) {
serde_json::to_string(&self.stacks).expect("Could not serialize data."); Ok(mut file) => {
match file.write(contents.as_bytes()) { let contents =
Ok(_) => {} serde_json::to_string(&self.stacks).expect("Could not serialize data.");
Err(e) => { match file.write(contents.as_bytes()) {
eprintln!("an error occured while saving: {}", e.to_string()); Ok(_) => {}
Err(e) => {
eprintln!("an error occured while saving: {}", e.to_string());
}
} }
} }
} Err(e) => {
Err(e) => { eprintln!("{}", e.to_string());
eprintln!("{}", e.to_string()); }
} };
}; }
} else { (_, _) => {
// TODO: error dialogue // TODO: error dialogue
eprintln!("No save path specified"); eprintln!("No save path specified");
}
} }
} }

View File

@@ -1,4 +1,4 @@
use std::sync::Arc; use std::{path::Path, sync::Arc};
use adw::{Application, ApplicationWindow, HeaderBar, SplitButton}; use adw::{Application, ApplicationWindow, HeaderBar, SplitButton};
use gtk::{ use gtk::{
@@ -196,19 +196,31 @@ impl ImageUI {
} }
pub fn update(&self, state: &State) { pub fn update(&self, state: &State) {
if let Some(annotation_image) = state.get_current_annotation_image() { match (
self.update_image(&annotation_image); state.get_current_annotation_image(),
state.root_path.clone(),
) {
(Some(annotation_image), Some(base_path)) => {
self.update_image(&annotation_image, base_path)
}
(_, _) => {}
} }
self.update_focus_scale(&state); self.update_focus_scale(&state);
} }
fn update_image(&self, annotation_image: &AnnotationImage) { fn update_image(&self, annotation_image: &AnnotationImage, base_path: String) {
self.individual self.individual.set_from_file(Some(
.set_from_file(Some(annotation_image.image_path.clone())); Path::new(&base_path).join(Path::new(&annotation_image.image_path)),
self.center ));
.set_from_file(Some(annotation_image.image_path.clone())); self.center.set_from_file(Some(
Path::new(&base_path).join(Path::new(&annotation_image.image_path)),
));
for index in 0..annotation_image.neighbours.len() { for index in 0..annotation_image.neighbours.len() {
self.neighbours[index].set_from_file(annotation_image.neighbours[index].clone()); self.neighbours[index].set_from_file(
annotation_image.neighbours[index]
.clone()
.map(|x| Path::new(&base_path).join(Path::new(&x))),
);
} }
} }