port: Rework the way to add a port/node

Allow to tell than a port has been added with its property
This commit is contained in:
Stéphane Cerveau 2022-01-21 17:50:14 +01:00
parent f483b51c9e
commit 375d01c0cd
3 changed files with 87 additions and 76 deletions

View file

@ -281,6 +281,17 @@ mod imp {
<()>::static_type().into(), <()>::static_type().into(),
) )
.build(), .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() SIGNALS.as_ref()
@ -347,7 +358,8 @@ mod imp {
let nodes = self.nodes.borrow(); let nodes = self.nodes.borrow();
let from_node = nodes.get(&link.node_from)?; 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) = ( let (mut fx, mut fy, fw, fh) = (
from_port.allocation().x(), from_port.allocation().x(),
@ -363,7 +375,7 @@ mod imp {
} }
let to_node = nodes.get(&link.node_to)?; 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) = ( let (mut tx, mut ty, th) = (
to_port.allocation().x(), to_port.allocation().x(),
@ -454,41 +466,39 @@ impl GraphView {
self.emit_by_name::<()>("node-added", &[&private.id.get(), &node_id]); self.emit_by_name::<()>("node-added", &[&private.id.get(), &node_id]);
self.graph_updated(); 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. /// Create a new node and add it to the graphview with input/output port number.
/// ///
pub fn create_node_with_port( pub fn create_node_with_port(
&self, &self,
id: u32,
name: &str, name: &str,
node_type: NodeType, node_type: NodeType,
input: u32, input: u32,
output: u32, output: u32,
) { ) -> Node {
let node = Node::new(id, name, node_type); let mut node = self.create_node(name, node_type);
self.add_node(node);
let _i = 0; let _i = 0;
for _i in 0..input { for _i in 0..input {
let port_id = self.next_port_id(); let port = self.create_port("in", PortDirection::Input, PortPresence::Always);
self.add_port( self.add_port_to_node(&mut node, port);
id,
port_id,
"in",
PortDirection::Input,
PortPresence::Always,
);
} }
let _i = 0; let _i = 0;
for _i in 0..output { for _i in 0..output {
let port_id = self.next_port_id(); let port = self.create_port("out", PortDirection::Output, PortPresence::Always);
self.add_port( self.add_port_to_node(&mut node, port);
id,
port_id,
"out",
PortDirection::Output,
PortPresence::Always,
);
} }
node
} }
/// Remove node from the graphview /// Remove node from the graphview
@ -560,30 +570,37 @@ impl GraphView {
} }
// Port // 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. /// Add the port with id from node with id.
/// ///
pub fn add_port( pub fn add_port_to_node(&self, node: &mut Node, port: Port) {
&self,
node_id: u32,
port_id: u32,
port_name: &str,
port_direction: PortDirection,
port_nature: PortPresence,
) {
let private = imp::GraphView::from_instance(self); let private = imp::GraphView::from_instance(self);
info!( let port_id = port.id();
"adding a port with port id {} to node id {}", node.add_port(port);
port_id, node_id
); self.emit_by_name::<()>("port-added", &[&private.id.get(), &node.id(), &port_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
);
}
} }
/// Check if the port with id from node with id can be removed. /// 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 parser = EventReader::new(file);
let mut current_node: Option<Node> = None; let mut current_node: Option<Node> = None;
let mut current_node_properties: HashMap<String, String> = HashMap::new();
let mut current_port: Option<Port> = None; let mut current_port: Option<Port> = None;
let mut current_port_properties: HashMap<String, String> = HashMap::new();
let mut current_link: Option<Link> = None; let mut current_link: Option<Link> = None;
for e in parser { for e in parser {
match e { match e {
@ -842,7 +861,7 @@ impl GraphView {
let pos_y: &String = attrs let pos_y: &String = attrs
.get::<String>(&String::from("pos_y")) .get::<String>(&String::from("pos_y"))
.unwrap_or(&default_value); .unwrap_or(&default_value);
let node = Node::new( let node = self.create_node_with_id(
id.parse::<u32>().unwrap(), id.parse::<u32>().unwrap(),
name, name,
NodeType::from_str(node_type.as_str()), NodeType::from_str(node_type.as_str()),
@ -860,10 +879,11 @@ impl GraphView {
let value: &String = attrs let value: &String = attrs
.get::<String>(&String::from("value")) .get::<String>(&String::from("value"))
.expect("Unable to find property value"); .expect("Unable to find property value");
if let Some(port) = current_port.clone() { if current_port.is_some() {
port.add_property(name.clone(), value.clone()); current_port_properties.insert(name.to_string(), value.to_string());
} else if let Some(node) = current_node.clone() { } else if current_node.is_some() {
node.add_property(name.clone(), value.clone()); info!("add property to node {}={}", name, value);
current_node_properties.insert(name.to_string(), value.to_string());
} }
} }
"Port" => { "Port" => {
@ -880,7 +900,7 @@ impl GraphView {
let presence: &String = attrs let presence: &String = attrs
.get::<String>(&String::from("presence")) .get::<String>(&String::from("presence"))
.unwrap_or(&default_value); .unwrap_or(&default_value);
current_port = Some(Port::new( current_port = Some(self.create_port_with_id(
id.parse::<u32>().unwrap(), id.parse::<u32>().unwrap(),
name, name,
PortDirection::from_str(direction), PortDirection::from_str(direction),
@ -929,10 +949,13 @@ impl GraphView {
if let Some(node) = current_node { if let Some(node) = current_node {
let id = node.id(); let id = node.id();
let position = node.position(); let position = node.position();
node.update_properties(&current_node_properties);
current_node_properties.clear();
self.add_node(node); self.add_node(node);
if let Some(node) = self.node(id) { if let Some(node) = self.node(id) {
self.move_node(&node.upcast(), position.0, position.1); self.move_node(&node.upcast(), position.0, position.1);
} }
self.update_current_node_id(id); self.update_current_node_id(id);
} }
current_node = None; current_node = None;
@ -940,16 +963,15 @@ impl GraphView {
"Property" => {} "Property" => {}
"Port" => { "Port" => {
if let Some(port) = current_port { if let Some(port) = current_port {
let node = current_node.clone(); if let Some(mut node) = current_node.clone() {
let id = port.id(); let id = port.id();
node.expect("No current node, error...").add_port( port.update_properties(&current_port_properties);
id, self.add_port_to_node(&mut node, port);
&port.name(), current_port_properties.clear();
port.direction(),
port.presence(),
);
self.update_current_port_id(id); self.update_current_port_id(id);
} }
}
current_port = None; current_port = None;
} }
"Link" => { "Link" => {

View file

@ -22,10 +22,7 @@ use gtk::prelude::*;
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
use log::trace; use log::trace;
use super::Port; use super::{Port, PortDirection, PortPresence, PropertyExt, SelectionExt};
use super::PropertyExt;
use super::SelectionExt;
use super::{PortDirection, PortPresence};
use std::cell::{Cell, Ref, RefCell}; use std::cell::{Cell, Ref, RefCell};
use std::collections::HashMap; use std::collections::HashMap;
@ -162,15 +159,8 @@ impl Node {
/// Add a new port to the node /// Add a new port to the node
/// ///
pub fn add_port( pub fn add_port(&mut self, port: Port) {
&mut self,
id: u32,
name: &str,
direction: PortDirection,
presence: PortPresence,
) {
let private = imp::Node::from_instance(self); let private = imp::Node::from_instance(self);
let port = Port::new(id, name, direction, presence);
match port.direction() { match port.direction() {
PortDirection::Input => { PortDirection::Input => {
private private
@ -186,8 +176,7 @@ impl Node {
} }
_ => panic!("Port without direction"), _ => panic!("Port without direction"),
} }
private.ports.borrow_mut().insert(port.id(), port);
private.ports.borrow_mut().insert(id, port);
} }
/// Retrieves all ports as an hashmap /// Retrieves all ports as an hashmap
@ -211,9 +200,9 @@ impl Node {
/// Retrieves the port with id /// Retrieves the port with id
/// ///
pub fn port(&self, id: &u32) -> Option<super::port::Port> { pub fn port(&self, id: u32) -> Option<super::port::Port> {
let private = imp::Node::from_instance(self); 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 /// Check if we can remove a port dependending on PortPrensence attribute

View file

@ -22,7 +22,7 @@ use gtk::{
prelude::*, prelude::*,
subclass::prelude::*, subclass::prelude::*,
}; };
use log::trace; use log::info;
use std::cell::RefCell; use std::cell::RefCell;
use std::cell::{Cell, Ref}; use std::cell::{Cell, Ref};
use std::collections::HashMap; use std::collections::HashMap;
@ -227,7 +227,7 @@ impl PropertyExt for Port {
/// ///
fn add_property(&self, name: &str, value: &str) { fn add_property(&self, name: &str, value: &str) {
let private = imp::Port::from_instance(self); let private = imp::Port::from_instance(self);
trace!("property name={} updated with value={}", name, value); info!("property name={} updated with value={}", name, value);
private private
.properties .properties
.borrow_mut() .borrow_mut()