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

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/de/johrpan/musicus_editor">
<file preprocess="xml-stripblanks">ui/person_editor.ui</file>
<file preprocess="xml-stripblanks">ui/window.ui</file>
</gresource>
</gresources>

96
res/ui/person_editor.ui Normal file
View file

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.36.0 -->
<interface>
<requires lib="gtk+" version="3.22"/>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="modal">True</property>
<property name="destroy_with_parent">True</property>
<property name="type_hint">dialog</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="border_width">18</property>
<property name="row_spacing">12</property>
<property name="column_spacing">6</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">First name</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="first_name_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Last name</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="last_name_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
</object>
</child>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">Person</property>
<child>
<object class="GtkButton" id="cancel_button">
<property name="label" translatable="yes">Cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
</child>
<child>
<object class="GtkButton" id="save_button">
<property name="label" translatable="yes">Save</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<style>
<class name="suggested-action"/>
</style>
</object>
<packing>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

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) {