Skip to content

Commit

Permalink
First normalize rule (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
qarmin authored Jul 26, 2021
1 parent 3eb9e32 commit eb4c710
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 38 deletions.
48 changes: 24 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ humansize = "1.1.1"
chrono = "0.4.19"

# For opening files
open = "1.7.0"
open = "2.0.0"

# For slugs
slugmin = "1.0.0"
slugmin = "1.0.1"

[dependencies.gtk]
version = "0.14.0"
Expand Down
19 changes: 19 additions & 0 deletions src/class_dialog_rule_normalize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use gtk::prelude::*;

#[derive(Clone)]
pub struct GuiNormalize {
pub radio_button_normalize_everything: gtk::RadioButton,
pub radio_button_normalize_partial: gtk::RadioButton,
}

impl GuiNormalize {
pub fn create_from_builder(builder: &gtk::Builder) -> Self {
let radio_button_normalize_everything: gtk::RadioButton = builder.object("radio_button_normalize_everything").unwrap();
let radio_button_normalize_partial: gtk::RadioButton = builder.object("radio_button_normalize_partial").unwrap();

Self {
radio_button_normalize_everything,
radio_button_normalize_partial,
}
}
}
4 changes: 4 additions & 0 deletions src/class_dialog_rules.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::class_dialog_rule_add_number::GuiAddNumber;
use crate::class_dialog_rule_add_text::GuiAddText;
use crate::class_dialog_rule_custom::GuiCustom;
use crate::class_dialog_rule_normalize::GuiNormalize;
use crate::class_dialog_rule_purge::GuiPurge;
use crate::class_dialog_rule_replace::GuiReplace;
use crate::class_dialog_rule_size_letters::GuiSizeLetters;
Expand All @@ -22,6 +23,7 @@ pub struct GuiDialogRules {
pub custom: GuiCustom,
pub replace: GuiReplace,
pub add_number: GuiAddNumber,
pub normalize: GuiNormalize,

pub entry_example_before: Entry,
pub label_example_after: Label,
Expand All @@ -43,6 +45,7 @@ impl GuiDialogRules {
let custom: GuiCustom = GuiCustom::create_from_builder(&builder);
let replace: GuiReplace = GuiReplace::create_from_builder(&builder);
let add_number: GuiAddNumber = GuiAddNumber::create_from_builder(&builder);
let normalize: GuiNormalize = GuiNormalize::create_from_builder(&builder);

let entry_example_before: gtk::Entry = builder.object("entry_example_before").unwrap();
let label_example_after: gtk::Label = builder.object("label_example_after").unwrap();
Expand All @@ -59,6 +62,7 @@ impl GuiDialogRules {
custom,
replace,
add_number,
normalize,
entry_example_before,
label_example_after,
button_example_reset,
Expand Down
10 changes: 10 additions & 0 deletions src/connect_rule_buttons_modify_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ pub fn connect_rule_modify_edit(gui_data: &GuiData) {
let entry_add_number_step = window_rules.add_number.entry_add_number_step.clone();
let entry_add_number_zeros = window_rules.add_number.entry_add_number_zeros;

let radio_button_normalize_everything = window_rules.normalize.radio_button_normalize_everything.clone();
let radio_button_normalize_partial = window_rules.normalize.radio_button_normalize_partial;

button_edit_rule.connect_clicked(move |_e| {
let mut rules = rules.borrow_mut();
let rules = rules.deref_mut();
Expand Down Expand Up @@ -370,6 +373,13 @@ pub fn connect_rule_modify_edit(gui_data: &GuiData) {
entry_add_number_step.set_text(rule_data.number_step.to_string().as_str());
entry_add_number_start_number.set_text(rule_data.number_start.to_string().as_str());
}
RuleType::Normalize => {
if rule_data.full_normalize {
radio_button_normalize_everything.set_active(true);
} else {
radio_button_normalize_partial.set_active(true);
}
}
}

button_rule_window_add.set_label("Edit Rule");
Expand Down
18 changes: 18 additions & 0 deletions src/connect_rule_window_normalize_click.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::class_gui_data::GuiData;
use crate::example_fields::update_examples;
use gtk::prelude::*;

pub fn connect_rule_window_normalize_click(gui_data: &GuiData) {
let window_rules = gui_data.window_rules.clone();

let radio_button_normalize_everything = gui_data.window_rules.normalize.radio_button_normalize_everything.clone();
let radio_button_normalize_partial = gui_data.window_rules.normalize.radio_button_normalize_partial.clone();

radio_button_normalize_everything.connect_clicked(move |_e| {
update_examples(&window_rules, None);
});
let window_rules = gui_data.window_rules.clone();
radio_button_normalize_partial.connect_clicked(move |_e| {
update_examples(&window_rules, None);
});
}
21 changes: 21 additions & 0 deletions src/help_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ pub fn read_rule_from_window(window_rules: &GuiDialogRules, notebook_number: Opt
let entry_add_number_step = window_rules.add_number.entry_add_number_step.clone();
let entry_add_number_zeros = window_rules.add_number.entry_add_number_zeros.clone();

let radio_button_normalize_everything = window_rules.normalize.radio_button_normalize_everything.clone();
let radio_button_normalize_partial = window_rules.normalize.radio_button_normalize_partial.clone();

let rule_type: RuleType;
let rule_place: RulePlace;
let mut rule_data: RuleData = RuleData::new();
Expand Down Expand Up @@ -420,6 +423,24 @@ pub fn read_rule_from_window(window_rules: &GuiDialogRules, notebook_number: Opt
};
rule_description = format!("Starting with {} with step {}{}", rule_data.number_step, rule_data.number_start, zeros);
}
NotebookEnum::Normalize => {
rule_type = RuleType::Normalize;
rule_place = RulePlace::ExtensionAndName;

if radio_button_normalize_everything.is_active() {
rule_data.full_normalize = true;
} else if radio_button_normalize_partial.is_active() {
rule_data.full_normalize = false;
} else {
panic!();
}

if rule_data.full_normalize {
rule_description = "Full normalize".to_string();
} else {
rule_description = "Partial normalize".to_string();
}
}
}
SingleRule {
rule_type,
Expand Down
11 changes: 2 additions & 9 deletions src/initialize_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,8 @@ pub fn common_open_function(tree_view: &gtk::TreeView, opening_mode: OpenMode) {
}
}

match open::that(&end_path) {
Ok(t) => {
if !t.success() {
println!("Failed to open {}, status {:?}", end_path, t.code());
}
}
Err(_) => {
println!("Failed to open {}", end_path);
}
if open::that(&end_path).is_err() {
println!("Failed to open {}", end_path);
}
}
}
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
mod class_dialog_rule_add_number;
mod class_dialog_rule_add_text;
mod class_dialog_rule_custom;
mod class_dialog_rule_normalize;
mod class_dialog_rule_purge;
mod class_dialog_rule_replace;
mod class_dialog_rule_size_letters;
Expand All @@ -25,6 +26,7 @@ mod connect_rule_window_add_number_click;
mod connect_rule_window_add_text_click;
mod connect_rule_window_close;
mod connect_rule_window_custom_click;
mod connect_rule_window_normalize_click;
mod connect_rule_window_purge_click;
mod connect_rule_window_replace_click;
mod connect_rule_window_size_letters_click;
Expand All @@ -41,6 +43,7 @@ mod rule_add_number;
mod rule_add_text;
mod rule_change_size_letters;
mod rule_custom;
mod rule_normalize;
mod rule_purge;
mod rule_replace;
mod rule_trim;
Expand All @@ -59,6 +62,7 @@ use crate::connect_rule_window_add_number_click::*;
use crate::connect_rule_window_add_text_click::*;
use crate::connect_rule_window_close::*;
use crate::connect_rule_window_custom_click::*;
use crate::connect_rule_window_normalize_click::*;
use crate::connect_rule_window_purge_click::*;
use crate::connect_rule_window_replace_click::*;
use crate::connect_rule_window_size_letters_click::*;
Expand Down Expand Up @@ -95,6 +99,7 @@ fn main() {
connect_rule_window_custom_click(&gui_data);
connect_rule_window_replace_click(&gui_data);
connect_rule_window_add_number_click(&gui_data);
connect_rule_window_normalize_click(&gui_data);

// Connect update examples in Rule Dialog
connect_update_examples(&gui_data);
Expand Down
4 changes: 3 additions & 1 deletion src/notebook_enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Needs to be updated when changed order of notebook tabs

pub(crate) const EXAMPLE_NAME: &str = "Czkawka.Txt";
pub(crate) const EXAMPLE_NAME: &str = "Gżegżółka.Txt";

#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub enum NotebookEnum {
Expand All @@ -11,6 +11,7 @@ pub enum NotebookEnum {
AddText,
Replace,
Trim,
Normalize,
}
pub fn to_notebook_enum(notebook_number: u32) -> NotebookEnum {
match notebook_number {
Expand All @@ -21,6 +22,7 @@ pub fn to_notebook_enum(notebook_number: u32) -> NotebookEnum {
4 => NotebookEnum::AddText,
5 => NotebookEnum::Replace,
6 => NotebookEnum::Trim,
7 => NotebookEnum::Normalize,
_ => panic!("Invalid Notebook Tab"),
}
}
5 changes: 3 additions & 2 deletions src/rule_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ pub fn rule_custom(data_to_change: &str, rule: &SingleRule, rule_number: u64, fi
} else {
number = step_number * rule_number as i64;
}
// TODO Better handle negative numbers

if number.checked_add(start_number).is_none() {
number = 0;
Expand All @@ -121,7 +120,9 @@ pub fn rule_custom(data_to_change: &str, rule: &SingleRule, rule_number: u64, fi
}
}
}
_ => {}
_ => {
// Just invalid thing
}
}

if invalid_data {
Expand Down
Loading

0 comments on commit eb4c710

Please sign in to comment.