I'm too lazy to commit to the latest files

This commit is contained in:
doryan 2024-08-15 23:37:34 +04:00
parent 3d896c7061
commit 62b7036933
5 changed files with 149 additions and 134 deletions

View File

@ -1,33 +0,0 @@
use gtk4 as gtk;
use crate::{
model::{models::*, Result},
model_utils::hamming_code_seven_four::*,
};
use gtk::{prelude::*, *};
pub fn start_hamming_algorithm(input: &TextView, state: bool) -> Result<String> {
let (iter_start, iter_end) = input.buffer().bounds();
let parsed_input: String = input
.buffer()
.text(&iter_start, &iter_end, false)
.to_string()
.trim()
.parse()
.unwrap();
let operation = if !state {
HammingMode::Encrypt
} else {
HammingMode::Decrypt
};
hamming(parsed_input, operation)
}
pub fn check_correct_binary_code(input: &str, prepared_input: &str, l: usize) -> (bool, bool) {
let first_condition: bool = prepared_input.len() % l == 0;
(first_condition, second_condition)
}

View File

@ -6,4 +6,29 @@ use crate::{
}; };
use gtk::{prelude::*, *}; use gtk::{prelude::*, *};
pub fn start_hamming_algorithm(input: &TextView, state: bool) -> Result<String> {
let (iter_start, iter_end) = input.buffer().bounds();
let parsed_input: String = input
.buffer()
.text(&iter_start, &iter_end, false)
.to_string()
.trim()
.parse()
.unwrap();
let operation = if !state {
HammingMode::Encrypt
} else {
HammingMode::Decrypt
};
hamming(parsed_input, operation)
}
pub fn check_correct_binary_code(prepared_input: &str, l: usize) -> (bool, bool) {
let first_condition: bool = prepared_input.len() % l == 0;
let second_condition: bool = prepared_input.chars().all(|c| c == '1' || c == '0'); let second_condition: bool = prepared_input.chars().all(|c| c == '1' || c == '0');
(first_condition, second_condition)
}

View File

@ -41,3 +41,88 @@ pub fn parse_fields(all_inputs: Vec<Input<Entry>>) -> Result<SignalReduce> {
} }
#[inline] #[inline]
pub fn update_column_view(column_view: &ColumnView) {
column_view.hide();
column_view.show();
}
#[inline]
pub fn column_view_setup_factory(_factory: &SignalListItemFactory, list_item: &ListItem) {
list_item
.downcast_ref::<ListItem>()
.expect("Needs to be ListItem")
.set_child(Some(&Label::new(None)));
}
pub fn column_view_bind_factory(
_factory: &SignalListItemFactory,
list_item: &ListItem,
values: SignalReduce,
label: &str,
) {
let cell_value = list_item
.downcast_ref::<ListItem>()
.expect("Needs to be ListItem")
.item()
.and_downcast::<Frequency>()
.expect("The item has to be an `IntegerObject`.");
let cell_label = list_item
.downcast_ref::<ListItem>()
.expect("Needs to be ListItem")
.child()
.and_downcast::<Label>()
.expect("The child has to be a `Label`.");
cell_value
.bind_property("frequency", &cell_label, "label")
.sync_create()
.build();
if cell_value.reactive_resist() == 0.0 {
cell_value.set_reactive_resist(reactive_resistance_of_capacitor(
values.wire_capacity * 10f64.powi(-12),
values.length,
cell_value.frequency() * 10f64.powi(6),
));
}
if cell_value.full_resistance() == 0.0 {
cell_value.set_full_resistance(full_resistance_of_capacitor(
cell_value.reactive_resist(),
values.source_resistance,
values.wire_resistance,
values.length,
));
}
if cell_value.signal_source_voltage() == 0.0 {
cell_value.set_signal_source_voltage(
voltage_from_signal_source(
values.source_voltage * 10f64.powi(-3),
cell_value.reactive_resist(),
cell_value.full_resistance(),
) * 1000.0,
);
}
match label {
"f, МГц" => {
cell_label.set_label(&cell_value.frequency().to_string());
}
"Xc, Ом" => {
cell_label.set_label(format!("{0:.1$}", cell_value.reactive_resist(), 6).as_str());
}
"Vп, мВ" => {
cell_label
.set_label(format!("{0:.1$}", cell_value.signal_source_voltage(), 6).as_str());
}
"ζ" => {
let coef: f64 =
coef_of_signal_reduce(values.source_voltage, cell_value.signal_source_voltage());
cell_label.set_label(format!("{0:.1$}", coef, 6).as_str());
}
_ => {}
}
}

