//! Size-independent Cairo drawing of the synthetic bedside monitor. use gtk::cairo::{Context, FontSlant, FontWeight, LineCap, LineJoin}; use crate::model::{mean_arterial_pressure, EcgRhythm, Parameters, PulseConduction}; use crate::simulation::{Sample, Simulator}; type Color = (f64, f64, f64); const BG: Color = (0.0, 0.0, 0.0); const GREEN: Color = (0.26, 1.0, 0.39); const YELLOW: Color = (1.0, 0.85, 0.17); const RED: Color = (1.0, 0.30, 0.31); const LIGHT_GREY: Color = (0.80, 0.84, 0.87); const MUTED: Color = (0.42, 0.46, 0.49); const GRID: Color = (0.10, 0.14, 0.13); #[derive(Clone, Copy)] enum Channel { Ecg, Pleth, Art, Capno } fn visible_channels(p: &Parameters) -> Vec<(Channel, &'static str, Color)> { let mut channels = Vec::with_capacity(4); if p.show_ecg { channels.push((Channel::Ecg, "II / ECG", GREEN)); } if p.show_spo2 { channels.push((Channel::Pleth, "SpO₂ / PLETH", YELLOW)); } if p.show_art { channels.push((Channel::Art, "ART", RED)); } if p.show_capno { channels.push((Channel::Capno, "CO₂", LIGHT_GREY)); } channels } pub fn draw(cr: &Context, width: i32, height: i32, p: &Parameters, sim: &Simulator) { let w = f64::from(width.max(1)); let h = f64::from(height.max(1)); set_color(cr, BG); let _ = cr.paint(); let pad = (w.min(h) * 0.018).clamp(6.0, 20.0); let header = (h * 0.083).clamp(32.0, 68.0); let footer = if p.show_nibp { (h * 0.13).clamp(52.0, 102.0) } else { 0.0 }; let channels = visible_channels(p); let row_height = ((h - header - footer - pad).max(20.0)) / (channels.len().max(1) as f64); let sidebar = (w * 0.235).clamp(125.0, 370.0).min(w * 0.44); let split_x = w - sidebar - pad; let trace_left = pad + 6.0; let trace_right = (split_x - pad).max(trace_left + 40.0); let text_scale = (w.min(h * 1.7) / 850.0).clamp(0.65, 1.5); text(cr, "PATIENT MONITOR", pad + 5.0, header * 0.58, 18.0 * text_scale, LIGHT_GREY, true); if w >= 850.0 { text(cr, "SIMULATION", split_x - 128.0 * text_scale, header * 0.58, 11.0 * text_scale, MUTED, false); } stroke_line(cr, pad, header, w - pad, header, MUTED, 0.8); stroke_line(cr, split_x, header, split_x, h - footer, MUTED, 0.9); for (index, (channel, title, color)) in channels.iter().copied().enumerate() { let top = header + index as f64 * row_height; let bottom = top + row_height; stroke_line(cr, pad, bottom, w - pad, bottom, MUTED, 0.65); text(cr, title, trace_left + 4.0, top + row_height * 0.17, (row_height * 0.11).clamp(10.0, 17.0), color, false); draw_trace(cr, channel, color, trace_left, trace_right, top + row_height * 0.25, bottom - row_height * 0.06, p, sim); draw_vitals(cr, channel, color, split_x + pad, top, row_height, sidebar - 2.0 * pad, p, sim); } if p.show_nibp { let footer_top = h - footer; text(cr, "NIBP", pad + 6.0, footer_top + footer * 0.32, (footer * 0.21).clamp(12.0, 20.0), LIGHT_GREY, false); text(cr, &format!("{} / {}", p.nibp_systolic, p.nibp_diastolic), pad + 6.0, footer_top + footer * 0.77, (footer * 0.42).clamp(20.0, 44.0), LIGHT_GREY, true); if w >= 510.0 { text(cr, &format!("({}) mmHg", mean_arterial_pressure(p.nibp_systolic, p.nibp_diastolic)), pad + (w * 0.28).min(260.0), footer_top + footer * 0.73, (footer * 0.24).clamp(12.0, 23.0), LIGHT_GREY, false); } if w >= 1020.0 { text(cr, "SYNTHETIC SIGNALS · NOT FOR CLINICAL USE", w * 0.56, footer_top + footer * 0.68, 10.0 * text_scale, MUTED, false); } } } fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64, top: f64, row_h: f64, width: f64, p: &Parameters, sim: &Simulator) { let label_size = (row_h * 0.135).clamp(11.0, 19.0); let number_size = (row_h * 0.375).min(width * 0.26).clamp(16.0, 59.0); let sub_size = (row_h * 0.16).clamp(11.0, 22.0); let has_capture = p.ecg_rhythm.has_qrs() && p.heart_rate > 0 && p.pulse_conduction != PulseConduction::None; let pulse_rate = if has_capture { sim.pulse_rate() } else { None }; let (label, value, info) = match ch { Channel::Ecg => ( "HR", if !p.ecg_rhythm.has_qrs() || p.heart_rate <= 0 { "--".to_owned() } else { p.heart_rate.to_string() }, format!("bpm · {}", match p.ecg_rhythm { EcgRhythm::Sinus => "Sinus", EcgRhythm::AtrialFibrillation => "AF", EcgRhythm::VentricularTachycardia => "VT", EcgRhythm::VentricularFibrillation => "VF", EcgRhythm::Asystole => "Asystole", }), ), Channel::Pleth => ( "SpO₂", if pulse_rate.is_some() { p.spo2.to_string() } else { "--".to_owned() }, format!("% PR {} bpm", pulse_rate.map_or("--".to_owned(), |pr| pr.to_string())), ), Channel::Art => ( "ART", if pulse_rate.is_some() { format!("{}/{}", p.art_systolic, p.art_diastolic) } else { "--/--".to_owned() }, if pulse_rate.is_some() { if p.pulse_pressure_variation > 0 { format!("MAP {} · PPV {}%", mean_arterial_pressure(p.art_systolic, p.art_diastolic), p.pulse_pressure_variation) } else { format!("MAP {} mmHg", mean_arterial_pressure(p.art_systolic, p.art_diastolic)) } } else { "MAP -- mmHg".to_owned() }, ), Channel::Capno => ("EtCO₂", p.etco2.to_string(), format!("mmHg RR {} /min", p.respiratory_rate)), }; text(cr, label, x, top + row_h * 0.24, label_size, color, false); // Reduce wide BP values so they fit within the sidebar. let value_size = if matches!(ch, Channel::Art) { number_size * 0.78 } else { number_size }; text(cr, &value, x, top + row_h * 0.64, value_size, color, true); text(cr, &info, x, top + row_h * 0.87, sub_size * 0.85, color, false); } #[allow(clippy::too_many_arguments)] fn draw_trace(cr: &Context, channel: Channel, color: Color, left: f64, right: f64, top: f64, bottom: f64, p: &Parameters, sim: &Simulator) { // ART is shown on an absolute fixed-pressure axis (0..200 mmHg), // unlike the automatically normalized ECG, pleth and CO2 traces. let axis_width = if matches!(channel, Channel::Art) { 36.0 } else { 0.0 }; let plot_left = left + axis_width; let trace_width = (right - plot_left).max(1.0); let trace_height = (bottom - top).max(1.0); const VISIBLE_SECONDS: f64 = 8.0; let last_t = sim.samples().back().map(|s| s.time).unwrap_or(0.0); let start_t = last_t - VISIBLE_SECONDS; let _ = cr.save(); cr.rectangle(left, top, right - left, trace_height); cr.clip(); // A subtle grid, one vertical division per second. for second in (start_t.ceil() as i64)..=(last_t.floor() as i64) { let x = right - (last_t - second as f64) / VISIBLE_SECONDS * trace_width; stroke_line(cr, x, top, x, bottom, GRID, 0.75); } if matches!(channel, Channel::Art) { for pressure in (0..=200).step_by(50) { let y = bottom - pressure as f64 / 200.0 * trace_height; stroke_line(cr, plot_left, y, right, y, GRID, 0.8); // Right-align tick labels against the axis. let label = pressure.to_string(); let label_offset = if pressure >= 100 { 23.0 } else if pressure >= 10 { 17.0 } else { 10.0 }; text(cr, &label, plot_left - label_offset, (y + 3.0).max(top + 9.0).min(bottom), 9.5, MUTED, false); } stroke_line(cr, plot_left, top, plot_left, bottom, MUTED, 0.75); } else { stroke_line(cr, left, top + trace_height * 0.5, right, top + trace_height * 0.5, GRID, 0.7); } set_color(cr, color); cr.set_line_width((trace_height * 0.016).clamp(1.2, 2.4)); cr.set_line_cap(LineCap::Round); cr.set_line_join(LineJoin::Round); let mut started = false; for s in sim.samples().iter().filter(|s| s.time >= start_t) { let x = right - (last_t - s.time) / VISIBLE_SECONDS * trace_width; let amplitude = normalized(channel, s, p); let y = bottom - amplitude * trace_height; if started { cr.line_to(x, y); } else { cr.move_to(x, y); started = true; } } if started { let _ = cr.stroke(); } let _ = cr.restore(); } fn normalized(channel: Channel, s: &Sample, p: &Parameters) -> f64 { match channel { Channel::Ecg => (0.40 + s.ecg * 0.38).clamp(0.02, 0.98), Channel::Pleth => (0.10 + s.pleth * 0.78).clamp(0.02, 0.98), Channel::Art => (s.art / 200.0).clamp(0.0, 1.0), Channel::Capno => (0.08 + (s.co2 / f64::from(p.etco2.max(1))) * 0.80) .clamp(0.02, 0.98), } } fn set_color(cr: &Context, c: Color) { cr.set_source_rgb(c.0, c.1, c.2); } fn stroke_line(cr: &Context, x1: f64, y1: f64, x2: f64, y2: f64, color: Color, width: f64) { set_color(cr, color); cr.set_line_width(width); cr.move_to(x1, y1); cr.line_to(x2, y2); let _ = cr.stroke(); } fn text(cr: &Context, value: &str, x: f64, baseline: f64, size: f64, color: Color, bold: bool) { set_color(cr, color); cr.select_font_face("Sans", FontSlant::Normal, if bold { FontWeight::Bold } else { FontWeight::Normal }); cr.set_font_size(size); cr.move_to(x, baseline); let _ = cr.show_text(value); } #[cfg(test)] mod tests { use super::*; #[test] fn toggled_channels_disappear_and_remaining_rows_reflow() { let mut p = Parameters::default(); assert_eq!(visible_channels(&p).len(), 4); p.show_ecg = false; p.show_art = false; assert_eq!(visible_channels(&p).len(), 2); assert!(matches!(visible_channels(&p)[0].0, Channel::Pleth)); assert!(matches!(visible_channels(&p)[1].0, Channel::Capno)); p.show_spo2 = false; p.show_capno = false; assert!(visible_channels(&p).is_empty()); } #[test] fn arterial_axis_is_absolute_and_fixed_to_200_mmhg() { let p = Parameters::default(); let mut s = Sample { time: 0.0, ecg: 0.0, pleth: 0.0, art: 100.0, co2: 0.0 }; assert_eq!(normalized(Channel::Art, &s, &p), 0.5); s.art = 200.0; assert_eq!(normalized(Channel::Art, &s, &p), 1.0); s.art = 0.0; assert_eq!(normalized(Channel::Art, &s, &p), 0.0); } }