node: update the layout and use GtkGrid

The node uses a GrkGrid to display
ports, name and description.
This commit is contained in:
Stéphane Cerveau 2022-01-21 15:44:27 +01:00
parent 39815eb8d9
commit b360f4a13a
2 changed files with 22 additions and 46 deletions

View file

@ -357,7 +357,7 @@ mod imp {
let (fnx, fny) = (from_node.allocation().x(), from_node.allocation().y()); let (fnx, fny) = (from_node.allocation().x(), from_node.allocation().y());
if let Some((port_x, port_y)) = from_port.translate_coordinates(from_node, 0.0, 0.0) { if let Some((port_x, port_y)) = from_port.translate_coordinates(from_node, 0.0, 0.0) {
fx += fnx + fw + port_x as i32; fx = fnx + fw + port_x as i32;
fy = fny + (fh / 2) + port_y as i32; fy = fny + (fh / 2) + port_y as i32;
} }

View file

@ -60,12 +60,9 @@ impl NodeType {
mod imp { mod imp {
use super::*; use super::*;
use gtk::Orientation;
use once_cell::unsync::OnceCell; use once_cell::unsync::OnceCell;
pub struct Node { pub struct Node {
pub(super) layoutbox: gtk::Box, pub(super) layoutgrid: gtk::Grid,
pub(super) inputs: gtk::Box,
pub(super) outputs: gtk::Box,
pub(super) name: gtk::Label, pub(super) name: gtk::Label,
pub(super) description: gtk::Label, pub(super) description: gtk::Label,
pub(super) id: OnceCell<u32>, pub(super) id: OnceCell<u32>,
@ -73,7 +70,7 @@ mod imp {
pub(super) ports: RefCell<HashMap<u32, Port>>, pub(super) ports: RefCell<HashMap<u32, Port>>,
pub(super) num_ports_in: Cell<i32>, pub(super) num_ports_in: Cell<i32>,
pub(super) num_ports_out: Cell<i32>, pub(super) num_ports_out: Cell<i32>,
// Properties are differnet from GObject properties // Properties are different from GObject properties
pub(super) properties: RefCell<HashMap<String, String>>, pub(super) properties: RefCell<HashMap<String, String>>,
pub(super) selected: Cell<bool>, pub(super) selected: Cell<bool>,
pub(super) position: Cell<(f32, f32)>, pub(super) position: Cell<(f32, f32)>,
@ -91,53 +88,28 @@ mod imp {
} }
fn new() -> Self { fn new() -> Self {
let layoutbox = gtk::Box::new(Orientation::Vertical, 6); let layoutgrid = gtk::Grid::builder()
let name_desc = gtk::Box::new(Orientation::Vertical, 6); .margin_start(6)
layoutbox.append(&name_desc); .margin_end(6)
let ports = gtk::Box::builder() .margin_top(6)
.orientation(Orientation::Horizontal) .margin_bottom(6)
.halign(gtk::Align::Start)
.spacing(10)
.margin_bottom(10)
.margin_top(10)
.build();
layoutbox.append(&ports);
let inputs = gtk::Box::builder()
.orientation(Orientation::Vertical)
.halign(gtk::Align::Start)
.spacing(10)
.build();
ports.append(&inputs);
let center = gtk::Box::builder()
.orientation(Orientation::Vertical)
.halign(gtk::Align::Center) .halign(gtk::Align::Center)
.hexpand(true) .valign(gtk::Align::Center)
.margin_start(20) .row_spacing(6)
.margin_end(20) .column_spacing(6)
.build(); .build();
ports.append(&center);
let outputs = gtk::Box::builder()
.orientation(Orientation::Vertical)
.halign(gtk::Align::End)
.spacing(10)
.build();
ports.append(&outputs);
let name = gtk::Label::new(None); let name = gtk::Label::new(None);
name_desc.append(&name); layoutgrid.attach(&name, 1, 0, 1, 1);
let description = gtk::Label::new(None); let description = gtk::Label::new(None);
name_desc.append(&description); layoutgrid.attach(&description, 1, 1, 1, 1);
// Display a grab cursor when the mouse is over the name so the user knows the node can be dragged. // Display a grab cursor when the mouse is over the name so the user knows the node can be dragged.
name.set_cursor(gtk::gdk::Cursor::from_name("grab", None).as_ref()); name.set_cursor(gtk::gdk::Cursor::from_name("grab", None).as_ref());
Self { Self {
layoutbox, layoutgrid,
inputs,
outputs,
name, name,
description, description,
id: OnceCell::new(), id: OnceCell::new(),
@ -155,11 +127,11 @@ mod imp {
impl ObjectImpl for Node { impl ObjectImpl for Node {
fn constructed(&self, obj: &Self::Type) { fn constructed(&self, obj: &Self::Type) {
self.parent_constructed(obj); self.parent_constructed(obj);
self.layoutbox.set_parent(obj); self.layoutgrid.set_parent(obj);
} }
fn dispose(&self, _obj: &Self::Type) { fn dispose(&self, _obj: &Self::Type) {
self.layoutbox.unparent(); self.layoutgrid.unparent();
} }
} }
@ -200,11 +172,15 @@ impl Node {
let port = Port::new(id, name, direction, presence); let port = Port::new(id, name, direction, presence);
match port.direction() { match port.direction() {
PortDirection::Input => { PortDirection::Input => {
private.inputs.append(&port); private
.layoutgrid
.attach(&port, 0, private.num_ports_in.get(), 1, 1);
private.num_ports_in.set(private.num_ports_in.get() + 1); private.num_ports_in.set(private.num_ports_in.get() + 1);
} }
PortDirection::Output => { PortDirection::Output => {
private.outputs.append(&port); private
.layoutgrid
.attach(&port, 2, private.num_ports_out.get(), 1, 1);
private.num_ports_out.set(private.num_ports_out.get() + 1); private.num_ports_out.set(private.num_ports_out.get() + 1);
} }
_ => panic!("Port without direction"), _ => panic!("Port without direction"),