Skip to main content

crisol_plot/plot/
state.rs

1//! The interactive state of one plot: the egui-free view state from
2//! `crisol-plot-core` plus the annotations, measurements, and live drag this crate
3//! paints.
4//!
5//! The application owns it, like every other component state.
6
7use std::ops::{Deref, DerefMut};
8
9use super::PlotAnnotation;
10use super::axis::{Bounds, Scale};
11use super::filter::FilterKind;
12use super::measure::{MeasurementConfig, PlotMeasurement};
13use super::pipeline::Pipeline;
14use super::series::Series;
15use super::transforms::TransformKind;
16use super::types::Drag;
17use crisol_plot_core::state::ViewState;
18
19/// The interactive state of one plot. Keep it in the application model.
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21#[derive(Clone, Debug, Default)]
22pub struct PlotState {
23    #[cfg_attr(feature = "serde", serde(flatten))]
24    inner: ViewState,
25    /// Text notes, arrows, spans, and regions.
26    #[cfg_attr(feature = "serde", serde(default))]
27    pub annotations: Vec<PlotAnnotation>,
28    /// Distance and area measurements drawn over the data.
29    #[cfg_attr(feature = "serde", serde(default))]
30    pub measurements: Vec<PlotMeasurement>,
31    /// Live measurement settings. The chart config seeds them until the user edits them.
32    #[cfg_attr(feature = "serde", serde(default))]
33    pub measurement: MeasurementConfig,
34    #[cfg_attr(feature = "serde", serde(skip))]
35    pub(super) drag: Option<Drag>,
36    /// Filter whose parameters are shown in the analysis menu.
37    #[cfg_attr(feature = "serde", serde(skip))]
38    pub(super) analysis_filter: Option<FilterKind>,
39}
40
41impl Deref for PlotState {
42    type Target = ViewState;
43
44    fn deref(&self) -> &Self::Target {
45        &self.inner
46    }
47}
48
49impl DerefMut for PlotState {
50    fn deref_mut(&mut self) -> &mut Self::Target {
51        &mut self.inner
52    }
53}
54
55impl PlotState {
56    /// The cached extent of `series`, if it is current.
57    pub(super) fn cached_bounds(&self, series: &Series<'_>, x_scale: Scale, y_scale: Scale) -> Bounds {
58        let key = series.fingerprint(x_scale, y_scale);
59        self.cached_extent(key)
60            .unwrap_or_else(|| series.bounds(x_scale, y_scale))
61    }
62
63    /// Every distance and area measurement, in the order they were added.
64    pub fn measurements(&self) -> &[PlotMeasurement] {
65        &self.measurements
66    }
67
68    /// Add a measurement. The plot draws it on the next frame.
69    pub fn add_measurement(&mut self, measurement: PlotMeasurement) {
70        self.measurements.push(measurement);
71    }
72
73    /// Remove the measurement at `index`.
74    pub fn remove_measurement(&mut self, index: usize) -> Option<PlotMeasurement> {
75        (index < self.measurements.len()).then(|| self.measurements.remove(index))
76    }
77
78    /// Remove the newest measurement and return it.
79    pub fn pop_measurement(&mut self) -> Option<PlotMeasurement> {
80        self.measurements.pop()
81    }
82
83    /// Remove every measurement.
84    pub fn clear_measurements(&mut self) {
85        self.measurements.clear();
86    }
87
88    /// The filter/transform pipeline.
89    pub fn pipeline(&self) -> &Pipeline {
90        &self.inner.pipeline
91    }
92
93    /// The filter/transform pipeline, mutably.
94    pub fn pipeline_mut(&mut self) -> &mut Pipeline {
95        &mut self.inner.pipeline
96    }
97
98    /// Enable or disable one filter.
99    pub fn toggle_filter(&mut self, filter: FilterKind) {
100        self.inner.pipeline.toggle_filter(filter);
101    }
102
103    /// Enable or disable one filter explicitly.
104    pub fn set_filter(&mut self, filter: FilterKind, enabled: bool) {
105        self.inner.pipeline.set_filter(filter, enabled);
106    }
107
108    /// Replace the transform. A new transform clears measurements because the data
109    /// domain changed.
110    pub fn set_transform(&mut self, transform: Option<TransformKind>) {
111        if self.inner.pipeline.transform != transform {
112            self.inner.pipeline.set_transform(transform);
113            self.measurements.clear();
114        }
115    }
116
117    /// Disable every filter and transform.
118    pub fn clear_analysis(&mut self) {
119        self.inner.pipeline.clear();
120    }
121
122    /// Replace the live measurement settings and mark them user-edited.
123    pub fn set_measurement(&mut self, measurement: MeasurementConfig) {
124        self.measurement = measurement;
125        self.inner.measurement_customized = true;
126    }
127
128    /// Clear every painted overlay: measurements, selection, annotations, and analysis.
129    pub fn clear_overlays(&mut self) {
130        self.measurements.clear();
131        self.inner.selection = None;
132        self.annotations.clear();
133        self.inner.pipeline.clear();
134    }
135
136    /// Whether a clear action would remove anything.
137    pub fn has_overlays(&self) -> bool {
138        !self.measurements.is_empty() || !self.annotations.is_empty() || self.inner.has_overlays()
139    }
140}