Add person editor dialog

This commit is contained in:
Elias Projahn 2020-09-28 15:02:19 +02:00
parent c67cefb38a
commit 9fcab517e4
6 changed files with 200 additions and 11 deletions

2
src/dialogs/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod person_editor;
pub use person_editor::*;

View file

@ -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<F: Fn(Person) -> () + 'static, P: IsA<gtk::Window>>(
db: Rc<Database>,
parent: &P,
person: Option<Person>,
callback: F,
) -> Rc<Self> {
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::<u32>().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();
}
}

View file

@ -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::<Window>);
let window: RefCell<Option<Rc<Window>>> = RefCell::new(None);
app.connect_activate(clone!(@strong app => move |_| {
let mut window = window.borrow_mut();

View file

@ -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<Database>,
}
impl Window {
pub fn new(app: &gtk::Application) -> Self {
pub fn new(app: &gtk::Application) -> Rc<Self> {
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) {