From 2c26155044e26ee4cb7040abe24dcddbb859e7c2 Mon Sep 17 00:00:00 2001 From: Hannes Kuchelmeister Date: Wed, 9 Feb 2022 16:34:26 +0100 Subject: [PATCH] change image paths are handled relative from the data file --- src/main.rs | 22 +++++++++++++++------- src/state/mod.rs | 45 ++++++++++++++++++++++++++------------------- src/ui/mod.rs | 30 +++++++++++++++++++++--------- 3 files changed, 62 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3d5f3c2..9eaa4b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ pub use crate::ui::ImageUI; use std::cell::{RefCell}; use std::fs; +use std::path::Path; use std::sync::{Arc}; use adw::{prelude::*, Application}; @@ -113,13 +114,20 @@ fn build_ui(app: &Application) { let mut state = state.borrow_mut(); 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()); + state.file_name = filename.clone().as_path().file_name().map(|x| x.to_str().expect("failed to convert filname to str").to_string()); + state.root_path = filename.clone().as_path().parent().map(|x| x.to_str().expect("failed to convert filname to str").to_string()); + + match (state.root_path.clone(), state.file_name.clone()) { + (Some(root_path), Some(file_name)) => { + let path = Path::new(&root_path).join(Path::new(&file_name)); + eprintln!("{:?}", path); + } + (_,_) => { + eprintln!("Path not properly set"); + } + } + + image_ui.update(&state); } dialog.close(); diff --git a/src/state/mod.rs b/src/state/mod.rs index 63ff497..6abea46 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -1,4 +1,5 @@ use std::io::Write; +use std::path::Path; use std::{collections::HashMap, fs::File}; use serde::{Deserialize, Serialize}; @@ -10,7 +11,8 @@ pub struct State { stacks: Vec, stack_index: Option, pub image_index: Option, - pub save_path: Option, + pub file_name: Option, + pub root_path: Option, } impl State { @@ -19,7 +21,8 @@ impl State { stacks: Vec::new(), stack_index: None, image_index: None, - save_path: None, + file_name: None, + root_path: None, } } pub fn set_image_index(&mut self, image_index: Option) { @@ -93,25 +96,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()); + match (self.root_path.clone(), self.file_name.clone()) { + (Some(root_path), Some(file_name)) => { + let path = Path::new(&root_path).join(Path::new(&file_name)); + 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"); + Err(e) => { + eprintln!("{}", e.to_string()); + } + }; + } + (_, _) => { + // TODO: error dialogue + eprintln!("No save path specified"); + } } } diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 252699f..fcd4be5 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{path::Path, sync::Arc}; use adw::{Application, ApplicationWindow, HeaderBar, SplitButton}; use gtk::{ @@ -196,19 +196,31 @@ impl ImageUI { } pub fn update(&self, state: &State) { - if let Some(annotation_image) = state.get_current_annotation_image() { - self.update_image(&annotation_image); + match ( + 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); } - fn update_image(&self, annotation_image: &AnnotationImage) { - self.individual - .set_from_file(Some(annotation_image.image_path.clone())); - self.center - .set_from_file(Some(annotation_image.image_path.clone())); + fn update_image(&self, annotation_image: &AnnotationImage, base_path: String) { + self.individual.set_from_file(Some( + Path::new(&base_path).join(Path::new(&annotation_image.image_path)), + )); + 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() { - 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))), + ); } }