From 8c9cb35928059314f1aebe7dd8aae77a63ac656c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Cerveau?= Date: Mon, 21 Feb 2022 17:52:01 +0100 Subject: [PATCH] graphview: node by unique name keep a unique name value in Node structure and provide an API to modify it. --- src/graphmanager/graphview.rs | 13 +++++++++++++ src/graphmanager/node.rs | 28 +++++++++++++++------------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/graphmanager/graphview.rs b/src/graphmanager/graphview.rs index 2ac7518..cf67cec 100644 --- a/src/graphmanager/graphview.rs +++ b/src/graphmanager/graphview.rs @@ -604,6 +604,19 @@ impl GraphView { private.nodes.borrow().get(&id).cloned() } + /// Get the node with the specified node name inside the graphview. + /// + /// Returns `None` if the node is not in the graphview. + pub fn node_by_unique_name(&self, unique_name: &str) -> Option { + let private = imp::GraphView::from_obj(self); + for node in private.nodes.borrow().values() { + if node.unique_name() == unique_name { + return Some(node.clone()); + } + } + None + } + /// Remove all the nodes from the graphview /// pub fn remove_all_nodes(&self) { diff --git a/src/graphmanager/node.rs b/src/graphmanager/node.rs index 9d01bb9..0dbea8b 100644 --- a/src/graphmanager/node.rs +++ b/src/graphmanager/node.rs @@ -50,6 +50,7 @@ impl NodeType { mod imp { use super::*; use once_cell::unsync::OnceCell; + #[derive(Default)] pub struct Node { pub(super) layoutgrid: gtk::Grid, pub(super) name: gtk::Label, @@ -64,6 +65,7 @@ mod imp { pub(super) selected: Cell, pub(super) light: Cell, pub(super) position: Cell<(f32, f32)>, + pub(super) unique_name: RefCell, } #[glib::object_subclass] @@ -102,15 +104,7 @@ mod imp { layoutgrid, name, description, - id: OnceCell::new(), - node_type: OnceCell::new(), - ports: RefCell::new(HashMap::new()), - num_ports_in: Cell::new(0), - num_ports_out: Cell::new(0), - properties: RefCell::new(HashMap::new()), - selected: Cell::new(false), - light: Cell::new(false), - position: Cell::new((0.0, 0.0)), + ..Default::default() } } } @@ -143,6 +137,9 @@ impl Node { let private = imp::Node::from_obj(&res); private.id.set(id).expect("Node id is already set"); res.set_name(name); + let mut unique_name = private.name.text().to_string(); + unique_name.push_str(&id.to_string()); + private.unique_name.replace(unique_name); res.add_css_class("node"); private .node_type @@ -242,10 +239,15 @@ impl Node { /// Retrieves the unique name composed with the node name and its id /// pub fn unique_name(&self) -> String { - let private = imp::Node::from_obj(self); - let mut unique_name = private.name.text().to_string(); - unique_name.push_str(&self.id().to_string()); - unique_name + let private = imp::Node::from_instance(self); + private.unique_name.borrow().clone() + } + + /// Update the unique name + /// + pub fn set_unique_name(&self, unique_name: &str) { + let private = imp::Node::from_instance(self); + private.unique_name.replace(unique_name.to_string()); } /// Retrieves the NodeType