From 21ad91d9a9737c8c00b8ca745ddb60a5f7d2124c Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Mon, 28 Sep 2020 20:09:49 +0200 Subject: [PATCH] Add preview of main window contents --- res/ui/window.ui | 144 ++++++++++++++++++++++++++++++++- src/database/database.rs | 13 ++- src/dialogs/mod.rs | 1 + src/dialogs/person_selector.rs | 4 +- src/window.rs | 117 +++++++++++++++++++++++++-- 5 files changed, 266 insertions(+), 13 deletions(-) diff --git a/res/ui/window.ui b/res/ui/window.ui index 76b4b41..b0e3242 100644 --- a/res/ui/window.ui +++ b/res/ui/window.ui @@ -1,13 +1,153 @@ - + False 800 566 - + + True + True + + + True + False + vertical + + + True + True + 6 + 6 + 6 + 6 + edit-find-symbolic + False + False + Search composers … + + + False + True + 0 + + + + + True + True + + + True + False + none + + + True + False + + + True + False + No persons found. + + + + + + + + + True + True + 1 + + + + + False + True + + + + + True + True + + + True + False + vertical + + + True + True + 6 + 6 + 6 + 6 + 6 + 6 + edit-find-symbolic + False + False + Search works … + + + False + True + 0 + + + + + True + True + + + True + False + none + + + True + False + + + True + False + No works found. + + + + + + + + + True + True + 1 + + + + + False + True + + + + + + + + True + True + + + diff --git a/src/database/database.rs b/src/database/database.rs index d4801b7..e28f976 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -137,13 +137,13 @@ impl Database { .cloned() } - pub fn get_work_description_for_work(&self, work: Work) -> WorkDescription { + pub fn get_work_description_for_work(&self, work: &Work) -> WorkDescription { WorkDescription { id: work.id, composer: self .get_person(work.composer) .expect("Could not find composer for work!"), - title: work.title, + title: work.title.clone(), instruments: instrumentations::table .filter(instrumentations::work.eq(work.id)) .load::(&self.c) @@ -195,7 +195,7 @@ impl Database { pub fn get_work_description(&self, id: i64) -> Option { match self.get_work(id) { - Some(work) => Some(self.get_work_description_for_work(work)), + Some(work) => Some(self.get_work_description_for_work(&work)), None => None, } } @@ -213,6 +213,13 @@ impl Database { .expect("Error loading works!") } + pub fn get_work_descriptions(&self, composer_id: i64) -> Vec { + self.get_works(composer_id) + .iter() + .map(|work| self.get_work_description_for_work(work)) + .collect() + } + pub fn update_ensemble(&self, ensemble: Ensemble) { diesel::replace_into(ensembles::table) .values(ensemble) diff --git a/src/dialogs/mod.rs b/src/dialogs/mod.rs index b9dcc53..8b6df96 100644 --- a/src/dialogs/mod.rs +++ b/src/dialogs/mod.rs @@ -20,6 +20,7 @@ pub mod section_editor; pub use section_editor::*; pub mod selector_row; +pub use selector_row::*; pub mod work_editor; pub use work_editor::*; diff --git a/src/dialogs/person_selector.rs b/src/dialogs/person_selector.rs index e76600c..234fb36 100644 --- a/src/dialogs/person_selector.rs +++ b/src/dialogs/person_selector.rs @@ -67,12 +67,12 @@ where .set_filter_func(Some(Box::new(clone!(@strong result => move |row| { let row = row.get_child().unwrap().downcast::().unwrap(); let index: usize = row.get_index().try_into().unwrap(); - let search = result.search_entry.get_text().to_string(); + let search = result.search_entry.get_text().to_string().to_lowercase(); search.is_empty() || result.persons.borrow()[index] .name_lf() .to_lowercase() - .contains(&result.search_entry.get_text().to_string().to_lowercase()) + .contains(&search) })))); result diff --git a/src/window.rs b/src/window.rs index d6ba19f..e7cdc27 100644 --- a/src/window.rs +++ b/src/window.rs @@ -5,32 +5,107 @@ use glib::clone; use gtk::prelude::*; use gtk_macros::{action, get_widget}; use std::cell::RefCell; +use std::convert::TryInto; use std::rc::Rc; pub struct Window { window: gtk::ApplicationWindow, db: Rc, + persons: RefCell>, + person_search_entry: gtk::SearchEntry, + person_list: gtk::ListBox, + works: RefCell>, + work_search_entry: gtk::SearchEntry, + work_list: gtk::ListBox, } impl Window { pub fn new(app: >k::Application) -> Rc { let builder = gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/window.ui"); get_widget!(builder, gtk::ApplicationWindow, window); + get_widget!(builder, gtk::SearchEntry, person_search_entry); + get_widget!(builder, gtk::ListBox, person_list); + get_widget!(builder, gtk::SearchEntry, work_search_entry); + get_widget!(builder, gtk::ListBox, work_list); let db = Rc::new(Database::new("test.sqlite")); + let persons = db.get_persons(); let result = Rc::new(Window { window: window, db: db, + persons: RefCell::new(persons), + person_list: person_list, + person_search_entry: person_search_entry, + works: RefCell::new(Vec::new()), + work_search_entry: work_search_entry, + work_list: work_list, }); + result + .person_list + .connect_row_activated(clone!(@strong result => move |_, row| { + let row = row.get_child().unwrap().downcast::().unwrap(); + let index: usize = row.get_index().try_into().unwrap(); + + let works = result.db.get_work_descriptions(result.persons.borrow()[index].id); + result.works.replace(works); + result.show_works(); + })); + + result + .person_list + .set_filter_func(Some(Box::new(clone!(@strong result => move |row| { + let row = row.get_child().unwrap().downcast::().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(); + })); + + // result + // .work_list + // .connect_row_activated(clone!(@strong result => move |_, row| { + // let row = row.get_child().unwrap().downcast::().unwrap(); + // let index: usize = row.get_index().try_into().unwrap(); + // })); + + result + .work_list + .set_filter_func(Some(Box::new(clone!(@strong result => move |row| { + let row = row.get_child().unwrap().downcast::().unwrap(); + let index: usize = row.get_index().try_into().unwrap(); + let search = result.work_search_entry.get_text().to_string().to_lowercase(); + + search.is_empty() || result.works.borrow()[index] + .title + .to_lowercase() + .contains(&search) + })))); + + result + .work_search_entry + .connect_search_changed(clone!(@strong result => move |_| { + result.work_list.invalidate_filter(); + })); + action!( result.window, "add-person", clone!(@strong result => move |_, _| { - PersonEditor::new(result.db.clone(), &result.window, None, |person| { - println!("{:?}", person); - }).show(); + PersonEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |_| { + result.persons.replace(result.db.get_persons()); + result.show_persons(); + })).show(); }) ); @@ -48,9 +123,10 @@ impl Window { result.window, "add-work", clone!(@strong result => move |_, _| { - WorkEditor::new(result.db.clone(), &result.window, None, |work| { - println!("{:?}", work); - }).show(); + WorkEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |_| { + result.persons.replace(result.db.get_persons()); + result.show_persons(); + })).show(); }) ); @@ -69,6 +145,7 @@ impl Window { }); result.window.set_application(Some(app)); + result.show_persons(); result } @@ -76,4 +153,32 @@ impl Window { pub fn present(&self) { self.window.present(); } + + 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); + } + } + + fn show_works(&self) { + for child in self.work_list.get_children() { + self.work_list.remove(&child); + } + + for (index, work) in self.works.borrow().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); + } + } }