mirror of
https://github.com/13hannes11/focus_annotator.git
synced 2024-09-03 23:21:01 +02:00
refactor split off application state file
This commit is contained in:
11
src/constants.rs
Normal file
11
src/constants.rs
Normal file
@@ -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<String> = 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;
|
||||||
69
src/main.rs
69
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::cell::{Cell, RefCell};
|
||||||
use std::sync::{Arc};
|
use std::sync::{Arc};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use adw::{prelude::*, ApplicationWindow, HeaderBar, SplitButton};
|
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 gio::SimpleAction;
|
||||||
use glib::clone;
|
use glib::clone;
|
||||||
use gtk::{gio, glib, FileChooserAction, FileChooserDialog, ResponseType};
|
use gtk::{gio, glib, FileChooserAction, FileChooserDialog, ResponseType};
|
||||||
@@ -12,60 +18,7 @@ use gtk::{
|
|||||||
ActionBar, Application, AspectFrame, Box, Button, FileFilter, Grid, Image, Orientation,
|
ActionBar, Application, AspectFrame, Box, Button, FileFilter, Grid, Image, Orientation,
|
||||||
PositionType, Scale, Separator, ToggleButton,
|
PositionType, Scale, Separator, ToggleButton,
|
||||||
};
|
};
|
||||||
|
use state::AnnotationZStack;
|
||||||
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<String> = 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<AnnotationImage>,
|
|
||||||
best_index: Option<usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AnnotationZStack {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
AnnotationZStack {
|
|
||||||
images: Vec::<AnnotationImage>::new(),
|
|
||||||
best_index: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn push(&mut self, image: AnnotationImage) -> &mut Self {
|
|
||||||
self.images.push(image);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
pub fn first(self) -> Option<AnnotationImage> {
|
|
||||||
self.images.first().map(|x| x.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
struct AnnotationImage {
|
|
||||||
image_path: String,
|
|
||||||
neighbours: [Option<String>; 8],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AnnotationImage {
|
|
||||||
pub fn from_vec(image_path: String, neighbours: Vec<Option<String>>) -> 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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct ImageUI {
|
struct ImageUI {
|
||||||
@@ -480,7 +433,7 @@ fn build_ui(app: &Application) {
|
|||||||
|
|
||||||
let mut new_dataset : Vec<AnnotationZStack> = serde_json::from_str(&contents).unwrap();
|
let mut new_dataset : Vec<AnnotationZStack> = serde_json::from_str(&contents).unwrap();
|
||||||
eprintln!("{}", contents);
|
eprintln!("{}", contents);
|
||||||
|
|
||||||
let mut dataset = annotaion_dataset.borrow_mut();
|
let mut dataset = annotaion_dataset.borrow_mut();
|
||||||
dataset.clear();
|
dataset.clear();
|
||||||
dataset.append(&mut new_dataset);
|
dataset.append(&mut new_dataset);
|
||||||
@@ -517,7 +470,7 @@ fn build_ui(app: &Application) {
|
|||||||
let index = current_z_stack_index.as_ref().get();
|
let index = current_z_stack_index.as_ref().get();
|
||||||
let mut dataset = annotaion_dataset.borrow_mut();
|
let mut dataset = annotaion_dataset.borrow_mut();
|
||||||
dataset[index].best_index = Some(focus_scale.value() as usize);
|
dataset[index].best_index = Some(focus_scale.value() as usize);
|
||||||
|
|
||||||
save_annotation(&dataset);
|
save_annotation(&dataset);
|
||||||
next_image(current_z_stack_index.clone().as_ref(), &dataset, focus_scale.as_ref(), image_ui.as_ref());
|
next_image(current_z_stack_index.clone().as_ref(), &dataset, focus_scale.as_ref(), image_ui.as_ref());
|
||||||
}));
|
}));
|
||||||
|
|||||||
45
src/state/mod.rs
Normal file
45
src/state/mod.rs
Normal file
@@ -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<AnnotationImage>,
|
||||||
|
pub best_index: Option<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AnnotationZStack {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
AnnotationZStack {
|
||||||
|
images: Vec::<AnnotationImage>::new(),
|
||||||
|
best_index: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn push(&mut self, image: AnnotationImage) -> &mut Self {
|
||||||
|
self.images.push(image);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn first(self) -> Option<AnnotationImage> {
|
||||||
|
self.images.first().map(|x| x.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct AnnotationImage {
|
||||||
|
pub image_path: String,
|
||||||
|
pub neighbours: [Option<String>; 8],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AnnotationImage {
|
||||||
|
pub fn from_vec(image_path: String, neighbours: Vec<Option<String>>) -> 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user