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}
37
38impl Deref for PlotState {
39    type Target = ViewState;
40
41    fn deref(&self) -> &Self::Target {
42        &self.inner
43    }
44}
45
46impl DerefMut for PlotState {
47    fn deref_mut(&mut self) -> &mut Self::Target {
48        &mut self.inner
49    }
50}
51
52impl PlotState {
53    /// The cached extent of `series`, if it is current.
54    pub(super) fn cached_bounds(&self, series: &Series<'_>, x_scale: Scale, y_scale: Scale) -> Bounds {
55        let key = series.fingerprint(x_scale, y_scale);
56        self.cached_extent(key)
57            .unwrap_or_else(|| series.bounds(x_scale, y_scale))
58    }
59
60    /// Every distance and area measurement, in the order they were added.
61    pub fn measurements(&self) -> &[PlotMeasurement] {
62        &self.measurements
63    }
64
65    /// Add a measurement. The plot draws it on the next frame.
66    pub fn add_measurement(&mut self, measurement: PlotMeasurement) {
67        self.measurements.push(measurement);
68    }
69
70    /// Remove the measurement at `index`.
71    pub fn remove_measurement(&mut self, index: usize) -> Option<PlotMeasurement> {
72        (index < self.measurements.len()).then(|| self.measurements.remove(index))
73    }
74
75    /// Remove the newest measurement and return it.
76    pub fn pop_measurement(&mut self) -> Option<PlotMeasurement> {
77        self.measurements.pop()
78    }
79
80    /// Remove every measurement.
81    pub fn clear_measurements(&mut self) {
82        self.measurements.clear();
83    }
84
85    /// The filter/transform pipeline.
86    pub fn pipeline(&self) -> &Pipeline {
87        &self.inner.pipeline
88    }
89
90    /// The filter/transform pipeline, mutably.
91    pub fn pipeline_mut(&mut self) -> &mut Pipeline {
92        &mut self.inner.pipeline
93    }
94
95    /// Enable or disable one filter.
96    pub fn toggle_filter(&mut self, filter: FilterKind) {
97        self.inner.pipeline.toggle_filter(filter);
98    }
99
100    /// Enable or disable one filter explicitly.
101    pub fn set_filter(&mut self, filter: FilterKind, enabled: bool) {
102        self.inner.pipeline.set_filter(filter, enabled);
103    }
104
105    /// Replace the transform. A new transform clears measurements because the data
106    /// domain changed.
107    pub fn set_transform(&mut self, transform: Option<TransformKind>) {
108        if self.inner.pipeline.transform != transform {
109            self.inner.pipeline.set_transform(transform);
110            self.measurements.clear();
111        }
112    }
113
114    /// Disable every filter and transform.
115    pub fn clear_analysis(&mut self) {
116        self.inner.pipeline.clear();
117    }
118
119    /// Replace the live measurement settings and mark them user-edited.
120    pub fn set_measurement(&mut self, measurement: MeasurementConfig) {
121        self.measurement = measurement;
122        self.inner.measurement_customized = true;
123    }
124
125    /// Clear every painted overlay: measurements, selection, annotations, and analysis.
126    pub fn clear_overlays(&mut self) {
127        self.measurements.clear();
128        self.inner.selection = None;
129        self.annotations.clear();
130        self.inner.pipeline.clear();
131    }
132
133    /// Whether a clear action would remove anything.
134    pub fn has_overlays(&self) -> bool {
135        !self.measurements.is_empty() || !self.annotations.is_empty() || self.inner.has_overlays()
136    }
137}