diff --git a/src/graphmanager/graphview.rs b/src/graphmanager/graphview.rs index 2c43a8f..c0a8fcc 100644 --- a/src/graphmanager/graphview.rs +++ b/src/graphmanager/graphview.rs @@ -281,6 +281,17 @@ mod imp { <()>::static_type().into(), ) .build(), + Signal::builder( + "port-added", + // returns graph ID, Node ID, Port ID + &[ + u32::static_type().into(), + u32::static_type().into(), + u32::static_type().into(), + ], + <()>::static_type().into(), + ) + .build(), ] }); SIGNALS.as_ref() @@ -347,7 +358,8 @@ mod imp { let nodes = self.nodes.borrow(); let from_node = nodes.get(&link.node_from)?; - let from_port = from_node.port(&link.port_from)?; + + let from_port = from_node.port(link.port_from)?; let (mut fx, mut fy, fw, fh) = ( from_port.allocation().x(), @@ -363,7 +375,7 @@ mod imp { } let to_node = nodes.get(&link.node_to)?; - let to_port = to_node.port(&link.port_to)?; + let to_port = to_node.port(link.port_to)?; let (mut tx, mut ty, th) = ( to_port.allocation().x(), @@ -454,41 +466,39 @@ impl GraphView { self.emit_by_name::<()>("node-added", &[&private.id.get(), &node_id]); self.graph_updated(); } - + /// Create a new node with id + /// + pub fn create_node_with_id(&self, id: u32, name: &str, node_type: NodeType) -> Node { + Node::new(id, name, node_type) + } + /// Create a new node with id + /// + pub fn create_node(&self, name: &str, node_type: NodeType) -> Node { + let id = self.next_node_id(); + self.create_node_with_id(id, name, node_type) + } /// Create a new node and add it to the graphview with input/output port number. /// pub fn create_node_with_port( &self, - id: u32, name: &str, node_type: NodeType, input: u32, output: u32, - ) { - let node = Node::new(id, name, node_type); - self.add_node(node); + ) -> Node { + let mut node = self.create_node(name, node_type); + let _i = 0; for _i in 0..input { - let port_id = self.next_port_id(); - self.add_port( - id, - port_id, - "in", - PortDirection::Input, - PortPresence::Always, - ); + let port = self.create_port("in", PortDirection::Input, PortPresence::Always); + self.add_port_to_node(&mut node, port); } let _i = 0; for _i in 0..output { - let port_id = self.next_port_id(); - self.add_port( - id, - port_id, - "out", - PortDirection::Output, - PortPresence::Always, - ); + let port = self.create_port("out", PortDirection::Output, PortPresence::Always); + self.add_port_to_node(&mut node, port); } + node } /// Remove node from the graphview @@ -560,30 +570,37 @@ impl GraphView { } // Port + pub fn create_port_with_id( + &self, + id: u32, + name: &str, + direction: PortDirection, + presence: PortPresence, + ) -> Port { + Port::new(id, name, direction, presence) + } + /// Add the port with id from node with id. + /// + pub fn create_port( + &self, + name: &str, + direction: PortDirection, + presence: PortPresence, + ) -> Port { + let id = self.next_port_id(); + info!("Create a port with port id {}", id); + + self.create_port_with_id(id, name, direction, presence) + } /// Add the port with id from node with id. /// - pub fn add_port( - &self, - node_id: u32, - port_id: u32, - port_name: &str, - port_direction: PortDirection, - port_nature: PortPresence, - ) { + pub fn add_port_to_node(&self, node: &mut Node, port: Port) { let private = imp::GraphView::from_instance(self); - info!( - "adding a port with port id {} to node id {}", - port_id, node_id - ); - if let Some(node) = private.nodes.borrow_mut().get_mut(&node_id) { - node.add_port(port_id, port_name, port_direction, port_nature); - } else { - error!( - "Node with id {} not found when trying to add port with id {} to graph", - node_id, port_id - ); - } + let port_id = port.id(); + node.add_port(port); + + self.emit_by_name::<()>("port-added", &[&private.id.get(), &node.id(), &port_id]); } /// Check if the port with id from node with id can be removed. @@ -804,7 +821,9 @@ impl GraphView { let parser = EventReader::new(file); let mut current_node: Option = None; + let mut current_node_properties: HashMap = HashMap::new(); let mut current_port: Option = None; + let mut current_port_properties: HashMap = HashMap::new(); let mut current_link: Option = None; for e in parser { match e { @@ -842,7 +861,7 @@ impl GraphView { let pos_y: &String = attrs .get::(&String::from("pos_y")) .unwrap_or(&default_value); - let node = Node::new( + let node = self.create_node_with_id( id.parse::().unwrap(), name, NodeType::from_str(node_type.as_str()), @@ -860,10 +879,11 @@ impl GraphView { let value: &String = attrs .get::(&String::from("value")) .expect("Unable to find property value"); - if let Some(port) = current_port.clone() { - port.add_property(name.clone(), value.clone()); - } else if let Some(node) = current_node.clone() { - node.add_property(name.clone(), value.clone()); + if current_port.is_some() { + current_port_properties.insert(name.to_string(), value.to_string()); + } else if current_node.is_some() { + info!("add property to node {}={}", name, value); + current_node_properties.insert(name.to_string(), value.to_string()); } } "Port" => { @@ -880,7 +900,7 @@ impl GraphView { let presence: &String = attrs .get::(&String::from("presence")) .unwrap_or(&default_value); - current_port = Some(Port::new( + current_port = Some(self.create_port_with_id( id.parse::().unwrap(), name, PortDirection::from_str(direction), @@ -929,10 +949,13 @@ impl GraphView { if let Some(node) = current_node { let id = node.id(); let position = node.position(); + node.update_properties(¤t_node_properties); + current_node_properties.clear(); self.add_node(node); if let Some(node) = self.node(id) { self.move_node(&node.upcast(), position.0, position.1); } + self.update_current_node_id(id); } current_node = None; @@ -940,16 +963,15 @@ impl GraphView { "Property" => {} "Port" => { if let Some(port) = current_port { - let node = current_node.clone(); - let id = port.id(); - node.expect("No current node, error...").add_port( - id, - &port.name(), - port.direction(), - port.presence(), - ); - self.update_current_port_id(id); + if let Some(mut node) = current_node.clone() { + let id = port.id(); + port.update_properties(¤t_port_properties); + self.add_port_to_node(&mut node, port); + current_port_properties.clear(); + self.update_current_port_id(id); + } } + current_port = None; } "Link" => { diff --git a/src/graphmanager/node.rs b/src/graphmanager/node.rs index 464e477..0393ad7 100644 --- a/src/graphmanager/node.rs +++ b/src/graphmanager/node.rs @@ -22,10 +22,7 @@ use gtk::prelude::*; use gtk::subclass::prelude::*; use log::trace; -use super::Port; -use super::PropertyExt; -use super::SelectionExt; -use super::{PortDirection, PortPresence}; +use super::{Port, PortDirection, PortPresence, PropertyExt, SelectionExt}; use std::cell::{Cell, Ref, RefCell}; use std::collections::HashMap; @@ -162,15 +159,8 @@ impl Node { /// Add a new port to the node /// - pub fn add_port( - &mut self, - id: u32, - name: &str, - direction: PortDirection, - presence: PortPresence, - ) { + pub fn add_port(&mut self, port: Port) { let private = imp::Node::from_instance(self); - let port = Port::new(id, name, direction, presence); match port.direction() { PortDirection::Input => { private @@ -186,8 +176,7 @@ impl Node { } _ => panic!("Port without direction"), } - - private.ports.borrow_mut().insert(id, port); + private.ports.borrow_mut().insert(port.id(), port); } /// Retrieves all ports as an hashmap @@ -211,9 +200,9 @@ impl Node { /// Retrieves the port with id /// - pub fn port(&self, id: &u32) -> Option { + pub fn port(&self, id: u32) -> Option { let private = imp::Node::from_instance(self); - private.ports.borrow().get(id).cloned() + private.ports.borrow().get(&id).cloned() } /// Check if we can remove a port dependending on PortPrensence attribute diff --git a/src/graphmanager/port.rs b/src/graphmanager/port.rs index 78f2f08..7eef6fc 100644 --- a/src/graphmanager/port.rs +++ b/src/graphmanager/port.rs @@ -22,7 +22,7 @@ use gtk::{ prelude::*, subclass::prelude::*, }; -use log::trace; +use log::info; use std::cell::RefCell; use std::cell::{Cell, Ref}; use std::collections::HashMap; @@ -227,7 +227,7 @@ impl PropertyExt for Port { /// fn add_property(&self, name: &str, value: &str) { let private = imp::Port::from_instance(self); - trace!("property name={} updated with value={}", name, value); + info!("property name={} updated with value={}", name, value); private .properties .borrow_mut()