Add instrument editor dialog

This commit is contained in:
Elias Projahn 2020-09-28 15:08:24 +02:00
parent 9fcab517e4
commit 1a6a6f7450
5 changed files with 152 additions and 3 deletions

View file

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

View file

@ -0,0 +1,73 @@
<?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">Name</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="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>
</object>
</child>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">Instrument</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>

View file

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

View file

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

View file

@ -34,9 +34,15 @@ impl Window {
}) })
); );
action!(result.window, "add-instrument", |_, _| { action!(
println!("TODO: Add instrument."); 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", |_, _| { action!(result.window, "add-work", |_, _| {
println!("TODO: Add work."); println!("TODO: Add work.");