refactor: move parse field algorithm to a separate file
This commit is contained in:
parent
18e857b40c
commit
09b05e7fa7
|
@ -1,119 +1,51 @@
|
|||
use gtk4 as gtk;
|
||||
|
||||
use crate::controller::model_utils::signal_reducer::*;
|
||||
use crate::model::models::SchemeCharacteristics;
|
||||
use gtk::{prelude::*, *};
|
||||
use std::{num::ParseFloatError, str::FromStr};
|
||||
|
||||
pub fn check_characteristics(
|
||||
raw_characteristics: &Vec<TextView>,
|
||||
all_inputs_data: &mut Vec<f64>,
|
||||
output: &mut String,
|
||||
) -> bool {
|
||||
for input in raw_characteristics {
|
||||
let (iter_start, iter_end) = input.buffer().bounds();
|
||||
let parsed_input: Result<f64, _> = input
|
||||
.buffer()
|
||||
.text(&iter_start, &iter_end, false)
|
||||
.to_string()
|
||||
.trim()
|
||||
.parse();
|
||||
use gtk::{
|
||||
prelude::{TextBufferExt, TextViewExt},
|
||||
TextBuffer,
|
||||
};
|
||||
|
||||
match parsed_input {
|
||||
Ok(int) => {
|
||||
all_inputs_data.push(int);
|
||||
println!("{:?}", int);
|
||||
}
|
||||
Err(err) => {
|
||||
if output.is_empty() {
|
||||
output.push_str("Введите пожалуйста числа в полях");
|
||||
println!("{:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::view::components::{info_bar::InfoBar, input::Input};
|
||||
|
||||
!output.is_empty()
|
||||
pub fn get_error_message(error_instance: ParseFloatError) -> Option<&'static str> {
|
||||
match error_instance.to_string().as_str() {
|
||||
"cannot parse float from empty string" => Some("Вы не ввели данные в поле/-я"),
|
||||
"invalid float literal" => Some("Вы ввели не корректные данные в поле/-я"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_algorithm(
|
||||
error_log_label: &Label,
|
||||
raw_characteristics: &Vec<TextView>,
|
||||
table: &Frame,
|
||||
header: &ListBoxRow,
|
||||
) {
|
||||
let mut all_inputs_data: Vec<f64> = Vec::new();
|
||||
let mut output: String = String::new();
|
||||
pub fn parse_fields(all_inputs: Vec<Input>) -> Option<[f64; 6]> {
|
||||
let mut values: [f64; 6] = [0f64; 6];
|
||||
|
||||
if !check_characteristics(raw_characteristics, &mut all_inputs_data, &mut output) {
|
||||
error_log_label.set_text(output.as_str());
|
||||
} else {
|
||||
error_log_label.set_text("");
|
||||
let info_bar = InfoBar::get_instance();
|
||||
|
||||
let new_table = ListBox::builder().hexpand(true).halign(Align::Fill).build();
|
||||
|
||||
new_table.append(header);
|
||||
|
||||
let (L, Rm, Cm, Vs, Rs, f): SchemeCharacteristics = (
|
||||
all_inputs_data[0],
|
||||
all_inputs_data[1],
|
||||
all_inputs_data[2],
|
||||
all_inputs_data[3],
|
||||
all_inputs_data[4],
|
||||
all_inputs_data[5],
|
||||
for (i, input) in all_inputs.iter().enumerate() {
|
||||
let input_text_buffer: TextBuffer = input.clone().get_input().buffer();
|
||||
let try_extract_value = f64::from_str(
|
||||
input_text_buffer
|
||||
.text(
|
||||
&input_text_buffer.start_iter(),
|
||||
&input_text_buffer.end_iter(),
|
||||
false,
|
||||
)
|
||||
.as_str()
|
||||
.trim(),
|
||||
);
|
||||
match try_extract_value {
|
||||
Ok(value) => values[i] = value,
|
||||
Err(error) => {
|
||||
let error_kind: Option<&str> = get_error_message(error);
|
||||
|
||||
let mut frequencies: Vec<usize> = vec![f as usize, 1usize];
|
||||
info_bar.set_text_label(error_kind);
|
||||
info_bar.show_infobar(5u64);
|
||||
|
||||
frequencies.append(&mut (0..100).step_by(5).collect());
|
||||
|
||||
for i in frequencies {
|
||||
if i == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let table_row = ListBoxRow::new();
|
||||
|
||||
let columns = ColumnView::builder().reorderable(false).build();
|
||||
|
||||
let frequency_column = ColumnViewColumn::builder()
|
||||
.title(format!("{i}"))
|
||||
.fixed_width(200)
|
||||
.expand(true)
|
||||
.build();
|
||||
|
||||
let Xc: f64 = reactive_resistance_of_capacitor(Cm, L, f);
|
||||
let Z: f64 = full_resistance_of_capacitor(Xc, Vs, Rm, L);
|
||||
let V: f64 = voltage_from_signal_source(Vs, Xc, Z);
|
||||
let xi: f64 = coef_of_signal_reduce(Vs, V);
|
||||
|
||||
let reactive_resistance_column = ColumnViewColumn::builder()
|
||||
.title(format!("{:?}", Xc))
|
||||
.fixed_width(200)
|
||||
.expand(true)
|
||||
.build();
|
||||
|
||||
let voltage_column = ColumnViewColumn::builder()
|
||||
.title(format!("{:?}", V))
|
||||
.fixed_width(200)
|
||||
.expand(true)
|
||||
.build();
|
||||
|
||||
let coef_column = ColumnViewColumn::builder()
|
||||
.title(format!("{:?}", xi))
|
||||
.fixed_width(200)
|
||||
.expand(true)
|
||||
.build();
|
||||
|
||||
columns.append_column(&frequency_column);
|
||||
columns.append_column(&reactive_resistance_column);
|
||||
columns.append_column(&voltage_column);
|
||||
columns.append_column(&coef_column);
|
||||
|
||||
table_row.set_child(Some(&columns));
|
||||
|
||||
new_table.append(&table_row);
|
||||
|
||||
table.set_child(Some(&new_table));
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(values)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue