From 17ecce97480cad233755f22ccdc046abe585f29d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Cerveau?= Date: Fri, 21 Jan 2022 16:44:12 +0100 Subject: [PATCH] port: support properties Allow port to have a property set and store it in the xml file Introduce PropertyExt Rewrote the port layout --- src/graphmanager/graphview.rs | 17 ++++++++-- src/graphmanager/mod.rs | 2 ++ src/graphmanager/node.rs | 62 ++++++++++++---------------------- src/graphmanager/port.rs | 63 ++++++++++++++++++++++++++--------- src/graphmanager/property.rs | 55 ++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 59 deletions(-) create mode 100644 src/graphmanager/property.rs diff --git a/src/graphmanager/graphview.rs b/src/graphmanager/graphview.rs index 9d75940..2c43a8f 100644 --- a/src/graphmanager/graphview.rs +++ b/src/graphmanager/graphview.rs @@ -28,6 +28,7 @@ use super::{ link::*, node::{Node, NodeType}, port::{Port, PortDirection, PortPresence}, + property::PropertyExt, selection::SelectionExt, }; @@ -756,6 +757,14 @@ impl GraphView { .attr("direction", &port.direction().to_string()) .attr("presence", &port.presence().to_string()), )?; + for (name, value) in port.properties().iter() { + writer.write( + XMLWEvent::start_element("Property") + .attr("name", name) + .attr("value", value), + )?; + writer.write(XMLWEvent::end_element())?; + } writer.write(XMLWEvent::end_element())?; } @@ -851,9 +860,11 @@ impl GraphView { let value: &String = attrs .get::(&String::from("value")) .expect("Unable to find property value"); - let node = current_node.clone(); - node.expect("current node does not exist") - .add_property(name.clone(), value.clone()); + 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()); + } } "Port" => { let id = attrs diff --git a/src/graphmanager/mod.rs b/src/graphmanager/mod.rs index f9604d4..61924f1 100644 --- a/src/graphmanager/mod.rs +++ b/src/graphmanager/mod.rs @@ -2,6 +2,7 @@ mod graphview; mod link; mod node; mod port; +mod property; mod selection; pub use graphview::GraphView; @@ -10,4 +11,5 @@ pub use node::Node; pub use node::NodeType; pub use port::Port; pub use port::{PortDirection, PortPresence}; +pub use property::PropertyExt; pub use selection::SelectionExt; diff --git a/src/graphmanager/node.rs b/src/graphmanager/node.rs index 3d49b03..464e477 100644 --- a/src/graphmanager/node.rs +++ b/src/graphmanager/node.rs @@ -23,6 +23,7 @@ use gtk::subclass::prelude::*; use log::trace; use super::Port; +use super::PropertyExt; use super::SelectionExt; use super::{PortDirection, PortPresence}; @@ -271,46 +272,6 @@ impl Node { private.node_type.get() } - /// Check if the name is an hidden property - /// - pub fn hidden_property(&self, name: &str) -> bool { - name.starts_with('_') - } - - /// Add a node property with a name and a value. - /// - pub fn add_property(&self, name: String, value: String) { - let private = imp::Node::from_instance(self); - trace!("property name={} updated with value={}", name, value); - private.properties.borrow_mut().insert(name, value); - self.update_description(); - } - - /// Update the node property. - /// - pub fn update_properties(&self, new_node_properties: &HashMap) { - for (key, value) in new_node_properties { - self.add_property(key.clone(), value.clone()); - } - } - - /// Retrieves node properties. - /// - pub fn properties(&self) -> Ref> { - let private = imp::Node::from_instance(self); - private.properties.borrow() - } - - /// Retrieves node property with the name. - /// - pub fn property(&self, name: &str) -> Option { - let private = imp::Node::from_instance(self); - if let Some(property) = private.properties.borrow().get(name) { - return Some(property.clone()); - } - None - } - /// Unselect all the ports of the given node. /// pub fn unselect_all_ports(&self) { @@ -380,3 +341,24 @@ impl SelectionExt for Node { private.selected.get() } } + +impl PropertyExt for Node { + /// Add a node property with a name and a value. + /// + fn add_property(&self, name: &str, value: &str) { + let private = imp::Node::from_instance(self); + trace!("property name={} updated with value={}", name, value); + private + .properties + .borrow_mut() + .insert(name.to_string(), value.to_string()); + self.update_description(); + } + + /// Retrieves node properties. + /// + fn properties(&self) -> Ref> { + let private = imp::Node::from_instance(self); + private.properties.borrow() + } +} diff --git a/src/graphmanager/port.rs b/src/graphmanager/port.rs index fb1e71b..78f2f08 100644 --- a/src/graphmanager/port.rs +++ b/src/graphmanager/port.rs @@ -22,10 +22,13 @@ use gtk::{ prelude::*, subclass::prelude::*, }; -use std::cell::Cell; -use std::{borrow::Borrow, fmt}; +use log::trace; +use std::cell::RefCell; +use std::cell::{Cell, Ref}; +use std::collections::HashMap; +use std::fmt; -use super::SelectionExt; +use super::{PropertyExt, SelectionExt}; #[derive(Debug, Clone, PartialOrd, PartialEq, Copy)] pub enum PortDirection { @@ -85,11 +88,12 @@ mod imp { /// Graphical representation of a port. #[derive(Default, Clone)] pub struct Port { - pub(super) label: OnceCell, + pub(super) label: gtk::Label, pub(super) id: OnceCell, pub(super) direction: OnceCell, pub(super) selected: Cell, pub(super) presence: OnceCell, + pub(super) properties: RefCell>, } #[glib::object_subclass] @@ -104,13 +108,27 @@ mod imp { // Make it look like a GTK button. klass.set_css_name("button"); } + + fn new() -> Self { + let label = gtk::Label::new(None); + Self { + label, + id: OnceCell::new(), + direction: OnceCell::new(), + selected: Cell::new(false), + presence: OnceCell::new(), + properties: RefCell::new(HashMap::new()), + } + } } impl ObjectImpl for Port { + fn constructed(&self, obj: &Self::Type) { + self.parent_constructed(obj); + self.label.set_parent(obj); + } fn dispose(&self, _obj: &Self::Type) { - if let Some(label) = self.label.get() { - label.unparent() - } + self.label.unparent() } } impl WidgetImpl for Port {} @@ -149,13 +167,7 @@ impl Port { } else { port.add_css_class("port-sometimes"); } - - let label = gtk::Label::new(Some(name)); - label.set_parent(&port); - private - .label - .set(label) - .expect("Port label was already set"); + private.label.set_text(name); port } @@ -171,8 +183,7 @@ impl Port { /// pub fn name(&self) -> String { let private = imp::Port::from_instance(self); - let label = private.label.borrow().get().unwrap(); - label.text().to_string() + private.label.text().to_string() } /// Retrieves the port direction @@ -210,3 +221,23 @@ impl SelectionExt for Port { private.selected.get() } } + +impl PropertyExt for Port { + /// Add a node property with a name and a value. + /// + fn add_property(&self, name: &str, value: &str) { + let private = imp::Port::from_instance(self); + trace!("property name={} updated with value={}", name, value); + private + .properties + .borrow_mut() + .insert(name.to_string(), value.to_string()); + } + + /// Retrieves node properties. + /// + fn properties(&self) -> Ref> { + let private = imp::Port::from_instance(self); + private.properties.borrow() + } +} diff --git a/src/graphmanager/property.rs b/src/graphmanager/property.rs new file mode 100644 index 0000000..ddd99ee --- /dev/null +++ b/src/graphmanager/property.rs @@ -0,0 +1,55 @@ +// property.rs +// +// Copyright 2022 Stéphane Cerveau +// +// This program 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. +// +// This program 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 this program. If not, see . +// +// SPDX-License-Identifier: GPL-3.0-only +use std::cell::Ref; +use std::collections::HashMap; + +pub trait PropertyExt { + fn hidden_property(&self, name: &str) -> bool { + name.starts_with('_') + } + + /// Add a node property with a name and a value. + /// + fn add_property(&self, name: &str, value: &str); + + /// Update the properties. + /// + /// Update the PropertyExt properties. + /// + fn update_properties(&self, new_properties: &HashMap) { + for (key, value) in new_properties { + self.add_property(key, value); + } + } + + /// Retrieves properties. + /// + fn properties(&self) -> Ref>; + + /// Retrieves property with the name. + /// + /// Retrieves node property with the name. + /// + fn property(&self, name: &str) -> Option { + if let Some(property) = self.properties().get(name) { + return Some(property.clone()); + } + None + } +}