add serialization, TODO: proper loading and saving

This commit is contained in:
2022-02-01 16:54:18 +01:00
parent f08b5ba90c
commit 995851413d
3 changed files with 70 additions and 20 deletions

33
Cargo.lock generated
View File

@@ -91,6 +91,8 @@ dependencies = [
"gls", "gls",
"gtk4", "gtk4",
"libadwaita", "libadwaita",
"serde",
"serde_json",
] ]
[[package]] [[package]]
@@ -439,6 +441,12 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "itoa"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]] [[package]]
name = "khronos_api" name = "khronos_api"
version = "3.1.0" version = "3.1.0"
@@ -709,6 +717,12 @@ dependencies = [
"semver", "semver",
] ]
[[package]]
name = "ryu"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
[[package]] [[package]]
name = "safe_arch" name = "safe_arch"
version = "0.6.0" version = "0.6.0"
@@ -738,24 +752,35 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.132" version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9875c23cf305cd1fd7eb77234cbb705f21ea6a72c637a5c6db5fe4b8e7f008" checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.132" version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecc0db5cb2556c0e558887d9bbdcf6ac4471e83ff66cf696e5419024d1606276" checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
] ]
[[package]]
name = "serde_json"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d23c1ba4cf0efd44be32017709280b32d1cea5c3f1275c3b6d9e8bc54f758085"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "simba" name = "simba"
version = "0.7.0" version = "0.7.0"

View File

@@ -6,7 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
adw = { version = "0.1.0", package = "libadwaita" } adw = { version = "0.1", package = "libadwaita" }
gtk = { version = "0.4", package = "gtk4" } gtk = { version = "0.4", package = "gtk4" }
gls = { version = "0.1.6" } gls = { version = "0.1" }
serde = { version = "1.0", features = ["derive"]}
serde_json = { version = "1.0" }

View File

@@ -1,13 +1,16 @@
use std::cell::Cell; use std::cell::Cell;
use std::sync::Arc; use std::sync::Arc;
use std::fs;
use serde::{Deserialize, Serialize};
use adw::{prelude::*, ApplicationWindow, HeaderBar, SplitButton}; use adw::{prelude::*, ApplicationWindow, HeaderBar, SplitButton};
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};
use gtk::{ use gtk::{
ActionBar, Application, AspectFrame, Box, Button, Grid, Image, Orientation, PositionType, ActionBar, Application, AspectFrame, Box, Button, FileFilter, Grid, Image, Orientation,
Scale, Separator, ToggleButton, PositionType, Scale, Separator, ToggleButton,
}; };
const MARGIN_TOP: i32 = 32; const MARGIN_TOP: i32 = 32;
@@ -22,7 +25,7 @@ const TOGGLE_NEIGHBOURS_TEXT: &str = "Show Neighbours";
const SCALE_STEP: f64 = 1.0; const SCALE_STEP: f64 = 1.0;
#[derive(Debug, Clone)] #[derive(Debug, Clone, Serialize, Deserialize)]
struct AnnotationZStack { struct AnnotationZStack {
images: Vec<AnnotationImage>, images: Vec<AnnotationImage>,
best_index: Option<usize>, best_index: Option<usize>,
@@ -44,7 +47,7 @@ impl AnnotationZStack {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Serialize, Deserialize)]
struct AnnotationImage { struct AnnotationImage {
image_path: String, image_path: String,
neighbours: [Option<String>; 8], neighbours: [Option<String>; 8],
@@ -178,9 +181,14 @@ fn previous_image(
); );
} }
fn save_annotation(z_stack: AnnotationZStack) { fn save_annotation(annotation_dataset: &Vec<AnnotationZStack>) {
// TODO: implement saving // TODO: implement saving
eprintln!("Saving is not implemented yet!") eprintln!("Saving is not implemented yet!");
// Serialize it to a JSON string.
let j = serde_json::to_string(&annotation_dataset).unwrap();
// Print, write to a file, or send to an HTTP server.
eprintln!("{}", &j);
} }
fn main() { fn main() {
@@ -434,15 +442,32 @@ fn main() {
.content(&application_vertical_widget) .content(&application_vertical_widget)
.build(); .build();
let file_chooser_action = FileChooserAction::Open;
let buttons = [("Open", ResponseType::Ok), ("Cancel", ResponseType::Cancel)];
let filter = FileFilter::new();
filter.add_pattern(r"*.json");
let file_chooser = FileChooserDialog::new(Some("Chose a data file!"), Some(&window), file_chooser_action, &buttons);
file_chooser.set_select_multiple(false);
file_chooser.set_filter(&filter);
file_chooser.connect_response(clone!(@strong annotaion_dataset => move |dialog: &FileChooserDialog, response: ResponseType| {
if response == ResponseType::Ok {
let file = dialog.file().expect("Couldn't get file");
eprintln!("Open");
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 dataset : Vec<AnnotationZStack> = serde_json::from_str(&contents).unwrap();
// TODO: update data after loading
}
dialog.close();
}));
open_button.connect_clicked(clone!(@weak window => move |_| { open_button.connect_clicked(clone!(@weak window => move |_| {
let file_chooser_action = FileChooserAction::Open;
// TODO: actually open and load data // TODO: actually open and load data
let buttons = [("Open", ResponseType::Accept)];
let file_chooser = FileChooserDialog::new(Some("Chose a data file!"), Some(&window), file_chooser_action, &buttons);
file_chooser.show(); file_chooser.show();
})); }));
//////////////////////// ////////////////////////
@@ -469,9 +494,7 @@ fn main() {
eprintln! {"Focus Set!"}; eprintln! {"Focus Set!"};
let index = current_z_stack_index.as_ref().get(); let index = current_z_stack_index.as_ref().get();
let mut z_stack = annotaion_dataset.get(index).unwrap().clone(); save_annotation(&annotaion_dataset);
z_stack.best_index = Some(focus_scale.value() as usize);
save_annotation(z_stack);
next_image(current_z_stack_index.clone().as_ref(), annotaion_dataset.clone(), focus_scale.as_ref(), image_ui.as_ref()); next_image(current_z_stack_index.clone().as_ref(), annotaion_dataset.clone(), focus_scale.as_ref(), image_ui.as_ref());
})); }));