Add section editor and handling

This commit is contained in:
Elias Projahn 2020-09-28 18:12:59 +02:00
parent 277a1f6189
commit 6cdaf97a36
7 changed files with 226 additions and 57 deletions

View file

@ -16,6 +16,9 @@ pub use person_editor::*;
pub mod person_selector;
pub use person_selector::*;
pub mod section_editor;
pub use section_editor::*;
pub mod selector_row;
pub mod work_editor;

View file

@ -0,0 +1,61 @@
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::rc::Rc;
pub struct SectionEditor {
window: gtk::Window,
title_entry: gtk::Entry,
}
impl SectionEditor {
pub fn new<F: Fn(WorkSectionDescription) -> () + 'static, P: IsA<gtk::Window>>(
parent: &P,
section: Option<WorkSectionDescription>,
callback: F,
) -> Rc<Self> {
let builder =
gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/section_editor.ui");
get_widget!(builder, gtk::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Entry, title_entry);
match section {
Some(section) => {
title_entry.set_text(&section.title);
}
None => (),
}
let result = Rc::new(SectionEditor {
window: window,
title_entry: title_entry,
});
cancel_button.connect_clicked(clone!(@strong result => move |_| {
result.window.close();
}));
save_button.connect_clicked(clone!(@strong result => move |_| {
result.window.close();
let section = WorkSectionDescription {
before_index: 0,
title: result.title_entry.get_text().to_string(),
};
callback(section);
}));
result.window.set_transient_for(Some(parent));
result
}
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,5 +1,5 @@
use super::selector_row::SelectorRow;
use super::{InstrumentSelector, PersonSelector, PartEditor};
use super::{InstrumentSelector, PersonSelector, PartEditor, SectionEditor};
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
@ -226,17 +226,15 @@ impl WorkEditor {
})).show();
}));
remove_part_button.connect_clicked(clone!(@strong result => move |_| {
let row = result.get_selected_part_row();
match row {
Some(row) => {
let index = row.get_index();
let index: usize = index.try_into().unwrap();
result.structure.borrow_mut().remove(index);
result.show_parts();
add_section_button.connect_clicked(clone!(@strong result => move |_| {
SectionEditor::new(&result.window, None, clone!(@strong result => move |section| {
{
let mut structure = result.structure.borrow_mut();
structure.push(PartOrSection::section(section));
}
None => (),
}
result.show_parts();
})).show();
}));
edit_part_button.connect_clicked(clone!(@strong result => move |_| {
@ -245,15 +243,38 @@ impl WorkEditor {
Some(row) => {
let index = row.get_index();
let index: usize = index.try_into().unwrap();
let part = &result.structure.borrow()[index];
let pos = &result.structure.borrow()[index];
if pos.is_part() {
let editor =
PartEditor::new(result.db.clone(), &result.window, Some(pos.unwrap_part()), clone!(@strong result => move |part| {
result.structure.borrow_mut()[index] = PartOrSection::part(part);
result.show_parts();
}));
let editor =
PartEditor::new(result.db.clone(), &result.window, Some(part.unwrap_part()), clone!(@strong result => move |part| {
result.structure.borrow_mut()[index] = PartOrSection::part(part);
result.show_parts();
}));
editor.show();
} else {
let editor =
SectionEditor::new(&result.window, Some(pos.unwrap_section()), clone!(@strong result => move |section| {
result.structure.borrow_mut()[index] = PartOrSection::section(section);
result.show_parts();
}));
editor.show();
editor.show();
}
}
None => (),
}
}));
remove_part_button.connect_clicked(clone!(@strong result => move |_| {
let row = result.get_selected_part_row();
match row {
Some(row) => {
let index = row.get_index();
let index: usize = index.try_into().unwrap();
result.structure.borrow_mut().remove(index);
result.show_parts();
}
None => (),
}
@ -330,6 +351,15 @@ impl WorkEditor {
for (index, part) in self.structure.borrow().iter().enumerate() {
let label = gtk::Label::new(Some(&part.get_title()));
label.set_halign(gtk::Align::Start);
if part.is_part() {
label.set_margin_start(6);
} else {
let attributes = pango::AttrList::new();
attributes.insert(pango::Attribute::new_weight(pango::Weight::Bold).unwrap());
label.set_attributes(Some(&attributes));
}
let row = SelectorRow::new(index.try_into().unwrap(), &label);
row.show_all();
self.part_list.insert(&row, -1);