Add instrument selector

This commit is contained in:
Elias Projahn 2020-09-28 17:19:34 +02:00
parent 9c8f32930f
commit 433c3284fe
5 changed files with 230 additions and 1 deletions

View file

@ -3,6 +3,7 @@
<gresource prefix="/de/johrpan/musicus_editor">
<file preprocess="xml-stripblanks">ui/ensemble_editor.ui</file>
<file preprocess="xml-stripblanks">ui/instrument_editor.ui</file>
<file preprocess="xml-stripblanks">ui/instrument_selector.ui</file>
<file preprocess="xml-stripblanks">ui/person_editor.ui</file>
<file preprocess="xml-stripblanks">ui/person_selector.ui</file>
<file preprocess="xml-stripblanks">ui/window.ui</file>

View file

@ -0,0 +1,91 @@
<?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="default_width">350</property>
<property name="default_height">300</property>
<property name="destroy_with_parent">True</property>
<property name="type_hint">dialog</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkSearchEntry" id="search_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_start">6</property>
<property name="margin_end">6</property>
<property name="margin_top">6</property>
<property name="margin_bottom">6</property>
<property name="primary_icon_name">edit-find-symbolic</property>
<property name="primary_icon_activatable">False</property>
<property name="primary_icon_sensitive">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkListBox" id="list">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child type="placeholder">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">No instruments found.</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">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">Select instrument</property>
<property name="show_close_button">True</property>
<child>
<object class="GtkButton" id="add_button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">list-add-symbolic</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>

View 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();
}
}

View file

@ -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::*;

View file

@ -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);
}
}
}