mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-27 04:07:25 +01:00
Revert merging of server and client repository
This commit is contained in:
parent
2b9cff885b
commit
8c3c439409
147 changed files with 53 additions and 2113 deletions
135
src/editors/ensemble.rs
Normal file
135
src/editors/ensemble.rs
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use crate::widgets::{Navigator, NavigatorScreen};
|
||||
use anyhow::Result;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A dialog for creating or editing a ensemble.
|
||||
pub struct EnsembleEditor {
|
||||
backend: Rc<Backend>,
|
||||
id: String,
|
||||
widget: gtk::Stack,
|
||||
info_bar: gtk::InfoBar,
|
||||
name_entry: gtk::Entry,
|
||||
upload_switch: gtk::Switch,
|
||||
saved_cb: RefCell<Option<Box<dyn Fn(Ensemble) -> ()>>>,
|
||||
navigator: RefCell<Option<Rc<Navigator>>>,
|
||||
}
|
||||
|
||||
impl EnsembleEditor {
|
||||
/// Create a new ensemble editor and optionally initialize it.
|
||||
pub fn new(backend: Rc<Backend>, ensemble: Option<Ensemble>) -> Rc<Self> {
|
||||
// Create UI
|
||||
|
||||
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/ensemble_editor.ui");
|
||||
|
||||
get_widget!(builder, gtk::Stack, widget);
|
||||
get_widget!(builder, gtk::Button, back_button);
|
||||
get_widget!(builder, gtk::Button, save_button);
|
||||
get_widget!(builder, gtk::InfoBar, info_bar);
|
||||
get_widget!(builder, gtk::Entry, name_entry);
|
||||
get_widget!(builder, gtk::Switch, upload_switch);
|
||||
|
||||
let id = match ensemble {
|
||||
Some(ensemble) => {
|
||||
name_entry.set_text(&ensemble.name);
|
||||
|
||||
ensemble.id
|
||||
}
|
||||
None => generate_id(),
|
||||
};
|
||||
|
||||
let this = Rc::new(Self {
|
||||
backend,
|
||||
id,
|
||||
widget,
|
||||
info_bar,
|
||||
name_entry,
|
||||
upload_switch,
|
||||
saved_cb: RefCell::new(None),
|
||||
navigator: RefCell::new(None),
|
||||
});
|
||||
|
||||
// Connect signals and callbacks
|
||||
|
||||
back_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
save_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let context = glib::MainContext::default();
|
||||
let clone = this.clone();
|
||||
context.spawn_local(async move {
|
||||
clone.widget.set_visible_child_name("loading");
|
||||
match clone.clone().save().await {
|
||||
Ok(_) => {
|
||||
let navigator = clone.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
clone.info_bar.set_revealed(true);
|
||||
clone.widget.set_visible_child_name("content");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}));
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// Set the closure to be called if the ensemble was saved.
|
||||
pub fn set_saved_cb<F: Fn(Ensemble) -> () + 'static>(&self, cb: F) {
|
||||
self.saved_cb.replace(Some(Box::new(cb)));
|
||||
}
|
||||
|
||||
/// Save the ensemble and possibly upload it to the server.
|
||||
async fn save(self: Rc<Self>) -> Result<()> {
|
||||
let name = self.name_entry.get_text().to_string();
|
||||
|
||||
let ensemble = Ensemble {
|
||||
id: self.id.clone(),
|
||||
name,
|
||||
};
|
||||
|
||||
let upload = self.upload_switch.get_active();
|
||||
if upload {
|
||||
self.backend.post_ensemble(&ensemble).await?;
|
||||
}
|
||||
|
||||
self.backend
|
||||
.db()
|
||||
.update_ensemble(ensemble.clone())
|
||||
.await?;
|
||||
self.backend.library_changed();
|
||||
|
||||
if let Some(cb) = &*self.saved_cb.borrow() {
|
||||
cb(ensemble.clone());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatorScreen for EnsembleEditor {
|
||||
fn attach_navigator(&self, navigator: Rc<Navigator>) {
|
||||
self.navigator.replace(Some(navigator));
|
||||
}
|
||||
|
||||
fn get_widget(&self) -> gtk::Widget {
|
||||
self.widget.clone().upcast()
|
||||
}
|
||||
|
||||
fn detach_navigator(&self) {
|
||||
self.navigator.replace(None);
|
||||
}
|
||||
}
|
||||
135
src/editors/instrument.rs
Normal file
135
src/editors/instrument.rs
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use crate::widgets::{Navigator, NavigatorScreen};
|
||||
use anyhow::Result;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A dialog for creating or editing a instrument.
|
||||
pub struct InstrumentEditor {
|
||||
backend: Rc<Backend>,
|
||||
id: String,
|
||||
widget: gtk::Stack,
|
||||
info_bar: gtk::InfoBar,
|
||||
name_entry: gtk::Entry,
|
||||
upload_switch: gtk::Switch,
|
||||
saved_cb: RefCell<Option<Box<dyn Fn(Instrument) -> ()>>>,
|
||||
navigator: RefCell<Option<Rc<Navigator>>>,
|
||||
}
|
||||
|
||||
impl InstrumentEditor {
|
||||
/// Create a new instrument editor and optionally initialize it.
|
||||
pub fn new(backend: Rc<Backend>, instrument: Option<Instrument>) -> Rc<Self> {
|
||||
// Create UI
|
||||
|
||||
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/instrument_editor.ui");
|
||||
|
||||
get_widget!(builder, gtk::Stack, widget);
|
||||
get_widget!(builder, gtk::Button, back_button);
|
||||
get_widget!(builder, gtk::Button, save_button);
|
||||
get_widget!(builder, gtk::InfoBar, info_bar);
|
||||
get_widget!(builder, gtk::Entry, name_entry);
|
||||
get_widget!(builder, gtk::Switch, upload_switch);
|
||||
|
||||
let id = match instrument {
|
||||
Some(instrument) => {
|
||||
name_entry.set_text(&instrument.name);
|
||||
|
||||
instrument.id
|
||||
}
|
||||
None => generate_id(),
|
||||
};
|
||||
|
||||
let this = Rc::new(Self {
|
||||
backend,
|
||||
id,
|
||||
widget,
|
||||
info_bar,
|
||||
name_entry,
|
||||
upload_switch,
|
||||
saved_cb: RefCell::new(None),
|
||||
navigator: RefCell::new(None),
|
||||
});
|
||||
|
||||
// Connect signals and callbacks
|
||||
|
||||
back_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
save_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let context = glib::MainContext::default();
|
||||
let clone = this.clone();
|
||||
context.spawn_local(async move {
|
||||
clone.widget.set_visible_child_name("loading");
|
||||
match clone.clone().save().await {
|
||||
Ok(_) => {
|
||||
let navigator = clone.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
clone.info_bar.set_revealed(true);
|
||||
clone.widget.set_visible_child_name("content");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}));
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// Set the closure to be called if the instrument was saved.
|
||||
pub fn set_saved_cb<F: Fn(Instrument) -> () + 'static>(&self, cb: F) {
|
||||
self.saved_cb.replace(Some(Box::new(cb)));
|
||||
}
|
||||
|
||||
/// Save the instrument and possibly upload it to the server.
|
||||
async fn save(self: Rc<Self>) -> Result<()> {
|
||||
let name = self.name_entry.get_text().to_string();
|
||||
|
||||
let instrument = Instrument {
|
||||
id: self.id.clone(),
|
||||
name,
|
||||
};
|
||||
|
||||
let upload = self.upload_switch.get_active();
|
||||
if upload {
|
||||
self.backend.post_instrument(&instrument).await?;
|
||||
}
|
||||
|
||||
self.backend
|
||||
.db()
|
||||
.update_instrument(instrument.clone())
|
||||
.await?;
|
||||
self.backend.library_changed();
|
||||
|
||||
if let Some(cb) = &*self.saved_cb.borrow() {
|
||||
cb(instrument.clone());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatorScreen for InstrumentEditor {
|
||||
fn attach_navigator(&self, navigator: Rc<Navigator>) {
|
||||
self.navigator.replace(Some(navigator));
|
||||
}
|
||||
|
||||
fn get_widget(&self) -> gtk::Widget {
|
||||
self.widget.clone().upcast()
|
||||
}
|
||||
|
||||
fn detach_navigator(&self) {
|
||||
self.navigator.replace(None);
|
||||
}
|
||||
}
|
||||
18
src/editors/mod.rs
Normal file
18
src/editors/mod.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
pub mod ensemble;
|
||||
pub use ensemble::*;
|
||||
|
||||
pub mod instrument;
|
||||
pub use instrument::*;
|
||||
|
||||
pub mod person;
|
||||
pub use person::*;
|
||||
|
||||
pub mod recording;
|
||||
pub use recording::*;
|
||||
|
||||
pub mod work;
|
||||
pub use work::*;
|
||||
|
||||
mod performance;
|
||||
mod work_part;
|
||||
mod work_section;
|
||||
210
src/editors/performance.rs
Normal file
210
src/editors/performance.rs
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use crate::selectors::{EnsembleSelector, InstrumentSelector, PersonSelector};
|
||||
use crate::widgets::{Navigator, NavigatorScreen};
|
||||
use gettextrs::gettext;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A dialog for editing a performance within a recording.
|
||||
pub struct PerformanceEditor {
|
||||
backend: Rc<Backend>,
|
||||
widget: gtk::Box,
|
||||
save_button: gtk::Button,
|
||||
person_label: gtk::Label,
|
||||
ensemble_label: gtk::Label,
|
||||
role_label: gtk::Label,
|
||||
reset_role_button: gtk::Button,
|
||||
person: RefCell<Option<Person>>,
|
||||
ensemble: RefCell<Option<Ensemble>>,
|
||||
role: RefCell<Option<Instrument>>,
|
||||
selected_cb: RefCell<Option<Box<dyn Fn(Performance) -> ()>>>,
|
||||
navigator: RefCell<Option<Rc<Navigator>>>,
|
||||
}
|
||||
|
||||
impl PerformanceEditor {
|
||||
/// Create a new performance editor.
|
||||
pub fn new(backend: Rc<Backend>, performance: Option<Performance>) -> Rc<Self> {
|
||||
// Create UI
|
||||
|
||||
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/performance_editor.ui");
|
||||
|
||||
get_widget!(builder, gtk::Box, widget);
|
||||
get_widget!(builder, gtk::Button, back_button);
|
||||
get_widget!(builder, gtk::Button, save_button);
|
||||
get_widget!(builder, gtk::Button, person_button);
|
||||
get_widget!(builder, gtk::Button, ensemble_button);
|
||||
get_widget!(builder, gtk::Button, role_button);
|
||||
get_widget!(builder, gtk::Button, reset_role_button);
|
||||
get_widget!(builder, gtk::Label, person_label);
|
||||
get_widget!(builder, gtk::Label, ensemble_label);
|
||||
get_widget!(builder, gtk::Label, role_label);
|
||||
|
||||
let this = Rc::new(PerformanceEditor {
|
||||
backend,
|
||||
widget,
|
||||
save_button,
|
||||
person_label,
|
||||
ensemble_label,
|
||||
role_label,
|
||||
reset_role_button,
|
||||
person: RefCell::new(None),
|
||||
ensemble: RefCell::new(None),
|
||||
role: RefCell::new(None),
|
||||
selected_cb: RefCell::new(None),
|
||||
navigator: RefCell::new(None),
|
||||
});
|
||||
|
||||
// Connect signals and callbacks
|
||||
|
||||
back_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
this.save_button
|
||||
.connect_clicked(clone!(@strong this => move |_| {
|
||||
if let Some(cb) = &*this.selected_cb.borrow() {
|
||||
cb(Performance {
|
||||
person: this.person.borrow().clone(),
|
||||
ensemble: this.ensemble.borrow().clone(),
|
||||
role: this.role.borrow().clone(),
|
||||
});
|
||||
}
|
||||
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
person_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let selector = PersonSelector::new(this.backend.clone());
|
||||
|
||||
selector.set_selected_cb(clone!(@strong this, @strong navigator => move |person| {
|
||||
this.show_person(Some(&person));
|
||||
this.person.replace(Some(person.clone()));
|
||||
this.show_ensemble(None);
|
||||
this.ensemble.replace(None);
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(selector);
|
||||
}
|
||||
}));
|
||||
|
||||
ensemble_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let selector = EnsembleSelector::new(this.backend.clone());
|
||||
|
||||
selector.set_selected_cb(clone!(@strong this, @strong navigator => move |ensemble| {
|
||||
this.show_person(None);
|
||||
this.person.replace(None);
|
||||
this.show_ensemble(Some(&ensemble));
|
||||
this.ensemble.replace(Some(ensemble.clone()));
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(selector);
|
||||
}
|
||||
}));
|
||||
|
||||
role_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let selector = InstrumentSelector::new(this.backend.clone());
|
||||
|
||||
selector.set_selected_cb(clone!(@strong this, @strong navigator => move |role| {
|
||||
this.show_role(Some(&role));
|
||||
this.role.replace(Some(role.clone()));
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(selector);
|
||||
}
|
||||
}));
|
||||
|
||||
this.reset_role_button
|
||||
.connect_clicked(clone!(@strong this => move |_| {
|
||||
this.show_role(None);
|
||||
this.role.replace(None);
|
||||
}));
|
||||
|
||||
// Initialize
|
||||
|
||||
if let Some(performance) = performance {
|
||||
if let Some(person) = performance.person {
|
||||
this.show_person(Some(&person));
|
||||
this.person.replace(Some(person));
|
||||
} else if let Some(ensemble) = performance.ensemble {
|
||||
this.show_ensemble(Some(&ensemble));
|
||||
this.ensemble.replace(Some(ensemble));
|
||||
}
|
||||
|
||||
if let Some(role) = performance.role {
|
||||
this.show_role(Some(&role));
|
||||
this.role.replace(Some(role));
|
||||
}
|
||||
}
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// Set a closure to be called when the user has chosen to save the performance.
|
||||
pub fn set_selected_cb<F: Fn(Performance) -> () + 'static>(&self, cb: F) {
|
||||
self.selected_cb.replace(Some(Box::new(cb)));
|
||||
}
|
||||
|
||||
/// Update the UI according to person.
|
||||
fn show_person(&self, person: Option<&Person>) {
|
||||
if let Some(person) = person {
|
||||
self.person_label.set_text(&person.name_fl());
|
||||
self.save_button.set_sensitive(true);
|
||||
} else {
|
||||
self.person_label.set_text(&gettext("Select …"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the UI according to ensemble.
|
||||
fn show_ensemble(&self, ensemble: Option<&Ensemble>) {
|
||||
if let Some(ensemble) = ensemble {
|
||||
self.ensemble_label.set_text(&ensemble.name);
|
||||
self.save_button.set_sensitive(true);
|
||||
} else {
|
||||
self.ensemble_label.set_text(&gettext("Select …"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the UI according to role.
|
||||
fn show_role(&self, role: Option<&Instrument>) {
|
||||
if let Some(role) = role {
|
||||
self.role_label.set_text(&role.name);
|
||||
self.reset_role_button.show();
|
||||
} else {
|
||||
self.role_label.set_text(&gettext("Select …"));
|
||||
self.reset_role_button.hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatorScreen for PerformanceEditor {
|
||||
fn attach_navigator(&self, navigator: Rc<Navigator>) {
|
||||
self.navigator.replace(Some(navigator));
|
||||
}
|
||||
|
||||
fn get_widget(&self) -> gtk::Widget {
|
||||
self.widget.clone().upcast()
|
||||
}
|
||||
|
||||
fn detach_navigator(&self) {
|
||||
self.navigator.replace(None);
|
||||
}
|
||||
}
|
||||
138
src/editors/person.rs
Normal file
138
src/editors/person.rs
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use crate::widgets::{Navigator, NavigatorScreen};
|
||||
use anyhow::Result;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A dialog for creating or editing a person.
|
||||
pub struct PersonEditor {
|
||||
backend: Rc<Backend>,
|
||||
id: String,
|
||||
widget: gtk::Stack,
|
||||
info_bar: gtk::InfoBar,
|
||||
first_name_entry: gtk::Entry,
|
||||
last_name_entry: gtk::Entry,
|
||||
upload_switch: gtk::Switch,
|
||||
saved_cb: RefCell<Option<Box<dyn Fn(Person) -> ()>>>,
|
||||
navigator: RefCell<Option<Rc<Navigator>>>,
|
||||
}
|
||||
|
||||
impl PersonEditor {
|
||||
/// Create a new person editor and optionally initialize it.
|
||||
pub fn new(backend: Rc<Backend>, person: Option<Person>) -> Rc<Self> {
|
||||
// Create UI
|
||||
|
||||
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/person_editor.ui");
|
||||
|
||||
get_widget!(builder, gtk::Stack, widget);
|
||||
get_widget!(builder, gtk::Button, back_button);
|
||||
get_widget!(builder, gtk::Button, save_button);
|
||||
get_widget!(builder, gtk::InfoBar, info_bar);
|
||||
get_widget!(builder, gtk::Entry, first_name_entry);
|
||||
get_widget!(builder, gtk::Entry, last_name_entry);
|
||||
get_widget!(builder, gtk::Switch, upload_switch);
|
||||
|
||||
let id = match person {
|
||||
Some(person) => {
|
||||
first_name_entry.set_text(&person.first_name);
|
||||
last_name_entry.set_text(&person.last_name);
|
||||
|
||||
person.id
|
||||
}
|
||||
None => generate_id(),
|
||||
};
|
||||
|
||||
let this = Rc::new(Self {
|
||||
backend,
|
||||
id,
|
||||
widget,
|
||||
info_bar,
|
||||
first_name_entry,
|
||||
last_name_entry,
|
||||
upload_switch,
|
||||
saved_cb: RefCell::new(None),
|
||||
navigator: RefCell::new(None),
|
||||
});
|
||||
|
||||
// Connect signals and callbacks
|
||||
|
||||
back_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
save_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let context = glib::MainContext::default();
|
||||
let clone = this.clone();
|
||||
context.spawn_local(async move {
|
||||
clone.widget.set_visible_child_name("loading");
|
||||
match clone.clone().save().await {
|
||||
Ok(_) => {
|
||||
let navigator = clone.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
clone.info_bar.set_revealed(true);
|
||||
clone.widget.set_visible_child_name("content");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}));
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// Set the closure to be called if the person was saved.
|
||||
pub fn set_saved_cb<F: Fn(Person) -> () + 'static>(&self, cb: F) {
|
||||
self.saved_cb.replace(Some(Box::new(cb)));
|
||||
}
|
||||
|
||||
/// Save the person and possibly upload it to the server.
|
||||
async fn save(self: Rc<Self>) -> Result<()> {
|
||||
let first_name = self.first_name_entry.get_text().to_string();
|
||||
let last_name = self.last_name_entry.get_text().to_string();
|
||||
|
||||
let person = Person {
|
||||
id: self.id.clone(),
|
||||
first_name,
|
||||
last_name,
|
||||
};
|
||||
|
||||
let upload = self.upload_switch.get_active();
|
||||
if upload {
|
||||
self.backend.post_person(&person).await?;
|
||||
}
|
||||
|
||||
self.backend.db().update_person(person.clone()).await?;
|
||||
self.backend.library_changed();
|
||||
|
||||
if let Some(cb) = &*self.saved_cb.borrow() {
|
||||
cb(person.clone());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatorScreen for PersonEditor {
|
||||
fn attach_navigator(&self, navigator: Rc<Navigator>) {
|
||||
self.navigator.replace(Some(navigator));
|
||||
}
|
||||
|
||||
fn get_widget(&self) -> gtk::Widget {
|
||||
self.widget.clone().upcast()
|
||||
}
|
||||
|
||||
fn detach_navigator(&self) {
|
||||
self.navigator.replace(None);
|
||||
}
|
||||
}
|
||||
276
src/editors/recording.rs
Normal file
276
src/editors/recording.rs
Normal file
|
|
@ -0,0 +1,276 @@
|
|||
use super::performance::PerformanceEditor;
|
||||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use crate::selectors::{PersonSelector, WorkSelector};
|
||||
use crate::widgets::{List, Navigator, NavigatorScreen};
|
||||
use anyhow::Result;
|
||||
use gettextrs::gettext;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A widget for creating or editing a recording.
|
||||
// TODO: Disable buttons if no performance is selected.
|
||||
pub struct RecordingEditor {
|
||||
pub widget: gtk::Stack,
|
||||
backend: Rc<Backend>,
|
||||
save_button: gtk::Button,
|
||||
info_bar: gtk::InfoBar,
|
||||
work_label: gtk::Label,
|
||||
comment_entry: gtk::Entry,
|
||||
upload_switch: gtk::Switch,
|
||||
performance_list: Rc<List<Performance>>,
|
||||
id: String,
|
||||
work: RefCell<Option<Work>>,
|
||||
performances: RefCell<Vec<Performance>>,
|
||||
selected_cb: RefCell<Option<Box<dyn Fn(Recording) -> ()>>>,
|
||||
navigator: RefCell<Option<Rc<Navigator>>>,
|
||||
}
|
||||
|
||||
impl RecordingEditor {
|
||||
/// Create a new recording editor widget and optionally initialize it.
|
||||
pub fn new(backend: Rc<Backend>, recording: Option<Recording>) -> Rc<Self> {
|
||||
// Create UI
|
||||
|
||||
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/recording_editor.ui");
|
||||
|
||||
get_widget!(builder, gtk::Stack, widget);
|
||||
get_widget!(builder, gtk::Button, back_button);
|
||||
get_widget!(builder, gtk::Button, save_button);
|
||||
get_widget!(builder, gtk::InfoBar, info_bar);
|
||||
get_widget!(builder, gtk::Button, work_button);
|
||||
get_widget!(builder, gtk::Label, work_label);
|
||||
get_widget!(builder, gtk::Entry, comment_entry);
|
||||
get_widget!(builder, gtk::Switch, upload_switch);
|
||||
get_widget!(builder, gtk::ScrolledWindow, scroll);
|
||||
get_widget!(builder, gtk::Button, add_performer_button);
|
||||
get_widget!(builder, gtk::Button, edit_performer_button);
|
||||
get_widget!(builder, gtk::Button, remove_performer_button);
|
||||
|
||||
let performance_list = List::new(&gettext("No performers added."));
|
||||
scroll.add(&performance_list.widget);
|
||||
|
||||
let (id, work, performances) = match recording {
|
||||
Some(recording) => {
|
||||
comment_entry.set_text(&recording.comment);
|
||||
(recording.id, Some(recording.work), recording.performances)
|
||||
}
|
||||
None => (generate_id(), None, Vec::new()),
|
||||
};
|
||||
|
||||
let this = Rc::new(RecordingEditor {
|
||||
widget,
|
||||
backend,
|
||||
save_button,
|
||||
info_bar,
|
||||
work_label,
|
||||
comment_entry,
|
||||
upload_switch,
|
||||
performance_list,
|
||||
id,
|
||||
work: RefCell::new(work),
|
||||
performances: RefCell::new(performances),
|
||||
selected_cb: RefCell::new(None),
|
||||
navigator: RefCell::new(None),
|
||||
});
|
||||
|
||||
// Connect signals and callbacks
|
||||
|
||||
back_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.clone().pop();
|
||||
}
|
||||
}));
|
||||
|
||||
this.save_button
|
||||
.connect_clicked(clone!(@strong this => move |_| {
|
||||
let context = glib::MainContext::default();
|
||||
let clone = this.clone();
|
||||
context.spawn_local(async move {
|
||||
clone.widget.set_visible_child_name("loading");
|
||||
match clone.clone().save().await {
|
||||
Ok(_) => {
|
||||
let navigator = clone.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.clone().pop();
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
clone.info_bar.set_revealed(true);
|
||||
clone.widget.set_visible_child_name("content");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}));
|
||||
|
||||
work_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let person_selector = PersonSelector::new(this.backend.clone());
|
||||
|
||||
person_selector.set_selected_cb(clone!(@strong this, @strong navigator => move |person| {
|
||||
let work_selector = WorkSelector::new(this.backend.clone(), person.clone());
|
||||
|
||||
work_selector.set_selected_cb(clone!(@strong this, @strong navigator => move |work| {
|
||||
this.work_selected(&work);
|
||||
this.work.replace(Some(work.clone()));
|
||||
|
||||
navigator.clone().pop();
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.clone().push(work_selector);
|
||||
}));
|
||||
|
||||
navigator.push(person_selector);
|
||||
}
|
||||
}));
|
||||
|
||||
this.performance_list.set_make_widget(|performance| {
|
||||
let label = gtk::Label::new(Some(&performance.get_title()));
|
||||
label.set_ellipsize(pango::EllipsizeMode::End);
|
||||
label.set_halign(gtk::Align::Start);
|
||||
label.set_margin_start(6);
|
||||
label.set_margin_end(6);
|
||||
label.set_margin_top(6);
|
||||
label.set_margin_bottom(6);
|
||||
label.upcast()
|
||||
});
|
||||
|
||||
add_performer_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let editor = PerformanceEditor::new(this.backend.clone(), None);
|
||||
|
||||
editor.set_selected_cb(clone!(@strong this, @strong navigator => move |performance| {
|
||||
let mut performances = this.performances.borrow_mut();
|
||||
|
||||
let index = match this.performance_list.get_selected_index() {
|
||||
Some(index) => index + 1,
|
||||
None => performances.len(),
|
||||
};
|
||||
|
||||
performances.insert(index, performance);
|
||||
this.performance_list.show_items(performances.clone());
|
||||
this.performance_list.select_index(index);
|
||||
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(editor);
|
||||
}
|
||||
}));
|
||||
|
||||
edit_performer_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
if let Some(index) = this.performance_list.get_selected_index() {
|
||||
let performance = &this.performances.borrow()[index];
|
||||
|
||||
let editor = PerformanceEditor::new(
|
||||
this.backend.clone(),
|
||||
Some(performance.clone()),
|
||||
);
|
||||
|
||||
editor.set_selected_cb(clone!(@strong this, @strong navigator => move |performance| {
|
||||
let mut performances = this.performances.borrow_mut();
|
||||
performances[index] = performance;
|
||||
this.performance_list.show_items(performances.clone());
|
||||
this.performance_list.select_index(index);
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(editor);
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
remove_performer_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
if let Some(index) = this.performance_list.get_selected_index() {
|
||||
let mut performances = this.performances.borrow_mut();
|
||||
performances.remove(index);
|
||||
this.performance_list.show_items(performances.clone());
|
||||
this.performance_list.select_index(index);
|
||||
}
|
||||
}));
|
||||
|
||||
// Initialize
|
||||
|
||||
if let Some(work) = &*this.work.borrow() {
|
||||
this.work_selected(work);
|
||||
}
|
||||
|
||||
this.performance_list
|
||||
.show_items(this.performances.borrow().clone());
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// Set the closure to be called if the recording was created.
|
||||
pub fn set_selected_cb<F: Fn(Recording) -> () + 'static>(&self, cb: F) {
|
||||
self.selected_cb.replace(Some(Box::new(cb)));
|
||||
}
|
||||
|
||||
/// Update the UI according to work.
|
||||
fn work_selected(&self, work: &Work) {
|
||||
self.work_label
|
||||
.set_text(&format!("{}: {}", work.composer.name_fl(), work.title));
|
||||
self.save_button.set_sensitive(true);
|
||||
}
|
||||
|
||||
/// Save the recording and possibly upload it to the server.
|
||||
async fn save(self: Rc<Self>) -> Result<()> {
|
||||
let recording = Recording {
|
||||
id: self.id.clone(),
|
||||
work: self
|
||||
.work
|
||||
.borrow()
|
||||
.clone()
|
||||
.expect("Tried to create recording without work!"),
|
||||
comment: self.comment_entry.get_text().to_string(),
|
||||
performances: self.performances.borrow().clone(),
|
||||
};
|
||||
|
||||
let upload = self.upload_switch.get_active();
|
||||
if upload {
|
||||
self.backend.post_recording(&recording).await?;
|
||||
}
|
||||
|
||||
self.backend
|
||||
.db()
|
||||
.update_recording(recording.clone().into())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
self.backend.library_changed();
|
||||
|
||||
if let Some(cb) = &*self.selected_cb.borrow() {
|
||||
cb(recording.clone());
|
||||
}
|
||||
|
||||
let navigator = self.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.clone().pop();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatorScreen for RecordingEditor {
|
||||
fn attach_navigator(&self, navigator: Rc<Navigator>) {
|
||||
self.navigator.replace(Some(navigator));
|
||||
}
|
||||
|
||||
fn get_widget(&self) -> gtk::Widget {
|
||||
self.widget.clone().upcast()
|
||||
}
|
||||
|
||||
fn detach_navigator(&self) {
|
||||
self.navigator.replace(None);
|
||||
}
|
||||
}
|
||||
432
src/editors/work.rs
Normal file
432
src/editors/work.rs
Normal file
|
|
@ -0,0 +1,432 @@
|
|||
use super::work_part::WorkPartEditor;
|
||||
use super::work_section::WorkSectionEditor;
|
||||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use crate::selectors::{InstrumentSelector, PersonSelector};
|
||||
use crate::widgets::{List, Navigator, NavigatorScreen};
|
||||
use anyhow::Result;
|
||||
use gettextrs::gettext;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::convert::TryInto;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Either a work part or a work section.
|
||||
#[derive(Clone)]
|
||||
enum PartOrSection {
|
||||
Part(WorkPart),
|
||||
Section(WorkSection),
|
||||
}
|
||||
|
||||
/// A widget for editing and creating works.
|
||||
pub struct WorkEditor {
|
||||
widget: gtk::Stack,
|
||||
backend: Rc<Backend>,
|
||||
save_button: gtk::Button,
|
||||
title_entry: gtk::Entry,
|
||||
info_bar: gtk::InfoBar,
|
||||
composer_label: gtk::Label,
|
||||
upload_switch: gtk::Switch,
|
||||
instrument_list: Rc<List<Instrument>>,
|
||||
part_list: Rc<List<PartOrSection>>,
|
||||
id: String,
|
||||
composer: RefCell<Option<Person>>,
|
||||
instruments: RefCell<Vec<Instrument>>,
|
||||
structure: RefCell<Vec<PartOrSection>>,
|
||||
saved_cb: RefCell<Option<Box<dyn Fn(Work) -> ()>>>,
|
||||
navigator: RefCell<Option<Rc<Navigator>>>,
|
||||
}
|
||||
|
||||
impl WorkEditor {
|
||||
/// Create a new work editor widget and optionally initialize it.
|
||||
pub fn new(backend: Rc<Backend>, work: Option<Work>) -> Rc<Self> {
|
||||
// Create UI
|
||||
|
||||
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/work_editor.ui");
|
||||
|
||||
get_widget!(builder, gtk::Stack, widget);
|
||||
get_widget!(builder, gtk::Button, back_button);
|
||||
get_widget!(builder, gtk::Button, save_button);
|
||||
get_widget!(builder, gtk::InfoBar, info_bar);
|
||||
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::Switch, upload_switch);
|
||||
get_widget!(builder, gtk::ScrolledWindow, instruments_scroll);
|
||||
get_widget!(builder, gtk::Button, add_instrument_button);
|
||||
get_widget!(builder, gtk::Button, remove_instrument_button);
|
||||
get_widget!(builder, gtk::ScrolledWindow, structure_scroll);
|
||||
get_widget!(builder, gtk::Button, add_part_button);
|
||||
get_widget!(builder, gtk::Button, remove_part_button);
|
||||
get_widget!(builder, gtk::Button, add_section_button);
|
||||
get_widget!(builder, gtk::Button, edit_part_button);
|
||||
get_widget!(builder, gtk::Button, move_part_up_button);
|
||||
get_widget!(builder, gtk::Button, move_part_down_button);
|
||||
|
||||
let instrument_list = List::new(&gettext("No instruments added."));
|
||||
instruments_scroll.add(&instrument_list.widget);
|
||||
|
||||
let part_list = List::new(&gettext("No work parts added."));
|
||||
structure_scroll.add(&part_list.widget);
|
||||
|
||||
let (id, composer, instruments, structure) = match work {
|
||||
Some(work) => {
|
||||
title_entry.set_text(&work.title);
|
||||
|
||||
let mut structure = Vec::new();
|
||||
|
||||
for part in work.parts {
|
||||
structure.push(PartOrSection::Part(part));
|
||||
}
|
||||
|
||||
for section in work.sections {
|
||||
structure.insert(
|
||||
section.before_index.try_into().unwrap(),
|
||||
PartOrSection::Section(section),
|
||||
);
|
||||
}
|
||||
|
||||
(work.id, Some(work.composer), work.instruments, structure)
|
||||
}
|
||||
None => (generate_id(), None, Vec::new(), Vec::new()),
|
||||
};
|
||||
|
||||
let this = Rc::new(Self {
|
||||
widget,
|
||||
backend,
|
||||
save_button,
|
||||
id,
|
||||
info_bar,
|
||||
title_entry,
|
||||
composer_label,
|
||||
upload_switch,
|
||||
instrument_list,
|
||||
part_list,
|
||||
composer: RefCell::new(composer),
|
||||
instruments: RefCell::new(instruments),
|
||||
structure: RefCell::new(structure),
|
||||
saved_cb: RefCell::new(None),
|
||||
navigator: RefCell::new(None),
|
||||
});
|
||||
|
||||
// Connect signals and callbacks
|
||||
|
||||
back_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
this.save_button
|
||||
.connect_clicked(clone!(@strong this => move |_| {
|
||||
let context = glib::MainContext::default();
|
||||
let clone = this.clone();
|
||||
context.spawn_local(async move {
|
||||
clone.widget.set_visible_child_name("loading");
|
||||
match clone.clone().save().await {
|
||||
Ok(_) => {
|
||||
let navigator = clone.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
clone.info_bar.set_revealed(true);
|
||||
clone.widget.set_visible_child_name("content");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}));
|
||||
|
||||
composer_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let selector = PersonSelector::new(this.backend.clone());
|
||||
|
||||
selector.set_selected_cb(clone!(@strong this, @strong navigator => move |person| {
|
||||
this.show_composer(person);
|
||||
this.composer.replace(Some(person.clone()));
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(selector);
|
||||
}
|
||||
}));
|
||||
|
||||
this.instrument_list.set_make_widget(|instrument| {
|
||||
let label = gtk::Label::new(Some(&instrument.name));
|
||||
label.set_ellipsize(pango::EllipsizeMode::End);
|
||||
label.set_halign(gtk::Align::Start);
|
||||
label.set_margin_start(6);
|
||||
label.set_margin_end(6);
|
||||
label.set_margin_top(6);
|
||||
label.set_margin_bottom(6);
|
||||
label.upcast()
|
||||
});
|
||||
|
||||
add_instrument_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let selector = InstrumentSelector::new(this.backend.clone());
|
||||
|
||||
selector.set_selected_cb(clone!(@strong this, @strong navigator => move |instrument| {
|
||||
let mut instruments = this.instruments.borrow_mut();
|
||||
|
||||
let index = match this.instrument_list.get_selected_index() {
|
||||
Some(index) => index + 1,
|
||||
None => instruments.len(),
|
||||
};
|
||||
|
||||
instruments.insert(index, instrument.clone());
|
||||
this.instrument_list.show_items(instruments.clone());
|
||||
this.instrument_list.select_index(index);
|
||||
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(selector);
|
||||
}
|
||||
}));
|
||||
|
||||
remove_instrument_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
if let Some(index) = this.instrument_list.get_selected_index() {
|
||||
let mut instruments = this.instruments.borrow_mut();
|
||||
instruments.remove(index);
|
||||
this.instrument_list.show_items(instruments.clone());
|
||||
this.instrument_list.select_index(index);
|
||||
}
|
||||
}));
|
||||
|
||||
this.part_list.set_make_widget(|pos| {
|
||||
let label = gtk::Label::new(None);
|
||||
label.set_ellipsize(pango::EllipsizeMode::End);
|
||||
label.set_halign(gtk::Align::Start);
|
||||
label.set_margin_end(6);
|
||||
label.set_margin_top(6);
|
||||
label.set_margin_bottom(6);
|
||||
|
||||
match pos {
|
||||
PartOrSection::Part(part) => {
|
||||
label.set_text(&part.title);
|
||||
label.set_margin_start(12);
|
||||
}
|
||||
PartOrSection::Section(section) => {
|
||||
let attrs = pango::AttrList::new();
|
||||
attrs.insert(pango::Attribute::new_weight(pango::Weight::Bold).unwrap());
|
||||
label.set_attributes(Some(&attrs));
|
||||
label.set_text(§ion.title);
|
||||
label.set_margin_start(6);
|
||||
}
|
||||
}
|
||||
|
||||
label.upcast()
|
||||
});
|
||||
|
||||
add_part_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let editor = WorkPartEditor::new(this.backend.clone(), None);
|
||||
|
||||
editor.set_ready_cb(clone!(@strong this, @strong navigator => move |part| {
|
||||
let mut structure = this.structure.borrow_mut();
|
||||
|
||||
let index = match this.part_list.get_selected_index() {
|
||||
Some(index) => index + 1,
|
||||
None => structure.len(),
|
||||
};
|
||||
|
||||
structure.insert(index, PartOrSection::Part(part));
|
||||
this.part_list.show_items(structure.clone());
|
||||
this.part_list.select_index(index);
|
||||
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(editor);
|
||||
}
|
||||
}));
|
||||
|
||||
add_section_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let editor = WorkSectionEditor::new(None);
|
||||
|
||||
editor.set_ready_cb(clone!(@strong this, @strong navigator => move |section| {
|
||||
let mut structure = this.structure.borrow_mut();
|
||||
|
||||
let index = match this.part_list.get_selected_index() {
|
||||
Some(index) => index + 1,
|
||||
None => structure.len(),
|
||||
};
|
||||
|
||||
structure.insert(index, PartOrSection::Section(section));
|
||||
this.part_list.show_items(structure.clone());
|
||||
this.part_list.select_index(index);
|
||||
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(editor);
|
||||
}
|
||||
}));
|
||||
|
||||
edit_part_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
if let Some(index) = this.part_list.get_selected_index() {
|
||||
match this.structure.borrow()[index].clone() {
|
||||
PartOrSection::Part(part) => {
|
||||
let editor = WorkPartEditor::new(this.backend.clone(), Some(part));
|
||||
|
||||
editor.set_ready_cb(clone!(@strong this, @strong navigator => move |part| {
|
||||
let mut structure = this.structure.borrow_mut();
|
||||
structure[index] = PartOrSection::Part(part);
|
||||
this.part_list.show_items(structure.clone());
|
||||
this.part_list.select_index(index);
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(editor);
|
||||
}
|
||||
PartOrSection::Section(section) => {
|
||||
let editor = WorkSectionEditor::new(Some(section));
|
||||
|
||||
editor.set_ready_cb(clone!(@strong this, @strong navigator => move |section| {
|
||||
let mut structure = this.structure.borrow_mut();
|
||||
structure[index] = PartOrSection::Section(section);
|
||||
this.part_list.show_items(structure.clone());
|
||||
this.part_list.select_index(index);
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(editor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
remove_part_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
if let Some(index) = this.part_list.get_selected_index() {
|
||||
let mut structure = this.structure.borrow_mut();
|
||||
structure.remove(index);
|
||||
this.part_list.show_items(structure.clone());
|
||||
this.part_list.select_index(index);
|
||||
}
|
||||
}));
|
||||
|
||||
move_part_up_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
if let Some(index) = this.part_list.get_selected_index() {
|
||||
if index > 0 {
|
||||
let mut structure = this.structure.borrow_mut();
|
||||
structure.swap(index - 1, index);
|
||||
this.part_list.show_items(structure.clone());
|
||||
this.part_list.select_index(index - 1);
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
move_part_down_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
if let Some(index) = this.part_list.get_selected_index() {
|
||||
let mut structure = this.structure.borrow_mut();
|
||||
if index < structure.len() - 1 {
|
||||
structure.swap(index, index + 1);
|
||||
this.part_list.show_items(structure.clone());
|
||||
this.part_list.select_index(index + 1);
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// Initialization
|
||||
|
||||
if let Some(composer) = &*this.composer.borrow() {
|
||||
this.show_composer(composer);
|
||||
}
|
||||
|
||||
this.instrument_list
|
||||
.show_items(this.instruments.borrow().clone());
|
||||
this.part_list.show_items(this.structure.borrow().clone());
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// The closure to call when a work was created.
|
||||
pub fn set_saved_cb<F: Fn(Work) -> () + 'static>(&self, cb: F) {
|
||||
self.saved_cb.replace(Some(Box::new(cb)));
|
||||
}
|
||||
|
||||
/// Update the UI according to person.
|
||||
fn show_composer(&self, person: &Person) {
|
||||
self.composer_label.set_text(&person.name_fl());
|
||||
self.save_button.set_sensitive(true);
|
||||
}
|
||||
|
||||
/// Save the work and possibly upload it to the server.
|
||||
async fn save(self: Rc<Self>) -> Result<()> {
|
||||
let mut section_count: usize = 0;
|
||||
let mut parts = Vec::new();
|
||||
let mut sections = Vec::new();
|
||||
|
||||
for (index, pos) in self.structure.borrow().iter().enumerate() {
|
||||
match pos {
|
||||
PartOrSection::Part(part) => parts.push(part.clone()),
|
||||
PartOrSection::Section(section) => {
|
||||
let mut section = section.clone();
|
||||
section.before_index = index - section_count;
|
||||
sections.push(section);
|
||||
section_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let work = Work {
|
||||
id: self.id.clone(),
|
||||
title: self.title_entry.get_text().to_string(),
|
||||
composer: self
|
||||
.composer
|
||||
.borrow()
|
||||
.clone()
|
||||
.expect("Tried to create work without composer!"),
|
||||
instruments: self.instruments.borrow().clone(),
|
||||
parts: parts,
|
||||
sections: sections,
|
||||
};
|
||||
|
||||
let upload = self.upload_switch.get_active();
|
||||
if upload {
|
||||
self.backend.post_work(&work).await?;
|
||||
}
|
||||
|
||||
self.backend
|
||||
.db()
|
||||
.update_work(work.clone().into())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
self.backend.library_changed();
|
||||
|
||||
if let Some(cb) = &*self.saved_cb.borrow() {
|
||||
cb(work.clone());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatorScreen for WorkEditor {
|
||||
fn attach_navigator(&self, navigator: Rc<Navigator>) {
|
||||
self.navigator.replace(Some(navigator));
|
||||
}
|
||||
|
||||
fn get_widget(&self) -> gtk::Widget {
|
||||
self.widget.clone().upcast()
|
||||
}
|
||||
|
||||
fn detach_navigator(&self) {
|
||||
self.navigator.replace(None);
|
||||
}
|
||||
}
|
||||
141
src/editors/work_part.rs
Normal file
141
src/editors/work_part.rs
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use crate::selectors::PersonSelector;
|
||||
use crate::widgets::{Navigator, NavigatorScreen};
|
||||
use gettextrs::gettext;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A dialog for creating or editing a work part.
|
||||
pub struct WorkPartEditor {
|
||||
backend: Rc<Backend>,
|
||||
widget: gtk::Box,
|
||||
title_entry: gtk::Entry,
|
||||
composer_label: gtk::Label,
|
||||
reset_composer_button: gtk::Button,
|
||||
composer: RefCell<Option<Person>>,
|
||||
ready_cb: RefCell<Option<Box<dyn Fn(WorkPart) -> ()>>>,
|
||||
navigator: RefCell<Option<Rc<Navigator>>>,
|
||||
}
|
||||
|
||||
impl WorkPartEditor {
|
||||
/// Create a new part editor and optionally initialize it.
|
||||
pub fn new(backend: Rc<Backend>, part: Option<WorkPart>) -> Rc<Self> {
|
||||
// Create UI
|
||||
|
||||
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/work_part_editor.ui");
|
||||
|
||||
get_widget!(builder, gtk::Box, widget);
|
||||
get_widget!(builder, gtk::Button, back_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);
|
||||
|
||||
let composer = match part {
|
||||
Some(part) => {
|
||||
title_entry.set_text(&part.title);
|
||||
part.composer
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
let this = Rc::new(Self {
|
||||
backend,
|
||||
widget,
|
||||
title_entry,
|
||||
composer_label,
|
||||
reset_composer_button,
|
||||
composer: RefCell::new(composer),
|
||||
ready_cb: RefCell::new(None),
|
||||
navigator: RefCell::new(None),
|
||||
});
|
||||
|
||||
// Connect signals and callbacks
|
||||
|
||||
back_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
save_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
if let Some(cb) = &*this.ready_cb.borrow() {
|
||||
cb(WorkPart {
|
||||
title: this.title_entry.get_text().to_string(),
|
||||
composer: this.composer.borrow().clone(),
|
||||
});
|
||||
}
|
||||
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
composer_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
let selector = PersonSelector::new(this.backend.clone());
|
||||
|
||||
selector.set_selected_cb(clone!(@strong this, @strong navigator => move |person| {
|
||||
this.show_composer(Some(person));
|
||||
this.composer.replace(Some(person.clone()));
|
||||
navigator.clone().pop();
|
||||
}));
|
||||
|
||||
navigator.push(selector);
|
||||
}
|
||||
|
||||
}));
|
||||
|
||||
this.reset_composer_button
|
||||
.connect_clicked(clone!(@strong this => move |_| {
|
||||
this.composer.replace(None);
|
||||
this.show_composer(None);
|
||||
}));
|
||||
|
||||
// Initialize
|
||||
|
||||
if let Some(composer) = &*this.composer.borrow() {
|
||||
this.show_composer(Some(composer));
|
||||
}
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// Set the closure to be called when the user wants to save the part.
|
||||
pub fn set_ready_cb<F: Fn(WorkPart) -> () + 'static>(&self, cb: F) {
|
||||
self.ready_cb.replace(Some(Box::new(cb)));
|
||||
}
|
||||
|
||||
/// Update the UI according to person.
|
||||
fn show_composer(&self, person: Option<&Person>) {
|
||||
if let Some(person) = person {
|
||||
self.composer_label.set_text(&person.name_fl());
|
||||
self.reset_composer_button.show();
|
||||
} else {
|
||||
self.composer_label.set_text(&gettext("Select …"));
|
||||
self.reset_composer_button.hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatorScreen for WorkPartEditor {
|
||||
fn attach_navigator(&self, navigator: Rc<Navigator>) {
|
||||
self.navigator.replace(Some(navigator));
|
||||
}
|
||||
|
||||
fn get_widget(&self) -> gtk::Widget {
|
||||
self.widget.clone().upcast()
|
||||
}
|
||||
|
||||
fn detach_navigator(&self) {
|
||||
self.navigator.replace(None);
|
||||
}
|
||||
}
|
||||
86
src/editors/work_section.rs
Normal file
86
src/editors/work_section.rs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
use crate::database::*;
|
||||
use crate::widgets::{Navigator, NavigatorScreen};
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A dialog for creating or editing a work section.
|
||||
pub struct WorkSectionEditor {
|
||||
widget: gtk::Box,
|
||||
title_entry: gtk::Entry,
|
||||
ready_cb: RefCell<Option<Box<dyn Fn(WorkSection) -> ()>>>,
|
||||
navigator: RefCell<Option<Rc<Navigator>>>,
|
||||
}
|
||||
|
||||
impl WorkSectionEditor {
|
||||
/// Create a new section editor and optionally initialize it.
|
||||
pub fn new(section: Option<WorkSection>) -> Rc<Self> {
|
||||
// Create UI
|
||||
|
||||
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/work_section_editor.ui");
|
||||
|
||||
get_widget!(builder, gtk::Box, widget);
|
||||
get_widget!(builder, gtk::Button, back_button);
|
||||
get_widget!(builder, gtk::Button, save_button);
|
||||
get_widget!(builder, gtk::Entry, title_entry);
|
||||
|
||||
if let Some(section) = section {
|
||||
title_entry.set_text(§ion.title);
|
||||
}
|
||||
|
||||
let this = Rc::new(Self {
|
||||
widget,
|
||||
title_entry,
|
||||
ready_cb: RefCell::new(None),
|
||||
navigator: RefCell::new(None),
|
||||
});
|
||||
|
||||
// Connect signals and callbacks
|
||||
|
||||
back_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
save_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
if let Some(cb) = &*this.ready_cb.borrow() {
|
||||
cb(WorkSection {
|
||||
before_index: 0,
|
||||
title: this.title_entry.get_text().to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
}));
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// Set the closure to be called when the user wants to save the section. Note that the
|
||||
/// resulting object will always have `before_index` set to 0. The caller is expected to
|
||||
/// change that later before adding the section to the database.
|
||||
pub fn set_ready_cb<F: Fn(WorkSection) -> () + 'static>(&self, cb: F) {
|
||||
self.ready_cb.replace(Some(Box::new(cb)));
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatorScreen for WorkSectionEditor {
|
||||
fn attach_navigator(&self, navigator: Rc<Navigator>) {
|
||||
self.navigator.replace(Some(navigator));
|
||||
}
|
||||
|
||||
fn get_widget(&self) -> gtk::Widget {
|
||||
self.widget.clone().upcast()
|
||||
}
|
||||
|
||||
fn detach_navigator(&self) {
|
||||
self.navigator.replace(None);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue