This commit is contained in:
sleptworld 2024-02-22 01:28:02 +08:00
parent 749326a502
commit 01d14f5f4e
3 changed files with 62 additions and 32 deletions

View File

@ -8,9 +8,11 @@ use gtk::ResponseType::No;
use gtk::Widget; use gtk::Widget;
use relm4::factory::FactoryVecDeque; use relm4::factory::FactoryVecDeque;
use crate::components::setting_item::{SettingItem, SettingType}; use crate::components::setting_item::{SettingItem, SettingType};
use crate::config::CommonConfig;
#[derive(Debug)] #[derive(Debug)]
pub struct NewPageModel { pub struct NewPageModel {
common_setting: CommonConfig
} }
#[relm4::component(pub)] #[relm4::component(pub)]
@ -48,7 +50,7 @@ impl SimpleComponent for NewPageModel {
set_title: "Common", set_title: "Common",
add=&adw::PreferencesGroup{ add=&adw::PreferencesGroup{
#[iterate] #[iterate]
add:widgets.iter() add: model.common_setting.to_settings().iter_mut().map(|v| v.to_widget()).collect::<Vec<_>>().iter()
} }
} }
}, },
@ -61,15 +63,8 @@ impl SimpleComponent for NewPageModel {
root: &Self::Root, root: &Self::Root,
sender: relm4::ComponentSender<Self>, sender: relm4::ComponentSender<Self>,
) -> relm4::ComponentParts<Self> { ) -> relm4::ComponentParts<Self> {
let mut common_settings = Vec::new(); let config = CommonConfig::default();
common_settings.push(SettingItem::new("Name".to_string(), SettingType::Entry(None, None))); let model = NewPageModel { common_setting: config };
common_settings.push(SettingItem::new("Name".to_string(), SettingType::Entry(None, None)));
common_settings.push(SettingItem::new("Name".to_string(), SettingType::Entry(None, None)));
let widgets:Vec<_> = common_settings.iter_mut().map(|setting: &mut SettingItem| {
setting.to_widget()
}).collect();
let model = NewPageModel {};
let widgets = view_output!(); let widgets = view_output!();
ComponentParts { model, widgets } ComponentParts { model, widgets }

View File

@ -1,8 +1,8 @@
use std::rc::Rc;
use adw::prelude::*; use adw::prelude::*;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{StringList, Widget}; use gtk::{StringList, Widget};
use relm4::{factory::FactoryView, gtk, prelude::*, FactorySender}; use relm4::{factory::FactoryView, gtk, prelude::*, FactorySender};
use std::rc::Rc;
#[derive(Debug)] #[derive(Debug)]
pub enum Msg {} pub enum Msg {}
@ -15,7 +15,7 @@ pub enum SettingType {
Action, Action,
Entry(Option<String>, Option<Box<dyn Fn(&str) -> bool>>), Entry(Option<String>, Option<Box<dyn Fn(&str) -> bool>>),
Switch(bool), Switch(bool),
Spin((f64, f64,f64,f64)) Spin(f64, f64, f64, f64),
} }
pub struct SettingItem { pub struct SettingItem {
@ -27,25 +27,32 @@ impl SettingItem {
pub fn new(title: String, _type: SettingType) -> Self { pub fn new(title: String, _type: SettingType) -> Self {
Self { title, _type } Self { title, _type }
} }
pub fn to_widget(&mut self) -> impl IsA<Widget>{ pub fn to_widget(&mut self) -> Widget {
(match &mut self._type { (match &mut self._type {
SettingType::Select(selects) => { SettingType::Select(selects) => {
let w = adw::ComboRow::builder().title(&self.title).build(); let w = adw::ComboRow::builder().title(&self.title).build();
let model = StringList::new(selects.iter().map(|s| s.as_str()).collect::<Vec<_>>().as_ref()); let model = StringList::new(
selects
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.as_ref(),
);
w.set_model(Some(&model)); w.set_model(Some(&model));
w.upcast::<Widget>() w.upcast::<Widget>()
} }
SettingType::Action => { SettingType::Action => adw::EntryRow::builder()
adw::EntryRow::builder().title(&self.title).build().upcast::<Widget>() .title(&self.title)
} .build()
.upcast::<Widget>(),
SettingType::Entry(text, f) => { SettingType::Entry(text, f) => {
let w = adw::EntryRow::new(); let w = adw::EntryRow::new();
w.set_title(&self.title); w.set_title(&self.title);
let f = f.take(); let f = f.take();
if let Some(f) = f{ if let Some(f) = f {
println!("test"); println!("test");
w.connect_text_notify(move |s|{ w.connect_text_notify(move |s| {
let text =s.text(); let text = s.text();
if !f(&text) { if !f(&text) {
s.set_text(""); s.set_text("");
} }
@ -62,14 +69,18 @@ impl SettingItem {
w.set_active(*t); w.set_active(*t);
w.upcast::<Widget>() w.upcast::<Widget>()
} }
SettingType::Spin((min, max,clamb, value)) => { SettingType::Spin(min, max, clamb, value) => {
let w = adw::SpinRow::new(Some(gtk::Adjustment::), *clamb,0); let w = adw::SpinRow::new(
w.set_numeric(true); Some(&gtk::Adjustment::new(*value, *min, *max, *clamb, 0.0, 0.0)),
// w.set_range(*min, *max); *clamb,
w.set_value(*value); 0,
w.set_title(&self.title); );
w.upcast::<Widget>() w.set_numeric(true);
} // w.set_range(*min, *max);
w.set_value(*value);
w.set_title(&self.title);
w.upcast::<Widget>()
}
}) })
} }
} }

View File

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::components::{SettingItem, SettingType}; use crate::components::{SettingItem, SettingType};
#[derive(Clone, Debug)] #[derive(Clone, Debug, Default)]
pub struct CommonConfig{ pub struct CommonConfig{
pub name: String, pub name: String,
pub version: String, pub version: String,
@ -28,5 +28,29 @@ pub struct AlgorithmConfig {
info: HashMap<String, String> info: HashMap<String, String>
} }
pub fn default_common_items() { impl CommonConfig{
pub fn to_settings(&self) -> Vec<SettingItem>{
let mut settings = Vec::new();
settings.push(SettingItem::new("Name".to_string(), SettingType::Entry(Some(self.name.clone()), None)));
settings.push(SettingItem::new("Version".to_string(), SettingType::Entry(Some(self.version.clone()), None)));
settings.push(SettingItem::new("Radar Num".to_string(), SettingType::Spin(0.0, 0.0, 100.0, 1.0)));
for radar in self.radars.iter(){
settings.push(SettingItem::new("Name".to_string(), SettingType::Entry(Some(radar.name.clone()), None)));
settings.push(SettingItem::new("Type".to_string(), SettingType::Entry(Some(radar._type.clone()), None)));
settings.push(SettingItem::new("Location".to_string(), SettingType::Entry(Some(format!("{},{},{}", radar.loc[0], radar.loc[1], radar.loc[2])), None)));
settings.push(SettingItem::new("Azimuth Beam Width".to_string(), SettingType::Entry(Some(radar.az_beam_width.to_string()), None)));
settings.push(SettingItem::new("Elevation Beam Width".to_string(), SettingType::Entry(Some(radar.el_beam_width.to_string()), None)));
settings.push(SettingItem::new("Azimuth Method".to_string(), SettingType::Select(vec!["1".to_string(), "2".to_string(), "3".to_string()])));
settings.push(SettingItem::new("Elevation Method".to_string(), SettingType::Select(vec!["1".to_string(), "2".to_string(), "3".to_string()])));
settings.push(SettingItem::new("Range Limit".to_string(), SettingType::Entry(Some(radar.r_limit.to_string()), None)));
}
for algorithm in self.algorithms.iter(){
settings.push(SettingItem::new("Name".to_string(), SettingType::Entry(Some(algorithm.name.clone()), None)));
settings.push(SettingItem::new("Version".to_string(), SettingType::Entry(Some(algorithm.version.clone()), None)));
for (k, v) in algorithm.info.iter(){
settings.push(SettingItem::new(k.clone(), SettingType::Entry(Some(v.clone()), None)));
}
}
settings
}
} }