This repository has been archived on 2025-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
AIT/src/view/pages/hamming_code.rs

114 lines
3.4 KiB
Rust
Raw Normal View History

use gtk4 as gtk;
use crate::{
model::builder_traits::Product,
view::{
components::{info_bar::InfoBar, input::Input, wrapper::*},
properties::*,
},
view_utils::{hamming_code_input_utils::start_hamming_algorithm, input_utils::clearing},
};
use gtk::{glib::clone, prelude::*, *};
2024-07-27 20:59:49 +03:00
pub fn hamming_code_page(wrapper: &Box) {
let info_bar = InfoBar::get_instance();
let input_code = Input::builder()
.label("Поле ввода для кода:")
.margins(MarginData::EqualsMargin(6))
.align(Alignment::new(Align::Start, Align::Start))
.build(true, WrapMode::Word, 64);
let output_code = Input::builder()
.label("Результат:")
.margins(MarginData::EqualsMargin(6))
.align(Alignment::new(Align::Start, Align::Start))
.build(true, WrapMode::Word, 64);
output_code.get_input().set_editable(false);
let clear_input_button = Button::builder()
.set_align(Alignment::new(Align::Fill, Align::Fill))
.label("Очистка полей")
.build();
let hamming_crypt_button = Button::builder()
.set_align(Alignment::new(Align::Fill, Align::Fill))
.label("Выполнить")
.build();
let crypt_mode_switch = Switch::new();
let crypt_mode_label = Label::builder().label("Режим: кодирование").build();
let crypt_mode_wrapper = Wrapper::col_builder()
.set_align(Alignment::new(Align::Fill, Align::Center))
.hexpand(true)
.spacing(10)
.build();
let action_components_wrapper = Wrapper::col_builder()
.set_align(Alignment::new(Align::Fill, Align::Fill))
.set_margin(MarginData::MultipleMargin((0, 5, 0, 5)))
.spacing(10)
.build();
crypt_mode_switch.connect_state_set(clone!(
#[strong]
crypt_mode_label,
move |_, toggled| {
if toggled {
crypt_mode_label.set_label("Режим: проверка");
} else {
crypt_mode_label.set_label("Режим: кодирование");
}
glib::Propagation::Proceed
}
));
clear_input_button.connect_clicked(clone!(
#[strong]
input_code,
#[strong]
output_code,
move |_| {
clearing(input_code.get_input(), output_code.get_input());
}
));
hamming_crypt_button.connect_clicked(clone!(
#[strong]
input_code,
#[strong]
output_code,
#[strong]
crypt_mode_switch,
#[strong]
info_bar,
move |_| {
let result = start_hamming_algorithm(input_code.get_input(), crypt_mode_switch.state());
match result {
Ok(result) => {
output_code.get_input().buffer().set_text(result.trim_end());
}
Err(reject) => {
info_bar.set_text_label(Some(&*format!("{reject}")));
info_bar.show_infobar(5);
}
}
}
));
crypt_mode_wrapper.append(&crypt_mode_switch);
crypt_mode_wrapper.append(&crypt_mode_label);
action_components_wrapper.append(&clear_input_button);
action_components_wrapper.append(&crypt_mode_wrapper);
action_components_wrapper.append(&hamming_crypt_button);
wrapper.append(input_code.get());
wrapper.append(&action_components_wrapper);
wrapper.append(output_code.get());
}