From ccdd5627663029d7489fa40e2d81d7cf4365de80 Mon Sep 17 00:00:00 2001 From: Hannes Kuchelmeister Date: Thu, 3 Feb 2022 13:29:10 +0100 Subject: [PATCH] refactor split off application state file --- src/constants.rs | 11 ++++++++ src/main.rs | 69 ++++++++---------------------------------------- src/state/mod.rs | 45 +++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 58 deletions(-) create mode 100644 src/constants.rs create mode 100644 src/state/mod.rs diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..8187173 --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,11 @@ +pub const MARGIN_TOP: i32 = 32; +pub const MARGIN_BOTTOM: i32 = 32; +pub const MARGIN_LEFT: i32 = 16; +pub const MARGIN_RIGHT_SCALE_ADDITIONAL: i32 = 38; + +pub const NONE_STRING_OPTION: Option = None; + +pub const TOGGLE_NEIGHBOURS_TEXT_TOGGLED: &str = "Hide Neighbours"; +pub const TOGGLE_NEIGHBOURS_TEXT: &str = "Show Neighbours"; + +pub const SCALE_STEP: f64 = 1.0; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ca365b8..defea1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,16 @@ +mod state; +mod constants; + +pub use crate::state::AnnotationImage; +pub use crate::constants::MARGIN_BOTTOM; + + use std::cell::{Cell, RefCell}; use std::sync::{Arc}; use std::fs; -use serde::{Deserialize, Serialize}; - use adw::{prelude::*, ApplicationWindow, HeaderBar, SplitButton}; +use constants::{MARGIN_TOP, MARGIN_LEFT, MARGIN_RIGHT_SCALE_ADDITIONAL, TOGGLE_NEIGHBOURS_TEXT, TOGGLE_NEIGHBOURS_TEXT_TOGGLED, SCALE_STEP}; use gio::SimpleAction; use glib::clone; use gtk::{gio, glib, FileChooserAction, FileChooserDialog, ResponseType}; @@ -12,60 +18,7 @@ use gtk::{ ActionBar, Application, AspectFrame, Box, Button, FileFilter, Grid, Image, Orientation, PositionType, Scale, Separator, ToggleButton, }; - -const MARGIN_TOP: i32 = 32; -const MARGIN_BOTTOM: i32 = 32; -const MARGIN_LEFT: i32 = 16; -const MARGIN_RIGHT_SCALE_ADDITIONAL: i32 = 38; - -const NONE_STRING_OPTION: Option = None; - -const TOGGLE_NEIGHBOURS_TEXT_TOGGLED: &str = "Hide Neighbours"; -const TOGGLE_NEIGHBOURS_TEXT: &str = "Show Neighbours"; - -const SCALE_STEP: f64 = 1.0; - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct AnnotationZStack { - images: Vec, - best_index: Option, -} - -impl AnnotationZStack { - pub fn new() -> Self { - AnnotationZStack { - images: Vec::::new(), - best_index: None, - } - } - pub fn push(&mut self, image: AnnotationImage) -> &mut Self { - self.images.push(image); - self - } - pub fn first(self) -> Option { - self.images.first().map(|x| x.clone()) - } -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -struct AnnotationImage { - image_path: String, - neighbours: [Option; 8], -} - -impl AnnotationImage { - pub fn from_vec(image_path: String, neighbours: Vec>) -> Self { - let mut _neighbours = [NONE_STRING_OPTION; 8]; - for (index, element) in (0..8).zip(neighbours.iter()) { - _neighbours[index] = element.clone(); - } - - AnnotationImage { - image_path, - neighbours: _neighbours, - } - } -} +use state::AnnotationZStack; #[derive(Debug, Clone)] struct ImageUI { @@ -480,7 +433,7 @@ fn build_ui(app: &Application) { let mut new_dataset : Vec = serde_json::from_str(&contents).unwrap(); eprintln!("{}", contents); - + let mut dataset = annotaion_dataset.borrow_mut(); dataset.clear(); dataset.append(&mut new_dataset); @@ -517,7 +470,7 @@ fn build_ui(app: &Application) { let index = current_z_stack_index.as_ref().get(); let mut dataset = annotaion_dataset.borrow_mut(); dataset[index].best_index = Some(focus_scale.value() as usize); - + save_annotation(&dataset); next_image(current_z_stack_index.clone().as_ref(), &dataset, focus_scale.as_ref(), image_ui.as_ref()); })); diff --git a/src/state/mod.rs b/src/state/mod.rs new file mode 100644 index 0000000..fcf4c6c --- /dev/null +++ b/src/state/mod.rs @@ -0,0 +1,45 @@ +use serde::{Serialize, Deserialize}; + +use crate::constants::NONE_STRING_OPTION; + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct AnnotationZStack { + pub images: Vec, + pub best_index: Option, + } + + impl AnnotationZStack { + pub fn new() -> Self { + AnnotationZStack { + images: Vec::::new(), + best_index: None, + } + } + pub fn push(&mut self, image: AnnotationImage) -> &mut Self { + self.images.push(image); + self + } + pub fn first(self) -> Option { + self.images.first().map(|x| x.clone()) + } + } + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct AnnotationImage { + pub image_path: String, + pub neighbours: [Option; 8], + } + + impl AnnotationImage { + pub fn from_vec(image_path: String, neighbours: Vec>) -> Self { + let mut _neighbours = [NONE_STRING_OPTION; 8]; + for (index, element) in (0..8).zip(neighbours.iter()) { + _neighbours[index] = element.clone(); + } + + AnnotationImage { + image_path, + neighbours: _neighbours, + } + } + } \ No newline at end of file