Add part editor

This commit is contained in:
Elias Projahn 2020-09-28 17:51:02 +02:00
parent a942a2e677
commit 277a1f6189
5 changed files with 517 additions and 1 deletions

View file

@ -4,6 +4,7 @@
<file preprocess="xml-stripblanks">ui/ensemble_editor.ui</file> <file preprocess="xml-stripblanks">ui/ensemble_editor.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/person_editor.ui</file> <file preprocess="xml-stripblanks">ui/person_editor.ui</file>
<file preprocess="xml-stripblanks">ui/person_selector.ui</file> <file preprocess="xml-stripblanks">ui/person_selector.ui</file>
<file preprocess="xml-stripblanks">ui/window.ui</file> <file preprocess="xml-stripblanks">ui/window.ui</file>

248
res/ui/part_editor.ui Normal file
View file

@ -0,0 +1,248 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.36.0 -->
<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">450</property>
<property name="default_height">300</property>
<property name="destroy_with_parent">True</property>
<property name="type_hint">dialog</property>
<child>
<object class="GtkNotebook">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="border_width">18</property>
<property name="row_spacing">12</property>
<property name="column_spacing">6</property>
<child>
<object class="GtkButton" id="composer_button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="hexpand">True</property>
<child>
<object class="GtkLabel" id="composer_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Auswählen …</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Composer</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="title_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="composer_labe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Title</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="reset_composer_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">edit-undo-symbolic</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
</packing>
</child>
</object>
</child>
<child type="tab">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Overview</property>
</object>
<packing>
<property name="tab_fill">False</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="border_width">18</property>
<property name="spacing">6</property>
<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="instrument_list">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child type="placeholder">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">No instruments added.</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="border_width">0</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<object class="GtkButton" id="add_instrument_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>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="remove_instrument_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-remove-symbolic</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child type="tab">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Instruments</property>
</object>
<packing>
<property name="position">1</property>
<property name="tab_fill">False</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">Work part</property>
<child>
<object class="GtkButton" id="cancel_button">
<property name="label" translatable="yes">Cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
</child>
<child>
<object class="GtkButton" id="save_button">
<property name="label" translatable="yes">Save</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<style>
<class name="suggested-action"/>
</style>
</object>
<packing>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

View file

@ -7,6 +7,9 @@ pub use instrument_editor::*;
pub mod instrument_selector; pub mod instrument_selector;
pub use instrument_selector::*; pub use instrument_selector::*;
pub mod part_editor;
pub use part_editor::*;
pub mod person_editor; pub mod person_editor;
pub use person_editor::*; pub use person_editor::*;

156
src/dialogs/part_editor.rs Normal file
View file

@ -0,0 +1,156 @@
use super::selector_row::SelectorRow;
use super::{InstrumentSelector, PersonSelector};
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::convert::TryInto;
use std::rc::Rc;
pub struct PartEditor {
db: Rc<Database>,
window: gtk::Window,
title_entry: gtk::Entry,
composer: RefCell<Option<Person>>,
composer_label: gtk::Label,
instruments: RefCell<Vec<Instrument>>,
instrument_list: gtk::ListBox,
}
impl PartEditor {
pub fn new<F: Fn(WorkPartDescription) -> () + 'static, P: IsA<gtk::Window>>(
db: Rc<Database>,
parent: &P,
part: Option<WorkPartDescription>,
callback: F,
) -> Rc<Self> {
let builder = gtk::Builder::from_resource("/de/johrpan/musicus_editor/ui/part_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);
get_widget!(builder, gtk::Button, composer_button);
get_widget!(builder, gtk::Label, composer_label);
get_widget!(builder, gtk::Button, reset_composer_button);
get_widget!(builder, gtk::ListBox, instrument_list);
get_widget!(builder, gtk::Button, add_instrument_button);
get_widget!(builder, gtk::Button, remove_instrument_button);
match part.clone() {
Some(part) => {
title_entry.set_text(&part.title);
}
None => (),
};
let composer = RefCell::new(match part.clone() {
Some(work) => {
match work.composer.clone() {
Some(composer) => composer_label.set_text(&composer.name_fl()),
None => (),
}
work.composer
},
None => None,
});
let instruments = RefCell::new(match part.clone() {
Some(work) => work.instruments,
None => Vec::new(),
});
let result = Rc::new(PartEditor {
db: db,
window: window,
title_entry: title_entry,
composer: composer,
composer_label: composer_label,
instruments: instruments,
instrument_list: instrument_list,
});
cancel_button.connect_clicked(clone!(@strong result => move |_| {
result.window.close();
}));
save_button.connect_clicked(clone!(@strong result => move |_| {
result.window.close();
callback(WorkPartDescription {
title: result.title_entry.get_text().to_string(),
composer: result.composer.borrow().clone(),
instruments: result.instruments.borrow().clone(),
});
}));
composer_button.connect_clicked(clone!(@strong result => move |_| {
PersonSelector::new(result.db.clone(), &result.window, clone!(@strong result => move |person| {
result.composer.replace(Some(person.clone()));
result.composer_label.set_text(&person.name_fl());
})).show();
}));
reset_composer_button.connect_clicked(clone!(@strong result => move |_| {
result.composer.replace(None);
result.composer_label.set_text("Select …");
}));
add_instrument_button.connect_clicked(clone!(@strong result => move |_| {
InstrumentSelector::new(result.db.clone(), &result.window, clone!(@strong result => move |instrument| {
{
let mut instruments = result.instruments.borrow_mut();
instruments.push(instrument);
}
result.show_instruments();
})).show();
}));
remove_instrument_button.connect_clicked(clone!(@strong result => move |_| {
let row = result.get_selected_instrument_row();
match row {
Some(row) => {
let index = row.get_index();
let index: usize = index.try_into().unwrap();
result.instruments.borrow_mut().remove(index);
result.show_instruments();
}
None => (),
}
}));
result.window.set_transient_for(Some(parent));
result
}
pub fn show(&self) {
self.window.show();
}
fn show_instruments(&self) {
for child in self.instrument_list.get_children() {
self.instrument_list.remove(&child);
}
for (index, instrument) in self.instruments.borrow().iter().enumerate() {
let label = gtk::Label::new(Some(&instrument.name));
label.set_halign(gtk::Align::Start);
let row = SelectorRow::new(index.try_into().unwrap(), &label);
row.show_all();
self.instrument_list.insert(&row, -1);
}
}
fn get_selected_instrument_row(&self) -> Option<SelectorRow> {
match self.instrument_list.get_selected_rows().first() {
Some(row) => match row.get_child() {
Some(child) => Some(child.downcast().unwrap()),
None => None,
},
None => None,
}
}
}

