| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  | use super::database::*;
 | 
					
						
							|  |  |  | use super::dialogs::*;
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | use gio::prelude::*;
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  | use glib::clone;
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | use gtk::prelude::*;
 | 
					
						
							|  |  |  | use gtk_macros::{action, get_widget};
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  | use libhandy::prelude::*;
 | 
					
						
							|  |  |  | use libhandy::HeaderBarExt;
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  | use std::cell::RefCell;
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  | use std::convert::TryInto;
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  | use std::rc::Rc;
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub struct Window {
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  |     window: libhandy::ApplicationWindow,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  |     db: Rc<Database>,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  |     leaflet: libhandy::Leaflet,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |     persons: RefCell<Vec<Person>>,
 | 
					
						
							| 
									
										
										
										
											2020-09-29 00:10:52 +02:00
										 |  |  |     works: RefCell<Vec<WorkDescription>>,
 | 
					
						
							|  |  |  |     recordings: RefCell<Vec<RecordingDescription>>,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |     sidebar_box: gtk::Box,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |     person_search_entry: gtk::SearchEntry,
 | 
					
						
							|  |  |  |     person_list: gtk::ListBox,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |     stack: gtk::Stack,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  |     header: libhandy::HeaderBar,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |     header_menu_button: gtk::MenuButton,
 | 
					
						
							| 
									
										
										
										
											2020-09-29 00:10:52 +02:00
										 |  |  |     work_box: gtk::Box,
 | 
					
						
							|  |  |  |     work_list: gtk::ListBox,
 | 
					
						
							|  |  |  |     recording_box: gtk::Box,
 | 
					
						
							|  |  |  |     recording_list: gtk::ListBox,
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl Window {
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  |     pub fn new(app: >k::Application) -> Rc<Self> {
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  |         let builder = gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/window.ui");
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         get_widget!(builder, libhandy::ApplicationWindow, window);
 | 
					
						
							|  |  |  |         get_widget!(builder, libhandy::Leaflet, leaflet);
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |         get_widget!(builder, gtk::Box, sidebar_box);
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |         get_widget!(builder, gtk::SearchEntry, person_search_entry);
 | 
					
						
							|  |  |  |         get_widget!(builder, gtk::ListBox, person_list);
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |         get_widget!(builder, gtk::Stack, stack);
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  |         get_widget!(builder, libhandy::HeaderBar, header);
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |         get_widget!(builder, gtk::MenuButton, header_menu_button);
 | 
					
						
							| 
									
										
										
										
											2020-09-29 00:10:52 +02:00
										 |  |  |         get_widget!(builder, gtk::Box, work_box);
 | 
					
						
							|  |  |  |         get_widget!(builder, gtk::ListBox, work_list);
 | 
					
						
							|  |  |  |         get_widget!(builder, gtk::Box, recording_box);
 | 
					
						
							|  |  |  |         get_widget!(builder, gtk::ListBox, recording_list);
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  |         let db = Rc::new(Database::new("test.sqlite"));
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |         let persons = db.get_persons();
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         let result = Rc::new(Window {
 | 
					
						
							|  |  |  |             window: window,
 | 
					
						
							|  |  |  |             db: db,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  |             leaflet: leaflet,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |             persons: RefCell::new(persons),
 | 
					
						
							| 
									
										
										
										
											2020-09-29 00:10:52 +02:00
										 |  |  |             works: RefCell::new(Vec::new()),
 | 
					
						
							|  |  |  |             recordings: RefCell::new(Vec::new()),
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |             sidebar_box: sidebar_box,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |             person_list: person_list,
 | 
					
						
							|  |  |  |             person_search_entry: person_search_entry,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |             stack: stack,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  |             header: header,
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |             header_menu_button: header_menu_button,
 | 
					
						
							| 
									
										
										
										
											2020-09-29 00:10:52 +02:00
										 |  |  |             work_box: work_box,
 | 
					
						
							|  |  |  |             work_list: work_list,
 | 
					
						
							|  |  |  |             recording_box: recording_box,
 | 
					
						
							|  |  |  |             recording_list: recording_list,
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  |         });
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |         result
 | 
					
						
							|  |  |  |             .person_list
 | 
					
						
							|  |  |  |             .connect_row_activated(clone!(@strong result => move |_, row| {
 | 
					
						
							|  |  |  |                 let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
 | 
					
						
							|  |  |  |                 let index: usize = row.get_index().try_into().unwrap();
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  |                 let person = result.persons.borrow()[index].clone();
 | 
					
						
							|  |  |  |                 result.show_person(person);
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |             }));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         result
 | 
					
						
							|  |  |  |             .person_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.person_search_entry.get_text().to_string().to_lowercase();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 search.is_empty() || result.persons.borrow()[index]
 | 
					
						
							|  |  |  |                     .name_lf()
 | 
					
						
							|  |  |  |                     .to_lowercase()
 | 
					
						
							|  |  |  |                     .contains(&search)
 | 
					
						
							|  |  |  |             }))));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         result
 | 
					
						
							|  |  |  |             .person_search_entry
 | 
					
						
							|  |  |  |             .connect_search_changed(clone!(@strong result => move |_| {
 | 
					
						
							|  |  |  |                 result.person_list.invalidate_filter();
 | 
					
						
							|  |  |  |             }));
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |         action!(
 | 
					
						
							|  |  |  |             result.window,
 | 
					
						
							|  |  |  |             "back",
 | 
					
						
							|  |  |  |             clone!(@strong result => move |_, _| {
 | 
					
						
							|  |  |  |                 result.back();
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |         );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  |         action!(
 | 
					
						
							|  |  |  |             result.window,
 | 
					
						
							|  |  |  |             "add-person",
 | 
					
						
							|  |  |  |             clone!(@strong result => move |_, _| {
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |                 PersonEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |_| {
 | 
					
						
							|  |  |  |                     result.persons.replace(result.db.get_persons());
 | 
					
						
							|  |  |  |                     result.show_persons();
 | 
					
						
							|  |  |  |                 })).show();
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  |             })
 | 
					
						
							|  |  |  |         );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:08:24 +02:00
										 |  |  |         action!(
 | 
					
						
							|  |  |  |             result.window,
 | 
					
						
							|  |  |  |             "add-instrument",
 | 
					
						
							|  |  |  |             clone!(@strong result => move |_, _| {
 | 
					
						
							|  |  |  |                 InstrumentEditor::new(result.db.clone(), &result.window, None, |instrument| {
 | 
					
						
							|  |  |  |                     println!("{:?}", instrument);
 | 
					
						
							|  |  |  |                 }).show();
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |         );
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 16:00:51 +02:00
										 |  |  |         action!(
 | 
					
						
							|  |  |  |             result.window,
 | 
					
						
							|  |  |  |             "add-work",
 | 
					
						
							|  |  |  |             clone!(@strong result => move |_, _| {
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |                 WorkEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |_| {
 | 
					
						
							|  |  |  |                     result.persons.replace(result.db.get_persons());
 | 
					
						
							|  |  |  |                     result.show_persons();
 | 
					
						
							|  |  |  |                 })).show();
 | 
					
						
							| 
									
										
										
										
											2020-09-28 16:00:51 +02:00
										 |  |  |             })
 | 
					
						
							|  |  |  |         );
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:14:05 +02:00
										 |  |  |         action!(
 | 
					
						
							|  |  |  |             result.window,
 | 
					
						
							|  |  |  |             "add-ensemble",
 | 
					
						
							|  |  |  |             clone!(@strong result => move |_, _| {
 | 
					
						
							|  |  |  |                 EnsembleEditor::new(result.db.clone(), &result.window, None, |ensemble| {
 | 
					
						
							|  |  |  |                     println!("{:?}", ensemble);
 | 
					
						
							|  |  |  |                 }).show();
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |         );
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  |         action!(result.window, "add-recording", |_, _| {
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  |             println!("TODO: Add recording.");
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |         action!(
 | 
					
						
							|  |  |  |             result.window,
 | 
					
						
							|  |  |  |             "edit-person",
 | 
					
						
							|  |  |  |             Some(glib::VariantTy::new("x").unwrap()),
 | 
					
						
							|  |  |  |             clone!(@strong result => move |_, id| {
 | 
					
						
							|  |  |  |                 let person = result.db.get_person(id.unwrap().get().unwrap()).unwrap();
 | 
					
						
							|  |  |  |                 PersonEditor::new(result.db.clone(), &result.window, Some(person), clone!(@strong result => move |person| {
 | 
					
						
							|  |  |  |                     result.persons.replace(result.db.get_persons());
 | 
					
						
							|  |  |  |                     result.show_persons();
 | 
					
						
							|  |  |  |                     result.show_person(person);
 | 
					
						
							|  |  |  |                 })).show();
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |         );
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         action!(
 | 
					
						
							|  |  |  |             result.window,
 | 
					
						
							|  |  |  |             "delete-person",
 | 
					
						
							|  |  |  |             Some(glib::VariantTy::new("x").unwrap()),
 | 
					
						
							|  |  |  |             clone!(@strong result => move |_, id| {
 | 
					
						
							|  |  |  |                 result.db.delete_person(id.unwrap().get().unwrap());
 | 
					
						
							|  |  |  |                 result.back();
 | 
					
						
							|  |  |  |                 result.persons.replace(result.db.get_persons());
 | 
					
						
							|  |  |  |                 result.show_persons();
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |         );
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  |         result.window.set_application(Some(app));
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |         result.show_persons();
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 15:02:19 +02:00
										 |  |  |         result
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pub fn present(&self) {
 | 
					
						
							|  |  |  |         self.window.present();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fn show_persons(&self) {
 | 
					
						
							|  |  |  |         for child in self.person_list.get_children() {
 | 
					
						
							|  |  |  |             self.person_list.remove(&child);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (index, person) in self.persons.borrow().iter().enumerate() {
 | 
					
						
							|  |  |  |             let label = gtk::Label::new(Some(&person.name_lf()));
 | 
					
						
							|  |  |  |             label.set_halign(gtk::Align::Start);
 | 
					
						
							|  |  |  |             let row = SelectorRow::new(index.try_into().unwrap(), &label);
 | 
					
						
							|  |  |  |             row.show_all();
 | 
					
						
							|  |  |  |             self.person_list.insert(&row, -1);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  |     fn show_person(&self, person: Person) {
 | 
					
						
							|  |  |  |         self.header.set_title(Some(&person.name_fl()));
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |         let edit_menu_item = gio::MenuItem::new(Some("Edit person"), None);
 | 
					
						
							|  |  |  |         edit_menu_item.set_action_and_target_value(
 | 
					
						
							|  |  |  |             Some("win.edit-person"),
 | 
					
						
							|  |  |  |             Some(&glib::Variant::from(person.id)),
 | 
					
						
							|  |  |  |         );
 | 
					
						
							|  |  |  |         let delete_menu_item = gio::MenuItem::new(Some("Delete person"), None);
 | 
					
						
							|  |  |  |         delete_menu_item.set_action_and_target_value(
 | 
					
						
							|  |  |  |             Some("win.delete-person"),
 | 
					
						
							|  |  |  |             Some(&glib::Variant::from(person.id)),
 | 
					
						
							|  |  |  |         );
 | 
					
						
							|  |  |  |         let menu = gio::Menu::new();
 | 
					
						
							|  |  |  |         menu.append_item(&edit_menu_item);
 | 
					
						
							|  |  |  |         menu.append_item(&delete_menu_item);
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |         self.header_menu_button.set_menu_model(Some(&menu));
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 00:10:52 +02:00
										 |  |  |         self.works.replace(self.db.get_work_descriptions(person.id));
 | 
					
						
							|  |  |  |         self.show_works();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.show_recordings();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |         self.stack.set_visible_child_name("person_screen");
 | 
					
						
							|  |  |  |         self.leaflet.set_visible_child(&self.stack);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:51:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 00:10:52 +02:00
										 |  |  |     fn show_works(&self) {
 | 
					
						
							|  |  |  |         for child in self.work_list.get_children() {
 | 
					
						
							|  |  |  |             self.work_list.remove(&child);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let works = self.works.borrow();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if works.is_empty() {
 | 
					
						
							|  |  |  |             self.work_box.hide();
 | 
					
						
							|  |  |  |         } else {
 | 
					
						
							|  |  |  |             self.work_box.show();
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (index, work) in works.iter().enumerate() {
 | 
					
						
							|  |  |  |             let label = gtk::Label::new(Some(&work.title));
 | 
					
						
							|  |  |  |             label.set_halign(gtk::Align::Start);
 | 
					
						
							|  |  |  |             let row = SelectorRow::new(index.try_into().unwrap(), &label);
 | 
					
						
							|  |  |  |             row.show_all();
 | 
					
						
							|  |  |  |             self.work_list.insert(&row, -1);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fn show_recordings(&self) {
 | 
					
						
							|  |  |  |         for child in self.recording_list.get_children() {
 | 
					
						
							|  |  |  |             self.recording_list.remove(&child);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let recordings = self.recordings.borrow();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if recordings.is_empty() {
 | 
					
						
							|  |  |  |             self.recording_box.hide();
 | 
					
						
							|  |  |  |         } else {
 | 
					
						
							|  |  |  |             self.recording_box.show();
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 23:33:48 +02:00
										 |  |  |     fn back(&self) {
 | 
					
						
							|  |  |  |         self.stack.set_visible_child_name("empty_screen");
 | 
					
						
							|  |  |  |         self.leaflet.set_visible_child(&self.sidebar_box);
 | 
					
						
							| 
									
										
										
										
											2020-09-28 20:09:49 +02:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2020-09-27 21:58:42 +02:00
										 |  |  | }
 |