BREAKING CHANGE: refactor code and add event handler for button

This commit is contained in:
doryan 2024-07-19 23:16:11 +04:00
parent 682682eb13
commit 2866d26253

View File

@ -1,211 +1,113 @@
use glib::property::PropertyGet;
use gtk4 as gtk; use gtk4 as gtk;
use gtk4::prelude::WidgetExt;
use std::{str::FromStr, time::Duration};
use crate::{ use crate::{
controller::event_handlers::button_event_handlers::*, event_handlers::button_event_handlers::BtnEventHandler,
model::{builder_traits::Product, model::*}, model::{builder_traits::Product, models::*},
view::{ view::{components::input::Input, properties::*},
components::{input::Input, wrapper::Wrapper},
properties::*,
},
view_utils::signal_reduce_input_utils::start_algorithm,
}; };
use gtk::{ use gtk::{
prelude::{BoxExt, ListBoxRowExt, WidgetExt}, prelude::{BoxExt, GridExt, TextBufferExt, TextViewExt},
Align, WrapMode, *, Align, WrapMode, *,
}; };
pub fn signal_reducing_page(wrapper: &Box) { pub fn signal_reducing_page(wrapper: &Box, info_bar: Revealer) {
let (input_height, monospace, input_alignment, input_wrapping): (i32, bool, Align, WrapMode) = let (input_height, monospace, input_alignment, input_wrapping): (i32, bool, Align, WrapMode) =
(24, true, Align::Fill, WrapMode::Word); (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 { let input_label_alignment = Alignment {
horizontal: Align::Start, horizontal: Align::Start,
vertical: Align::Fill, vertical: Align::Fill,
}; };
let wire_length_input: Input = Input::builder() let input_labels: [&str; 6] = [
.set_label("l, м:") "l, м:",
.set_margins(MarginData::EqualsMargin(5)) "Rм, Ом:",
.set_align(input_label_alignment) "Cм, пФ:",
.build(monospace, input_wrapping, input_height); "Vи, мВ",
"Rи, Ом:",
let resistance_per_meter_input: Input = Input::builder() "f, мГц:",
.set_label("Rм, Ом:")
.set_margins(MarginData::EqualsMargin(5))
.set_align(input_label_alignment)
.build(monospace, input_wrapping, input_height);
let capacity_per_meter_input: Input = Input::builder()
.set_label("Cм, пФ:")
.set_margins(MarginData::EqualsMargin(5))
.set_align(input_label_alignment)
.build(monospace, input_wrapping, input_height);
let wrapper_first_row = Wrapper::col_builder()
.halign(Align::Fill)
.hexpand(true)
.spacing(5)
.build();
let first_row_elements: Vec<&Input> = vec![
&wire_length_input,
&resistance_per_meter_input,
&capacity_per_meter_input,
]; ];
for elem in first_row_elements { let all_inputs: Vec<Input> = input_labels
let input: Box = elem.clone().get(); .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();
input.set_halign(input_alignment); let mut row_position = 0i32;
input.set_hexpand(true);
wrapper_first_row.append(&input); for (id, elem) in all_inputs.iter().enumerate() {
} if id % 3 == 0 {
row_position += 1;
let info_signal_voltage_input: Input = Input::builder()
.set_label("Vи, мВ:")
.set_margins(MarginData::EqualsMargin(5))
.set_align(input_label_alignment)
.build(monospace, input_wrapping, input_height);
let resistance_of_info_voltage_source_input: Input = Input::builder()
.set_label("Rи, Ом:")
.set_margins(MarginData::EqualsMargin(5))
.set_align(input_label_alignment)
.build(monospace, input_wrapping, input_height);
let info_voltage_source_frequency_input: Input = Input::builder()
.set_label("f, мГц:")
.set_margins(MarginData::EqualsMargin(5))
.set_align(input_label_alignment)
.build(monospace, input_wrapping, input_height);
let wrapper_second_row = Wrapper::col_builder()
.halign(Align::Fill)
.hexpand(true)
.spacing(5)
.build();
let second_row_elements: Vec<&Input> = vec![
&info_signal_voltage_input,
&resistance_of_info_voltage_source_input,
&info_voltage_source_frequency_input,
];
for elem in second_row_elements {
let input: Box = elem.clone().get();
input.set_halign(input_alignment);
input.set_hexpand(true);
wrapper_second_row.append(&input);
}
let all_text_views: Vec<TextView> = vec![
wire_length_input.get_input(),
resistance_per_meter_input.get_input(),
capacity_per_meter_input.get_input(),
info_signal_voltage_input.get_input(),
resistance_of_info_voltage_source_input.get_input(),
info_voltage_source_frequency_input.get_input(),
];
let main_wrapper = Wrapper::row_builder().spacing(5).build();
main_wrapper.append(&wrapper_first_row);
main_wrapper.append(&wrapper_second_row);
let calculate_button = Button::builder()
.label("Расчитать")
.halign(Align::End)
.set_margin(MarginData::MultipleMargin((10, 5, 0, 0)))
.build();
let table_header_labels = vec!["f, Гц", "Xc, пФ", "Vп, мВ", "ζ"];
let table_header = ColumnView::builder()
.focusable(false)
.reorderable(false)
.show_row_separators(true)
.show_column_separators(true)
.halign(Align::Fill)
.hexpand(true)
.build();
for table_header_label in table_header_labels {
let column = ColumnViewColumn::builder()
.title(table_header_label)
.fixed_width(200)
.expand(true)
.build();
table_header.append_column(&column);
}
let mut table = ListBox::builder().hexpand(true).halign(Align::Fill).build();
let header = ListBoxRow::builder().child(&table_header).build();
table.append(&header);
for i in (0..=100).step_by(5) {
if i == 0 {
continue;
} }
let table_row = ListBoxRow::new(); input_block.attach(
&elem.clone().get(),
let columns = ColumnView::builder().reorderable(false).build(); (id as i32) - (3 * row_position),
row_position,
let column = ColumnViewColumn::builder() 1,
.title(format!("{i}")) 1,
.fixed_width(200) );
.expand(true)
.build();
columns.append_column(&column);
for table_row_label in 1..=3 {
let column = ColumnViewColumn::builder()
.title(format!("{table_row_label}"))
.fixed_width(200)
.expand(true)
.build();
columns.append_column(&column);
}
table_row.set_child(Some(&columns));
table.append(&table_row);
} }
let table_of_calculated_values = Frame::builder() let calculate_button = Button::builder().label("Расчитать").build();
.height_request(64)
.hexpand(true)
.child(&table)
.halign(Align::Fill)
.set_margin(MarginData::MultipleMargin((10, 5, 0, 0)))
.build();
let table_frame = table_of_calculated_values.clone(); // TODO: Make asynchronious auto-hide info bar.
let test = info_bar.clone();
let errors_label: Label = Label::new(Some("")); // let (tx, rx) = std::sync::mpsc::channel::<bool>();
let errors_label_for_handler: Label = errors_label.clone();
EventHandler::new(calculate_button.clone(), move |_| { EventHandler::new(calculate_button.clone(), move |_| {
start_algorithm( let mut values: [f64; 6] = [0f64; 6];
&errors_label_for_handler,
&all_text_views, for (i, input) in all_inputs.iter().enumerate() {
&table_frame.clone(), let input_text_buffer: TextBuffer = input.clone().get_input().buffer();
&header, 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(); .on_click();
main_wrapper.append(&errors_label); wrapper.append(&input_block);
main_wrapper.append(&calculate_button); wrapper.append(&calculate_button);
main_wrapper.append(&table_of_calculated_values);
wrapper.append(&main_wrapper);
} }