AIT/src/view/pages/signal_reducing.rs

108 lines
3.1 KiB
Rust

use gtk4 as gtk;
use std::{str::FromStr, sync::Arc, time::Duration};
use crate::{
event_handlers::button_event_handlers::BtnEventHandler,
model::{builder_traits::Product, models::*},
view::{
components::{info_bar::InfoBar, input::Input},
properties::*,
},
};
use gtk::{
prelude::{BoxExt, GridExt, TextBufferExt, TextViewExt},
Align, WrapMode, *,
};
pub fn signal_reducing_page(wrapper: &Box) {
let (input_height, monospace, input_alignment, input_wrapping): (i32, bool, Align, WrapMode) =
(24, true, Align::Fill, WrapMode::Word);
let input_block: Grid = Grid::new();
input_block.set_column_homogeneous(true);
input_block.set_row_homogeneous(true);
let input_label_alignment = Alignment {
horizontal: Align::Start,
vertical: Align::Fill,
};
let input_labels: [&str; 6] = [
"l, м:",
"Rм, Ом:",
"Cм, пФ:",
"Vи, мВ",
"Rи, Ом:",
"f, мГц:",
];
let all_inputs: Vec<Input> = input_labels
.iter()
.map(move |label| {
Input::builder()
.set_label(label)
.set_margins(MarginData::EqualsMargin(5))
.set_align(input_label_alignment)
.build(monospace, input_wrapping, input_height)
})
.collect();
let mut row_position = 0i32;
for (id, elem) in all_inputs.iter().enumerate() {
if id % 3 == 0 {
row_position += 1;
}
input_block.attach(
&elem.clone().get(),
(id as i32) - (3 * row_position),
row_position,
1,
1,
);
}
let calculate_button = Button::builder().label("Расчитать").build();
let info_bar: Arc<InfoBar> = Arc::new(InfoBar::get_instance());
EventHandler::new(calculate_button.clone(), move |_| {
let mut values: [f64; 6] = [0f64; 6];
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(),
);
if let Ok(value) = try_extract_value {
values[i] = value;
} else {
info_bar.set_text_label(Some("Вы ввели некорректное значение поля."));
info_bar.set_reveal_child(true);
let info_bar_reference = info_bar.clone();
gio::spawn_blocking(move || {
std::thread::sleep(Duration::from_secs(5));
info_bar_reference.set_reveal_child(false);
});
break;
}
}
})
.on_click();
wrapper.append(&input_block);
wrapper.append(&calculate_button);
}