AIT/src/view/pages/signal_reducing.rs

114 lines
3.3 KiB
Rust
Raw Normal View History

use glib::property::PropertyGet;
use gtk4 as gtk;
use gtk4::prelude::WidgetExt;
use std::{str::FromStr, time::Duration};
use crate::{
event_handlers::button_event_handlers::BtnEventHandler,
model::{builder_traits::Product, models::*},
view::{components::input::Input, properties::*},
};
2024-04-19 11:03:16 +03:00
use gtk::{
prelude::{BoxExt, GridExt, TextBufferExt, TextViewExt},
2024-04-19 11:03:16 +03:00
Align, WrapMode, *,
};
pub fn signal_reducing_page(wrapper: &Box, info_bar: Revealer) {
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;
2024-04-19 11:03:16 +03:00
}
input_block.attach(
&elem.clone().get(),
(id as i32) - (3 * row_position),
row_position,
1,
1,
);
2024-04-19 11:03:16 +03:00
}
let calculate_button = Button::builder().label("Расчитать").build();
// TODO: Make asynchronious auto-hide info bar.
let test = info_bar.clone();
2024-04-19 11:03:16 +03:00
// let (tx, rx) = std::sync::mpsc::channel::<bool>();
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 {
test.set_reveal_child(true);
// TODO: Make asynchronious auto-hide info bar.
// let transmitter = tx.clone();
//
// gio::spawn_blocking(move || {
// std::thread::sleep(Duration::from_secs(5));
// transmitter.send(false).unwrap();
// });
//
// test.set_reveal_child(rx.recv().unwrap());
println!("Вы ввели некорректное значение поля.");
return;
}
}
println!("{:?}", values);
})
.on_click();
wrapper.append(&input_block);
wrapper.append(&calculate_button);
}