Skip to content

Commit

Permalink
Add capability to load and save Text Extraction preferences.
Browse files Browse the repository at this point in the history
  • Loading branch information
divark committed May 14, 2024
1 parent 3ac10be commit 9b938e4
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 14 deletions.
32 changes: 32 additions & 0 deletions src/sessions/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub struct Session {

audio_input: AudioInput,
audio_output: AudioOutput,

gathering_choice: String,
gathering_amount: usize,
gathering_delimiters: String,
}

fn get_projects_path() -> PathBuf {
Expand Down Expand Up @@ -70,6 +74,10 @@ impl Session {

audio_input: AudioInput::new(),
audio_output: AudioOutput::new(),

gathering_choice: String::from("Sentences"),
gathering_amount: 4,
gathering_delimiters: String::from(".?!"),
}
}

Expand Down Expand Up @@ -153,4 +161,28 @@ impl Session {
pub fn audio_input_mut(&mut self) -> &mut AudioInput {
&mut self.audio_input
}

pub fn gathering_choice(&self) -> String {
self.gathering_choice.clone()
}

pub fn set_gathering_choice(&mut self, gathering_choice: &str) {
self.gathering_choice = String::from(gathering_choice);
}

pub fn gathering_amount(&self) -> usize {
self.gathering_amount
}

pub fn set_gathering_amount(&mut self, amount: usize) {
self.gathering_amount = amount;
}

pub fn gathering_delimiters(&self) -> String {
self.gathering_delimiters.clone()
}

pub fn set_gathering_delimiters(&mut self, delimiters: &str) {
self.gathering_delimiters = String::from(delimiters);
}
}
119 changes: 105 additions & 14 deletions src/ui/dialogs/preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use fltk::{
group::{Flex, FlexType, Group, Tabs},
input::Input,
misc::{InputChoice, Spinner},
prelude::{DisplayExt, GroupExt, WidgetBase, WidgetExt, WindowExt},
prelude::{DisplayExt, GroupExt, InputExt, WidgetBase, WidgetExt, WindowExt},
text::{TextBuffer, TextDisplay},
window::Window,
};
Expand Down Expand Up @@ -54,6 +54,11 @@ pub struct PreferencesDialog {
audio_input_sample_rate: InputChoice,
audio_input_channels: InputChoice,

gathering_choice: InputChoice,
custom_gathering: CheckButton,
gathering_amount: Spinner,
gathering_delimiters: Input,

save_button: Button,
}

Expand Down Expand Up @@ -114,12 +119,12 @@ fn create_general_tab() -> GeneralTabWidgets {
}

struct TextTabWidgets {
gatherer_name: InputChoice,
gatherer_custom_enabler: CheckButton,
gathering_choice: InputChoice,
custom_gathering: CheckButton,

gatherer_amount: Spinner,
gathering_amount: Spinner,

gatherer_delimiters: Input,
gathering_delimiters: Input,
}

const TEXT_TAB_LABEL_LENGTH: i32 = 100;
Expand Down Expand Up @@ -151,7 +156,7 @@ fn create_text_tab() -> TextTabWidgets {
gatherer_group.fixed(&gatherer_label, TEXT_TAB_LABEL_LENGTH);

let gatherer_selector = InputChoice::default()
.with_label("Gatherer:")
.with_label("Gathering:")
.with_align(Align::Left);
gatherer_group.fixed(&gatherer_selector, TEXT_TAB_INPUT_LENGTH);

Expand All @@ -166,9 +171,10 @@ fn create_text_tab() -> TextTabWidgets {
let amount_label = Frame::default();
amount_group.fixed(&amount_label, TEXT_TAB_LABEL_LENGTH);

let amount_spinner = Spinner::default()
let mut amount_spinner = Spinner::default()
.with_label("Amount:")
.with_align(Align::Left);
amount_spinner.deactivate();
amount_group.end();

let mut ending_with_group = Flex::default().with_type(FlexType::Row);
Expand All @@ -178,19 +184,20 @@ fn create_text_tab() -> TextTabWidgets {
let ending_with_label = Frame::default();
ending_with_group.fixed(&ending_with_label, TEXT_TAB_LABEL_LENGTH);

let ending_with_delimiters_input = Input::default()
.with_label("Ending With:")
let mut ending_with_delimiters_input = Input::default()
.with_label("Splitting By:")
.with_align(Align::Left);
ending_with_delimiters_input.deactivate();
ending_with_group.end();

extraction_group.end();
text_tab.end();

TextTabWidgets {
gatherer_name: gatherer_selector,
gatherer_amount: amount_spinner,
gatherer_custom_enabler,
gatherer_delimiters: ending_with_delimiters_input,
gathering_choice: gatherer_selector,
gathering_amount: amount_spinner,
custom_gathering: gatherer_custom_enabler,
gathering_delimiters: ending_with_delimiters_input,
}
}

Expand Down Expand Up @@ -276,7 +283,7 @@ impl PreferencesDialog {

let general_tab = create_general_tab();
let mut audio_tab = create_audio_tab();
let text_tab = create_text_tab();
let mut text_tab = create_text_tab();

preference_topics.end();

Expand Down Expand Up @@ -321,6 +328,42 @@ impl PreferencesDialog {
);
});

let mut gathering_choice = text_tab.gathering_choice.clone();
let mut gathering_amount = text_tab.gathering_amount.clone();
let mut gathering_delimiters = text_tab.gathering_delimiters.clone();

text_tab.custom_gathering.set_callback(move |check_button| {
if check_button.is_checked() {
gathering_choice.set_value("Custom");
gathering_choice.deactivate();
gathering_amount.activate();
gathering_delimiters.activate();
} else {
gathering_choice.set_value_index(0);
gathering_amount.set_value(1.0);
gathering_delimiters.set_value("\t");

gathering_choice.activate();
gathering_amount.deactivate();
gathering_delimiters.deactivate();
}
});

let mut gathering_amount = text_tab.gathering_amount.clone();
let mut gathering_delimiters = text_tab.gathering_delimiters.clone();

text_tab.gathering_choice.set_callback(move |input_choice| {
if let Some(current_choice) = input_choice.value() {
if current_choice == "Paragraphs" {
gathering_amount.set_value(1.0);
gathering_delimiters.set_value("\t");
} else if current_choice == "Sentences" {
gathering_amount.set_value(4.0);
gathering_delimiters.set_value(".?!");
}
}
});

preferences_window.end();

PreferencesDialog {
Expand All @@ -333,6 +376,11 @@ impl PreferencesDialog {
audio_input_sample_rate: audio_tab.audio_input_sample_rate,
audio_input_channels: audio_tab.audio_input_channels,

gathering_choice: text_tab.gathering_choice,
custom_gathering: text_tab.custom_gathering,
gathering_amount: text_tab.gathering_amount,
gathering_delimiters: text_tab.gathering_delimiters,

save_button,
}
}
Expand Down Expand Up @@ -373,6 +421,47 @@ impl PreferencesDialog {
);
}

/// Clears and fills in Text Preferences to the relevant text input
/// widgets.
fn populate_text_tab_inputs(&mut self, session: &Session) {
let gathering_choice_names = ["Paragraphs", "Sentences"];
repopulate_input_choices(&mut self.gathering_choice, &gathering_choice_names);

if session.gathering_choice() == "Custom" {
self.custom_gathering.set_checked(true);
self.gathering_choice
.set_value(session.gathering_choice().as_str());
self.gathering_choice.deactivate();
self.gathering_amount
.set_value(session.gathering_amount() as f64);
self.gathering_amount.activate();
self.gathering_delimiters
.set_value(session.gathering_delimiters().as_str());
self.gathering_delimiters.activate();

return;
}

set_active_in_input_choices(
&mut self.gathering_choice,
&gathering_choice_names,
&session.gathering_choice().as_str(),
);
}

/// Pulls the currently selected values for all Text Preference
/// widgets and updates the current session accordingly.
fn save_text_preferences(&self, session: &mut Session) {
session.set_gathering_choice(
&self
.gathering_choice
.value()
.expect("save_text_preferences: Gathering Choice does not have a value."),
);
session.set_gathering_amount(self.gathering_amount.value() as usize);
session.set_gathering_delimiters(&self.gathering_delimiters.value());
}

/// Pulls the currently selected values for all audio input widgets
/// and updates the current session accordingly.
fn save_audio_preferences(&self, session: &mut Session) {
Expand Down Expand Up @@ -406,6 +495,7 @@ impl PreferencesDialog {
.unwrap()
.set_text(session.project_directory().to_str().unwrap());
self.populate_audio_tab_inputs(session);
self.populate_text_tab_inputs(session);

self.window.show();

Expand All @@ -422,5 +512,6 @@ impl PreferencesDialog {
session.set_project_directory(audio_output_dir);

self.save_audio_preferences(session);
self.save_text_preferences(session);
}
}

0 comments on commit 9b938e4

Please sign in to comment.