View File

@ -14,20 +14,25 @@ use gtk::{glib::clone, prelude::*, *};
pub fn hamming_code_page(wrapper: &Box) { pub fn hamming_code_page(wrapper: &Box) {
let info_bar = InfoBar::get_instance(); let info_bar = InfoBar::get_instance();
let input_code = Input::builder() let input_code = Input::<TextView>::builder()
.label("Поле ввода для кода:") .label("Поле ввода для кода:")
.margins(MarginData::EqualsMargin(6)) .margins(MarginData::EqualsMargin(6))
.align(Alignment::new(Align::Start, Align::Start)) .align(Alignment::new(Align::Start, Align::Start))
.build(true, WrapMode::Word, 64); .build(Some(64));
let output_code = Input::builder() let output_code = Input::<TextView>::builder()
.label("Результат:") .label("Результат:")
.margins(MarginData::EqualsMargin(6)) .margins(MarginData::EqualsMargin(6))
.align(Alignment::new(Align::Start, Align::Start)) .align(Alignment::new(Align::Start, Align::Start))
.build(true, WrapMode::Word, 64); .build(Some(64));
output_code.get_input().set_editable(false); output_code.get_input().set_editable(false);
for input in [&input_code, &output_code] {
input.get_input().set_monospace(true);
input.get_input().set_wrap_mode(WrapMode::Word);
}
let clear_input_button = Button::builder() let clear_input_button = Button::builder()
.set_align(Alignment::new(Align::Fill, Align::Fill)) .set_align(Alignment::new(Align::Fill, Align::Fill))
.label("Очистка полей") .label("Очистка полей")

View File

@ -26,9 +26,6 @@ pub fn signal_reducing_page(wrapper: &Box) {
let info_bar = InfoBar::get_instance(); let info_bar = InfoBar::get_instance();
let (input_height, monospace, input_wrapping): (i32, bool, WrapMode) =
(24, true, WrapMode::Word);
let input_block: Grid = Grid::new(); let input_block: Grid = Grid::new();
input_block.set_column_homogeneous(true); input_block.set_column_homogeneous(true);
@ -41,31 +38,30 @@ pub fn signal_reducing_page(wrapper: &Box) {
let input_labels: [&str; 6] = ["l, м:", "Rм, Ом", "Cм, пФ:", "Rи, Ом:", "Vи, мВ", "f, мГц:"]; let input_labels: [&str; 6] = ["l, м:", "Rм, Ом", "Cм, пФ:", "Rи, Ом:", "Vи, мВ", "f, мГц:"];
let all_inputs: Vec<Input> = input_labels let all_inputs: Vec<Input<Entry>> = input_labels
.iter() .iter()
.map(move |label| { .enumerate()
Input::builder() .map(|(index, label)| {
let elem = Input::<Entry>::builder()
.label(label) .label(label)
.margins(MarginData::EqualsMargin(6)) .margins(MarginData::EqualsMargin(6))
.align(input_label_alignment) .align(input_label_alignment)
.build(monospace, input_wrapping, input_height) .build_entry(None);
let row = index as i32 / 3;
input_block.attach(elem.get(), (index as i32) - (3 * row), row, 1, 1);
elem
}) })
.collect(); .collect();
for (id, elem) in all_inputs.iter().enumerate() {
let row = id as i32 / 3;
input_block.attach(elem.get(), (id as i32) - (3 * row), row, 1, 1);
}
let calculate_button = Button::builder().label("Расчитать").build(); let calculate_button = Button::builder().label("Расчитать").build();
let result_table_headers_labels: [&str; 4] = ["f, МГц", "Xc, Ом", "Vп, мВ", "ζ"]; let result_table_headers_labels: [&str; 4] = ["f, МГц", "Xc, Ом", "Vп, мВ", "ζ"];
let model = ListStore::new::<Frequency>(); let model = ListStore::new::<Frequency>();
let model_for_events = model.clone();
set_default_values(&model); set_default_values(&model);
let numeric_sorter = CustomSorter::new(|a, b| { let numeric_sorter = CustomSorter::new(|a, b| {
@ -75,12 +71,11 @@ pub fn signal_reducing_page(wrapper: &Box) {
a.total_cmp(&b).into() a.total_cmp(&b).into()
}); });
let sorted_model = SortListModel::new(Some(model), Some(numeric_sorter.clone())); let sorted_model = SortListModel::new(Some(model.clone()), Some(numeric_sorter.clone()));
let selection_model = NoSelection::new(Some(sorted_model)); let selection_model = NoSelection::new(Some(sorted_model.clone()));
let result_table = ColumnView::builder() let result_table = ColumnView::builder()
.reorderable(true)
.show_row_separators(true) .show_row_separators(true)
.model(&selection_model) .model(&selection_model)
.build(); .build();
@ -93,70 +88,14 @@ pub fn signal_reducing_page(wrapper: &Box) {
for label in result_table_headers_labels { for label in result_table_headers_labels {
let factory = SignalListItemFactory::new(); let factory = SignalListItemFactory::new();
let values_for_factory = values.clone();
factory.connect_setup(move |_, list_item| { factory.connect_setup(column_view_setup_factory);
list_item
.downcast_ref::<ListItem>()
.expect("Needs to be ListItem")
.set_child(Some(&Label::new(None)));
});
factory.connect_bind(move |_, list_item| { factory.connect_bind(clone!(
let cell_value = list_item #[strong]
.downcast_ref::<ListItem>() values,
.expect("Needs to be ListItem") move |factory, list| column_view_bind_factory(factory, list, values.get(), label)
.item() ));
.and_downcast::<Frequency>()
.expect("The item has to be an `IntegerObject`.");
let cell_label = list_item
.downcast_ref::<ListItem>()
.expect("Needs to be ListItem")
.child()
.and_downcast::<Label>()
.expect("The child has to be a `Label`.");
let result_values = values_for_factory.get();
let reactive_resist: f64 = reactive_resistance_of_capacitor(
result_values.wire_capacity * 10f64.powi(-12),
result_values.length,
cell_value.frequency() * 10f64.powi(6),
);
let full_resistance: f64 = full_resistance_of_capacitor(
reactive_resist,
result_values.source_resistance,
result_values.wire_resistance,
result_values.length,
);
let signal_source_voltage: f64 = voltage_from_signal_source(
result_values.source_voltage * 10f64.powi(-3),
reactive_resist,
full_resistance,
) * 1000.0;
match label {
"f, МГц" => {
cell_label.set_label(&cell_value.frequency().to_string());
}
"Xc, Ом" => {
cell_label.set_label(format!("{0:.1$}", reactive_resist, 6).as_str());
}
"Vп, мВ" => {
cell_label.set_label(format!("{0:.1$}", signal_source_voltage, 6).as_str());
}
"ζ" => {
let coef: f64 =
coef_of_signal_reduce(result_values.source_voltage, signal_source_voltage);
cell_label.set_label(format!("{0:.1$}", coef, 6).as_str());
}
_ => {}
}
});
let column = ColumnViewColumn::builder() let column = ColumnViewColumn::builder()
.title(label) .title(label)
@ -180,29 +119,23 @@ pub fn signal_reducing_page(wrapper: &Box) {
calculate_button.connect_clicked(clone!( calculate_button.connect_clicked(clone!(
#[strong] #[strong]
model_for_events, model,
#[strong]
result_table,
move |_| match parse_fields(all_inputs.clone()) { move |_| match parse_fields(all_inputs.clone()) {
Ok(results) => { Ok(results) => {
values.set(results); values.set(results);
let new_elem = &Frequency::new(values.get().frequency); let new_elem = Frequency::new(values.get().frequency);
let exist_elem_pos = model_for_events.find_with_equal_func(|elem| { let exist_elem_pos = find_by_frequency_value(&model, &new_elem);
elem.downcast_ref::<Frequency>().unwrap().frequency() == new_elem.frequency()
});
match exist_elem_pos { if exist_elem_pos.is_some() {
Some(_) => {
info_bar.set_text_label(Some("Данная частота уже была задана.")); info_bar.set_text_label(Some("Данная частота уже была задана."));
info_bar.show_infobar(5u64); info_bar.show_infobar(5u64);
} } else {
None => { model.append(&new_elem);
model_for_events.append(&Frequency::new(values.get().frequency)); update_column_view(&result_table)
let a = model_for_events.n_items();
model_for_events.items_changed(0, 0, a - 1);
model_for_events.items_changed(a - 1, a - 1, 0);
}
} }
} }
Err(error) => { Err(error) => {