graphview: node by unique name

keep a unique name value in Node structure
and provide an API to modify it.
This commit is contained in:
Stéphane Cerveau 2022-02-21 17:52:01 +01:00 committed by Stéphane Cerveau
parent 16fee2a289
commit 8c9cb35928
2 changed files with 28 additions and 13 deletions

View file

@ -604,6 +604,19 @@ impl GraphView {
private.nodes.borrow().get(&id).cloned() 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<Node> {
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 /// Remove all the nodes from the graphview
/// ///
pub fn remove_all_nodes(&self) { pub fn remove_all_nodes(&self) {

View file

@ -50,6 +50,7 @@ impl NodeType {
mod imp { mod imp {
use super::*; use super::*;
use once_cell::unsync::OnceCell; use once_cell::unsync::OnceCell;
#[derive(Default)]
pub struct Node { pub struct Node {
pub(super) layoutgrid: gtk::Grid, pub(super) layoutgrid: gtk::Grid,
pub(super) name: gtk::Label, pub(super) name: gtk::Label,
@ -64,6 +65,7 @@ mod imp {
pub(super) selected: Cell<bool>, pub(super) selected: Cell<bool>,
pub(super) light: Cell<bool>, pub(super) light: Cell<bool>,
pub(super) position: Cell<(f32, f32)>, pub(super) position: Cell<(f32, f32)>,
pub(super) unique_name: RefCell<String>,
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -102,15 +104,7 @@ mod imp {
layoutgrid, layoutgrid,
name, name,
description, description,
id: OnceCell::new(), ..Default::default()
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)),
} }
} }
} }
@ -143,6 +137,9 @@ impl Node {
let private = imp::Node::from_obj(&res); let private = imp::Node::from_obj(&res);
private.id.set(id).expect("Node id is already set"); private.id.set(id).expect("Node id is already set");
res.set_name(name); 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"); res.add_css_class("node");
private private
.node_type .node_type
@ -242,10 +239,15 @@ impl Node {
/// Retrieves the unique name composed with the node name and its id /// Retrieves the unique name composed with the node name and its id
/// ///
pub fn unique_name(&self) -> String { pub fn unique_name(&self) -> String {
let private = imp::Node::from_obj(self); let private = imp::Node::from_instance(self);
let mut unique_name = private.name.text().to_string(); private.unique_name.borrow().clone()
unique_name.push_str(&self.id().to_string()); }
unique_name
/// 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 /// Retrieves the NodeType