Skip to content

Commit 8727a30

Browse files
committed
implement labels of loaded files
1 parent 994d1d0 commit 8727a30

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/data.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct DataContainer {
4848
pub absolute_time: Vec<f64>,
4949
pub dataset: Vec<Vec<f32>>,
5050
pub raw_traffic: Vec<Packet>,
51+
pub loaded_from_file: bool,
5152
}
5253

5354
impl Default for DataContainer {
@@ -57,6 +58,7 @@ impl Default for DataContainer {
5758
absolute_time: vec![],
5859
dataset: vec![vec![]],
5960
raw_traffic: vec![],
61+
loaded_from_file: false,
6062
}
6163
}
6264
}

src/gui.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::f32;
22
use std::cmp::max;
33
use std::ops::RangeInclusive;
44
use std::path::PathBuf;
5-
use std::sync::mpsc::Sender;
5+
use std::sync::mpsc::{Receiver, Sender};
66
use std::sync::{Arc, RwLock};
77
use std::time::Duration;
88

@@ -128,6 +128,7 @@ pub struct MyApp {
128128
data_lock: Arc<RwLock<DataContainer>>,
129129
save_tx: Sender<FileOptions>,
130130
load_tx: Sender<PathBuf>,
131+
load_names_rx: Receiver<Vec<String>>,
131132
send_tx: Sender<String>,
132133
clear_tx: Sender<bool>,
133134
history: Vec<String>,
@@ -157,6 +158,7 @@ impl MyApp {
157158
gui_conf: GuiSettingsContainer,
158159
save_tx: Sender<FileOptions>,
159160
load_tx: Sender<PathBuf>,
161+
load_names_rx: Receiver<Vec<String>>,
160162
send_tx: Sender<String>,
161163
clear_tx: Sender<bool>,
162164
) -> Self {
@@ -217,6 +219,7 @@ impl MyApp {
217219
data_lock,
218220
save_tx,
219221
load_tx,
222+
load_names_rx,
220223
send_tx,
221224
clear_tx,
222225
plotting_range: usize::MAX,
@@ -317,8 +320,20 @@ impl MyApp {
317320
if let Ok(read_guard) = self.data_lock.read() {
318321
self.data = read_guard.clone();
319322
}
323+
324+
if self.data.loaded_from_file && self.file_opened {
325+
if let Ok(labels) =
326+
self.load_names_rx.recv_timeout(Duration::from_millis(10))
327+
{
328+
self.labels = labels;
329+
self.colors = (0..max(self.labels.len(), 1))
330+
.map(|i| COLORS[i % COLORS.len()])
331+
.collect();
332+
self.color_vals = (0..max(self.labels.len(), 1)).map(|_| 0.0).collect();
333+
}
334+
}
320335
if self.serial_devices.number_of_plots[self.device_idx] > 0 {
321-
if self.data.dataset.len() != self.labels.len() {
336+
if self.data.dataset.len() != self.labels.len() && !self.file_opened {
322337
self.labels = (0..max(self.data.dataset.len(), 1))
323338
.map(|i| format!("Column {i}"))
324339
.collect();

src/io.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub fn open_from_csv(
6262
}
6363
}
6464

65+
data.loaded_from_file = true;
66+
6567
Ok(())
6668
}
6769

src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fn main_thread(
5353
raw_data_rx: Receiver<Packet>,
5454
save_rx: Receiver<FileOptions>,
5555
load_rx: Receiver<PathBuf>,
56+
load_names_tx: Sender<Vec<String>>,
5657
clear_rx: Receiver<bool>,
5758
) {
5859
// reads data from mutex, samples and saves if needed
@@ -70,6 +71,7 @@ fn main_thread(
7071
}
7172
if !file_opened {
7273
if let Ok(packet) = raw_data_rx.recv_timeout(Duration::from_millis(1)) {
74+
data.loaded_from_file = false;
7375
if !packet.payload.is_empty() {
7476
sync_tx.send(true).expect("unable to send sync tx");
7577
data.raw_traffic.push(packet.clone());
@@ -114,6 +116,9 @@ fn main_thread(
114116
match open_from_csv(&mut data, &mut file_options) {
115117
Ok(_) => {
116118
log::info!("opened {:?}", fp);
119+
load_names_tx
120+
.send(file_options.names)
121+
.expect("unable to send names on channel after loading");
117122
}
118123
Err(err) => {
119124
file_opened = false;
@@ -170,6 +175,8 @@ fn main() {
170175

171176
let (save_tx, save_rx): (Sender<FileOptions>, Receiver<FileOptions>) = mpsc::channel();
172177
let (load_tx, load_rx): (Sender<PathBuf>, Receiver<PathBuf>) = mpsc::channel();
178+
let (loaded_names_tx, loaded_names_rx): (Sender<Vec<String>>, Receiver<Vec<String>>) =
179+
mpsc::channel();
173180
let (send_tx, send_rx): (Sender<String>, Receiver<String>) = mpsc::channel();
174181
let (clear_tx, clear_rx): (Sender<bool>, Receiver<bool>) = mpsc::channel();
175182
let (raw_data_tx, raw_data_rx): (Sender<Packet>, Receiver<Packet>) = mpsc::channel();
@@ -198,6 +205,7 @@ fn main() {
198205
raw_data_rx,
199206
save_rx,
200207
load_rx,
208+
loaded_names_tx,
201209
clear_rx,
202210
);
203211
});
@@ -245,6 +253,7 @@ fn main() {
245253
gui_settings,
246254
save_tx,
247255
load_tx,
256+
loaded_names_rx,
248257
send_tx,
249258
clear_tx,
250259
)))

0 commit comments

Comments
 (0)