From 1a6a6f74506e97c9b3511ab31677e8ccf821e90d Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Mon, 28 Sep 2020 15:08:24 +0200 Subject: [PATCH] Add instrument editor dialog --- res/resources.xml | 1 + res/ui/instrument_editor.ui | 73 ++++++++++++++++++++++++++++++++ src/dialogs/instrument_editor.rs | 66 +++++++++++++++++++++++++++++ src/dialogs/mod.rs | 3 ++ src/window.rs | 12 ++++-- 5 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 res/ui/instrument_editor.ui create mode 100644 src/dialogs/instrument_editor.rs diff --git a/res/resources.xml b/res/resources.xml index 85360bf..188506f 100644 --- a/res/resources.xml +++ b/res/resources.xml @@ -1,6 +1,7 @@ + ui/instrument_editor.ui ui/person_editor.ui ui/window.ui diff --git a/res/ui/instrument_editor.ui b/res/ui/instrument_editor.ui new file mode 100644 index 0000000..c77eb81 --- /dev/null +++ b/res/ui/instrument_editor.ui @@ -0,0 +1,73 @@ + + + + + + False + True + True + dialog + + + True + False + 18 + 12 + 6 + + + True + False + end + Name + + + 0 + 0 + + + + + True + True + True + + + 1 + 0 + + + + + + + True + False + Instrument + + + Cancel + True + True + True + + + + + Save + True + True + True + + + + end + 1 + + + + + + diff --git a/src/dialogs/instrument_editor.rs b/src/dialogs/instrument_editor.rs new file mode 100644 index 0000000..b9e454b --- /dev/null +++ b/src/dialogs/instrument_editor.rs @@ -0,0 +1,66 @@ +use crate::database::*; +use glib::clone; +use gtk::prelude::*; +use gtk_macros::get_widget; +use std::rc::Rc; + +pub struct InstrumentEditor { + window: gtk::Window, + id: i64, + name_entry: gtk::Entry, +} + +impl InstrumentEditor { + pub fn new () + 'static, P: IsA>( + db: Rc, + parent: &P, + instrument: Option, + callback: F, + ) -> Rc { + let builder = + gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/instrument_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, name_entry); + + let id = match instrument { + Some(instrument) => { + name_entry.set_text(&instrument.name); + instrument.id + } + None => rand::random::().into(), + }; + + let result = Rc::new(InstrumentEditor { + id: id, + window: window, + name_entry: 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 instrument = Instrument { + id: result.id, + name: result.name_entry.get_text().to_string(), + }; + + db.update_instrument(instrument.clone()); + callback(instrument); + })); + + result.window.set_transient_for(Some(parent)); + + result + } + + pub fn show(&self) { + self.window.show(); + } +} diff --git a/src/dialogs/mod.rs b/src/dialogs/mod.rs index 76b4d04..b50f93b 100644 --- a/src/dialogs/mod.rs +++ b/src/dialogs/mod.rs @@ -1,2 +1,5 @@ +pub mod instrument_editor; +pub use instrument_editor::*; + pub mod person_editor; pub use person_editor::*; \ No newline at end of file diff --git a/src/window.rs b/src/window.rs index 0e54651..d45ff7f 100644 --- a/src/window.rs +++ b/src/window.rs @@ -34,9 +34,15 @@ impl Window { }) ); - action!(result.window, "add-instrument", |_, _| { - println!("TODO: Add instrument."); - }); + action!( + result.window, + "add-instrument", + clone!(@strong result => move |_, _| { + InstrumentEditor::new(result.db.clone(), &result.window, None, |instrument| { + println!("{:?}", instrument); + }).show(); + }) + ); action!(result.window, "add-work", |_, _| { println!("TODO: Add work.");