View file

@ -1,5 +1,5 @@
use super::selector_row::SelectorRow; use super::selector_row::SelectorRow;
use super::{InstrumentSelector, PersonSelector}; use super::{InstrumentSelector, PersonSelector, PartEditor};
use crate::database::*; use crate::database::*;
use glib::clone; use glib::clone;
use gtk::prelude::*; use gtk::prelude::*;
@ -39,6 +39,14 @@ impl PartOrSection {
pub fn unwrap_section(&self) -> WorkSectionDescription { pub fn unwrap_section(&self) -> WorkSectionDescription {
self.section.as_ref().unwrap().clone() self.section.as_ref().unwrap().clone()
} }
pub fn get_title(&self) -> String {
if self.is_part() {
self.unwrap_part().title
} else {
self.unwrap_section().title
}
}
} }
pub struct WorkEditor { pub struct WorkEditor {
@ -52,6 +60,7 @@ pub struct WorkEditor {
instruments: RefCell<Vec<Instrument>>, instruments: RefCell<Vec<Instrument>>,
instrument_list: gtk::ListBox, instrument_list: gtk::ListBox,
structure: RefCell<Vec<PartOrSection>>, structure: RefCell<Vec<PartOrSection>>,
part_list: gtk::ListBox,
} }
impl WorkEditor { impl WorkEditor {
@ -135,6 +144,7 @@ impl WorkEditor {
instruments: instruments, instruments: instruments,
instrument_list: instrument_list, instrument_list: instrument_list,
structure: structure, structure: structure,
part_list: part_list,
}); });
cancel_button.connect_clicked(clone!(@strong result => move |_| { cancel_button.connect_clicked(clone!(@strong result => move |_| {
@ -205,6 +215,80 @@ impl WorkEditor {
} }
})); }));
add_part_button.connect_clicked(clone!(@strong result => move |_| {
PartEditor::new(result.db.clone(), &result.window, None, clone!(@strong result => move |part| {
{
let mut structure = result.structure.borrow_mut();
structure.push(PartOrSection::part(part));
}
result.show_parts();
})).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();
}
None => (),
}
}));
edit_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();
let part = &result.structure.borrow()[index];
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();
}
None => (),
}
}));
move_part_up_button.connect_clicked(clone!(@strong result => move |_| {
let row = result.get_selected_part_row();
match row {
Some(row) => {
let index = row.get_index();
if index > 0 {
let index: usize = index.try_into().unwrap();
result.structure.borrow_mut().swap(index - 1, index);
result.show_parts();
}
}
None => (),
}
}));
move_part_down_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();
if index < result.structure.borrow().len() - 1 {
result.structure.borrow_mut().swap(index, index + 1);
result.show_parts();
}
}
None => (),
}
}));
result.window.set_transient_for(Some(parent)); result.window.set_transient_for(Some(parent));
result result
@ -237,4 +321,28 @@ impl WorkEditor {
None => None, None => None,
} }
} }
fn show_parts(&self) {
for child in self.part_list.get_children() {
self.part_list.remove(&child);
}
for (index, part) in self.structure.borrow().iter().enumerate() {
let label = gtk::Label::new(Some(&part.get_title()));
label.set_halign(gtk::Align::Start);
let row = SelectorRow::new(index.try_into().unwrap(), &label);
row.show_all();
self.part_list.insert(&row, -1);
}
}
fn get_selected_part_row(&self) -> Option<SelectorRow> {
match self.part_list.get_selected_rows().first() {
Some(row) => match row.get_child() {
Some(child) => Some(child.downcast().unwrap()),
None => None,
},
None => None,
}
}
} }