diff --git a/res/resources.xml b/res/resources.xml index 222bb34..85360bf 100644 --- a/res/resources.xml +++ b/res/resources.xml @@ -1,6 +1,7 @@ + ui/person_editor.ui ui/window.ui diff --git a/res/ui/person_editor.ui b/res/ui/person_editor.ui new file mode 100644 index 0000000..e99de6e --- /dev/null +++ b/res/ui/person_editor.ui @@ -0,0 +1,96 @@ + + + + + + False + True + True + dialog + + + True + False + 18 + 12 + 6 + + + True + False + end + First name + + + 0 + 0 + + + + + True + True + True + + + 1 + 0 + + + + + True + False + end + Last name + + + 0 + 1 + + + + + True + True + True + + + 1 + 1 + + + + + + + True + False + Person + + + Cancel + True + True + True + + + + + Save + True + True + True + + + + end + 1 + + + + + + diff --git a/src/dialogs/mod.rs b/src/dialogs/mod.rs new file mode 100644 index 0000000..76b4d04 --- /dev/null +++ b/src/dialogs/mod.rs @@ -0,0 +1,2 @@ +pub mod person_editor; +pub use person_editor::*; \ No newline at end of file diff --git a/src/dialogs/person_editor.rs b/src/dialogs/person_editor.rs new file mode 100644 index 0000000..c48bd08 --- /dev/null +++ b/src/dialogs/person_editor.rs @@ -0,0 +1,70 @@ +use crate::database::*; +use glib::clone; +use gtk::prelude::*; +use gtk_macros::get_widget; +use std::rc::Rc; + +pub struct PersonEditor { + window: gtk::Window, + id: i64, + first_name_entry: gtk::Entry, + last_name_entry: gtk::Entry, +} + +impl PersonEditor { + pub fn new () + 'static, P: IsA>( + db: Rc, + parent: &P, + person: Option, + callback: F, + ) -> Rc { + let builder = gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/person_editor.ui"); + + get_widget!(builder, gtk::Window, window); + get_widget!(builder, gtk::Button, cancel_button); + get_widget!(builder, gtk::Button, save_button); + get_widget!(builder, gtk::Entry, first_name_entry); + get_widget!(builder, gtk::Entry, last_name_entry); + + let id = match person { + Some(person) => { + first_name_entry.set_text(&person.first_name); + last_name_entry.set_text(&person.last_name); + person.id + } + None => rand::random::().into(), + }; + + let result = Rc::new(PersonEditor { + id: id, + window: window, + first_name_entry: first_name_entry, + last_name_entry: last_name_entry, + }); + + cancel_button.connect_clicked(clone!(@strong result => move |_| { + result.window.close(); + })); + + save_button.connect_clicked(clone!(@strong result => move |_| { + result.window.close(); + + let person = Person { + id: result.id, + first_name: result.first_name_entry.get_text().to_string(), + last_name: result.last_name_entry.get_text().to_string(), + }; + + db.update_person(person.clone()); + callback(person); + })); + + result.window.set_transient_for(Some(parent)); + + result + } + + pub fn show(&self) { + self.window.show(); + } +} diff --git a/src/main.rs b/src/main.rs index b225e47..7d8203e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,10 @@ extern crate diesel_migrations; use gio::prelude::*; use glib::clone; use std::cell::RefCell; +use std::rc::Rc; mod database; +mod dialogs; mod window; use window::Window; @@ -26,7 +28,7 @@ fn main() { ) .expect("Failed to initialize GTK application!"); - let window = RefCell::new(None::); + let window: RefCell>> = RefCell::new(None); app.connect_activate(clone!(@strong app => move |_| { let mut window = window.borrow_mut(); diff --git a/src/window.rs b/src/window.rs index c53a6e1..0e54651 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,40 +1,58 @@ +use super::database::*; +use super::dialogs::*; use gio::prelude::*; +use glib::clone; use gtk::prelude::*; use gtk_macros::{action, get_widget}; +use std::cell::RefCell; +use std::rc::Rc; pub struct Window { window: gtk::ApplicationWindow, + db: Rc, } impl Window { - pub fn new(app: >k::Application) -> Self { + pub fn new(app: >k::Application) -> Rc { let builder = gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/window.ui"); - get_widget!(builder, gtk::ApplicationWindow, window); - action!(window, "add-person", |_, _| { - println!("TODO: Add person."); + let db = Rc::new(Database::new("test.sqlite")); + + let result = Rc::new(Window { + window: window, + db: db, }); - action!(window, "add-instrument", |_, _| { + action!( + result.window, + "add-person", + clone!(@strong result => move |_, _| { + PersonEditor::new(result.db.clone(), &result.window, None, |person| { + println!("{:?}", person); + }).show(); + }) + ); + + action!(result.window, "add-instrument", |_, _| { println!("TODO: Add instrument."); }); - action!(window, "add-work", |_, _| { + action!(result.window, "add-work", |_, _| { println!("TODO: Add work."); }); - action!(window, "add-ensemble", |_, _| { + action!(result.window, "add-ensemble", |_, _| { println!("TODO: Add ensemble."); }); - action!(window, "add-recording", |_, _| { + action!(result.window, "add-recording", |_, _| { println!("TODO: Add recording."); }); - window.set_application(Some(app)); + result.window.set_application(Some(app)); - Window { window: window } + result } pub fn present(&self) {