port: support properties

Allow port to have a property set
and store it in the xml file

Introduce PropertyExt

Rewrote the port layout
This commit is contained in:
Stéphane Cerveau 2022-01-21 16:44:12 +01:00
parent b360f4a13a
commit 17ecce9748
5 changed files with 140 additions and 59 deletions

View file

@ -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>(&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

View file

@ -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;

View file

@ -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<String, String>) {
for (key, value) in new_node_properties {
self.add_property(key.clone(), value.clone());
}
}
/// Retrieves node properties.
///
pub fn properties(&self) -> Ref<HashMap<String, String>> {
let private = imp::Node::from_instance(self);
private.properties.borrow()
}
/// Retrieves node property with the name.
///
pub fn property(&self, name: &str) -> Option<String> {
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<HashMap<String, String>> {
let private = imp::Node::from_instance(self);
private.properties.borrow()
}
}

View file

@ -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<gtk::Label>,
pub(super) label: gtk::Label,
pub(super) id: OnceCell<u32>,
pub(super) direction: OnceCell<PortDirection>,
pub(super) selected: Cell<bool>,
pub(super) presence: OnceCell<PortPresence>,
pub(super) properties: RefCell<HashMap<String, String>>,
}
#[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 dispose(&self, _obj: &Self::Type) {
if let Some(label) = self.label.get() {
label.unparent()
fn constructed(&self, obj: &Self::Type) {
self.parent_constructed(obj);
self.label.set_parent(obj);
}
fn dispose(&self, _obj: &Self::Type) {
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<HashMap<String, String>> {
let private = imp::Port::from_instance(self);
private.properties.borrow()
}
}

View file

@ -0,0 +1,55 @@
// property.rs
//
// Copyright 2022 Stéphane Cerveau <scerveau@collabora.com>
//
// 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 <http://www.gnu.org/licenses/>.
//
// 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<String, String>) {
for (key, value) in new_properties {
self.add_property(key, value);
}
}
/// Retrieves properties.
///
fn properties(&self) -> Ref<HashMap<String, String>>;
/// Retrieves property with the name.
///
/// Retrieves node property with the name.
///
fn property(&self, name: &str) -> Option<String> {
if let Some(property) = self.properties().get(name) {
return Some(property.clone());
}
None
}
}