From 4c14c62c05bc3df0d003d6f68018d1673a342979 Mon Sep 17 00:00:00 2001 From: Piyush Mishra Date: Fri, 26 Feb 2021 20:48:25 +0530 Subject: [PATCH] Fixed Missing Graph Feature --- Cargo.lock | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 6 +++-- src/config.rs | 36 +++++++++++++++++++++++++ src/graph.rs | 1 + src/lib.rs | 32 ++++++++++++++++++---- src/main.rs | 6 ++++- ui.glade | 29 +++++++++++++++++--- 7 files changed, 173 insertions(+), 12 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 29c5ca2..646529a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + [[package]] name = "anyhow" version = "1.0.32" @@ -68,6 +77,17 @@ dependencies = [ "system-deps", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -124,6 +144,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "crc32fast" version = "1.2.0" @@ -471,6 +506,15 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "hermit-abi" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +dependencies = [ + "libc", +] + [[package]] name = "itertools" version = "0.9.0" @@ -847,6 +891,12 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "strum" version = "0.18.0" @@ -893,9 +943,11 @@ dependencies = [ [[package]] name = "tarangam" -version = "0.1.1" +version = "0.1.2" dependencies = [ "cairo-rs", + "clap", + "gdk", "gio", "glib", "gtk", @@ -905,6 +957,15 @@ dependencies = [ "serialport", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.20" @@ -949,12 +1010,24 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version-compare" version = "0.0.10" diff --git a/Cargo.toml b/Cargo.toml index 90d5a00..2044a50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tarangam" -version = "0.1.1" +version = "0.1.2" authors = ["PiyushXCoder "] license = "GPL 3.0" edition = "2018" @@ -12,10 +12,12 @@ keywords = ["plotter","serial","serialplotter","arduino","gtk"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] gtk = "0.9.2" +gdk = "0.13.2" gio = "0.9.1" glib = "0.10.3" png = "0.16.8" cairo-rs = { version = "0.9.1", features = ["png"] } rand = "0.8.1" libmath = "0.2.1" -serialport = "4.0.0" \ No newline at end of file +serialport = "4.0.0" +clap = "2.33.3" \ No newline at end of file diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..361b1d2 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,36 @@ +/* + This file is part of Tarangam. + + Tarangam is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Tarangam is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Tarangam. If not, see +*/ + +//! Feel free to see through codes. Application is not written to be used as a library for other app. :) + +pub struct Config { + pub ui_file: String +} + +impl Config { + pub fn generate() -> Self { + let ui_file = std::env::var("TARANGAM_UI_FILE"); + + Config { + ui_file: match ui_file { + Ok(val) => val, + Err(_) => std::env::current_exe().unwrap().parent().unwrap() + .join("ui.glade").to_str().unwrap().to_owned() + } + } + } +} \ No newline at end of file diff --git a/src/graph.rs b/src/graph.rs index 61456c9..9c24176 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -24,6 +24,7 @@ use std::rc::Rc; use std::cell::RefCell; use std::collections::HashMap; /// A single line +#[derive(Debug)] pub struct Line { pub points: Vec<(f64,f64)>, pub color: (f64, f64, f64) diff --git a/src/lib.rs b/src/lib.rs index 02f5e3a..bfaba44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,21 +58,23 @@ impl Config { } /// For communication between mpsc of graph and serial port +#[derive(Debug)] enum MessageSerialThread { Msg(String, MessageSerialThreadMsgType), Points(Vec<(String, f64)>), Status(String) } +#[derive(Debug)] enum MessageSerialThreadMsgType { Point, Log } // Building and configuring GUI -pub fn build_ui(app: >k::Application) { +pub fn build_ui(app: >k::Application, ui_file: &str) { let config = Arc::new(Mutex::new(Config::new())); - let builder = gtk::Builder::from_file(std::env::current_exe().unwrap().parent().unwrap().join("ui.glade")); + let builder = gtk::Builder::from_file(ui_file); let win = builder.get_object::("win").expect("Resource file missing!"); win.set_application(Some(app)); @@ -107,7 +109,7 @@ pub fn build_ui(app: >k::Application) { let about_menu = builder.get_object::("about_menu").expect("Resource file missing!"); let about_window = builder.get_object::("about_window").expect("Resource file missing!"); about_window.set_transient_for(Some(&win)); - + about_window.set_version(Some(env!("CARGO_PKG_VERSION"))); about_window.connect_delete_event(|win,_| { win.hide(); Inhibit(true) @@ -389,6 +391,7 @@ pub fn build_ui(app: >k::Application) { // Reciver for MessageSerialThread from the "Thread to manage Serial Port" and works accordingly let full_log = builder.get_object::("full_log").expect("Resource file missing!"); + let graph_data = builder.get_object::("graph_data").expect("Resource file missing!"); let tmp_graph = Rc::clone(&graph); receiver.attach(None, move |msg| { match msg { @@ -396,7 +399,7 @@ pub fn build_ui(app: >k::Application) { receiver_for_msg(text, &msg_type, &full_log, &log_area); }, MessageSerialThread::Points(points) => { - receiver_for_points(points, &tmp_graph); + receiver_for_points(points, &tmp_graph, &graph_data); } MessageSerialThread::Status(text) => { bar.push(1, &text); @@ -499,7 +502,7 @@ fn receiver_for_msg(text: String, msg_type: &MessageSerialThreadMsgType, full_lo } // Receives MessageSerialThread from Serial Port managing thread and add points to draw on graph -fn receiver_for_points(points: Vec<(String, f64)>, graph: &Rc>) { +fn receiver_for_points(points: Vec<(String, f64)>, graph: &Rc>, graph_data: >k::TextView) { for (line, point) in points { let mut gp = graph.borrow_mut(); @@ -511,6 +514,25 @@ fn receiver_for_points(points: Vec<(String, f64)>, graph: &Rc>) { let v = vec![(sankhya, point)]; let mut rng = rand::thread_rng(); gp.lines.insert(line, graph::Line::new(rng.gen_range(0.0..1.0), 0.0, rng.gen_range(0.0..1.0), v)); + let buf = graph_data.get_buffer().expect("Couldn't get graph_data"); + buf.set_text(""); + gp.lines.iter().for_each(|(key, line)| { + buf.insert(&mut buf.get_end_iter(), "##"); + + let tag = gtk::TextTag::new(None); + let rgba = gdk::RGBA { + red: line.color.0, + green: line.color.1, + blue: line.color.2, + alpha: 1.0 + }; + tag.set_property_background_rgba(Some(&rgba)); + tag.set_property_foreground_rgba(Some(&rgba)); + buf.get_tag_table().unwrap().add(&tag); + buf.apply_tag(&tag, &buf.get_iter_at_offset(buf.get_end_iter().get_offset() - 2), &buf.get_end_iter()); + buf.insert(&mut buf.get_end_iter(), &format!(" {}, ", key)); + }); + graph_data.queue_draw(); } } gp.redraw(); diff --git a/src/main.rs b/src/main.rs index 1338cd3..dc099a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,15 +13,19 @@ along with this program. If not, see . */ +mod config; + use gio::prelude::*; use std::env::args; fn main() { + let conf = config::Config::generate(); + let app = gtk::Application::new(Some("sng.tarangm"), Default::default()) .expect("Failed to initiate gtk"); app.connect_activate(move |app| { - tarangam::build_ui(app); + tarangam::build_ui(app, &conf.ui_file); }); app.run(&args().collect::>()); diff --git a/ui.glade b/ui.glade index b2f1e4d..38e0098 100644 --- a/ui.glade +++ b/ui.glade @@ -8,7 +8,6 @@ chitra.svg dialog Tarangam (तरंगम्) - 0.1.1 A simple serial plotter. एक सरल सीरीय्ल पलौटर। https://github.com/PiyushXCoder/Tarangam @@ -422,6 +421,30 @@ 0 + + + 35 + True + True + never + in + + + True + True + False + 5 + 7 + False + + + + + False + True + 1 + + 500 @@ -434,7 +457,7 @@ False True - 1 + 2 @@ -540,7 +563,7 @@ False True - 2 + 3