mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 11:47:25 +01:00
Add instrument selector
This commit is contained in:
parent
9c8f32930f
commit
433c3284fe
5 changed files with 230 additions and 1 deletions
106
src/dialogs/instrument_selector.rs
Normal file
106
src/dialogs/instrument_selector.rs
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
use super::selector_row::SelectorRow;
|
||||
use super::InstrumentEditor;
|
||||
use crate::database::*;
|
||||
use gio::prelude::*;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::convert::TryInto;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct InstrumentSelector<F>
|
||||
where
|
||||
F: Fn(Instrument) -> () + 'static,
|
||||
{
|
||||
db: Rc<Database>,
|
||||
window: gtk::Window,
|
||||
callback: F,
|
||||
instruments: RefCell<Vec<Instrument>>,
|
||||
list: gtk::ListBox,
|
||||
search_entry: gtk::SearchEntry,
|
||||
}
|
||||
|
||||
impl<F> InstrumentSelector<F>
|
||||
where
|
||||
F: Fn(Instrument) -> () + 'static,
|
||||
{
|
||||
pub fn new<P: IsA<gtk::Window>>(db: Rc<Database>, parent: &P, callback: F) -> Rc<Self> {
|
||||
let builder =
|
||||
gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/instrument_selector.ui");
|
||||
|
||||
get_widget!(builder, gtk::Window, window);
|
||||
get_widget!(builder, gtk::Button, add_button);
|
||||
get_widget!(builder, gtk::SearchEntry, search_entry);
|
||||
get_widget!(builder, gtk::ListBox, list);
|
||||
|
||||
let instruments = db.get_instruments();
|
||||
|
||||
for (index, instrument) in instruments.iter().enumerate() {
|
||||
let label = gtk::Label::new(Some(&instrument.name));
|
||||
label.set_halign(gtk::Align::Start);
|
||||
let row = SelectorRow::new(index.try_into().unwrap(), &label);
|
||||
row.show_all();
|
||||
list.insert(&row, -1);
|
||||
}
|
||||
|
||||
let result = Rc::new(InstrumentSelector {
|
||||
db: db,
|
||||
window: window,
|
||||
callback: callback,
|
||||
instruments: RefCell::new(instruments),
|
||||
search_entry: search_entry,
|
||||
list: list,
|
||||
});
|
||||
|
||||
result
|
||||
.list
|
||||
.connect_row_activated(clone!(@strong result => move |_, row| {
|
||||
result.window.close();
|
||||
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
|
||||
let index: usize = row.get_index().try_into().unwrap();
|
||||
(result.callback)(result.instruments.borrow()[index].clone());
|
||||
}));
|
||||
|
||||
result
|
||||
.list
|
||||
.set_filter_func(Some(Box::new(clone!(@strong result => move |row| {
|
||||
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
|
||||
let index: usize = row.get_index().try_into().unwrap();
|
||||
let search = result.search_entry.get_text().to_string();
|
||||
|
||||
search.is_empty() || result.instruments.borrow()[index]
|
||||
.name
|
||||
.to_lowercase()
|
||||
.contains(&result.search_entry.get_text().to_string().to_lowercase())
|
||||
}))));
|
||||
|
||||
result
|
||||
.search_entry
|
||||
.connect_search_changed(clone!(@strong result => move |_| {
|
||||
result.list.invalidate_filter();
|
||||
}));
|
||||
|
||||
add_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
let editor = InstrumentEditor::new(
|
||||
result.db.clone(),
|
||||
&result.window,
|
||||
None,
|
||||
clone!(@strong result => move |instrument| {
|
||||
result.window.close();
|
||||
(result.callback)(instrument);
|
||||
}),
|
||||
);
|
||||
|
||||
editor.show();
|
||||
}));
|
||||
|
||||
result.window.set_transient_for(Some(parent));
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn show(&self) {
|
||||
self.window.show();
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,9 @@ pub use ensemble_editor::*;
|
|||
pub mod instrument_editor;
|
||||
pub use instrument_editor::*;
|
||||
|
||||
pub mod instrument_selector;
|
||||
pub use instrument_selector::*;
|
||||
|
||||
pub mod person_editor;
|
||||
pub use person_editor::*;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use super::person_selector::PersonSelector;
|
||||
use super::selector_row::SelectorRow;
|
||||
use super::{InstrumentSelector, PersonSelector};
|
||||
use crate::database::*;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
|
|
@ -49,6 +50,7 @@ pub struct WorkEditor {
|
|||
composer: RefCell<Option<Person>>,
|
||||
composer_label: gtk::Label,
|
||||
instruments: RefCell<Vec<Instrument>>,
|
||||
instrument_list: gtk::ListBox,
|
||||
structure: RefCell<Vec<PartOrSection>>,
|
||||
}
|
||||
|
||||
|
|
@ -131,6 +133,7 @@ impl WorkEditor {
|
|||
composer: composer,
|
||||
composer_label: composer_label,
|
||||
instruments: instruments,
|
||||
instrument_list: instrument_list,
|
||||
structure: structure,
|
||||
});
|
||||
|
||||
|
|
@ -178,6 +181,17 @@ impl WorkEditor {
|
|||
})).show();
|
||||
}));
|
||||
|
||||
add_instrument_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
InstrumentSelector::new(result.db.clone(), &result.window, clone!(@strong result => move |instrument| {
|
||||
{
|
||||
let mut instruments = result.instruments.borrow_mut();
|
||||
instruments.push(instrument);
|
||||
}
|
||||
|
||||
result.show_instruments();
|
||||
})).show();
|
||||
}));
|
||||
|
||||
result.window.set_transient_for(Some(parent));
|
||||
|
||||
result
|
||||
|
|
@ -186,4 +200,18 @@ impl WorkEditor {
|
|||
pub fn show(&self) {
|
||||
self.window.show();
|
||||
}
|
||||
|
||||
fn show_instruments(&self) {
|
||||
for child in self.instrument_list.get_children() {
|
||||
self.instrument_list.remove(&child);
|
||||
}
|
||||
|
||||
for (index, instrument) in self.instruments.borrow().iter().enumerate() {
|
||||
let label = gtk::Label::new(Some(&instrument.name));
|
||||
label.set_halign(gtk::Align::Start);
|
||||
let row = SelectorRow::new(index.try_into().unwrap(), &label);
|
||||
row.show_all();
|
||||
self.instrument_list.insert(&row, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue