Add ensemble selector

This commit is contained in:
Elias Projahn 2020-10-09 23:01:56 +02:00
parent a222e00688
commit a7e7fe7b89
4 changed files with 200 additions and 0 deletions

View file

@ -2,6 +2,7 @@
<gresources> <gresources>
<gresource prefix="/de/johrpan/musicus_editor"> <gresource prefix="/de/johrpan/musicus_editor">
<file preprocess="xml-stripblanks">ui/ensemble_editor.ui</file> <file preprocess="xml-stripblanks">ui/ensemble_editor.ui</file>
<file preprocess="xml-stripblanks">ui/ensemble_selector.ui</file>
<file preprocess="xml-stripblanks">ui/instrument_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/instrument_selector.ui</file>
<file preprocess="xml-stripblanks">ui/part_editor.ui</file> <file preprocess="xml-stripblanks">ui/part_editor.ui</file>

View file

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.1 -->
<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 ensembles 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 ensemble</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,105 @@
use super::selector_row::SelectorRow;
use super::EnsembleEditor;
use crate::backend::Backend;
use crate::database::*;
use gio::prelude::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::convert::TryInto;
use std::rc::Rc;
pub struct EnsembleSelector<F>
where
F: Fn(Ensemble) -> () + 'static,
{
backend: Rc<Backend>,
window: gtk::Window,
callback: F,
list: gtk::ListBox,
search_entry: gtk::SearchEntry,
}
impl<F> EnsembleSelector<F>
where
F: Fn(Ensemble) -> () + 'static,
{
pub fn new<P: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &P, callback: F) -> Rc<Self> {
let builder =
gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/ensemble_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 result = Rc::new(EnsembleSelector {
backend: backend,
window: window,
callback: callback,
search_entry: search_entry,
list: list,
});
result
.backend
.get_ensembles(clone!(@strong result => move |ensembles| {
for (index, ensemble) in ensembles.iter().enumerate() {
let label = gtk::Label::new(Some(&ensemble.name));
label.set_halign(gtk::Align::Start);
let row = SelectorRow::new(index.try_into().unwrap(), &label);
row.show_all();
result.list.insert(&row, -1);
}
result
.list
.connect_row_activated(clone!(@strong result, @strong ensembles => 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)(ensembles[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().to_lowercase();
search.is_empty() || ensembles[index]
.name
.to_lowercase()
.contains(&search)
}))));
}));
result
.search_entry
.connect_search_changed(clone!(@strong result => move |_| {
result.list.invalidate_filter();
}));
add_button.connect_clicked(clone!(@strong result => move |_| {
let editor = EnsembleEditor::new(
result.backend.clone(),
&result.window,
None,
clone!(@strong result => move |ensemble| {
result.window.close();
(result.callback)(ensemble);
}),
);
editor.show();
}));
result.window.set_transient_for(Some(parent));
result
}
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,6 +1,9 @@
pub mod ensemble_editor; pub mod ensemble_editor;
pub use ensemble_editor::*; pub use ensemble_editor::*;
pub mod ensemble_selector;
pub use ensemble_selector::*;
pub mod instrument_editor; pub mod instrument_editor;
pub use instrument_editor::*; pub use instrument_editor::*;