Add preview of main window contents

This commit is contained in:
Elias Projahn 2020-09-28 20:09:49 +02:00
parent 6cdaf97a36
commit 21ad91d9a9
5 changed files with 266 additions and 13 deletions

View file

@ -1,13 +1,153 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.36.0 --> <!-- Generated with glade 3.36.0 -->
<interface> <interface>
<requires lib="gtk+" version="3.22" /> <requires lib="gtk+" version="3.22"/>
<object class="GtkApplicationWindow" id="window"> <object class="GtkApplicationWindow" id="window">
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="default_width">800</property> <property name="default_width">800</property>
<property name="default_height">566</property> <property name="default_height">566</property>
<child> <child>
<placeholder /> <object class="GtkPaned">
<property name="visible">True</property>
<property name="can_focus">True</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="person_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>
<property name="placeholder_text" translatable="yes">Search composers …</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>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="shadow_type">none</property>
<child>
<object class="GtkListBox" id="person_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 persons 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>
<packing>
<property name="resize">False</property>
<property name="shrink">True</property>
</packing>
</child>
<child>
<object class="GtkPaned">
<property name="visible">True</property>
<property name="can_focus">True</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="work_search_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">6</property>
<property name="margin_right">6</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>
<property name="placeholder_text" translatable="yes">Search works …</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>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="shadow_type">none</property>
<child>
<object class="GtkListBox" id="work_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 works 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>
<packing>
<property name="resize">False</property>
<property name="shrink">True</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
</packing>
</child>
</object>
</child> </child>
<child type="titlebar"> <child type="titlebar">
<object class="GtkHeaderBar"> <object class="GtkHeaderBar">

View file

@ -137,13 +137,13 @@ impl Database {
.cloned() .cloned()
} }
pub fn get_work_description_for_work(&self, work: Work) -> WorkDescription { pub fn get_work_description_for_work(&self, work: &Work) -> WorkDescription {
WorkDescription { WorkDescription {
id: work.id, id: work.id,
composer: self composer: self
.get_person(work.composer) .get_person(work.composer)
.expect("Could not find composer for work!"), .expect("Could not find composer for work!"),
title: work.title, title: work.title.clone(),
instruments: instrumentations::table instruments: instrumentations::table
.filter(instrumentations::work.eq(work.id)) .filter(instrumentations::work.eq(work.id))
.load::<Instrumentation>(&self.c) .load::<Instrumentation>(&self.c)
@ -195,7 +195,7 @@ impl Database {
pub fn get_work_description(&self, id: i64) -> Option<WorkDescription> { pub fn get_work_description(&self, id: i64) -> Option<WorkDescription> {
match self.get_work(id) { 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, None => None,
} }
} }
@ -213,6 +213,13 @@ impl Database {
.expect("Error loading works!") .expect("Error loading works!")
} }
pub fn get_work_descriptions(&self, composer_id: i64) -> Vec<WorkDescription> {
self.get_works(composer_id)
.iter()
.map(|work| self.get_work_description_for_work(work))
.collect()
}
pub fn update_ensemble(&self, ensemble: Ensemble) { pub fn update_ensemble(&self, ensemble: Ensemble) {
diesel::replace_into(ensembles::table) diesel::replace_into(ensembles::table)
.values(ensemble) .values(ensemble)

View file

@ -20,6 +20,7 @@ pub mod section_editor;
pub use section_editor::*; pub use section_editor::*;
pub mod selector_row; pub mod selector_row;
pub use selector_row::*;
pub mod work_editor; pub mod work_editor;
pub use work_editor::*; pub use work_editor::*;

View file

@ -67,12 +67,12 @@ where
.set_filter_func(Some(Box::new(clone!(@strong result => move |row| { .set_filter_func(Some(Box::new(clone!(@strong result => move |row| {
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap(); let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
let index: usize = row.get_index().try_into().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] search.is_empty() || result.persons.borrow()[index]
.name_lf() .name_lf()
.to_lowercase() .to_lowercase()
.contains(&result.search_entry.get_text().to_string().to_lowercase()) .contains(&search)
})))); }))));
result result

View file

@ -5,32 +5,107 @@ use glib::clone;
use gtk::prelude::*; use gtk::prelude::*;
use gtk_macros::{action, get_widget}; use gtk_macros::{action, get_widget};
use std::cell::RefCell; use std::cell::RefCell;
use std::convert::TryInto;
use std::rc::Rc; use std::rc::Rc;
pub struct Window { pub struct Window {
window: gtk::ApplicationWindow, window: gtk::ApplicationWindow,
db: Rc<Database>, db: Rc<Database>,
persons: RefCell<Vec<Person>>,
person_search_entry: gtk::SearchEntry,
person_list: gtk::ListBox,
works: RefCell<Vec<WorkDescription>>,
work_search_entry: gtk::SearchEntry,
work_list: gtk::ListBox,
} }
impl Window { impl Window {
pub fn new(app: &gtk::Application) -> Rc<Self> { pub fn new(app: &gtk::Application) -> Rc<Self> {
let builder = gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/window.ui"); let builder = gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/window.ui");
get_widget!(builder, gtk::ApplicationWindow, window); 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 db = Rc::new(Database::new("test.sqlite"));
let persons = db.get_persons();
let result = Rc::new(Window { let result = Rc::new(Window {
window: window, window: window,
db: db, 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::<SelectorRow>().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::<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();
}));
// result
// .work_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();
// }));
result
.work_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.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!( action!(
result.window, result.window,
"add-person", "add-person",
clone!(@strong result => move |_, _| { clone!(@strong result => move |_, _| {
PersonEditor::new(result.db.clone(), &result.window, None, |person| { PersonEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |_| {
println!("{:?}", person); result.persons.replace(result.db.get_persons());
}).show(); result.show_persons();
})).show();
}) })
); );
@ -48,9 +123,10 @@ impl Window {
result.window, result.window,
"add-work", "add-work",
clone!(@strong result => move |_, _| { clone!(@strong result => move |_, _| {
WorkEditor::new(result.db.clone(), &result.window, None, |work| { WorkEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |_| {
println!("{:?}", work); result.persons.replace(result.db.get_persons());
}).show(); result.show_persons();
})).show();
}) })
); );
@ -69,6 +145,7 @@ impl Window {
}); });
result.window.set_application(Some(app)); result.window.set_application(Some(app));
result.show_persons();
result result
} }
@ -76,4 +153,32 @@ impl Window {
pub fn present(&self) { pub fn present(&self) {
self.window.present(); 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);
}
}
} }