1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*Copyright (c) 2022 Diego da Silva Lima. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, see <https://opensource.org/licenses/MIT>.*/
use gdk4::RGBA;
use cairo::Context;
use super::super::context_mapper::ContextMapper;
use std::collections::HashMap;
use super::*;
use std::cmp::*;
use std::default::Default;
use super::super::{MappingProperty, LineProperty};
use std::borrow::Borrow;
use crate::model::MappingType;
#[derive(Debug, Clone)]
pub struct LineMapping {
color : RGBA,
x : Vec<f64>,
y : Vec<f64>,
width : f64,
dash_n : i32,
col_names : [String; 2],
source : String
}
impl Default for LineMapping {
fn default() -> Self {
Self {
color : RGBA::BLACK,
x : Vec::new(),
y : Vec::new(),
width : 1.0,
dash_n : 1,
col_names : [String::new(), String::new()],
source : String::new()
}
}
}
impl LineMapping {
pub fn width(mut self, w : f64) -> Self {
self.width = w;
self
}
pub fn color(mut self, color : String) -> Self {
self.color = color.parse().unwrap();
self
}
pub fn dash_n(mut self, dash_n : i32) -> Self {
self.dash_n = dash_n;
self
}
// TODO rename to data.
pub fn map<D>(x : impl IntoIterator<Item=D>, y : impl IntoIterator<Item=D>) -> Self
where
D : Borrow<f64>
{
let mut line : LineMapping = Default::default();
let x : Vec<_> = x.into_iter().map(|d| *d.borrow() ).collect();
let y : Vec<_> = y.into_iter().map(|d| *d.borrow() ).collect();
line.update_data(vec![x, y]);
line
}
/*pub fn new(node : &Node) -> Result<Self, String> {
let color = gdk::RGBA{
red:0.0,
green:0.0,
blue:0.0,
alpha : 0.0
};
let width = 1.0;
let dash_n = 1;
let x = Vec::<f64>::new();
let y = Vec::<f64>::new();
let col_names = [
String::from("None"),
String::from("None")
];
let source = String::new();
let mut mapping = LineMapping{color, x, y, width, dash_n, col_names, source};
mapping.update_layout(node)?;
Ok(mapping)
}*/
fn build_dash(n : i32) -> Vec<f64> {
let dash_sz = 10.0 / (n as f64);
let mut dashes = Vec::<f64>::new();
for _i in 1..n {
dashes.push(dash_sz);
}
dashes
}
}
impl Mapping for LineMapping {
fn update(&mut self, prop : MappingProperty) -> bool {
match prop {
MappingProperty::Line(line) => {
match line {
LineProperty::Color(col) => { self.color = col.parse().unwrap() },
LineProperty::Width(w) => { self.width = w },
LineProperty::Dash(d) => { self.dash_n = d },
LineProperty::X(x) => { self.x = x },
LineProperty::Y(y) => { self.y = y }
}
true
},
_ => false
}
}
fn clone_boxed(&self) -> Box<dyn Mapping> {
Box::new(self.clone())
}
fn draw(&self, mapper : &ContextMapper, ctx : &Context) -> Result<(), Box<dyn Error>> {
//println!("{:?}", self);
if self.x.len() < 2 || self.y.len() < 2 {
return Ok(());
}
ctx.save()?;
ctx.set_source_rgb(
self.color.red().into(),
self.color.green().into(),
self.color.blue().into()
);
ctx.set_line_width(self.width);
let dashes = LineMapping::build_dash(self.dash_n);
ctx.set_dash(&dashes[..], 0.0);
//println!("Received for drawing {:?} {:?}", self.x, self.y);
let zip_xy = self.x[1..].iter().zip(self.y[1..].iter());
/*let (mut prev_x, mut prev_y) = match zip_xy.next() {
Some((prev_x, prev_y)) => (prev_x, prev_y),
None => {
ctx.restore();
return;
}
};*/
let from = mapper.map(self.x[0], self.y[0]);
ctx.move_to(from.x, from.y);
for (curr_x, curr_y) in zip_xy {
if mapper.check_bounds(*curr_x, *curr_y) {
// let from = mapper.map(*prev_x, *prev_y);
let to = mapper.map(*curr_x, *curr_y);
ctx.line_to(to.x, to.y);
} else {
//println!("Out of bounds mapping");
}
// println!("Now drawing to {:?} {:?}", to.x, to.y);
// prev_x = curr_x;
// prev_y = curr_y;
}
ctx.stroke()?;
ctx.restore()?;
Ok(())
}
fn update_data(&mut self, values : Vec<Vec<f64>>) {
self.x = values[0].clone();
self.y = values[1].clone();
}
fn update_from_json(&mut self, rep : crate::model::Mapping) {
if let Some(width) = rep.width {
self.width = width;
}
if let Some(dash_n) = rep.spacing {
self.dash_n = dash_n;
}
if let Some(color) = rep.color.clone() {
self.color = color.parse().unwrap();
}
// println!("Mapping json rep: {:?}", rep);
super::update_data_pair_from_json(&mut self.x, &mut self.y, rep);
// TODO check properties of other mappings are None.
}
fn update_extra_data(&mut self, _values : Vec<Vec<String>>) {
// println!("Mapping has no extra data");
}
/*fn update_layout(&mut self, node : &Node) -> Result<(), String> {
let props = utils::children_as_hash(node, "property");
self.color = props.get("color")
.ok_or(format!("color property not found"))?
.parse()
.map_err(|_| format!("Unable to parse color property"))?;
self.width = props.get("width")
.ok_or(format!("width property not found"))?
.parse()
.map_err(|_| format!("Unable to parse width property"))?;
self.dash_n = props.get("dash")
.ok_or(format!("dash property not found"))?
.parse()
.map_err(|_| format!("Unable to parse dash property"))?;
self.col_names[0] = props.get("x")
.ok_or(format!("x property not found"))?
.clone();
self.col_names[1] = props.get("y")
.ok_or(format!("y property not found"))?
.clone();
self.source = props.get("source")
.ok_or(format!("Source property not found"))?
.clone();
Ok(())
}*/
fn properties(&self) -> HashMap<String, String> {
let mut properties = MappingType::Line.default_hash();
if let Some(e) = properties.get_mut("color") {
*e = self.color.to_string();
}
if let Some(e) = properties.get_mut("width") {
*e = self.width.to_string();
}
if let Some(e) = properties.get_mut("dash"){
*e = self.dash_n.to_string();
}
if let Some(e) = properties.get_mut("x") {
*e = self.col_names[0].clone();
}
if let Some(e) = properties.get_mut("y") {
*e = self.col_names[1].clone();
}
if let Some(e) = properties.get_mut("source") {
*e = self.source.clone();
}
properties
}
fn mapping_type(&self) -> String {
"line".into()
}
fn get_col_name(&self, col : &str) -> String {
match col {
"x" => self.col_names[0].clone(),
"y" => self.col_names[1].clone(),
_ => String::new()
}
}
fn get_ordered_col_names(&self) -> Vec<(String, String)> {
vec![
(String::from("x"), self.get_col_name("x")),
(String::from("y"), self.get_col_name("y"))
]
}
fn get_hash_col_names(&self) -> HashMap<String, String> {
let mut cols = HashMap::new();
cols.insert("x".into(), self.col_names[0].clone());
cols.insert("y".into(), self.col_names[1].clone());
cols
}
fn set_col_name(&mut self, col : &str, name : &str) {
match col {
"x" => { self.col_names[0] = name.into(); },
"y" => { self.col_names[1] = name.into(); },
_ => { }
}
}
fn set_col_names(&mut self, cols : Vec<String>) -> Result<(), &'static str> {
if cols.len() != 2 {
Err("Wrong number of columns.")
} else {
self.set_col_name("x", &cols[0]);
self.set_col_name("y", &cols[1]);
Ok(())
}
}
fn data_limits(&self) -> Option<((f64, f64), (f64, f64))> {
let xmin = self.x.iter().min_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal) )?;
let xmax = self.x.iter().max_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal))?;
let ymin = self.y.iter().min_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal))?;
let ymax = self.y.iter().max_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal))?;
Some(((*xmin, *xmax), (*ymin, *ymax)))
}
fn set_source(&mut self, source : String) {
self.source = source;
}
fn get_source(&self) -> String {
self.source.clone()
}
}