feat(table): implement a result table

This commit is contained in:
doryan 2024-08-03 00:00:05 +04:00
parent d92b2419e0
commit ebf7d9b85c

View File

@ -1,23 +1,38 @@
use gtk4 as gtk;
use std::{fmt::Debug, num::ParseFloatError, str::FromStr, sync::Arc, time::Duration};
use std::{cell::Cell, rc::Rc};
use crate::{
event_handlers::button_event_handlers::BtnEventHandler,
model::{builder_traits::Product, models::*},
model::{
builder_traits::Product,
models::{EventHandler, SignalReduce},
ResultValue,
},
model_utils::signal_reducer::{
coef_of_signal_reduce, full_resistance_of_capacitor, reactive_resistance_of_capacitor,
voltage_from_signal_source,
},
view::{
components::{info_bar::InfoBar, input::Input},
properties::*,
},
view_utils::signal_reduce_input_utils::{get_error_message, parse_fields},
};
use gtk::{
prelude::{BoxExt, GridExt, TextBufferExt, TextViewExt},
prelude::{BoxExt, Cast, CastNone, GridExt, ListItemExt},
Align, WrapMode, *,
};
use gtk4 as gtk;
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 values = Rc::new(Cell::new(SignalReduce::default()));
let last_query: Vec<TreeIter> = Vec::new();
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();
@ -67,13 +82,123 @@ pub fn signal_reducing_page(wrapper: &Box) {
let calculate_button = Button::builder().label("Расчитать").build();
EventHandler::new(calculate_button.clone(), move |_| {
let values: Option<[f64; 6]> = parse_fields(all_inputs.clone());
let result_table_headers_labels: [&str; 4] = ["f, МГц", "Xc, пФ", "Vп, мВ", "ζ"];
println!("values: {:?}", values);
let model = gio::ListStore::new::<ResultValue>();
let cloned_model = model.clone();
let selection_model = SingleSelection::new(Some(model));
let result_table = ColumnView::builder()
.reorderable(true)
.model(&selection_model)
.build();
for label in result_table_headers_labels {
let factory = SignalListItemFactory::new();
factory.connect_setup(move |_, list_item| {
list_item
.downcast_ref::<ListItem>()
.expect("Needs to be ListItem")
.set_child(Some(&Label::new(None)));
});
let values_for_factory = values.clone();
factory.connect_bind(move |_, list_item| {
let cell_value = list_item
.downcast_ref::<ListItem>()
.expect("Needs to be ListItem")
.item()
.and_downcast::<ResultValue>()
.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.clone().get();
let reactive_resist: f64 = reactive_resistance_of_capacitor(
result_values.2,
result_values.0,
cell_value.value(),
);
let full_resistance: f64 = full_resistance_of_capacitor(
reactive_resist,
result_values.4,
result_values.1,
result_values.0,
);
let signal_source_voltage: f64 =
voltage_from_signal_source(result_values.3, reactive_resist, full_resistance);
let coef: f64 = coef_of_signal_reduce(signal_source_voltage, signal_source_voltage);
match label {
"f, МГц" => {
cell_label.set_label(&cell_value.value().to_string());
}
"Xc, пФ" => {
cell_label.set_label(&reactive_resist.to_string());
}
"Vп, мВ" => {
cell_label.set_label(&signal_source_voltage.to_string());
}
"ζ" => {
cell_label.set_label(&coef.to_string());
}
_ => {}
}
});
let column = ColumnViewColumn::builder()
.title(label)
.expand(true)
.resizable(false)
.factory(&factory)
.build();
result_table.append_column(&column);
}
let scrollable_table = ScrolledWindow::builder()
.vexpand(true)
.child(&result_table)
.build();
let table = Frame::builder()
.child(&scrollable_table)
.vexpand(true)
.build();
EventHandler::new(calculate_button.clone(), move |_| {
match parse_fields(all_inputs.clone()) {
Ok(results) => {
values.set(results);
let tree_iter: TreeIter = cloned_model.append(&ResultValue::new(results.5));
last_query.append(tree_iter);
}
Err(error) => {
let error_kind: Option<&str> = get_error_message(error);
info_bar.set_text_label(error_kind);
info_bar.show_infobar(5u64);
}
}
})
.on_click();
wrapper.append(&input_block);
wrapper.append(&calculate_button);
wrapper.append(&table);
}